Hello,
We are using bazel to build a large cpp project with gcc:
https://github.com/UBC-Thunderbots/Software. In particular, the linux_gcc toolchain. We build targets that have dependencies on other targets, who in turn depend on other libraries or targets. This requires `-Wl,--as-needed` flag to be put before the -l[targets] that a target depends on. For example, (from a .params file)
===
-o
bazel-out/k8-fastbuild/bin/software/new_geom/polygon_test
-Lbazel-out/k8-fastbuild/bin/_solib_k8
-Lbazel-out/k8-fastbuild/bin/_solib__cc_Utoolchain_Ccc_Utoolchain_Ulinux_Ugcc_U7_U4_U0
bazel-out/k8-fastbuild/bin/software/new_geom/_objs/polygon_test/polygon_test.pic.o
[Insert `-Wl,--as-needed` here]
-lsoftware_Snew_Ugeom_Slibpolygon
-lsoftware_Snew_Ugeom_Slibpoint
-lsoftware_Snew_Ugeom_Slibvector
-lexternal_Sboost_Slibchrono
-lexternal_Sboost_Slibsystem
-lexternal_Sboost_Slibcontainer
-lexternal_Sboost_Slibregex
-lexternal_Sboost_Slibexception
-lexternal_Sgtest_Slibgtest_Umain
-lexternal_Sgtest_Slibgtest
-lstdc++
-static-libgcc
-Wl,-S
-Wl,-z,relro,-z,now
-Wl,--build-id=md5
-Wl,--hash-style=gnu
-pthread
-std=c++17
-Wno-unused-command-line-argument
-lm
-lpthread
-ldl
-lrt
-fuse-ld=gold
-Wl,-rpath,$ORIGIN/../../_solib_k8/
-Wl,-rpath,$ORIGIN/../../_solib__cc_Utoolchain_Ccc_Utoolchain_Ulinux_Ugcc_U7_U4_U0/
-Wl,-rpath,$ORIGIN/_solib__cc_Utoolchain_Ccc_Utoolchain_Ulinux_Ugcc_U7_U4_U0/
===
The only way we’ve been able to do this is by adding it to the end of the gcc path in src/cc_toolchain/wrapper/linux_gcc-gcc. We don’t think this is an elegant solution and that there must be a way to add the `-Wl,--as-needed` through other options.
Please let us know what you think and what are the possible solutions.
Thanks,
---
UBC ThunderbotsFaculty of Applied Science
The University of British Columbia | Vancouver Campus