Skip to content

Commit 94b7830

Browse files
hlopkolaurentlb
authored andcommitted
Fix autodetection of linker flags
Flags passed through clang to linker get -Wl, stripped in the error message (e.g. -Wl,-no-as-needed will be reported as "ld: unknwon option: -no-as-needed"). This cl fixes the autodetection to expect the stripped variant. Fixes #5468. RELNOTES: None. PiperOrigin-RevId: 203341563
1 parent 2ff8c5f commit 94b7830

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

tools/cpp/unix_cc_configure.bzl

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -162,16 +162,16 @@ def _is_compiler_option_supported(repository_ctx, cc, option):
162162
])
163163
return result.stderr.find(option) == -1
164164

165-
def _is_linker_option_supported(repository_ctx, cc, option):
166-
"""Checks that `option` is supported by the C compiler. Doesn't %-escape the option."""
165+
def _is_linker_option_supported(repository_ctx, cc, option, pattern):
166+
"""Checks that `option` is supported by the C linker. Doesn't %-escape the option."""
167167
result = repository_ctx.execute([
168168
cc,
169169
option,
170170
"-o",
171171
"/dev/null",
172172
str(repository_ctx.path("tools/cpp/empty.cc")),
173173
])
174-
return result.stderr.find(option) == -1
174+
return result.stderr.find(pattern) == -1
175175

176176
def _is_gold_supported(repository_ctx, cc):
177177
"""Checks that `gold` is supported by the C compiler."""
@@ -193,9 +193,9 @@ def _add_compiler_option_if_supported(repository_ctx, cc, option):
193193
"""Returns `[option]` if supported, `[]` otherwise. Doesn't %-escape the option."""
194194
return [option] if _is_compiler_option_supported(repository_ctx, cc, option) else []
195195

196-
def _add_linker_option_if_supported(repository_ctx, cc, option):
196+
def _add_linker_option_if_supported(repository_ctx, cc, option, pattern):
197197
"""Returns `[option]` if supported, `[]` otherwise. Doesn't %-escape the option."""
198-
return [option] if _is_linker_option_supported(repository_ctx, cc, option) else []
198+
return [option] if _is_linker_option_supported(repository_ctx, cc, option, pattern) else []
199199

200200
def _get_no_canonical_prefixes_opt(repository_ctx, cc):
201201
# If the compiler sometimes rewrites paths in the .d files without symlinks
@@ -270,10 +270,12 @@ def _crosstool_content(repository_ctx, cc, cpu_value, darwin):
270270
repository_ctx,
271271
cc,
272272
"-Wl,-no-as-needed",
273+
"-no-as-needed",
273274
) + _add_linker_option_if_supported(
274275
repository_ctx,
275276
cc,
276277
"-Wl,-z,relro,-z,now",
278+
"-z,relro,-z,now",
277279
) + (
278280
[
279281
"-undefined",
@@ -360,7 +362,12 @@ def _opt_content(repository_ctx, cc, darwin):
360362
"-fdata-sections",
361363
],
362364
"linker_flag": (
363-
[] if darwin else _add_linker_option_if_supported(repository_ctx, cc, "-Wl,--gc-sections")
365+
[] if darwin else _add_linker_option_if_supported(
366+
repository_ctx,
367+
cc,
368+
"-Wl,--gc-sections",
369+
"-gc-sections",
370+
)
364371
),
365372
}
366373

0 commit comments

Comments
 (0)