aboutsummaryrefslogtreecommitdiff
path: root/compiler_wrapper/config.go
blob: b445ce95744bc5f0ec6c056a5b1b3d9ba481ec67 (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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// Copyright 2019 The ChromiumOS Authors
// 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
	// Whether llvmNext wrapper.
	useLlvmNext 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
	// Flags to add to clang only, AFTER user flags (cannot be overridden
	// by the user).
	clangPostFlags []string
	// Toolchain root path relative to the wrapper binary.
	clangRootRelPath string
	gccRootRelPath   string
	// Version. Only exposed via -print-config.
	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")
	}
	config, err := getConfig(ConfigName, useCCache, useLlvmNext, Version)
	if err != nil {
		return nil, err
	}
	return config, nil
}

func isAndroidConfig() bool {
	return ConfigName == "android"
}

func getConfig(configName string, useCCache bool, useLlvmNext bool, 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
	cfg.useLlvmNext = useLlvmNext
	if useLlvmNext {
		cfg.clangFlags = append(cfg.clangFlags, llvmNextFlags...)
		cfg.clangPostFlags = append(cfg.clangPostFlags, llvmNextPostFlags...)
	}
	cfg.version = version
	return &cfg, nil
}

func crosCommonClangFlags() []string {
	// Temporarily disable tautological-*-compare chromium:778316.
	// Temporarily add no-unknown-warning-option to deal with old clang versions.
	// Temporarily disable Wdeprecated-declarations. b/193860318
	// b/230345382: Temporarily disable Wimplicit-function-declaration.
	// b/231987783: Temporarily disable Wimplicit-int.
	return []string{
		"-Qunused-arguments",
		"-Werror=poison-system-directories",
		"-Wno-deprecated-declarations",
		"-Wno-enum-constexpr-conversion",
		"-Wno-error=implicit-function-declaration",
		"-Wno-error=implicit-int",
		"-Wno-final-dtor-non-final-class",
		"-Wno-single-bit-bitfield-constant-conversion",
		"-Wno-tautological-constant-compare",
		"-Wno-tautological-unsigned-enum-zero-compare",
		"-Wno-unknown-warning-option",
		"-fdebug-default-version=5",
		"-Wno-int-conversion",
		"-Wno-incompatible-function-pointer-types",
		// TODO(b/316021385): Temporarily disables warnings for variable length arrays.
		"-Wno-error=vla-cxx-extension",
		"-D_LIBCPP_ENABLE_CXX17_REMOVED_FEATURES",
		// TODO(b/315504245): Temporarily prevents new mangling rules from taking effect.
		"-fclang-abi-compat=17",
	}
}

func crosCommonClangPostFlags() []string {
	// Flags added to the _end_ of every build command. If a flag is added here, file a bug at
	// go/crostc-bug to clean it up. Use of postflags is discouraged, since it prevents users
	// from determining their own preferences for warnings/etc.
	return []string{}
}

// Full hardening.
// Temporarily disable function splitting because of chromium:434751.
var crosHardenedConfig = config{
	clangRootRelPath: "../..",
	gccRootRelPath:   "../../../../..",
	// Pass "-fcommon" till the packages are fixed to work with new clang/gcc
	// default of "-fno-common", crbug.com/1060413.
	commonFlags: []string{
		"-fcommon",
		"-fstack-protector-strong",
		"-D_FORTIFY_SOURCE=3",
		"-fno-omit-frame-pointer",
	},
	gccFlags: []string{
		"-fno-reorder-blocks-and-partition",
		"-Wno-unused-local-typedefs",
		"-Wno-maybe-uninitialized",
	},
	// 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.
	// crbug.com/1103065: -grecord-gcc-switches pollutes the Goma cache;
	//   removed that flag for now.
	clangFlags: append(
		crosCommonClangFlags(),
		"--unwindlib=libunwind",
		"-Wno-section",
		"-fno-addrsig",
		"-ftrivial-auto-var-init=zero",
	),
	clangPostFlags: crosCommonClangPostFlags(),
}

// Flags to be added to non-hardened toolchain.
var crosNonHardenedConfig = config{
	clangRootRelPath: "../..",
	gccRootRelPath:   "../../../../..",
	commonFlags:      []string{},
	gccFlags: []string{
		"-Wno-maybe-uninitialized",
		"-Wno-unused-local-typedefs",
		"-Wno-deprecated-declarations",
		"-Wtrampolines",
	},
	// Temporarily disable Wsection since kernel gets a bunch of these. chromium:778867
	clangFlags: append(
		crosCommonClangFlags(),
		"-Wno-section",
	),
	clangPostFlags: crosCommonClangPostFlags(),
}

// Flags to be added to host toolchain.
var crosHostConfig = config{
	isHostWrapper:    true,
	clangRootRelPath: "../..",
	gccRootRelPath:   "../..",
	// Pass "-fcommon" till the packages are fixed to work with new clang/gcc
	// default of "-fno-common", crbug.com/1060413.
	commonFlags: []string{
		"-fcommon",
	},
	gccFlags: []string{
		"-Wno-maybe-uninitialized",
		"-Wno-unused-local-typedefs",
		"-Wno-deprecated-declarations",
	},
	// crbug.com/1103065: -grecord-gcc-switches pollutes the Goma cache;
	//   removed that flag for now.
	clangFlags: append(
		crosCommonClangFlags(),
		"-Wno-unused-local-typedefs",
		"-fno-addrsig",
	),
	// Temporarily disable Wdeprecated-copy. b/191479033
	clangPostFlags: crosCommonClangPostFlags(),
}

var androidConfig = config{
	isHostWrapper:    false,
	isAndroidWrapper: true,
	gccRootRelPath:   "./",
	clangRootRelPath: "./",
	commonFlags:      []string{},
	gccFlags:         []string{},
	clangFlags:       []string{},
	clangPostFlags:   []string{},
}