Built-in GCC library dependencies to create_cc_toolchain_config_info?

769 views
Skip to first unread message

Carlos Galvez

unread,
Nov 3, 2021, 4:19:18 AM11/3/21
to bazel-discuss
Hi,

"create_cc_toolchain_config_info" allows one to specify the implicit include directories for a given compiler, via "cxx_builtin_include_directories".

Would it be possible to also add the built-in libraries that e.g. GCC links against?

When compiling C++ code, GCC automagically links the following libraries:

* libstdc++.so.6
* libgcc_s.so.1
* libasan.so.4
* libubsan.so.0
...

Now, the use case is to not use the GCC that is installed in the system (since every user could have a different one). Instead, we want to to download a pre-built GCC via e.g. http_archive and set it up as a toolchain. But by doing that, the above-mentioned libraries will not be properly added as dependencies by Bazel, so the RPATH will not be set correctly and the binaries will not be able to the libraries at runtime.

Best regards,
Carlos

Philipp Schrader

unread,
Nov 3, 2021, 11:40:07 PM11/3/21
to bazel-discuss
Unless I'm misunderstanding, you should only need to add those libs to your cc_toolchain() config and then you can link against them explicitly.

Carlos Galvez

unread,
Nov 23, 2021, 9:51:09 AM11/23/21
to bazel-discuss
I'm not sure my reply made it here, so I'll ask again.

Sure, you can add those dependencies explicitly and build the code, but when you run the executable the dynamic loader will pick the libraries installed in the system (/usr/lib). So if you build with "too new" libraries on an "old" system, you won't be able to run the binary. How would you solve that use case?

Best regards,
Carlos

Carlos Galvez

unread,
Jan 26, 2022, 9:33:40 AM1/26/22
to bazel-discuss
This message was lost as well in the mail:

So for example, if I:
  • Clone you repo
  • Create a hello-world cc_binary 
  • Build it
I get:

$ ldd bazel-bin/sandbox/main

linux-vdso.so.1 (0x00007fff347d3000)
librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007fbb2663c000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fbb26619000)
libstdc++.so.6 => /lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007fbb263ff000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fbb2620d000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007fbb261f2000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fbb260a3000)
/lib64/ld-linux-x86-64.so.2 (0x00007fbb266f8000)

As you can see, it's picking all the libraries from my system, instead of from the amd64_debian_sysroot that you declared. That's what I want to avoid. I want that, at runtime, it uses the libraries from amd64_debian_sysroot. For this to work, Bazel needs to set the RPATH of the binary to tell it where to find these libraries.

Best regards,
Carlos

Brian Silverman

unread,
Feb 6, 2022, 10:38:23 PM2/6/22
to Carlos Galvez, bazel-discuss
I ran across https://docs.bazel.build/versions/main/be/c-cpp.html#cc_toolchain.dynamic_runtime_lib and thought of this. Does that do what you're looking for?

--
You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/a39cfd92-fc6d-4f53-92a3-2a9607877b3en%40googlegroups.com.

Carlos Galvez

unread,
Feb 7, 2022, 3:12:24 AM2/7/22
to Brian Silverman, bazel-discuss
@Brian yeah I saw that too, unfortunately it doesn't seem to change anything (or I'm using it incorrectly). Do you know of any example where this is used that I can test?

Besides, that argument only supports 1 library. I need to point out 4 libraries (libstdc++.so, libgcc_s.so, libasan.so, libubsan.so).

It just seems so strange to me - Bazel is all about hermeticity. Being able to use a hermetic toolchain should be the most basic thing to do in Bazel. Otherwise we are just relying on whatever toolchain the users have installed in their system. Sounds like this should have been solved by design from the beginning?

/Carlos

Brian Silverman

unread,
Feb 7, 2022, 3:31:35 AM2/7/22
to Carlos Galvez, bazel-discuss
I don't know of any examples. Did you remember to enable the static_link_cpp_runtimes feature in your toolchain? It's similar to supports_pic, you have to add an empty feature that enables it.

