Skip to content

Commit

Permalink
Pass -undefined dynamic_lookup to dynamic library linking actions on Mac
Browse files Browse the repository at this point in the history
This change fixes a bug introduced in 2d0e27e. Before, we allowed dynamic libraries to have undefined symbols on mac, but after that change this stopped working.

Fixes #7607.

RELNOTES: None.
PiperOrigin-RevId: 238644308
  • Loading branch information
hlopko authored and katre committed Mar 19, 2019
1 parent 60b6d4d commit 906f6a2
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
48 changes: 48 additions & 0 deletions src/test/shell/bazel/cc_integration_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,52 @@ EOF
bazel build :ok || fail "Should have found include at synthetic path"
}

# This test tests that Bazel can produce dynamic libraries that have undefined
# symbols on Mac and Linux. Not sure it is a sane default to allow undefined
# symbols, but it's the default we had historically. This test creates
# an executable (main) that defines bar(), and a shared library (plugin) that
# calls bar(). When linking the libplugin.so, symbol 'bar' is undefined.
# +-----------------------------+ +----------------------------------+
# | main | | libplugin.so |
# | | | |
# | main() { return foo(); } +---------> foo() { return bar() - 42; } |
# | | | + |
# | | | | |
# | bar() { return 42; } <------------------+ |
# | | | |
# +-----------------------------+ +----------------------------------+
function test_undefined_dynamic_lookup() {
if is_windows; then
# Windows doesn't allow undefined symbols in shared libraries.
return 0
fi
mkdir -p "dynamic_lookup"
cat > "dynamic_lookup/BUILD" <<EOF
cc_binary(
name = "libplugin.so",
srcs = ["plugin.cc"],
linkshared = 1,
)
cc_binary(
name = "main",
srcs = ["main.cc", "libplugin.so"],
)
EOF

cat > "dynamic_lookup/plugin.cc" <<EOF
int bar();
int foo() { return bar() - 42; }
EOF

cat > "dynamic_lookup/main.cc" <<EOF
int foo();
int bar() { return 42; }
int main() { return foo(); }
EOF

bazel build //dynamic_lookup:main || fail "Bazel couldn't build the binary."
bazel run //dynamic_lookup:main || fail "Run of the binary failed."
}

run_suite "cc_integration_test"
6 changes: 4 additions & 2 deletions tools/osx/crosstool/cc_toolchain_config.bzl.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -3517,13 +3517,15 @@ def _impl(ctx):
],
),
flag_set(
actions = [ACTION_NAMES.cpp_link_nodeps_dynamic_library],
actions = [
ACTION_NAMES.cpp_link_dynamic_library,
ACTION_NAMES.cpp_link_nodeps_dynamic_library,
],
flag_groups = [flag_group(flags = ["-undefined", "dynamic_lookup"])],
),
flag_set(
actions = [
ACTION_NAMES.cpp_link_executable,
ACTION_NAMES.cpp_link_dynamic_library,
"objc-executable",
"objc++-executable",
],
Expand Down

0 comments on commit 906f6a2

Please sign in to comment.