Conditional Dependency in cc_library

223 views
Skip to first unread message

Alok Ranjan Srivastava

unread,
Nov 25, 2022, 4:12:07 AM11/25/22
to bazel-discuss
Hi,

Is it possible to give conditional dependency in the cc_library deps field?

cc_library(
name, src,
deps = ["@bls//:blas"]
)

Is it possible to give any condition check and based on that condition check, enable the dependency of "@bls//:blas"? 


Thanks,
Alok.

Philipp Schrader

unread,
Nov 25, 2022, 7:14:56 PM11/25/22
to bazel-discuss
You can do this with select(). The example in the docs are pretty decent at conveying the concept I hope.
Phil

Alok Ranjan Srivastava

unread,
Nov 26, 2022, 12:14:19 AM11/26/22
to Philipp Schrader, bazel-discuss
Thanks for your reply.
I will try this.

Thanks,
Alok.

--
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/f732a042-3c42-44f9-8dd7-f1c855436dbdn%40googlegroups.com.

Alok Ranjan Srivastava

unread,
Dec 1, 2022, 3:30:28 AM12/1/22
to bazel-discuss
Hi,

I have tried select construct and config_setting, but I have one issue, I want to give conditions for the gcc-compiler version check. How is it possible?

Thanks,
Alok.

Philipp Schrader

unread,
Dec 1, 2022, 11:40:46 PM12/1/22
to bazel-discuss
Do you mean the specific gcc version? Or whether it's gcc vs. say clang?
If the latter, that's pretty easy:
If the former, that's a little more difficult. I wrote something that lets you select() on enabled features, but have never looked into select()ing on exact compiler versions.

Most use cases I've seen that care about exact compiler versions (e.g. gcc 5 vs. 6+) use #if __GNUC__ constructs instead.

Alok Ranjan Srivastava

unread,
Dec 2, 2022, 12:45:19 AM12/2/22
to Philipp Schrader, bazel-discuss
Hi Philipp,

I just need to check the gcc version, need is to compile the project and create executable only when gcc version is above or equal to 9.2. 

Please let me know what are the ways i can do.

Thanks,
Alok.

Philipp Schrader

unread,
Dec 2, 2022, 2:12:28 AM12/2/22
to bazel-discuss
Probably the simplest thing is to use the same config_setting's in your select() that you used in the toolchain's target_settings attribute.
The second simplest thing might be to select() on constraints that indicate the compiler version in some fashion. I.e. correlate certain platform constraints with what compiler will be used for that platform.
The last idea I have is to see if you can extract version information from the toolchain similar to how @bazel_tools//tools/cpp:compiler extracts the name of the toolchain: https://github.com/bazelbuild/bazel/blob/e2549df933c59c70ce4f039d0ed2cd4308c1835f/tools/cpp/compiler_flag.bzl#L19-L30
Phil
Reply all
Reply to author
Forward
0 new messages