I think you can pass a filegroup to link multiple libraries.

Carlos Galvez

unread,
Feb 7, 2022, 4:22:04 AM2/7/22
to Brian Silverman, bazel-discuss
I have added:

+filegroup(
+    name = "stdlibs_dynamic",
+    srcs = [
+        "usr/lib/x86_64-linux-gnu/libstdc++.so.6",
+    ],
+    data = [
+        "usr/lib/x86_64-linux-gnu/libstdc++.so.6",
+    ]
+
+)
+
+filegroup(
+    name = "stdlibs_static",
+    srcs = [
+        "usr/lib/gcc/x86_64-linux-gnu/7/libstdc++.a",
+    ],
+    data = [
+        "usr/lib/gcc/x86_64-linux-gnu/7/libstdc++.a",
+    ]
+
+)
+

+    dynamic_runtime_lib = ":stdlibs_dynamic",
+    static_runtime_lib = ":stdlibs_static",

+    static_link_cpp_runtimes_feature = feature(
+        name = "static_link_cpp_runtimes",
+        enabled = True,
+    )

+        static_link_cpp_runtimes_feature,

+      linkstatic = 0,

And it leads me to the following crash:

FATAL: bazel crashed due to an internal error. Printing stack trace:
java.lang.RuntimeException: Unrecoverable error while evaluating node 'ActionLookupData{actionLookupKey=ConfiguredTargetKey{label=@gcc_x86_64_linux//:cc-gcc-x86_64-linux, config=BuildConfigurationValue.Key[48bec7d7f0cfee89a93b149ef566c71979caa05e62572cdd191af7d6a9333cdc]}, actionIndex=0}' (requested by nodes 'ArtifactNestedSetKey{rawChildren=[File:[[<execution_root>]bazel-out/k8-fastbuild/bin]sandbox/hello_world/_objs/foo/main.pic.o, File:[[<execution_root>]bazel-out/k8-fastbuild/bin]_solib___Ccc-gcc-x86_U64-linux/libstdc++.so.6]}', 'ArtifactNestedSetKey{rawChildren=[File:[[<execution_root>]bazel-out/k8-fastbuild/bin]sandbox/hello_world/foo, File:[[<execution_root>]bazel-out/k8-fastbuild/bin]_solib___Ccc-gcc-x86_U64-linux/libstdc++.so.6]}')
at com.google.devtools.build.skyframe.AbstractParallelEvaluator$Evaluate.run(AbstractParallelEvaluator.java:644)
at com.google.devtools.build.lib.concurrent.AbstractQueueVisitor$WrappedRunnable.run(AbstractQueueVisitor.java:382)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.base/java.lang.Thread.run(Unknown Source)
Caused by: java.lang.IllegalStateException: Not-yet-present artifact owner: ConfiguredTargetKey{label=@gcc_x86_64_linux//:cc-gcc-x86_64-linux, config=BuildConfigurationValue.Key[48bec7d7f0cfee89a93b149ef566c71979caa05e62572cdd191af7d6a9333cdc]}
at com.google.common.base.Preconditions.checkState(Preconditions.java:591)
at com.google.devtools.build.lib.skyframe.ArtifactFunction.getActionLookupValue(ArtifactFunction.java:419)
at com.google.devtools.build.lib.skyframe.ActionUtils.getActionForLookupData(ActionUtils.java:29)
at com.google.devtools.build.lib.skyframe.ActionExecutionFunction.computeInternal(ActionExecutionFunction.java:179)
at com.google.devtools.build.lib.skyframe.ActionExecutionFunction.compute(ActionExecutionFunction.java:169)
at com.google.devtools.build.skyframe.AbstractParallelEvaluator$Evaluate.run(AbstractParallelEvaluator.java:560)
... 4 more

Reply all
Reply to author
Forward
0 new messages