aboutsummaryrefslogtreecommitdiff
path: root/compiler_wrapper/config.go
blob: 3720018e2b933d3b7795bf51b4a750fd63df4e4e (plain)
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// Copyright 2019 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package main

import (
	"strconv"
)

type config struct {
	// TODO: Refactor this flag into more generic configuration properties.
	isHostWrapper    bool
	isAndroidWrapper bool
	// Whether to use ccache.
	useCCache bool
	// Flags to add to gcc and clang.
	commonFlags []string
	// Flags to add to gcc only.
	gccFlags []string
	// Flags to add to clang only.
	clangFlags []string
	// Toolchain root path relative to the wrapper binary.
	rootRelPath string
	// Path of the old wrapper using the toolchain root.
	oldWrapperPath string
	// Whether to mock out the calls that the old wrapper does.
	mockOldWrapperCmds bool
	// Directory to store errors that were prevented with -Wno-error.
	newWarningsDir string
	// Version. Only used for printing via -print-cmd.
	version string
}

// Version can be set via a linker flag.
// Values fills config.version.
var Version = ""

// UseCCache can be set via a linker flag.
// Value will be passed to strconv.ParseBool.
// E.g. go build -ldflags '-X config.UseCCache=true'.
var UseCCache = "unknown"

// UseLlvmNext can be set via a linker flag.
// Value will be passed to strconv.ParseBool.
// E.g. go build -ldflags '-X config.UseLlvmNext=true'.
var UseLlvmNext = "unknown"

// ConfigName can be set via a linker flag.
// Value has to be one of:
// - "cros.hardened"
// - "cros.nonhardened"
var ConfigName = "unknown"

// Returns the configuration matching the UseCCache and ConfigName.
func getRealConfig() (*config, error) {
	useCCache, err := strconv.ParseBool(UseCCache)
	if err != nil {
		return nil, wrapErrorwithSourceLocf(err, "invalid format for UseCCache")
	}
	useLlvmNext, err := strconv.ParseBool(UseLlvmNext)
	if err != nil {
		return nil, wrapErrorwithSourceLocf(err, "invalid format for UseLLvmNext")
	}
	// FIXME: Remove comparison to old wrapper once the new wrapper has landed.
	oldWrapperPath := ""
	config, err := getConfig(ConfigName, useCCache, useLlvmNext, oldWrapperPath, Version)
	if err != nil {
		return nil, err
	}
	return config, nil
}

func getConfig(configName string, useCCache bool, useLlvmNext bool, oldWrapperPath string, version string) (*config, error) {
	cfg := config{}
	switch configName {
	case "cros.hardened":
		cfg = *crosHardenedConfig
	case "cros.nonhardened":
		cfg = *crosNonHardenedConfig
	case "cros.host":
		cfg = *crosHostConfig
	case "android":
		cfg = *androidConfig
	default:
		return nil, newErrorwithSourceLocf("unknown config name: %s", configName)
	}
	cfg.useCCache = useCCache
	if useLlvmNext {
		cfg.clangFlags = append(cfg.clangFlags, llvmNextFlags...)
	}
	cfg.oldWrapperPath = oldWrapperPath
	cfg.version = version
	return &cfg, nil
}

var llvmNextFlags = []string{
	"-Wno-reorder-init-list",
	"-Wno-final-dtor-non-final-class",
	"-Wno-implicit-int-float-conversion",
	"-Wno-return-stack-address",
	"-Werror=poison-system-directories",
}

// Full hardening.
// Temporarily disable function splitting because of chromium:434751.
var crosHardenedConfig = &config{
	rootRelPath: "../../../../..",
	commonFlags: []string{
		"-fstack-protector-strong",
		"-fPIE",
		"-pie",
		"-D_FORTIFY_SOURCE=2",
		"-fno-omit-frame-pointer",
	},
	gccFlags: []string{
		"-fno-reorder-blocks-and-partition",
		"-Wno-unused-local-typedefs",
		"-Wno-maybe-uninitialized",
	},
	// Temporarily disable tautological-*-compare chromium:778316.
	// Temporarily add no-unknown-warning-option to deal with old clang versions.
	// Temporarily disable Wsection since kernel gets a bunch of these. chromium:778867
	// Disable "-faddrsig" since it produces object files that strip doesn't understand, chromium:915742.
	clangFlags: []string{
		"-Qunused-arguments",
		"-grecord-gcc-switches",
		"-fno-addrsig",
		"-Wno-tautological-constant-compare",
		"-Wno-tautological-unsigned-enum-zero-compare",
		"-Wno-unknown-warning-option",
		"-Wno-section",
		"-static-libgcc",
		"-fuse-ld=lld",
	},
	newWarningsDir: "/tmp/fatal_clang_warnings",
}

// Flags to be added to non-hardened toolchain.
var crosNonHardenedConfig = &config{
	rootRelPath: "../../../../..",
	commonFlags: []string{},
	gccFlags: []string{
		"-Wno-maybe-uninitialized",
		"-Wno-unused-local-typedefs",
		"-Wno-deprecated-declarations",
		"-Wtrampolines",
	},
	// Temporarily disable tautological-*-compare chromium:778316.
	// Temporarily add no-unknown-warning-option to deal with old clang versions.
	// Temporarily disable Wsection since kernel gets a bunch of these. chromium:778867
	clangFlags: []string{
		"-Qunused-arguments",
		"-Wno-tautological-constant-compare",
		"-Wno-tautological-unsigned-enum-zero-compare",
		"-Wno-unknown-warning-option",
		"-Wno-section",
		"-static-libgcc",
	},
	newWarningsDir: "/tmp/fatal_clang_warnings",
}

// Flags to be added to host toolchain.
var crosHostConfig = &config{
	isHostWrapper: true,
	rootRelPath:   "../..",
	commonFlags:   []string{},
	gccFlags: []string{
		"-Wno-maybe-uninitialized",
		"-Wno-unused-local-typedefs",
		"-Wno-deprecated-declarations",
	},
	// Temporarily disable tautological-*-compare chromium:778316.
	// Temporarily add no-unknown-warning-option to deal with old clang versions.
	clangFlags: []string{
		"-Qunused-arguments",
		"-grecord-gcc-switches",
		"-fno-addrsig",
		"-fuse-ld=lld",
		"-Wno-unused-local-typedefs",
		"-Wno-deprecated-declarations",
		"-Wno-tautological-constant-compare",
		"-Wno-tautological-unsigned-enum-zero-compare",
		"-Wno-unknown-warning-option",
	},
	newWarningsDir: "/tmp/fatal_clang_warnings",
}

var androidConfig = &config{
	isHostWrapper:    false,
	isAndroidWrapper: true,
	rootRelPath:      "./",
	commonFlags:      []string{},
	gccFlags:         []string{},
	clangFlags:       []string{},
	newWarningsDir:   "/tmp/fatal_clang_warnings",
}