blob: e82e6b3dcb26a27d77cd8723aebcc2ec6b454343 [file] [log] [blame]
Tobias Boschcfa8c242019-07-19 03:29:40 -07001// Copyright 2019 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Tobias Boschef8f9692019-06-10 15:50:33 -07005package main
6
Tobias Boschaa311162019-06-20 17:47:19 -07007import (
Tobias Boschaa311162019-06-20 17:47:19 -07008 "strconv"
9)
10
Tobias Boschef8f9692019-06-10 15:50:33 -070011type config struct {
Tobias Bosch31dec2c2019-07-18 07:34:03 -070012 // TODO: Refactor this flag into more generic configuration properties.
Tobias Boschbbb3c812019-09-30 10:11:25 -070013 isHostWrapper bool
14 isAndroidWrapper bool
Tobias Boschaa311162019-06-20 17:47:19 -070015 // Whether to use ccache.
16 useCCache bool
Pirama Arumuga Nainaree7e22c2020-02-20 15:08:30 -080017 // Whether llvmNext wrapper.
18 useLlvmNext bool
Tobias Boschef8f9692019-06-10 15:50:33 -070019 // Flags to add to gcc and clang.
20 commonFlags []string
21 // Flags to add to gcc only.
22 gccFlags []string
23 // Flags to add to clang only.
24 clangFlags []string
Caroline Tice8ac33e02019-10-28 09:48:18 -070025 // Flags to add to clang only, AFTER user flags (cannot be overridden
26 // by the user).
27 clangPostFlags []string
Tobias Boschef8f9692019-06-10 15:50:33 -070028 // Toolchain root path relative to the wrapper binary.
29 rootRelPath string
Tobias Boschf6d9f4f2019-07-09 08:09:01 -070030 // Directory to store errors that were prevented with -Wno-error.
31 newWarningsDir string
Tobias Boschd8aa0d02019-08-19 09:55:30 -070032 // Version. Only used for printing via -print-cmd.
33 version string
Tobias Boschef8f9692019-06-10 15:50:33 -070034}
35
Tobias Boschd8aa0d02019-08-19 09:55:30 -070036// Version can be set via a linker flag.
37// Values fills config.version.
38var Version = ""
39
Tobias Boschaa311162019-06-20 17:47:19 -070040// UseCCache can be set via a linker flag.
41// Value will be passed to strconv.ParseBool.
42// E.g. go build -ldflags '-X config.UseCCache=true'.
43var UseCCache = "unknown"
44
Tobias Bosch8bb8b5b2019-09-26 09:45:35 -070045// UseLlvmNext can be set via a linker flag.
46// Value will be passed to strconv.ParseBool.
47// E.g. go build -ldflags '-X config.UseLlvmNext=true'.
48var UseLlvmNext = "unknown"
49
Tobias Boschaa311162019-06-20 17:47:19 -070050// ConfigName can be set via a linker flag.
51// Value has to be one of:
52// - "cros.hardened"
53// - "cros.nonhardened"
54var ConfigName = "unknown"
55
56// Returns the configuration matching the UseCCache and ConfigName.
57func getRealConfig() (*config, error) {
58 useCCache, err := strconv.ParseBool(UseCCache)
59 if err != nil {
Tobias Bosch900dbc92019-06-24 09:31:39 -070060 return nil, wrapErrorwithSourceLocf(err, "invalid format for UseCCache")
Tobias Boschaa311162019-06-20 17:47:19 -070061 }
Tobias Bosch8bb8b5b2019-09-26 09:45:35 -070062 useLlvmNext, err := strconv.ParseBool(UseLlvmNext)
63 if err != nil {
64 return nil, wrapErrorwithSourceLocf(err, "invalid format for UseLLvmNext")
65 }
Tobias Bosch8dd67e12019-10-28 14:26:51 -070066 config, err := getConfig(ConfigName, useCCache, useLlvmNext, Version)
Tobias Boschaa311162019-06-20 17:47:19 -070067 if err != nil {
68 return nil, err
69 }
70 return config, nil
71}
72
Tobias Bosch8dd67e12019-10-28 14:26:51 -070073func getConfig(configName string, useCCache bool, useLlvmNext bool, version string) (*config, error) {
Tobias Bosch8bb8b5b2019-09-26 09:45:35 -070074 cfg := config{}
Tobias Boschaa311162019-06-20 17:47:19 -070075 switch configName {
76 case "cros.hardened":
Tobias Bosch8bb8b5b2019-09-26 09:45:35 -070077 cfg = *crosHardenedConfig
Tobias Boschaa311162019-06-20 17:47:19 -070078 case "cros.nonhardened":
Tobias Bosch8bb8b5b2019-09-26 09:45:35 -070079 cfg = *crosNonHardenedConfig
Tobias Bosch31dec2c2019-07-18 07:34:03 -070080 case "cros.host":
Tobias Bosch8bb8b5b2019-09-26 09:45:35 -070081 cfg = *crosHostConfig
Tobias Boschbbb3c812019-09-30 10:11:25 -070082 case "android":
83 cfg = *androidConfig
Tobias Boschaa311162019-06-20 17:47:19 -070084 default:
Tobias Bosch900dbc92019-06-24 09:31:39 -070085 return nil, newErrorwithSourceLocf("unknown config name: %s", configName)
Tobias Boschaa311162019-06-20 17:47:19 -070086 }
Tobias Boschd8aa0d02019-08-19 09:55:30 -070087 cfg.useCCache = useCCache
Pirama Arumuga Nainaree7e22c2020-02-20 15:08:30 -080088 cfg.useLlvmNext = useLlvmNext
Tobias Bosch8bb8b5b2019-09-26 09:45:35 -070089 if useLlvmNext {
90 cfg.clangFlags = append(cfg.clangFlags, llvmNextFlags...)
91 }
Tobias Boschd8aa0d02019-08-19 09:55:30 -070092 cfg.version = version
Tobias Bosch8bb8b5b2019-09-26 09:45:35 -070093 return &cfg, nil
94}
95
Tobias Boschef8f9692019-06-10 15:50:33 -070096// Full hardening.
Tobias Boschd8aa0d02019-08-19 09:55:30 -070097// Temporarily disable function splitting because of chromium:434751.
98var crosHardenedConfig = &config{
99 rootRelPath: "../../../../..",
100 commonFlags: []string{
101 "-fstack-protector-strong",
102 "-fPIE",
103 "-pie",
104 "-D_FORTIFY_SOURCE=2",
105 "-fno-omit-frame-pointer",
106 },
107 gccFlags: []string{
108 "-fno-reorder-blocks-and-partition",
109 "-Wno-unused-local-typedefs",
110 "-Wno-maybe-uninitialized",
111 },
112 // Temporarily disable tautological-*-compare chromium:778316.
113 // Temporarily add no-unknown-warning-option to deal with old clang versions.
114 // Temporarily disable Wsection since kernel gets a bunch of these. chromium:778867
115 // Disable "-faddrsig" since it produces object files that strip doesn't understand, chromium:915742.
Manoj Gupta99b3ff92020-03-13 10:33:13 -0700116 // Pass "-fcommon" till the packages are fixed to work with new clang default
117 // "-fno-common", crbug.com/1060413.
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700118 clangFlags: []string{
119 "-Qunused-arguments",
120 "-grecord-gcc-switches",
121 "-fno-addrsig",
Manoj Gupta99b3ff92020-03-13 10:33:13 -0700122 "-fcommon",
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700123 "-Wno-tautological-constant-compare",
124 "-Wno-tautological-unsigned-enum-zero-compare",
125 "-Wno-unknown-warning-option",
126 "-Wno-section",
127 "-static-libgcc",
Manoj Gupta802e83e2019-09-13 13:08:14 -0700128 "-fuse-ld=lld",
Caroline Ticeb9228602019-10-29 13:21:19 -0700129 "-Wno-final-dtor-non-final-class",
Caroline Ticeb9228602019-10-29 13:21:19 -0700130 "-Werror=poison-system-directories",
131 },
132 clangPostFlags: []string{
133 "-Wno-implicit-int-float-conversion",
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700134 },
135 newWarningsDir: "/tmp/fatal_clang_warnings",
Tobias Boschef8f9692019-06-10 15:50:33 -0700136}
137
138// Flags to be added to non-hardened toolchain.
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700139var crosNonHardenedConfig = &config{
140 rootRelPath: "../../../../..",
141 commonFlags: []string{},
142 gccFlags: []string{
143 "-Wno-maybe-uninitialized",
144 "-Wno-unused-local-typedefs",
145 "-Wno-deprecated-declarations",
146 "-Wtrampolines",
147 },
148 // Temporarily disable tautological-*-compare chromium:778316.
149 // Temporarily add no-unknown-warning-option to deal with old clang versions.
150 // Temporarily disable Wsection since kernel gets a bunch of these. chromium:778867
151 clangFlags: []string{
152 "-Qunused-arguments",
153 "-Wno-tautological-constant-compare",
154 "-Wno-tautological-unsigned-enum-zero-compare",
155 "-Wno-unknown-warning-option",
156 "-Wno-section",
157 "-static-libgcc",
Caroline Ticeb9228602019-10-29 13:21:19 -0700158 "-Wno-final-dtor-non-final-class",
Caroline Ticeb9228602019-10-29 13:21:19 -0700159 "-Werror=poison-system-directories",
160 },
161 clangPostFlags: []string{
162 "-Wno-implicit-int-float-conversion",
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700163 },
164 newWarningsDir: "/tmp/fatal_clang_warnings",
Tobias Boschef8f9692019-06-10 15:50:33 -0700165}
Tobias Bosch31dec2c2019-07-18 07:34:03 -0700166
167// Flags to be added to host toolchain.
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700168var crosHostConfig = &config{
169 isHostWrapper: true,
170 rootRelPath: "../..",
171 commonFlags: []string{},
172 gccFlags: []string{
173 "-Wno-maybe-uninitialized",
174 "-Wno-unused-local-typedefs",
175 "-Wno-deprecated-declarations",
176 },
177 // Temporarily disable tautological-*-compare chromium:778316.
178 // Temporarily add no-unknown-warning-option to deal with old clang versions.
Manoj Gupta99b3ff92020-03-13 10:33:13 -0700179 // Pass "-fcommon" till the packages are fixed to work with new clang default
180 // "-fno-common", crbug.com/1060413.
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700181 clangFlags: []string{
182 "-Qunused-arguments",
183 "-grecord-gcc-switches",
184 "-fno-addrsig",
Manoj Gupta99b3ff92020-03-13 10:33:13 -0700185 "-fcommon",
Manoj Gupta802e83e2019-09-13 13:08:14 -0700186 "-fuse-ld=lld",
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700187 "-Wno-unused-local-typedefs",
188 "-Wno-deprecated-declarations",
189 "-Wno-tautological-constant-compare",
190 "-Wno-tautological-unsigned-enum-zero-compare",
Caroline Ticeb9228602019-10-29 13:21:19 -0700191 "-Wno-final-dtor-non-final-class",
Caroline Ticeb9228602019-10-29 13:21:19 -0700192 "-Werror=poison-system-directories",
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700193 "-Wno-unknown-warning-option",
194 },
Caroline Ticeb9228602019-10-29 13:21:19 -0700195 clangPostFlags: []string{
196 "-Wno-implicit-int-float-conversion",
197 },
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700198 newWarningsDir: "/tmp/fatal_clang_warnings",
Tobias Bosch31dec2c2019-07-18 07:34:03 -0700199}
Tobias Boschbbb3c812019-09-30 10:11:25 -0700200
201var androidConfig = &config{
202 isHostWrapper: false,
203 isAndroidWrapper: true,
204 rootRelPath: "./",
205 commonFlags: []string{},
206 gccFlags: []string{},
207 clangFlags: []string{},
Caroline Tice8ac33e02019-10-28 09:48:18 -0700208 clangPostFlags: []string{},
Pirama Arumuga Nainaree7e22c2020-02-20 15:08:30 -0800209 newWarningsDir: "",
Tobias Boschbbb3c812019-09-30 10:11:25 -0700210}