Skip to content

Commit db80d30

Browse files
authored
Wrapper fixes for rules_foreign_cc (#167)
* `MODULE` does not need to register all those toolchains, because the consumer has to do it. * wrapper files have been adapted to support `rules_foreign_cc` used with `bzl_mod`. Tested in - [x] orion (`WORKSPACE`) - [x] starling (`WORKSPACE`) - [x] libsbp (`MODULE`)
1 parent c68035c commit db80d30

File tree

17 files changed

+348
-61
lines changed

17 files changed

+348
-61
lines changed

MODULE.bazel

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (C) 2022 Swift Navigation Inc.
1+
# Copyright (C) 2025 Swift Navigation Inc.
22
# Contact: Swift Navigation <[email protected]>
33
#
44
# This source is subject to the license found in the file 'LICENSE' which must
@@ -10,7 +10,7 @@
1010

1111
module(
1212
name = "rules_swiftnav",
13-
version = "0.1.0",
13+
version = "0.2.0",
1414
compatibility_level = 1,
1515
)
1616

@@ -43,21 +43,22 @@ use_repo(
4343
"x86_64_linux_gcc_arm_embedded_toolchain",
4444
)
4545

46-
register_toolchains(
47-
"@rules_swiftnav//cc/toolchains/llvm/aarch64-darwin:cc-toolchain-aarch64-darwin",
48-
"@rules_swiftnav//cc/toolchains/llvm/x86_64-darwin:cc-toolchain-x86_64-darwin",
49-
"@rules_swiftnav//cc/toolchains/llvm/aarch64-linux:cc-toolchain-aarch64-linux",
50-
"@rules_swiftnav//cc/toolchains/llvm/x86_64-linux:cc-toolchain-x86_64-linux",
51-
"@rules_swiftnav//cc/toolchains/llvm/x86_64-linux:cc-toolchain-intel-mkl",
52-
"@rules_swiftnav//cc/toolchains/llvm/x86_64-aarch64-linux:cc-toolchain-aarch64-bullseye-graviton2",
53-
"@rules_swiftnav//cc/toolchains/llvm/x86_64-aarch64-linux:cc-toolchain-aarch64-bullseye-graviton3",
54-
"@rules_swiftnav//cc/toolchains/musl/aarch64:toolchain",
55-
"@rules_swiftnav//cc/toolchains/musl/armhf:toolchain",
56-
"@rules_swiftnav//cc/toolchains/musl/x86_64:toolchain",
57-
"@rules_swiftnav//cc/toolchains/llvm_x86_64_windows:mingw_toolchain",
58-
"@rules_swiftnav//cc/toolchains/gcc_arm_embedded:toolchain",
59-
"@rules_swiftnav//cc/toolchains/yocto_generic:toolchain",
60-
)
46+
# On consumer side, register toolchains for the supported architectures:
47+
# register_toolchains(
48+
# "@rules_swiftnav//cc/toolchains/llvm/aarch64-darwin:cc-toolchain-aarch64-darwin",
49+
# "@rules_swiftnav//cc/toolchains/llvm/x86_64-darwin:cc-toolchain-x86_64-darwin",
50+
# "@rules_swiftnav//cc/toolchains/llvm/aarch64-linux:cc-toolchain-aarch64-linux",
51+
# "@rules_swiftnav//cc/toolchains/llvm/x86_64-linux:cc-toolchain-x86_64-linux",
52+
# "@rules_swiftnav//cc/toolchains/llvm/x86_64-linux:cc-toolchain-intel-mkl",
53+
# "@rules_swiftnav//cc/toolchains/llvm/x86_64-aarch64-linux:cc-toolchain-aarch64-bullseye-graviton2",
54+
# "@rules_swiftnav//cc/toolchains/llvm/x86_64-aarch64-linux:cc-toolchain-aarch64-bullseye-graviton3",
55+
# "@rules_swiftnav//cc/toolchains/musl/aarch64:toolchain",
56+
# "@rules_swiftnav//cc/toolchains/musl/armhf:toolchain",
57+
# "@rules_swiftnav//cc/toolchains/musl/x86_64:toolchain",
58+
# "@rules_swiftnav//cc/toolchains/llvm_x86_64_windows:mingw_toolchain",
59+
# "@rules_swiftnav//cc/toolchains/gcc_arm_embedded:toolchain",
60+
# "@rules_swiftnav//cc/toolchains/yocto_generic:toolchain",
61+
# )
6162

6263
yocto_generic_ext = use_extension("@rules_swiftnav//cc/toolchains/yocto_generic:yocto_generic_extension.bzl", "yocto_generic_extension")
6364
use_repo(yocto_generic_ext, "yocto_generic")

MODULE.bazel.lock

Lines changed: 40 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cc/toolchains/gcc_arm_embedded/wrappers/wrapper

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,22 +41,37 @@ toolchain_bindir_as_bzlmod="external/rules_swiftnav~~swift_cc_toolchain_extensio
4141

4242
if [[ -f "${toolchain_bindir}"/"${tool_name}" ]]; then
4343
# We're running under _execroot_, call the real tool.
44+
# use-case: WORKSPACE
4445
exec "${toolchain_bindir}"/"${tool_name}" "$@"
4546
elif [[ -f "${toolchain_bindir_as_bzlmod}"/"${tool_name}" ]]; then
4647
# We're running under _execroot_, call the real tool.
48+
# Use-case: bazelmod
4749
exec "${toolchain_bindir_as_bzlmod}"/"${tool_name}" "$@"
4850
elif [[ "${BASH_SOURCE[0]}" == "/"* ]]; then
4951
# This branch exists because some users of the toolchain,
5052
# namely rules_foreign_cc, will change CWD and call $CC (this script)
5153
# with its absolute path.
5254
#
5355
# To deal with this we find the tool relative to this script, which is at
54-
# _execroot_/external/rules_swiftnav/cc/toolchain/llvm/x86_64-linux/wrappers/wrapper.
56+
# _execroot_/external/rules_swiftnav/cc/toolchains/gcc_arm_embedded/wrappers/wrapper.
5557
#
5658
# If the wrapper is relocated then this line needs to be adjusted.
57-
execroot_path="${BASH_SOURCE[0]%/*/*/*/*/*/*/*/*}"
59+
60+
execroot_path="${BASH_SOURCE[0]%/*/*/*/*/*/*/*/*}"
61+
62+
# Legacy path for non-bazelmod
5863
tool="${execroot_path}/${toolchain_bindir}/${tool_name}"
59-
exec "${tool}" "${@}"
64+
if [[ -f "${execroot_path}/${toolchain_bindir}/${tool_name}" ]];
65+
then
66+
exec "${tool}" "${@}"
67+
fi
68+
69+
# After bazelmod migration, the directory structure is different
70+
tool_as_bzlmod="${execroot_path}/${toolchain_bindir_as_bzlmod}/${tool_name}"
71+
if [[ -f "${tool_as_bzlmod}" ]];
72+
then
73+
exec "${tool_as_bzlmod}" "${@}"
74+
fi
6075
else
6176
>&2 echo "ERROR: could not find ${tool_name}; PWD=\"$(pwd)\"; PATH=\"${PATH}\"."
6277
exit 5

cc/toolchains/gcc_arm_gnu_8_3/wrappers/wrapper

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,22 +41,37 @@ toolchain_bindir_as_bzlmod="external/rules_swiftnav~~swift_cc_toolchain_extensio
4141

4242
if [[ -f "${toolchain_bindir}"/"${tool_name}" ]]; then
4343
# We're running under _execroot_, call the real tool.
44+
# use-case: WORKSPACE
4445
exec "${toolchain_bindir}"/"${tool_name}" "$@"
4546
elif [[ -f "${toolchain_bindir_as_bzlmod}"/"${tool_name}" ]]; then
4647
# We're running under _execroot_, call the real tool.
48+
# Use-case: bazelmod
4749
exec "${toolchain_bindir_as_bzlmod}"/"${tool_name}" "$@"
4850
elif [[ "${BASH_SOURCE[0]}" == "/"* ]]; then
4951
# This branch exists because some users of the toolchain,
5052
# namely rules_foreign_cc, will change CWD and call $CC (this script)
5153
# with its absolute path.
5254
#
5355
# To deal with this we find the tool relative to this script, which is at
54-
# _execroot_/external/rules_swiftnav/cc/toolchain/llvm/x86_64-linux/wrappers/wrapper.
56+
# _execroot_/external/rules_swiftnav/cc/toolchains/gcc_arm_gnu_8_3/wrappers/wrapper.
5557
#
5658
# If the wrapper is relocated then this line needs to be adjusted.
57-
execroot_path="${BASH_SOURCE[0]%/*/*/*/*/*/*/*/*}"
59+
60+
execroot_path="${BASH_SOURCE[0]%/*/*/*/*/*/*/*/*}"
61+
62+
# Legacy path for non-bazelmod
5863
tool="${execroot_path}/${toolchain_bindir}/${tool_name}"
59-
exec "${tool}" "${@}"
64+
if [[ -f "${execroot_path}/${toolchain_bindir}/${tool_name}" ]];
65+
then
66+
exec "${tool}" "${@}"
67+
fi
68+
69+
# After bazelmod migration, the directory structure is different
70+
tool_as_bzlmod="${execroot_path}/${toolchain_bindir_as_bzlmod}/${tool_name}"
71+
if [[ -f "${tool_as_bzlmod}" ]];
72+
then
73+
exec "${tool_as_bzlmod}" "${@}"
74+
fi
6075
else
6176
>&2 echo "ERROR: could not find ${tool_name}; PWD=\"$(pwd)\"; PATH=\"${PATH}\"."
6277
exit 5

cc/toolchains/llvm/aarch64-darwin/wrappers/wrapper

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,22 +41,37 @@ toolchain_bindir_as_bzlmod="external/rules_swiftnav~~swift_cc_toolchain_extensio
4141

4242
if [[ -f "${toolchain_bindir}"/"${tool_name}" ]]; then
4343
# We're running under _execroot_, call the real tool.
44+
# use-case: WORKSPACE
4445
exec "${toolchain_bindir}"/"${tool_name}" "$@"
4546
elif [[ -f "${toolchain_bindir_as_bzlmod}"/"${tool_name}" ]]; then
4647
# We're running under _execroot_, call the real tool.
48+
# Use-case: bazelmod
4749
exec "${toolchain_bindir_as_bzlmod}"/"${tool_name}" "$@"
4850
elif [[ "${BASH_SOURCE[0]}" == "/"* ]]; then
4951
# This branch exists because some users of the toolchain,
5052
# namely rules_foreign_cc, will change CWD and call $CC (this script)
5153
# with its absolute path.
5254
#
5355
# To deal with this we find the tool relative to this script, which is at
54-
# _execroot_/external/rules_swiftnav/cc/toolchain/llvm/x86_64-linux/wrappers/wrapper.
56+
# _execroot_/external/rules_swiftnav/cc/toolchains/llvm/aarch64-darwin/wrappers/wrapper.
5557
#
5658
# If the wrapper is relocated then this line needs to be adjusted.
57-
execroot_path="${BASH_SOURCE[0]%/*/*/*/*/*/*/*/*}"
59+
60+
execroot_path="${BASH_SOURCE[0]%/*/*/*/*/*/*/*/*}"
61+
62+
# Legacy path for non-bazelmod
5863
tool="${execroot_path}/${toolchain_bindir}/${tool_name}"
59-
exec "${tool}" "${@}"
64+
if [[ -f "${execroot_path}/${toolchain_bindir}/${tool_name}" ]];
65+
then
66+
exec "${tool}" "${@}"
67+
fi
68+
69+
# After bazelmod migration, the directory structure is different
70+
tool_as_bzlmod="${execroot_path}/${toolchain_bindir_as_bzlmod}/${tool_name}"
71+
if [[ -f "${tool_as_bzlmod}" ]];
72+
then
73+
exec "${tool_as_bzlmod}" "${@}"
74+
fi
6075
else
6176
>&2 echo "ERROR: could not find ${tool_name}; PWD=\"$(pwd)\"; PATH=\"${PATH}\"."
6277
exit 5

cc/toolchains/llvm/aarch64-linux/wrappers/wrapper

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,41 @@ fi
3535

3636
tool_name=$(basename "${BASH_SOURCE[0]}")
3737
toolchain_bindir=external/aarch64-linux-llvm/bin
38+
toolchain_bindir_as_bzlmod="external/rules_swiftnav~~swift_cc_toolchain_extension~aarch64-linux-llvm/bin"
3839

3940
if [[ -f "${toolchain_bindir}"/"${tool_name}" ]]; then
4041
# We're running under _execroot_, call the real tool.
42+
# use-case: WORKSPACE
4143
exec "${toolchain_bindir}"/"${tool_name}" "$@"
44+
elif [[ -f "${toolchain_bindir_as_bzlmod}"/"${tool_name}" ]]; then
45+
# We're running under _execroot_, call the real tool.
46+
# Use-case: bazelmod
47+
exec "${toolchain_bindir_as_bzlmod}"/"${tool_name}" "$@"
4248
elif [[ "${BASH_SOURCE[0]}" == "/"* ]]; then
4349
# This branch exists because some users of the toolchain,
4450
# namely rules_foreign_cc, will change CWD and call $CC (this script)
4551
# with its absolute path.
4652
#
4753
# To deal with this we find the tool relative to this script, which is at
48-
# _execroot_/external/rules_swiftnav/cc/toolchain/llvm/aarch64-linux/wrappers/wrapper.
54+
# _execroot_/external/rules_swiftnav/cc/toolchains/llvm/aarch64-linux/wrappers/wrapper.
4955
#
5056
# If the wrapper is relocated then this line needs to be adjusted.
51-
execroot_path="${BASH_SOURCE[0]%/*/*/*/*/*/*/*/*}"
57+
58+
execroot_path="${BASH_SOURCE[0]%/*/*/*/*/*/*/*/*}"
59+
60+
# Legacy path for non-bazelmod
5261
tool="${execroot_path}/${toolchain_bindir}/${tool_name}"
53-
exec "${tool}" "${@}"
62+
if [[ -f "${execroot_path}/${toolchain_bindir}/${tool_name}" ]];
63+
then
64+
exec "${tool}" "${@}"
65+
fi
66+
67+
# After bazelmod migration, the directory structure is different
68+
tool_as_bzlmod="${execroot_path}/${toolchain_bindir_as_bzlmod}/${tool_name}"
69+
if [[ -f "${tool_as_bzlmod}" ]];
70+
then
71+
exec "${tool_as_bzlmod}" "${@}"
72+
fi
5473
else
5574
>&2 echo "ERROR: could not find ${tool_name}; PWD=\"$(pwd)\"; PATH=\"${PATH}\"."
5675
exit 5

cc/toolchains/llvm/x86_64-aarch64-linux/wrappers/wrapper

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,37 @@ toolchain_bindir=external/x86_64-linux-llvm/bin
3838

3939
if [[ -f "${toolchain_bindir}"/"${tool_name}" ]]; then
4040
# We're running under _execroot_, call the real tool.
41+
# use-case: WORKSPACE
4142
exec "${toolchain_bindir}"/"${tool_name}" "$@"
43+
elif [[ -f "${toolchain_bindir_as_bzlmod}"/"${tool_name}" ]]; then
44+
# We're running under _execroot_, call the real tool.
45+
# Use-case: bazelmod
46+
exec "${toolchain_bindir_as_bzlmod}"/"${tool_name}" "$@"
4247
elif [[ "${BASH_SOURCE[0]}" == "/"* ]]; then
4348
# This branch exists because some users of the toolchain,
4449
# namely rules_foreign_cc, will change CWD and call $CC (this script)
4550
# with its absolute path.
4651
#
4752
# To deal with this we find the tool relative to this script, which is at
48-
# _execroot_/external/rules_swiftnav/cc/toolchain/llvm/x86_64-linux/wrappers/wrapper.
53+
# _execroot_/external/rules_swiftnav/cc/toolchains/llvm/x86_64-aarch64-linux/wrappers/wrapper.
4954
#
5055
# If the wrapper is relocated then this line needs to be adjusted.
51-
execroot_path="${BASH_SOURCE[0]%/*/*/*/*/*/*/*/*}"
56+
57+
execroot_path="${BASH_SOURCE[0]%/*/*/*/*/*/*/*/*}"
58+
59+
# Legacy path for non-bazelmod
5260
tool="${execroot_path}/${toolchain_bindir}/${tool_name}"
53-
exec "${tool}" "${@}"
61+
if [[ -f "${execroot_path}/${toolchain_bindir}/${tool_name}" ]];
62+
then
63+
exec "${tool}" "${@}"
64+
fi
65+
66+
# After bazelmod migration, the directory structure is different
67+
tool_as_bzlmod="${execroot_path}/${toolchain_bindir_as_bzlmod}/${tool_name}"
68+
if [[ -f "${tool_as_bzlmod}" ]];
69+
then
70+
exec "${tool_as_bzlmod}" "${@}"
71+
fi
5472
else
5573
>&2 echo "ERROR: could not find ${tool_name}; PWD=\"$(pwd)\"; PATH=\"${PATH}\"."
5674
exit 5

cc/toolchains/llvm/x86_64-darwin/wrappers/wrapper

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,37 @@ toolchain_bindir=external/x86_64-darwin-llvm/bin
3838

3939
if [[ -f "${toolchain_bindir}"/"${tool_name}" ]]; then
4040
# We're running under _execroot_, call the real tool.
41+
# use-case: WORKSPACE
4142
exec "${toolchain_bindir}"/"${tool_name}" "$@"
43+
elif [[ -f "${toolchain_bindir_as_bzlmod}"/"${tool_name}" ]]; then
44+
# We're running under _execroot_, call the real tool.
45+
# Use-case: bazelmod
46+
exec "${toolchain_bindir_as_bzlmod}"/"${tool_name}" "$@"
4247
elif [[ "${BASH_SOURCE[0]}" == "/"* ]]; then
4348
# This branch exists because some users of the toolchain,
4449
# namely rules_foreign_cc, will change CWD and call $CC (this script)
4550
# with its absolute path.
4651
#
4752
# To deal with this we find the tool relative to this script, which is at
48-
# _execroot_/external/rules_swiftnav/cc/toolchain/llvm/x86_64-linux/wrappers/wrapper.
53+
# _execroot_/external/rules_swiftnav/cc/toolchains/llvm/x86_64-darwin/wrappers/wrapper.
4954
#
5055
# If the wrapper is relocated then this line needs to be adjusted.
51-
execroot_path="${BASH_SOURCE[0]%/*/*/*/*/*/*/*/*}"
56+
57+
execroot_path="${BASH_SOURCE[0]%/*/*/*/*/*/*/*/*}"
58+
59+
# Legacy path for non-bazelmod
5260
tool="${execroot_path}/${toolchain_bindir}/${tool_name}"
53-
exec "${tool}" "${@}"
61+
if [[ -f "${execroot_path}/${toolchain_bindir}/${tool_name}" ]];
62+
then
63+
exec "${tool}" "${@}"
64+
fi
65+
66+
# After bazelmod migration, the directory structure is different
67+
tool_as_bzlmod="${execroot_path}/${toolchain_bindir_as_bzlmod}/${tool_name}"
68+
if [[ -f "${tool_as_bzlmod}" ]];
69+
then
70+
exec "${tool_as_bzlmod}" "${@}"
71+
fi
5472
else
5573
>&2 echo "ERROR: could not find ${tool_name}; PWD=\"$(pwd)\"; PATH=\"${PATH}\"."
5674
exit 5

cc/toolchains/llvm/x86_64-linux/wrappers/wrapper

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,11 @@ toolchain_bindir_as_bzlmod="external/rules_swiftnav~~swift_cc_toolchain_extensio
4141

4242
if [[ -f "${toolchain_bindir}"/"${tool_name}" ]]; then
4343
# We're running under _execroot_, call the real tool.
44+
# use-case: WORKSPACE
4445
exec "${toolchain_bindir}"/"${tool_name}" "$@"
4546
elif [[ -f "${toolchain_bindir_as_bzlmod}"/"${tool_name}" ]]; then
4647
# We're running under _execroot_, call the real tool.
48+
# Use-case: bazelmod
4749
exec "${toolchain_bindir_as_bzlmod}"/"${tool_name}" "$@"
4850
elif [[ "${BASH_SOURCE[0]}" == "/"* ]]; then
4951
# This branch exists because some users of the toolchain,
@@ -54,9 +56,22 @@ elif [[ "${BASH_SOURCE[0]}" == "/"* ]]; then
5456
# _execroot_/external/rules_swiftnav/cc/toolchain/llvm/x86_64-linux/wrappers/wrapper.
5557
#
5658
# If the wrapper is relocated then this line needs to be adjusted.
57-
execroot_path="${BASH_SOURCE[0]%/*/*/*/*/*/*/*/*}"
59+
60+
execroot_path="${BASH_SOURCE[0]%/*/*/*/*/*/*/*/*}"
61+
62+
# Legacy path for non-bazelmod
5863
tool="${execroot_path}/${toolchain_bindir}/${tool_name}"
59-
exec "${tool}" "${@}"
64+
if [[ -f "${execroot_path}/${toolchain_bindir}/${tool_name}" ]];
65+
then
66+
exec "${tool}" "${@}"
67+
fi
68+
69+
# After bazelmod migration, the directory structure is different
70+
tool_as_bzlmod="${execroot_path}/${toolchain_bindir_as_bzlmod}/${tool_name}"
71+
if [[ -f "${tool_as_bzlmod}" ]];
72+
then
73+
exec "${tool_as_bzlmod}" "${@}"
74+
fi
6075
else
6176
>&2 echo "ERROR: could not find ${tool_name}; PWD=\"$(pwd)\"; PATH=\"${PATH}\"."
6277
exit 5

0 commit comments

Comments
 (0)