Skip to content

bazel: add uber's hermetic c++ toolchain#6

Merged
AlexTereshenkov merged 2 commits intomainfrom
bazel/add-uber-hermetic-cc-toolchain
May 15, 2025
Merged

bazel: add uber's hermetic c++ toolchain#6
AlexTereshenkov merged 2 commits intomainfrom
bazel/add-uber-hermetic-cc-toolchain

Conversation

@AlexTereshenkov
Copy link
Copy Markdown
Owner

@AlexTereshenkov AlexTereshenkov commented May 14, 2025

Using https://github.com/uber/hermetic_cc_toolchain to build c++ code hermetically without having access to a local c++ toolchain.

Run

$ bazel build --toolchain_resolution_debug=".*" --config=hermetic-linux-amd64 //...

to see the toolchain resolution process and to build all targets for linux-amd64, hermetically.

Using a particular glibc:

$ ldd --version
ldd (Ubuntu GLIBC 2.31-0ubuntu9.17) 2.31

Building all targets (including test targets) works fine and one can even run the test binaries:

$ bazel-bin/test/geometry/rectangle-gtest-test                                     
Running main() from gmock_main.cc
[==========] Running 2 tests from 1 test suite.
[----------] Global test environment set-up.
[----------] 2 tests from RectangleTest
[ RUN      ] RectangleTest.GetRectangleArea
[       OK ] RectangleTest.GetRectangleArea (0 ms)
[ RUN      ] RectangleTest.GetRectangleAreaIfIsASquare
[       OK ] RectangleTest.GetRectangleAreaIfIsASquare (0 ms)
[----------] 2 tests from RectangleTest (0 ms total)

[----------] Global test environment tear-down
[==========] 2 tests from 1 test suite ran. (0 ms total)
[  PASSED  ] 2 tests.

however, the test command fails when run via the bazel test:

$ bazel test --toolchain_resolution_debug=".*" --config=hermetic-linux-amd64 //...
==================== Test output for //test/geometry:rectangle-gtest-test:
/home/user/.cache/bazel/_bazel_homeuser/51966dc905d3201ed8d44fb4278efba7/sandbox/linux-sandbox/1252/execroot/_main/bazel-out/k8-fastbuild/bin/test/geometry/rectangle-gtest-test.runfiles/_main/test/geometry/rectangle-gtest-test: error while loading shared libraries: bazel-out/k8-fastbuild/bin/_solib_k8/libsrc_Sgeometry_Slibrectangle.so: cannot open shared object file: No such file or directory

This is resolved by linking the test targets statically, see https://bazel.build/reference/be/c-cpp#cc_test

Comment thread .bazelrc Outdated
# https://bazel.build/reference/command-line-reference#build-flag--sandbox_add_mount_pair
common --sandbox_add_mount_pair=/tmp/zig-cache
# https://bazel.build/reference/command-line-reference#build-flag--sandbox_writable_path
common --sandbox_writable_path=/tmp/zig-cache
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is required otherwise getting error: unable to create compilation: ReadOnlyFileSystem.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Submitted a patch in uber/hermetic_cc_toolchain#221

@AlexTereshenkov AlexTereshenkov force-pushed the bazel/add-uber-hermetic-cc-toolchain branch from b8e1d6a to 246f76b Compare May 15, 2025 22:21
@AlexTereshenkov AlexTereshenkov merged commit aa883cb into main May 15, 2025
@AlexTereshenkov AlexTereshenkov deleted the bazel/add-uber-hermetic-cc-toolchain branch May 15, 2025 22:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant