From 149c67d036883bf6b707e43940483735fa4b5379 Mon Sep 17 00:00:00 2001 From: Gabriel Feron Date: Wed, 2 Nov 2022 11:31:44 +0100 Subject: [PATCH 01/11] Skip adding -lstatic to libtest and libstd on macOS --- rust/private/rustc.bzl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/rust/private/rustc.bzl b/rust/private/rustc.bzl index 7a05798d32..47630c57cb 100644 --- a/rust/private/rustc.bzl +++ b/rust/private/rustc.bzl @@ -1609,7 +1609,7 @@ def _get_crate_dirname(crate): """ return crate.output.dirname -def _portable_link_flags(lib, use_pic, ambiguous_libs, get_lib_name, for_windows): +def _portable_link_flags(lib, use_pic, ambiguous_libs, get_lib_name, for_windows=False, for_macos=False): artifact = get_preferred_artifact(lib, use_pic) if ambiguous_libs and artifact.path in ambiguous_libs: artifact = ambiguous_libs[artifact.path] @@ -1647,7 +1647,7 @@ def _portable_link_flags(lib, use_pic, ambiguous_libs, get_lib_name, for_windows artifact.basename.startswith("libtest-") or artifact.basename.startswith("libstd-") or artifact.basename.startswith("test-") or artifact.basename.startswith("std-") ): - return ["-lstatic=%s" % get_lib_name(artifact)] + return [] if for_macos else ["-lstatic=%s" % get_lib_name(artifact)] return [ "-lstatic=%s" % get_lib_name(artifact), "-Clink-arg=-l%s" % (get_lib_name(artifact) if not for_windows else artifact.basename), @@ -1679,7 +1679,7 @@ def _make_link_flags_darwin(linker_input_and_use_pic_and_ambiguous_libs): ("link-arg=-Wl,-force_load,%s" % get_preferred_artifact(lib, use_pic).path), ]) else: - ret.extend(_portable_link_flags(lib, use_pic, ambiguous_libs, get_lib_name_default, for_windows = False)) + ret.extend(_portable_link_flags(lib, use_pic, ambiguous_libs, get_lib_name_default, for_macos = True)) return ret def _make_link_flags_default(linker_input_and_use_pic_and_ambiguous_libs): @@ -1696,7 +1696,7 @@ def _make_link_flags_default(linker_input_and_use_pic_and_ambiguous_libs): "link-arg=-Wl,--no-whole-archive", ]) else: - ret.extend(_portable_link_flags(lib, use_pic, ambiguous_libs, get_lib_name_default, for_windows = False)) + ret.extend(_portable_link_flags(lib, use_pic, ambiguous_libs, get_lib_name_default)) return ret def _libraries_dirnames(linker_input_and_use_pic_and_ambiguous_libs): From 2e20776fbdc901a8ceb5be9a38aae3f95c8b5e36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20F=C3=A9ron?= Date: Fri, 4 Nov 2022 11:29:05 +0100 Subject: [PATCH 02/11] Reformat --- rust/private/rustc.bzl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rust/private/rustc.bzl b/rust/private/rustc.bzl index 47630c57cb..f413f9db70 100644 --- a/rust/private/rustc.bzl +++ b/rust/private/rustc.bzl @@ -1609,7 +1609,7 @@ def _get_crate_dirname(crate): """ return crate.output.dirname -def _portable_link_flags(lib, use_pic, ambiguous_libs, get_lib_name, for_windows=False, for_macos=False): +def _portable_link_flags(lib, use_pic, ambiguous_libs, get_lib_name, for_windows = False, for_macos = False): artifact = get_preferred_artifact(lib, use_pic) if ambiguous_libs and artifact.path in ambiguous_libs: artifact = ambiguous_libs[artifact.path] From bb2f71447647a5e53eaba9328f9c6663a1e43baa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20F=C3=A9ron?= Date: Wed, 22 Mar 2023 16:00:16 +0100 Subject: [PATCH 03/11] Add test --- rust/private/rustc.bzl | 6 ++--- test/portable_link_flags_macos/BUILD.bazel | 27 ++++++++++++++++++++++ test/portable_link_flags_macos/api.c | 5 ++++ test/portable_link_flags_macos/lib.rs | 3 +++ test/portable_link_flags_macos/main.rs | 3 +++ test/portable_link_flags_macos/test.sh | 4 ++++ 6 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 test/portable_link_flags_macos/BUILD.bazel create mode 100644 test/portable_link_flags_macos/api.c create mode 100644 test/portable_link_flags_macos/lib.rs create mode 100644 test/portable_link_flags_macos/main.rs create mode 100755 test/portable_link_flags_macos/test.sh diff --git a/rust/private/rustc.bzl b/rust/private/rustc.bzl index 9713dbfe3b..d96cde801e 100644 --- a/rust/private/rustc.bzl +++ b/rust/private/rustc.bzl @@ -1668,7 +1668,7 @@ def _get_crate_dirname(crate): """ return crate.output.dirname -def _portable_link_flags(lib, use_pic, ambiguous_libs, get_lib_name, for_windows = False, for_macos = False): +def _portable_link_flags(lib, use_pic, ambiguous_libs, get_lib_name, for_windows = False, for_darwin = False): artifact = get_preferred_artifact(lib, use_pic) if ambiguous_libs and artifact.path in ambiguous_libs: artifact = ambiguous_libs[artifact.path] @@ -1706,7 +1706,7 @@ def _portable_link_flags(lib, use_pic, ambiguous_libs, get_lib_name, for_windows artifact.basename.startswith("libtest-") or artifact.basename.startswith("libstd-") or artifact.basename.startswith("test-") or artifact.basename.startswith("std-") ): - return [] if for_macos else ["-lstatic=%s" % get_lib_name(artifact)] + return [] if for_darwin else ["-lstatic=%s" % get_lib_name(artifact)] return [ "-lstatic=%s" % get_lib_name(artifact), "-Clink-arg=-l%s" % (get_lib_name(artifact) if not for_windows else artifact.basename), @@ -1738,7 +1738,7 @@ def _make_link_flags_darwin(linker_input_and_use_pic_and_ambiguous_libs): ("link-arg=-Wl,-force_load,%s" % get_preferred_artifact(lib, use_pic).path), ]) else: - ret.extend(_portable_link_flags(lib, use_pic, ambiguous_libs, get_lib_name_default, for_macos = True)) + ret.extend(_portable_link_flags(lib, use_pic, ambiguous_libs, get_lib_name_default, for_darwin = True)) return ret def _make_link_flags_default(linker_input_and_use_pic_and_ambiguous_libs): diff --git a/test/portable_link_flags_macos/BUILD.bazel b/test/portable_link_flags_macos/BUILD.bazel new file mode 100644 index 0000000000..eb6f3f3e2e --- /dev/null +++ b/test/portable_link_flags_macos/BUILD.bazel @@ -0,0 +1,27 @@ +load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_library") + +rust_library( + name = "rust-lib", + edition = "2021", + srcs = ["lib.rs"], +) + +cc_library( + name = "c-lib", + srcs = ["api.c"], + deps = [":rust-lib"], +) + +rust_binary( + name = "app", + edition = "2021", + srcs = ["main.rs"], + deps = [":c-lib"], +) + +sh_test( + name = "test", + data = [":app"], + srcs = ["test.sh"], + args = ["$(location :app)"] +) \ No newline at end of file diff --git a/test/portable_link_flags_macos/api.c b/test/portable_link_flags_macos/api.c new file mode 100644 index 0000000000..bf6adb4043 --- /dev/null +++ b/test/portable_link_flags_macos/api.c @@ -0,0 +1,5 @@ +#include + +void lib() { + printf("Hello, C lib!"); +} \ No newline at end of file diff --git a/test/portable_link_flags_macos/lib.rs b/test/portable_link_flags_macos/lib.rs new file mode 100644 index 0000000000..ca5b2e9de4 --- /dev/null +++ b/test/portable_link_flags_macos/lib.rs @@ -0,0 +1,3 @@ +pub fn lib() { + println!("Hello, Rust lib!"); +} \ No newline at end of file diff --git a/test/portable_link_flags_macos/main.rs b/test/portable_link_flags_macos/main.rs new file mode 100644 index 0000000000..fbedd92052 --- /dev/null +++ b/test/portable_link_flags_macos/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} \ No newline at end of file diff --git a/test/portable_link_flags_macos/test.sh b/test/portable_link_flags_macos/test.sh new file mode 100755 index 0000000000..d843a13cdf --- /dev/null +++ b/test/portable_link_flags_macos/test.sh @@ -0,0 +1,4 @@ +#!/bin/sh +set -e + +$1 \ No newline at end of file From d10a159c65f030bb70877d6108d1d944ae9d80f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20F=C3=A9ron?= Date: Wed, 22 Mar 2023 16:01:57 +0100 Subject: [PATCH 04/11] Print nothing --- test/portable_link_flags_macos/main.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/test/portable_link_flags_macos/main.rs b/test/portable_link_flags_macos/main.rs index fbedd92052..ea3850b23a 100644 --- a/test/portable_link_flags_macos/main.rs +++ b/test/portable_link_flags_macos/main.rs @@ -1,3 +1,2 @@ fn main() { - println!("Hello, world!"); } \ No newline at end of file From 4798f34e61fc8c1253687def474b263df8c64565 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20F=C3=A9ron?= Date: Wed, 22 Mar 2023 16:02:33 +0100 Subject: [PATCH 05/11] Rename dir --- .../BUILD.bazel | 0 .../api.c | 0 .../lib.rs | 0 .../main.rs | 0 .../test.sh | 0 5 files changed, 0 insertions(+), 0 deletions(-) rename test/{portable_link_flags_macos => portable_link_flags_darwin}/BUILD.bazel (100%) rename test/{portable_link_flags_macos => portable_link_flags_darwin}/api.c (100%) rename test/{portable_link_flags_macos => portable_link_flags_darwin}/lib.rs (100%) rename test/{portable_link_flags_macos => portable_link_flags_darwin}/main.rs (100%) rename test/{portable_link_flags_macos => portable_link_flags_darwin}/test.sh (100%) diff --git a/test/portable_link_flags_macos/BUILD.bazel b/test/portable_link_flags_darwin/BUILD.bazel similarity index 100% rename from test/portable_link_flags_macos/BUILD.bazel rename to test/portable_link_flags_darwin/BUILD.bazel diff --git a/test/portable_link_flags_macos/api.c b/test/portable_link_flags_darwin/api.c similarity index 100% rename from test/portable_link_flags_macos/api.c rename to test/portable_link_flags_darwin/api.c diff --git a/test/portable_link_flags_macos/lib.rs b/test/portable_link_flags_darwin/lib.rs similarity index 100% rename from test/portable_link_flags_macos/lib.rs rename to test/portable_link_flags_darwin/lib.rs diff --git a/test/portable_link_flags_macos/main.rs b/test/portable_link_flags_darwin/main.rs similarity index 100% rename from test/portable_link_flags_macos/main.rs rename to test/portable_link_flags_darwin/main.rs diff --git a/test/portable_link_flags_macos/test.sh b/test/portable_link_flags_darwin/test.sh similarity index 100% rename from test/portable_link_flags_macos/test.sh rename to test/portable_link_flags_darwin/test.sh From c9ddcd6fe14fcb7a626274eada7d63119a568535 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20F=C3=A9ron?= Date: Wed, 22 Mar 2023 16:36:50 +0100 Subject: [PATCH 06/11] Use otool in test --- test/portable_link_flags_darwin/BUILD.bazel | 5 ++++- test/portable_link_flags_darwin/test.sh | 10 +++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/test/portable_link_flags_darwin/BUILD.bazel b/test/portable_link_flags_darwin/BUILD.bazel index eb6f3f3e2e..37009f27b5 100644 --- a/test/portable_link_flags_darwin/BUILD.bazel +++ b/test/portable_link_flags_darwin/BUILD.bazel @@ -23,5 +23,8 @@ sh_test( name = "test", data = [":app"], srcs = ["test.sh"], - args = ["$(location :app)"] + args = ["$(location :app)"], + target_compatible_with = [ + "@platforms//os:macos", + ] ) \ No newline at end of file diff --git a/test/portable_link_flags_darwin/test.sh b/test/portable_link_flags_darwin/test.sh index d843a13cdf..63fc4edbb9 100755 --- a/test/portable_link_flags_darwin/test.sh +++ b/test/portable_link_flags_darwin/test.sh @@ -1,4 +1,12 @@ #!/bin/sh set -e -$1 \ No newline at end of file +if otool -L $1 | grep -q libtest; then + echo "built binary is dynamically linked against libtest!" + exit 1 +fi + +if otool -L $1 | grep -q libstd; then + echo "built binary is dynamically linked against libstd!" + exit 1 +fi \ No newline at end of file From 2607c73d7b0af61aacc456d322be14b6b3477b2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20F=C3=A9ron?= Date: Wed, 22 Mar 2023 16:38:11 +0100 Subject: [PATCH 07/11] Adjust msg --- test/portable_link_flags_darwin/test.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/portable_link_flags_darwin/test.sh b/test/portable_link_flags_darwin/test.sh index 63fc4edbb9..01879b8d2f 100755 --- a/test/portable_link_flags_darwin/test.sh +++ b/test/portable_link_flags_darwin/test.sh @@ -2,11 +2,11 @@ set -e if otool -L $1 | grep -q libtest; then - echo "built binary is dynamically linked against libtest!" + echo "error: rust_binary is dynamically linked against libtest!" exit 1 fi if otool -L $1 | grep -q libstd; then - echo "built binary is dynamically linked against libstd!" + echo "error: rust_binary is dynamically linked against libstd!" exit 1 fi \ No newline at end of file From 9849439b7606b759adfe2a2054b3240d8bb5aaf6 Mon Sep 17 00:00:00 2001 From: Daniel Wagner-Hall Date: Wed, 22 Mar 2023 15:40:09 +0000 Subject: [PATCH 08/11] Buildifier --- test/portable_link_flags_darwin/BUILD.bazel | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test/portable_link_flags_darwin/BUILD.bazel b/test/portable_link_flags_darwin/BUILD.bazel index 37009f27b5..df5e8da5e6 100644 --- a/test/portable_link_flags_darwin/BUILD.bazel +++ b/test/portable_link_flags_darwin/BUILD.bazel @@ -2,8 +2,8 @@ load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_library") rust_library( name = "rust-lib", - edition = "2021", srcs = ["lib.rs"], + edition = "2021", ) cc_library( @@ -14,17 +14,17 @@ cc_library( rust_binary( name = "app", - edition = "2021", srcs = ["main.rs"], + edition = "2021", deps = [":c-lib"], ) sh_test( name = "test", - data = [":app"], srcs = ["test.sh"], args = ["$(location :app)"], + data = [":app"], target_compatible_with = [ "@platforms//os:macos", - ] -) \ No newline at end of file + ], +) From d9bbc4909b348ecc4a956f8a2a71eb50c6a0995a Mon Sep 17 00:00:00 2001 From: Daniel Wagner-Hall Date: Wed, 22 Mar 2023 15:43:02 +0000 Subject: [PATCH 09/11] Add trailing newlines --- test/portable_link_flags_darwin/api.c | 2 +- test/portable_link_flags_darwin/lib.rs | 2 +- test/portable_link_flags_darwin/main.rs | 3 +-- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/test/portable_link_flags_darwin/api.c b/test/portable_link_flags_darwin/api.c index bf6adb4043..419c5ce11a 100644 --- a/test/portable_link_flags_darwin/api.c +++ b/test/portable_link_flags_darwin/api.c @@ -2,4 +2,4 @@ void lib() { printf("Hello, C lib!"); -} \ No newline at end of file +} diff --git a/test/portable_link_flags_darwin/lib.rs b/test/portable_link_flags_darwin/lib.rs index ca5b2e9de4..c446273285 100644 --- a/test/portable_link_flags_darwin/lib.rs +++ b/test/portable_link_flags_darwin/lib.rs @@ -1,3 +1,3 @@ pub fn lib() { println!("Hello, Rust lib!"); -} \ No newline at end of file +} diff --git a/test/portable_link_flags_darwin/main.rs b/test/portable_link_flags_darwin/main.rs index ea3850b23a..f328e4d9d0 100644 --- a/test/portable_link_flags_darwin/main.rs +++ b/test/portable_link_flags_darwin/main.rs @@ -1,2 +1 @@ -fn main() { -} \ No newline at end of file +fn main() {} From 9ccd5abd13851367d448c140f86262629e833f1e Mon Sep 17 00:00:00 2001 From: Daniel Wagner-Hall Date: Wed, 22 Mar 2023 15:46:12 +0000 Subject: [PATCH 10/11] buildifier --- test/portable_link_flags_darwin/BUILD.bazel | 1 + 1 file changed, 1 insertion(+) diff --git a/test/portable_link_flags_darwin/BUILD.bazel b/test/portable_link_flags_darwin/BUILD.bazel index df5e8da5e6..c883b4f6bf 100644 --- a/test/portable_link_flags_darwin/BUILD.bazel +++ b/test/portable_link_flags_darwin/BUILD.bazel @@ -1,3 +1,4 @@ +load("@rules_cc//cc:defs.bzl", "cc_library") load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_library") rust_library( From ed01fe541f71a8afd924075d26280f6d53ac4aa1 Mon Sep 17 00:00:00 2001 From: Daniel Wagner-Hall Date: Wed, 22 Mar 2023 15:53:18 +0000 Subject: [PATCH 11/11] Kick CI