-1

I'm trying to write bazel rules for a 3rd party library that specify different pre-processor define values per file i.e.

c++ ... -DVALUE=file1.cpp -c file1.cpp
c++ ... -DVALUE=file2.cpp -c file2.cpp
c++ ... -DVALUE=file3.cpp -c file3.cpp

This is basically set_property() on the file to add COMPILE_DEFINITIONS in CMake.

Using copts or local_defines for cc_library() doesn't seem to support this use case as they are common for all source files in the library.

The terrible ways to do this that I can think of:

  • have a private library per file and consolidate all of them to one library.
  • Patch the library or inject a header file using -include to add the definition.

Is there a better way to achieve this? Thanks!

3
  • Note: if the values given aren't just an example and you actually need the source filename as a preprocessor constant, you have __FILE__ to your disposal. Commented Oct 2, 2023 at 21:03
  • The values are a bit of both. The filename is an extreme example of every compilation unit having a different value for a 3rd party lib I'm using. I completely agree that using __FILE__ is the right thing to do but note that these are 3rd party libs and we try to avoid modifications when we can. I think the question is still valid when you have to deal with 3rd party libs. Commented Oct 3, 2023 at 3:33
  • If you're just trying to avoid modifications to the source files, you could generate wrapper .cpp files that define the macros and then #include the original ones. Commented Oct 3, 2023 at 20:37

1 Answer 1

0

The simplest approach to that is to just make a separate cc_library for each file, and then aggregate them together. Bazel isn't actually going to create a separate .a file in most configurations, so it doesn't change very much. A macro or list comprehension may be helpful to avoid copy/paste for the attribute values shared between all of the cc_libary targets.

The other option is writing a custom rule. my_c_compile and my_c_archive are some basic examples, you could combine them pretty easily into a single rule that takes multiple source files and applies separate user_compile_flags to each. That seems like a lot of work though.

Bazel also includes a full implementation of cc_library in Starlark which you could start from when writing a custom rule, but that's really complicated in many ways which you don't need. It's in src/main/starlark/builtins_bzl/common/cc/cc_library.bzl.

Sign up to request clarification or add additional context in comments.

1 Comment

This is what I was mentioning about creating one lib per source file. I'm going for the macro approach for now until I find a better solution. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.