forked from google/syzkaller
-
Notifications
You must be signed in to change notification settings - Fork 1
/
make.go
77 lines (70 loc) · 1.95 KB
/
make.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright 2018 syzkaller project authors. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
// syz-make provides information required to build native code for the Makefile.
package main
import (
"fmt"
"os"
"runtime"
"strconv"
"strings"
"github.com/google/syzkaller/pkg/osutil"
"github.com/google/syzkaller/sys/targets"
)
func main() {
vars, err := impl()
if err != nil {
fmt.Printf("export SYZERROR=%v\n", err)
os.Exit(1)
}
for _, v := range vars {
fmt.Printf("export %v=%v\\n", v.Name, v.Val)
}
}
type Var struct {
Name string
Val string
}
func impl() ([]Var, error) {
hostOS := or(os.Getenv("HOSTOS"), runtime.GOOS)
hostArch := or(os.Getenv("HOSTARCH"), runtime.GOARCH)
targetOS := or(os.Getenv("TARGETOS"), hostOS)
targetArch := or(os.Getenv("TARGETARCH"), hostArch)
targetVMArch := or(os.Getenv("TARGETVMARCH"), targetArch)
target := targets.Get(targetOS, targetArch)
if target == nil {
return nil, fmt.Errorf("unknown target %v/%v", targetOS, targetArch)
}
parallelism := runtime.NumCPU()
if mem := osutil.SystemMemorySize(); mem != 0 {
// Ensure that we have at least 1GB per Makefile job.
// Go compiler/linker can consume significant amount of memory
// (observed to consume at least 600MB). See #1276 for context.
memLimit := int(mem / (1 << 30))
if parallelism > memLimit {
parallelism = memLimit
}
}
vars := []Var{
{"BUILDOS", runtime.GOOS},
{"NATIVEBUILDOS", target.BuildOS},
{"HOSTOS", hostOS},
{"HOSTARCH", hostArch},
{"TARGETOS", targetOS},
{"TARGETARCH", targetArch},
{"TARGETVMARCH", targetVMArch},
{"CC", target.CCompiler},
{"ADDCFLAGS", strings.Join(target.CFlags, " ")},
{"NCORES", strconv.Itoa(parallelism)},
{"EXE", target.ExeExtension},
{"NATIVEBUILDOS", target.BuildOS},
{"NO_CROSS_COMPILER", target.BrokenCompiler},
}
return vars, nil
}
func or(s1, s2 string) string {
if s1 != "" {
return s1
}
return s2
}