-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtarget_flags.go
More file actions
102 lines (88 loc) · 2.11 KB
/
target_flags.go
File metadata and controls
102 lines (88 loc) · 2.11 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package main
import (
"os"
"strings"
)
const targetCPUEnv = "PLUTO_TARGET_CPU"
type targetCPUSetting struct {
bare string
explicitKind string
disabled bool
defaulted bool
}
type buildConfig struct {
targetCPU targetCPUSetting
}
func currentTargetCPUSetting() targetCPUSetting {
value := strings.TrimSpace(os.Getenv(targetCPUEnv))
defaulted := value == ""
if defaulted {
value = "native"
}
lower := strings.ToLower(value)
switch lower {
case "default", "off", "none", "portable":
return targetCPUSetting{disabled: true, defaulted: defaulted}
}
setting := targetCPUSetting{
bare: value,
defaulted: defaulted,
}
if strings.HasPrefix(lower, "-mcpu=") || strings.HasPrefix(lower, "-march=") {
if idx := strings.IndexByte(value, '='); idx >= 0 {
setting.bare = value[idx+1:]
}
if strings.HasPrefix(lower, "-march=") {
setting.explicitKind = "march"
} else {
setting.explicitKind = "mcpu"
}
}
return setting
}
func currentBuildConfig() buildConfig {
return buildConfig{targetCPU: currentTargetCPUSetting()}
}
func (cfg buildConfig) targetCPUFlag() string {
if cfg.targetCPU.disabled {
return ""
}
return "-mcpu=" + cfg.targetCPU.bare
}
func (cfg buildConfig) clangTargetFlag(goarch string) string {
if cfg.targetCPU.disabled {
return ""
}
if cfg.targetCPU.explicitKind != "" {
return "-" + cfg.targetCPU.explicitKind + "=" + cfg.targetCPU.bare
}
switch goarch {
case "386", "amd64":
return "-march=" + cfg.targetCPU.bare
default:
return "-mcpu=" + cfg.targetCPU.bare
}
}
func (cfg buildConfig) llvmCodegenFlags() []string {
if cpu := cfg.targetCPUFlag(); cpu != "" {
return []string{cpu}
}
return nil
}
func (cfg buildConfig) targetCPUCacheSegment() (string, error) {
if !cfg.targetCPU.disabled && strings.EqualFold(cfg.targetCPU.bare, "native") {
return "", nil
}
key := "portable"
if !cfg.targetCPU.disabled {
key = cfg.targetCPU.bare
if cfg.targetCPU.explicitKind != "" {
key = cfg.targetCPU.explicitKind + "-" + key
}
}
safeKey, err := sanitizeCacheComponent(key)
if err != nil {
return "", err
}
return "target_cpu-" + safeKey, nil
}