I am trying to build Makefile of a submodule in a bazel project. I see that bazel does provide genrule to execute bash command. I am facing two issues currently -
Instead of rolling your own genrule
to build a (presumably C++) project that uses a Makefile
, check out rules_foreign_cc instead. rules_foreign_cc
is used by envoy
and other various large C++ programs to build external CMake and Make based dependencies.
See the simple_make example. In it, you would first make a filegroup
to collect all the sources and files related to the project to be built, including the Makefile
itself:
filegroup(
name = "sources",
srcs = glob(["**"]),
visibility = ["//simple_make:__subpackages__"],
)
Then, call rules_foreign_cc
's make rule, which also comes with other make
-specific attributes like prefix
, make_env_vars
, and even a way to override the entire make
command with make_commands
. In this example, we're just using the lib_source
and static_libraries
attributes:
load("@rules_foreign_cc//tools/build_defs:make.bzl", "make")
make(
name = "make_lib",
lib_source = "//simple_make/code:sources",
static_libraries = ["liba.a"],
)
Finally, run bazel build //package/to:make_lib
to invoke the make
rule.
How do I update update submodule before executing genrule?
Try not to do this especially if it updates the sources of the project in-place. genrules
and other rules should not modify the state of your sources during a build, but instead only build output files. Consider running a separate script to update submodules prior to the build.