Skip to content

Commit

Permalink
Fix autodetection of linker flags
Browse files Browse the repository at this point in the history
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 bazelbuild#5468.

RELNOTES: None.
PiperOrigin-RevId: 203341563
  • Loading branch information
hlopko authored and Copybara-Service committed Jul 5, 2018
1 parent 59f17d6 commit 8ff87c1
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions tools/cpp/unix_cc_configure.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -162,16 +162,16 @@ def _is_compiler_option_supported(repository_ctx, cc, option):
])
return result.stderr.find(option) == -1

def _is_linker_option_supported(repository_ctx, cc, option):
"""Checks that `option` is supported by the C compiler. Doesn't %-escape the option."""
def _is_linker_option_supported(repository_ctx, cc, option, pattern):
"""Checks that `option` is supported by the C linker. Doesn't %-escape the option."""
result = repository_ctx.execute([
cc,
option,
"-o",
"/dev/null",
str(repository_ctx.path("tools/cpp/empty.cc")),
])
return result.stderr.find(option) == -1
return result.stderr.find(pattern) == -1

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

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

def _get_no_canonical_prefixes_opt(repository_ctx, cc):
# If the compiler sometimes rewrites paths in the .d files without symlinks
Expand Down Expand Up @@ -270,10 +270,12 @@ def _crosstool_content(repository_ctx, cc, cpu_value, darwin):
repository_ctx,
cc,
"-Wl,-no-as-needed",
"-no-as-needed",
) + _add_linker_option_if_supported(
repository_ctx,
cc,
"-Wl,-z,relro,-z,now",
"-z,relro,-z,now",
) + (
[
"-undefined",
Expand Down Expand Up @@ -360,7 +362,12 @@ def _opt_content(repository_ctx, cc, darwin):
"-fdata-sections",
],
"linker_flag": (
[] if darwin else _add_linker_option_if_supported(repository_ctx, cc, "-Wl,--gc-sections")
[] if darwin else _add_linker_option_if_supported(
repository_ctx,
cc,
"-Wl,--gc-sections",
"-gc-sections",
)
),
}

Expand Down

0 comments on commit 8ff87c1

Please sign in to comment.