Skip to content

feat: Add default_preprocessopts and preprocessopts for C preprocessor flags - #110

Merged
Gansito144 merged 1 commit into
bazel-contrib:mainfrom
UebelAndre:opts
Sep 15, 2026
Merged

Gansito144 merged 1 commit into
bazel-contrib:mainfrom
UebelAndre:opts

Conversation

@UebelAndre

@UebelAndre UebelAndre commented Sep 14, 2026

Copy link
Copy Markdown
Contributor

devicetree_toolchain() already had default_dtcopts, but the C preprocessor invocation in dtb() and dtbo() had no equivalent at either level. Its command line was hardcoded, and it resolves only the tool for ACTION_NAMES.preprocess_assemble without building a command line from that action's flag sets, so there was no way to pass a -D to DTS preprocessing at all.

devicetree_toolchain(default_preprocessopts=) now sets project-wide flags and dtb(preprocessopts=)/dtbo(preprocessopts=) append to them, mirroring the existing default_dtcopts/dtcopts pair.

Both are placed after -undef -D__DTS__, so -undef does not discard them, and before -x assembler-with-cpp, so the flags cannot change the preprocessing mode.

Setting either one when the source is not preprocessed is an error rather than a no-op. The two cases are a toolchain with preprocess = False, and a toolchain that leaves preprocessing optional with no CC toolchain registered. Dropping the flags silently would build a devicetree that does not match what they ask for.

The preprocess action now carries a DtPreprocess mnemonic so it can be identified without matching on output filenames.

preprocessopts_test asserts the resulting flag order by inspecting the preprocess action's argv, and preprocessopts_error_test asserts the failure above. Both reach a purpose-built toolchain through the --extra_toolchains config setting, which avoids registering a test toolchain module-wide.

@jacky8hyf jacky8hyf left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

re default_copts:

This is the simple path to resolve this (custom flags to the preprocessor), but IMHO, a more decoupled path should be:

(1) In the registered toolchain, add flags for the action "preprocess_assemble"
(2) Here, we retrieve these flags from cc_common, e.g. via get_memory_inefficient_command_line(), and feed them in.

Reason: the preprocessor binary comes from the C toolchain, so any flags to invoke it should also come from the C toolchain, not in the DT toolchain.

If my comment is not clear, feel free to open an issue in https://github.com/bazel-contrib/rules_devicetree/issues and we can discuss there.


OTOH, the mechanism for copts looks okay. Though I am not sure about the naming. This is not the C language. We are just using the C preprocessor to preprocess, as an implementation detail. But OTOH, preprocessopts or preprocess_opts looks unreadable/long. @Gansito144 WDYT?

Comment thread devicetree/private/dtb.bzl
@jacky8hyf

Copy link
Copy Markdown
Collaborator

On a second thought, copts is fine -- it is simple and does not have ambiguity. I'll still ask @Gansito144 to confirm this.

@UebelAndre please remove the default_copts change from this PR for now, and let's get copts in to potentially unblock your use cases. Then we can talk about how to approach default_copts separately, perhaps in an issue thread.

@UebelAndre

Copy link
Copy Markdown
Contributor Author

On a second thought, copts is fine -- it is simple and does not have ambiguity. I'll still ask @Gansito144 to confirm this.

@UebelAndre please remove the default_copts change from this PR for now, and let's get copts in to potentially unblock your use cases. Then we can talk about how to approach default_copts separately, perhaps in an issue thread.

It's actually default_copts that best solves my needs. I would like to avoid needing to configure an entire cc_toolchain for just the DtPreprocess actions. I would like this to be independently configurable on my existing registeredcc_toolchain targets.

…essor flags

`devicetree_toolchain()` already had `default_dtcopts`, but the C
preprocessor invocation in `dtb()` and `dtbo()` had no equivalent at
either level. Its command line was hardcoded, and it resolves only the
tool for `ACTION_NAMES.preprocess_assemble` without building a command
line from that action's flag sets, so there was no way to pass a `-D`
to DTS preprocessing at all.

`devicetree_toolchain(default_preprocessopts=)` now sets project-wide
flags and `dtb(preprocessopts=)`/`dtbo(preprocessopts=)` append to them,
mirroring the existing `default_dtcopts`/`dtcopts` pair.

Both are placed after `-undef -D__DTS__`, so `-undef` does not discard
them, and before `-x assembler-with-cpp`, so the flags cannot change the
preprocessing mode.

Setting either one when the source is not preprocessed is an error
rather than a no-op. The two cases are a toolchain with
`preprocess = False`, and a toolchain that leaves preprocessing optional
with no CC toolchain registered. Dropping the flags silently would build
a devicetree that does not match what they ask for.

The preprocess action now carries a `DtPreprocess` mnemonic so it can be
identified without matching on output filenames.

`preprocessopts_test` asserts the resulting flag order by inspecting the
preprocess action's `argv`, and `preprocessopts_error_test` asserts the
failure above. Both reach a purpose-built toolchain through the
`--extra_toolchains` config setting, which avoids registering a test
toolchain module-wide.
@jacky8hyf

Copy link
Copy Markdown
Collaborator

I would like to avoid needing to configure an entire cc_toolchain for just the DtPreprocess actions.

Do you have a custom CC toolchain, or an automatically configured one?

If you have the former I would suggest doing it there, but if you have the latter it makse sense to add this default_copts thing here to make it more user friendly.

@UebelAndre

Copy link
Copy Markdown
Contributor Author

Do you have a custom CC toolchain, or an automatically configured one?

If you have the former I would suggest doing it there, but if you have the latter it makse sense to add this default_copts thing here to make it more user friendly.

It's custom (the former). But I don't want to alter my toolchain just for flags added to DtPreprocess actions. This would be a massive change that impacts many more teams than my own which is the only consumer of rules_devicetree. Is there an issue allowing for this level of configuration? It has no impact on the ability to configure this in a cc_toolchain, but it seems reasonable to allow the rules_devicetree toolchains to have a way to set defaults for rules_devicetree actions.

@UebelAndre UebelAndre changed the title feat: Add default_copts and copts for C preprocessor flags feat: Add default_preprocessopts and preprocessopts for C preprocessor flags Sep 14, 2026

@jacky8hyf jacky8hyf left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You convinced me. However, I still think DtPreprocess should both take the flags from the CC toolchain, and be configurable with default_copts. But your change alone is good enough for the latter -- We can do the "take flags from the CC toolchain" later.

@jacky8hyf

Copy link
Copy Markdown
Collaborator

Waiting for @Gansito144 for a second review (in a different time zone)

@Gansito144 -- if this looks good to you, please click the "Merge when ready" green button. Thanks!

@Gansito144 Gansito144 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@Gansito144
Gansito144 added this pull request to the merge queue Sep 15, 2026
Merged via the queue into bazel-contrib:main with commit dc7bcb9 Sep 15, 2026
12 checks passed
@UebelAndre

Copy link
Copy Markdown
Contributor Author

@jacky8hyf @Gansito144 would it be possible to get a release for this change?

@jacky8hyf

Copy link
Copy Markdown
Collaborator

@jacky8hyf @Gansito144 would it be possible to get a release for this change?

It's automatic (once a day) and it is kicked off just now: https://github.com/bazel-contrib/rules_devicetree/actions/runs/35135260809

@jacky8hyf

jacky8hyf commented Sep 16, 2026

Copy link
Copy Markdown
Collaborator

Actually -- it is once per two-weeks, so next time would be at the end of September I think.

https://github.com/bazel-contrib/rules_devicetree/blob/main/.github/workflows/tag.yaml#L33

Do you urgently need it? If so I can trigger the workflow manually now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants