blob: a122b5a22e85c0bbce8e8aa4844b8e18910efc95 [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
George Burgess IV2efe72e2020-06-18 20:37:28 -070073func isAndroidConfig() bool {
74 return ConfigName == "android"
75}
76
Tobias Bosch8dd67e12019-10-28 14:26:51 -070077func getConfig(configName string, useCCache bool, useLlvmNext bool, version string) (*config, error) {
Tobias Bosch8bb8b5b2019-09-26 09:45:35 -070078 cfg := config{}
Tobias Boschaa311162019-06-20 17:47:19 -070079 switch configName {
80 case "cros.hardened":
Tobias Bosch8bb8b5b2019-09-26 09:45:35 -070081 cfg = *crosHardenedConfig
Tobias Boschaa311162019-06-20 17:47:19 -070082 case "cros.nonhardened":
Tobias Bosch8bb8b5b2019-09-26 09:45:35 -070083 cfg = *crosNonHardenedConfig
Tobias Bosch31dec2c2019-07-18 07:34:03 -070084 case "cros.host":
Tobias Bosch8bb8b5b2019-09-26 09:45:35 -070085 cfg = *crosHostConfig
Tobias Boschbbb3c812019-09-30 10:11:25 -070086 case "android":
87 cfg = *androidConfig
Tobias Boschaa311162019-06-20 17:47:19 -070088 default:
Tobias Bosch900dbc92019-06-24 09:31:39 -070089 return nil, newErrorwithSourceLocf("unknown config name: %s", configName)
Tobias Boschaa311162019-06-20 17:47:19 -070090 }
Tobias Boschd8aa0d02019-08-19 09:55:30 -070091 cfg.useCCache = useCCache
Pirama Arumuga Nainaree7e22c2020-02-20 15:08:30 -080092 cfg.useLlvmNext = useLlvmNext
Tobias Bosch8bb8b5b2019-09-26 09:45:35 -070093 if useLlvmNext {
94 cfg.clangFlags = append(cfg.clangFlags, llvmNextFlags...)
95 }
Tobias Boschd8aa0d02019-08-19 09:55:30 -070096 cfg.version = version
Tobias Bosch8bb8b5b2019-09-26 09:45:35 -070097 return &cfg, nil
98}
99
Tobias Boschef8f9692019-06-10 15:50:33 -0700100// Full hardening.
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700101// Temporarily disable function splitting because of chromium:434751.
102var crosHardenedConfig = &config{
103 rootRelPath: "../../../../..",
104 commonFlags: []string{
105 "-fstack-protector-strong",
106 "-fPIE",
107 "-pie",
108 "-D_FORTIFY_SOURCE=2",
109 "-fno-omit-frame-pointer",
110 },
111 gccFlags: []string{
112 "-fno-reorder-blocks-and-partition",
113 "-Wno-unused-local-typedefs",
114 "-Wno-maybe-uninitialized",
115 },
116 // Temporarily disable tautological-*-compare chromium:778316.
117 // Temporarily add no-unknown-warning-option to deal with old clang versions.
118 // Temporarily disable Wsection since kernel gets a bunch of these. chromium:778867
119 // Disable "-faddrsig" since it produces object files that strip doesn't understand, chromium:915742.
Manoj Gupta99b3ff92020-03-13 10:33:13 -0700120 // Pass "-fcommon" till the packages are fixed to work with new clang default
121 // "-fno-common", crbug.com/1060413.
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700122 clangFlags: []string{
123 "-Qunused-arguments",
124 "-grecord-gcc-switches",
125 "-fno-addrsig",
Manoj Gupta99b3ff92020-03-13 10:33:13 -0700126 "-fcommon",
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700127 "-Wno-tautological-constant-compare",
128 "-Wno-tautological-unsigned-enum-zero-compare",
129 "-Wno-unknown-warning-option",
130 "-Wno-section",
131 "-static-libgcc",
Manoj Gupta802e83e2019-09-13 13:08:14 -0700132 "-fuse-ld=lld",
Caroline Ticeb9228602019-10-29 13:21:19 -0700133 "-Wno-final-dtor-non-final-class",
Caroline Ticeb9228602019-10-29 13:21:19 -0700134 "-Werror=poison-system-directories",
135 },
136 clangPostFlags: []string{
137 "-Wno-implicit-int-float-conversion",
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700138 },
139 newWarningsDir: "/tmp/fatal_clang_warnings",
Tobias Boschef8f9692019-06-10 15:50:33 -0700140}
141
142// Flags to be added to non-hardened toolchain.
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700143var crosNonHardenedConfig = &config{
144 rootRelPath: "../../../../..",
145 commonFlags: []string{},
146 gccFlags: []string{
147 "-Wno-maybe-uninitialized",
148 "-Wno-unused-local-typedefs",
149 "-Wno-deprecated-declarations",
150 "-Wtrampolines",
151 },
152 // Temporarily disable tautological-*-compare chromium:778316.
153 // Temporarily add no-unknown-warning-option to deal with old clang versions.
154 // Temporarily disable Wsection since kernel gets a bunch of these. chromium:778867
155 clangFlags: []string{
156 "-Qunused-arguments",
157 "-Wno-tautological-constant-compare",
158 "-Wno-tautological-unsigned-enum-zero-compare",
159 "-Wno-unknown-warning-option",
160 "-Wno-section",
161 "-static-libgcc",
Caroline Ticeb9228602019-10-29 13:21:19 -0700162 "-Wno-final-dtor-non-final-class",
Caroline Ticeb9228602019-10-29 13:21:19 -0700163 "-Werror=poison-system-directories",
164 },
165 clangPostFlags: []string{
166 "-Wno-implicit-int-float-conversion",
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700167 },
168 newWarningsDir: "/tmp/fatal_clang_warnings",
Tobias Boschef8f9692019-06-10 15:50:33 -0700169}
Tobias Bosch31dec2c2019-07-18 07:34:03 -0700170
171// Flags to be added to host toolchain.
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700172var crosHostConfig = &config{
173 isHostWrapper: true,
174 rootRelPath: "../..",
175 commonFlags: []string{},
176 gccFlags: []string{
177 "-Wno-maybe-uninitialized",
178 "-Wno-unused-local-typedefs",
179 "-Wno-deprecated-declarations",
180 },
181 // Temporarily disable tautological-*-compare chromium:778316.
182 // Temporarily add no-unknown-warning-option to deal with old clang versions.
Manoj Gupta99b3ff92020-03-13 10:33:13 -0700183 // Pass "-fcommon" till the packages are fixed to work with new clang default
184 // "-fno-common", crbug.com/1060413.
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700185 clangFlags: []string{
186 "-Qunused-arguments",
187 "-grecord-gcc-switches",
188 "-fno-addrsig",
Manoj Gupta99b3ff92020-03-13 10:33:13 -0700189 "-fcommon",
Manoj Gupta802e83e2019-09-13 13:08:14 -0700190 "-fuse-ld=lld",
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700191 "-Wno-unused-local-typedefs",
192 "-Wno-deprecated-declarations",
193 "-Wno-tautological-constant-compare",
194 "-Wno-tautological-unsigned-enum-zero-compare",
Caroline Ticeb9228602019-10-29 13:21:19 -0700195 "-Wno-final-dtor-non-final-class",
Caroline Ticeb9228602019-10-29 13:21:19 -0700196 "-Werror=poison-system-directories",
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700197 "-Wno-unknown-warning-option",
198 },
Caroline Ticeb9228602019-10-29 13:21:19 -0700199 clangPostFlags: []string{
200 "-Wno-implicit-int-float-conversion",
201 },
Tobias Boschd8aa0d02019-08-19 09:55:30 -0700202 newWarningsDir: "/tmp/fatal_clang_warnings",
Tobias Bosch31dec2c2019-07-18 07:34:03 -0700203}
Tobias Boschbbb3c812019-09-30 10:11:25 -0700204
205var androidConfig = &config{
206 isHostWrapper: false,
207 isAndroidWrapper: true,
208 rootRelPath: "./",
209 commonFlags: []string{},
210 gccFlags: []string{},
211 clangFlags: []string{},
Caroline Tice8ac33e02019-10-28 09:48:18 -0700212 clangPostFlags: []string{},
Pirama Arumuga Nainaree7e22c2020-02-20 15:08:30 -0800213 newWarningsDir: "",
Tobias Boschbbb3c812019-09-30 10:11:25 -0700214}