Best practice to specify Bazel compiler and linker flags

3,997 views
Skip to first unread message

Michael Fitzner

unread,
Jan 18, 2022, 7:54:53 AM1/18/22
to bazel-discuss

Hi Bazel experts,

We want to use Bazel to build one ECU that consists of different controllers. For each controller an own elf file is built and needs partly separate compiler and linker flags.

In addition, we want to build the complete ECU by calling one ECU target and build the dependencies via transitions. For the development use case it should be possible to call individual targets with the right configuration from the command line. It should be avoided to define compiler and linker flags twice.

I am aware of the following mechanisms to specify compiler and linker flags:

1. Bazel provides the option to specify compiler and linker flags via --cxxopt, --copt, --conlyopt, --linkopt e.g. from the command line or .bazelrc file. For c and cpp different compiler flags can be specified.

2. CC rules provide the option to specify compiler and linker flags via copts and linkopts to the attributes. This is valid for the rule target. If you have c/cpp mixed; it is just possible to specify it to c/cpp executions and not individual to c or cpp executions.

Do you have some ideas or best practices how to work with compiler and linker flags in our scenario?

If I read it correctly https://docs.bazel.build/versions/main/skylark/config.html it is also no option to specify the flags in .bazelrc in different configs and reuse it inside the transition. “Bazel doesn’t support transitioning on --config. This is because --config is an “expansion” flag that expands to other flags.”

Best regards,
Michael F.

Jingwen Chen

unread,
Jan 18, 2022, 8:12:08 AM1/18/22
to Michael Fitzner, bazel-discuss
In AOSP, we do this with cc toolchain features. It's a bit of work to set up, but it'll give you the level of granularity you're asking for. It lets you specify a list of `features` for a rule target, and each feature can enable multiple flag_sets, where each flag_set is unique to a specific type of action (e.g. c compile, cpp compile, ar, linking).

For example, we set `-frtti` on cpp compile actions by enabling the rtti feature on targets. The rtti feature is defined here



--
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/773fa058-3e17-4d3b-9fd2-ebf38b82e3a2n%40googlegroups.com.

Michael Fitzner

unread,
Feb 18, 2022, 5:04:00 AM2/18/22
to bazel-discuss

Thx for your comment.

I think your solution works well in a scenario where you use an individual toolchain configuration and modify the toolchain itself.  With the “feature” approach it is possible to use it from the command line / .bazelrc or in the context of transitions.

In a scenarion where the auto detected c/c++ toolchain or any generic toolchain are used I don't see a proper solution yet. It would be great if the individual flags could be organized on a central location and grouped together for managing different sets of flags.

Reply all
Reply to author
Forward
0 new messages