I have a problem with building my target, because it needs to build files which aren't available prior to bazel build. I'd like to generate those files during building my target. Problem is that it's hard to tell how many files will be generated. I want to create bigger monorepo with possibility of build many languages/platforms, for now I stuck cuz I'm trying to use script which uses SWIG to generate cpp/java glue code which are placed in unknown amount of files. Additionally I need those files to be part of my project tree, cuz I'd like to be able to debug them in runtime and to have possibility of committing changes in them.
This my proposition of soultion to that problem. (https://github.com/AnDevi/bazel-generating-files-example)
This example (BUILD.bazel) uses only cc_library
, cc_binary
and genrule
.
In cc_libary
I'd like to compile files which are created in genrule
via script generator.sh (we need to assume that this script may generate unknown amount files).
Due to that genrule
can't have directory as output it generates dummy file as output, but the main objective of it is to run script and generate other files.
(genrule
also works in local mode
cuz I need to place generated files in my project tree and be able to debug them.)
My cc_libarary
uses this genrule
as part of its srcs
, but another part its srcs
are files generated by script under the path source/Generated/*
. Problem is that cc_libarary
may want to use files under source/Generated/*
before they will be created and that is why this solution is unfortunately incomplete.
So I wonder if this approach doomed to failure (?) cuz I can't to ensure in any way those files generated by this genrule
will be ready before cc_library
will want to use them under the given path.
As different approach I tried to use aspect-build and run_binary but I don't think that it can solve my problem. I tried to run script generating files using run_binary
, but I failed. Nonetheless there is second reason why I think it's not suitable for my problem, cuz I can't use it in local mode
(as genrule
) and due to that I can't add generated files to my project tree.
Maybe there is completely different preferable solution ? Maybe someone else faced similar problem already ?
Ok, I found solution for my problem:
I used this github.com/bazelbuild/bazelisk?tab=readme-ov-file#toolsbazel
In this wrapper binary tools/bazel
I'm running separate step
#!/usr/bin/env bash
$BAZEL_REAL build //target_with_genrule_in_here
$BAZEL_REAL $@
before actual build I'm running genrule
which runs script which creates all files I wanted to create (genrule
has set local=True
, so I can create those files in my project tree) and build them in the next step.
Thanks to what I can still achieve all I wanted with calling only one bazel build
command.