Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Bazel rule with unknow output file

458 views
Skip to first unread message

Helcio Mayor

unread,
Jul 26, 2024, 4:39:02 AM7/26/24
to bazel-discuss
Hello everyone! My understanding is that the output should be declared exactly as so Software is Hermetic and idempotent in its behavior.

Could you please point me on how I hand a situation where I don't know how many files are generated from a given compilation?

For example, the MSVC "MC.exe" compiler (language resource compiler) generates filename.rc, filename.h and one or more  Msg0000X.bin files from a "filename.mc" . The number of .bin files depends on the languages supported.

Kind regards and thanks in advance.
Helcio

Tiago Quelhas

unread,
Jul 26, 2024, 5:17:48 AM7/26/24
to Helcio Mayor, bazel-discuss
You can write a Starlark rule with a directory output (`ctx.actions.declare_directory`) which is allowed to contain an arbitrary number of files, determined at execution time. However, subsequent rules will have to consume the directory as an indivisible unit; it's not possible for them to depend only on individual files inside the directory.

You might also want to consider a simpler solution: ask your users to pass the expected set of languages as a parameter to the rule, then use it to declare the expected output files. At execution time, fail if the set doesn't match the .mc file contents. This does mean that users have to edit a BUILD file whenever they add or remove a language from the .mc file.

--
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/dc81737f-da03-4586-ad77-7facfc2443c4n%40googlegroups.com.

Filip Filmar

unread,
Jul 29, 2024, 2:35:49 AM7/29/24
to Tiago Quelhas, Helcio Mayor, bazel-discuss
On Fri, Jul 26, 2024 at 2:17 AM 'Tiago Quelhas' via bazel-discuss <bazel-...@googlegroups.com> wrote:
You can write a Starlark rule with a directory output (`ctx.actions.declare_directory`) which is allowed to contain an arbitrary number of files, determined at execution time. However, subsequent rules will have to consume the directory as an indivisible unit; it's not possible for them to depend only on individual files inside the directory.

Well, yes and no.

While you can not depend on individual file in the output directory, you can excavate a file from a directory if it has known structure and have it be available as a filegroup. 

F

David Turner

unread,
Jul 29, 2024, 5:55:14 AM7/29/24
to Filip Filmar, Tiago Quelhas, Helcio Mayor, bazel-discuss
Thanks for the link Filip.

FWIW, it seems there is no need to wrap the output directory in a filegroup(), to use it as an input for a genrule(), see this test in the Bazel sources for example: https://cs.opensource.google/bazel/bazel/+/master:src/test/shell/bazel/bazel_execlog_test.sh;drc=3b0b6ed419e68046ac516c60b236624ee6f570c4;l=55

On the other hand, I think there is no way for a genrule() to produce an output directory. A custom rule() definition is always necessary for any action that produces these.
 
F

--
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.
Reply all
Reply to author
Forward
0 new messages