blob: 9256559272d93ff5b1f90573d70a31b516ab323a [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 Boschaa311162019-06-20 17:47:19 -070012 // Whether to use ccache.
13 useCCache bool
Tobias Boschef8f9692019-06-10 15:50:33 -070014 // Flags to add to gcc and clang.
15 commonFlags []string
16 // Flags to add to gcc only.
17 gccFlags []string
18 // Flags to add to clang only.
19 clangFlags []string
20 // Toolchain root path relative to the wrapper binary.
21 rootRelPath string
22 // Path of the old wrapper using the toolchain root.
Tobias Bosch900dbc92019-06-24 09:31:39 -070023 oldWrapperPath string
24 // Whether to mock out the calls that the old wrapper does.
25 mockOldWrapperCmds bool
Tobias Boschf6d9f4f2019-07-09 08:09:01 -070026 // Directory to store errors that were prevented with -Wno-error.
27 newWarningsDir string
Tobias Boschef8f9692019-06-10 15:50:33 -070028}
29
Tobias Boschaa311162019-06-20 17:47:19 -070030// UseCCache can be set via a linker flag.
31// Value will be passed to strconv.ParseBool.
32// E.g. go build -ldflags '-X config.UseCCache=true'.
33var UseCCache = "unknown"
34
35// ConfigName can be set via a linker flag.
36// Value has to be one of:
37// - "cros.hardened"
38// - "cros.nonhardened"
39var ConfigName = "unknown"
40
41// Returns the configuration matching the UseCCache and ConfigName.
42func getRealConfig() (*config, error) {
43 useCCache, err := strconv.ParseBool(UseCCache)
44 if err != nil {
Tobias Bosch900dbc92019-06-24 09:31:39 -070045 return nil, wrapErrorwithSourceLocf(err, "invalid format for UseCCache")
Tobias Boschaa311162019-06-20 17:47:19 -070046 }
47 config, err := getConfig(useCCache, ConfigName)
48 if err != nil {
49 return nil, err
50 }
51 return config, nil
52}
53
54func getConfig(useCCache bool, configName string) (*config, error) {
55 switch configName {
56 case "cros.hardened":
57 return getCrosHardenedConfig(useCCache), nil
58 case "cros.nonhardened":
59 return getCrosNonHardenedConfig(useCCache), nil
60 default:
Tobias Bosch900dbc92019-06-24 09:31:39 -070061 return nil, newErrorwithSourceLocf("unknown config name: %s", configName)
Tobias Boschaa311162019-06-20 17:47:19 -070062 }
63}
64
Tobias Boschef8f9692019-06-10 15:50:33 -070065// Full hardening.
Tobias Boschaa311162019-06-20 17:47:19 -070066func getCrosHardenedConfig(useCCache bool) *config {
67 // Temporarily disable function splitting because of chromium:434751.
68 return &config{
69 useCCache: useCCache,
70 rootRelPath: "../../../../..",
71 oldWrapperPath: "./sysroot_wrapper.hardened.old",
72 commonFlags: []string{
Tobias Boschaa311162019-06-20 17:47:19 -070073 "-fstack-protector-strong",
Tobias Bosch198a3c92019-07-17 04:22:34 -070074 "-fPIE",
Tobias Boschaa311162019-06-20 17:47:19 -070075 "-pie",
Tobias Bosch198a3c92019-07-17 04:22:34 -070076 "-D_FORTIFY_SOURCE=2",
Tobias Boschaa311162019-06-20 17:47:19 -070077 "-fno-omit-frame-pointer",
78 },
79 gccFlags: []string{
Tobias Bosch198a3c92019-07-17 04:22:34 -070080 "-fno-reorder-blocks-and-partition",
Tobias Boschaa311162019-06-20 17:47:19 -070081 "-Wno-unused-local-typedefs",
82 "-Wno-maybe-uninitialized",
Tobias Boschaa311162019-06-20 17:47:19 -070083 },
84 // Temporarily disable tautological-*-compare chromium:778316.
85 // Temporarily add no-unknown-warning-option to deal with old clang versions.
86 // Temporarily disable Wsection since kernel gets a bunch of these. chromium:778867
87 // Disable "-faddrsig" since it produces object files that strip doesn't understand, chromium:915742.
88 clangFlags: []string{
Tobias Boschaa311162019-06-20 17:47:19 -070089 "-Qunused-arguments",
90 "-grecord-gcc-switches",
Tobias Boschaa311162019-06-20 17:47:19 -070091 "-fno-addrsig",
92 "-Wno-tautological-constant-compare",
Tobias Bosch198a3c92019-07-17 04:22:34 -070093 "-Wno-tautological-unsigned-enum-zero-compare",
94 "-Wno-unknown-warning-option",
95 "-Wno-section",
Tobias Boschaa311162019-06-20 17:47:19 -070096 },
Tobias Boschf6d9f4f2019-07-09 08:09:01 -070097 newWarningsDir: "/tmp/fatal_clang_warnings",
Tobias Boschaa311162019-06-20 17:47:19 -070098 }
Tobias Boschef8f9692019-06-10 15:50:33 -070099}
100
101// Flags to be added to non-hardened toolchain.
Tobias Boschaa311162019-06-20 17:47:19 -0700102func getCrosNonHardenedConfig(useCCache bool) *config {
103 return &config{
104 useCCache: useCCache,
105 rootRelPath: "../../../../..",
106 oldWrapperPath: "./sysroot_wrapper.old",
107 commonFlags: []string{},
108 gccFlags: []string{
Tobias Boschaa311162019-06-20 17:47:19 -0700109 "-Wno-maybe-uninitialized",
Tobias Bosch198a3c92019-07-17 04:22:34 -0700110 "-Wno-unused-local-typedefs",
Tobias Boschaa311162019-06-20 17:47:19 -0700111 "-Wno-deprecated-declarations",
Tobias Bosch198a3c92019-07-17 04:22:34 -0700112 "-Wtrampolines",
Tobias Boschaa311162019-06-20 17:47:19 -0700113 },
114 // Temporarily disable tautological-*-compare chromium:778316.
115 // Temporarily add no-unknown-warning-option to deal with old clang versions.
116 // Temporarily disable Wsection since kernel gets a bunch of these. chromium:778867
117 clangFlags: []string{
Tobias Boschaa311162019-06-20 17:47:19 -0700118 "-Qunused-arguments",
Tobias Boschaa311162019-06-20 17:47:19 -0700119 "-Wno-tautological-constant-compare",
Tobias Bosch198a3c92019-07-17 04:22:34 -0700120 "-Wno-tautological-unsigned-enum-zero-compare",
121 "-Wno-unknown-warning-option",
122 "-Wno-section",
Tobias Boschaa311162019-06-20 17:47:19 -0700123 },
Tobias Boschf6d9f4f2019-07-09 08:09:01 -0700124 newWarningsDir: "/tmp/fatal_clang_warnings",
Tobias Boschaa311162019-06-20 17:47:19 -0700125 }
Tobias Boschef8f9692019-06-10 15:50:33 -0700126}