7

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 -

1. How to cd into a directory before executing command, something like -

genrule(
    name = "hello-make",
    srcs = ["hello"] + glob(["hello/**"]),
    outs = ["hello/main"],
    cmd = "(cd $(location :hello) && make)",
)

2. How do I update update submodule before executing genrule?

Adding cmd = "(git submodule init && git submodule update)" gives fatal: Not a git repository (or any of the parent directories): .git

Step to reproduce:

  1. git clone git@github.com:bazelbuild/examples.git
  2. cd examples/ && git submodule add git@github.com:mucsi96/cpp-hello-world.git cpp-tutorial/stage1/main/hello
  3. cd cpp-tutorial/stage1/ && bazel build //main:hello-world

After this step I want to add a rule that allows me init, update and make the hello submodule.

Is there a better way to build git@github.com:mucsi96/cpp-hello-world.git than creating it as a submodule of git clone git@github.com:bazelbuild/examples.git?

The actual projects are more complex and creating a BUILD file for cpp-hello-world.git is not feasible.

Aditya
  • 5,509
  • 4
  • 31
  • 51
  • Maybe [this](https://github.com/deepmind/lab/blob/master/glib.BUILD#L17-L19) is a semi-useful example -- something something `$(location ...)`? – Kerrek SB Sep 20 '19 at 22:28
  • Thanks, yeah I hope to fix this incrementally. I get `$(location) expression is not a declared prerequisite of this rule` on using `cd ./$(location hello)` – Aditya Sep 20 '19 at 22:58
  • You need to refer to one of the declared names -- e.g. if you have `srcs = ["foo.txt"] + glob(["*.json]),`, then you could refer to `$(location :foo.txt)` etc. Maybe list your sources explicitly, or at least one of them (the `Makefile`)? – Kerrek SB Sep 20 '19 at 23:16

1 Answers1

10

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.

Jin
  • 12,748
  • 3
  • 36
  • 41
  • Thanks, this is a very detailed and helpful answer! :) Say I do not have write access to the subproject I am trying to build. I the example, I can not have `BUILD` file in `code` dir and only parent dir. Is there a way to create filegroup in the `simple_make/BUILD` file, instead of `simple_make/code/BUILD`? – Aditya Sep 23 '19 at 17:50
  • 1
    You can have the BUILD file in the parent dir - no worries. The alternative is to create a `new_local_repository` on that subproject, and apply your own `BUILD` file on it as though you're "writing" into the subproject, but I'd recommend to just write a BUILD file in the parent directory. You can learn more here: https://docs.bazel.build/versions/master/be/workspace.html#new_local_repository – Jin Sep 23 '19 at 18:19
  • followed up in a stackpost instead - https://stackoverflow.com/questions/58211438/output-external-name-repo-lib-lib-so-was-not-created-using-bazel-make – Aditya Oct 03 '19 at 02:07
  • Another followup regarding [linking this so to cc_library rule](https://stackoverflow.com/questions/58329033/how-to-link-a-library-build-using-make-rule-in-bazel) – Aditya Oct 10 '19 at 18:32