Skip to content

Commit d0e7272

Browse files
authored
Enable -c opt and -c dbg for cross compilation (#26)
The toolchain needs to explicitly add the options to enable optimization and debug flags. I found this by diffing the resulting executables from both bazel and gradle, and debugging why they were different. This should pretty closely match both the builtin toolchain flags for Bazel, and the gradle flags. Signed-off-by: Austin Schuh <[email protected]>
1 parent 91f9e89 commit d0e7272

File tree

2 files changed

+102
-12
lines changed

2 files changed

+102
-12
lines changed

toolchains/configure_cross_compiler.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ def configure_cross_compiler_impl(repository_ctx):
3737
BINARIES = [
3838
"ar",
3939
"cpp",
40+
"dwp",
4041
"gcc",
4142
"gcov",
4243
"ld",

toolchains/cross_compiler/cc-toolchain-config.bzl

Lines changed: 101 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ load(
55
"flag_group",
66
"flag_set",
77
"tool_path",
8+
"with_feature_set",
89
)
910
load("@rules_cc//cc/common:cc_common.bzl", "cc_common")
1011

@@ -41,15 +42,20 @@ def _impl(ctx):
4142
ACTION_NAMES.clif_match,
4243
]
4344

45+
# TODO(austin): Turn this on when the new toolchain is released.
46+
use_gold = False
47+
4448
tool_paths = [
4549
tool_path(name = "gcc", path = "bin/gcc" + wrapper_extension),
46-
tool_path(name = "ld", path = "bin/ld" + wrapper_extension),
50+
tool_path(name = "ld", path = "bin/ld.gold" + wrapper_extension if use_gold else "bin/ld" + wrapper_extension),
4751
tool_path(name = "ar", path = "bin/ar" + wrapper_extension),
4852
tool_path(name = "cpp", path = "bin/cpp" + wrapper_extension),
4953
tool_path(name = "gcov", path = "bin/gcov" + wrapper_extension),
5054
tool_path(name = "nm", path = "bin/nm" + wrapper_extension),
5155
tool_path(name = "objdump", path = "bin/objdump" + wrapper_extension),
5256
tool_path(name = "strip", path = "bin/strip" + wrapper_extension),
57+
tool_path(name = "dwp", path = "bin/dwp" + wrapper_extension),
58+
tool_path(name = "objcopy", path = "bin/objcopy" + wrapper_extension),
5359
]
5460

5561
unfiltered_compile_flags_feature = feature(
@@ -83,10 +89,11 @@ def _impl(ctx):
8389
actions = all_link_actions,
8490
flag_groups = ([
8591
flag_group(
86-
flags = [
87-
"-rdynamic",
92+
flags = ([
93+
# Enables --start-lib
94+
"-fuse-ld=gold",
95+
] if use_gold else []) + [
8896
"-pthread",
89-
"-ldl",
9097
"-latomic",
9198
"-lstdc++",
9299
"-lm",
@@ -105,28 +112,78 @@ def _impl(ctx):
105112
flag_set(
106113
actions = all_compile_actions,
107114
flag_groups = [
115+
flag_group(
116+
# Security hardening requires optimization.
117+
# We need to undef it as some distributions now have it enabled by default.
118+
flags = ["-U_FORTIFY_SOURCE"],
119+
),
120+
],
121+
with_features = [
122+
with_feature_set(
123+
not_features = ["thin_lto"],
124+
),
125+
],
126+
),
127+
flag_set(
128+
actions = all_compile_actions,
129+
flag_groups = ([
108130
flag_group(
109131
flags = [
110132
"-Wformat=2",
111133
"-pedantic",
112134
"-Wno-psabi",
113135
"-Wno-unused-parameter",
114-
"-fPIC",
115-
"-rdynamic",
116136
"-pthread",
137+
"-fstack-protector",
138+
"-Wall",
139+
"-fno-omit-frame-pointer",
117140
],
118141
),
119-
],
142+
]),
120143
),
121144
flag_set(
122-
actions = all_cpp_compile_actions,
123-
flag_groups = [
145+
actions = all_compile_actions,
146+
flag_groups = ([
124147
flag_group(
125148
flags = [
126-
"-lstdc++",
149+
"-g",
150+
"-Og",
151+
"-g",
152+
"-gz=zlib",
153+
"-ffunction-sections",
154+
"-fdata-sections",
127155
],
128156
),
129-
],
157+
]),
158+
with_features = [with_feature_set(features = ["dbg"])],
159+
),
160+
flag_set(
161+
actions = all_compile_actions,
162+
flag_groups = ([
163+
flag_group(
164+
flags = [
165+
"-g0",
166+
"-O2",
167+
"-D_FORTIFY_SOURCE=1",
168+
"-DNDEBUG",
169+
"-ffunction-sections",
170+
"-fdata-sections",
171+
],
172+
),
173+
]),
174+
with_features = [with_feature_set(features = ["opt"])],
175+
),
176+
flag_set(
177+
actions = [ACTION_NAMES.c_compile],
178+
flag_groups = [],
179+
),
180+
flag_set(
181+
actions = all_cpp_compile_actions + [ACTION_NAMES.lto_backend],
182+
flag_groups = ([
183+
flag_group(
184+
flags = ["-std=c++20"],
185+
),
186+
]),
130187
),
131188
],
132189
)
@@ -159,12 +216,44 @@ def _impl(ctx):
159216
enabled = True,
160217
)
161218

162-
features = [
219+
supports_pic_feature = feature(
220+
name = "supports_pic",
221+
enabled = True,
222+
)
223+
224+
features = []
225+
if use_gold:
226+
supports_start_end_lib_feature = feature(
227+
name = "supports_start_end_lib",
228+
enabled = True,
229+
)
230+
features.append(supports_start_end_lib_feature)
231+
232+
gcc_quoting_for_param_files_feature = feature(
233+
name = "gcc_quoting_for_param_files",
234+
enabled = True,
235+
)
236+
237+
static_link_cpp_runtimes_feature = feature(
238+
name = "static_link_cpp_runtimes",
239+
enabled = False,
240+
)
241+
242+
dbg_feature = feature(name = "dbg")
243+
244+
opt_feature = feature(name = "opt")
245+
246+
features += [
163247
unfiltered_compile_flags_feature,
164248
default_link_flags_feature,
165249
default_compile_flags_feature,
166250
sysroot_feature,
251+
dbg_feature,
252+
opt_feature,
167253
compiler_param_feature,
254+
supports_pic_feature,
255+
gcc_quoting_for_param_files_feature,
256+
static_link_cpp_runtimes_feature,
168257
archive_param_file_feature,
169258
]
170259

0 commit comments

Comments
 (0)