使用bazel构建Makefile [英] Building Makefile using bazel
问题描述
我正在尝试在bazel项目中构建子模块的Makefile.我看到bazel确实提供了执行bash命令的规则.我目前面临两个问题-
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.如何在执行命令之前将CD插入目录,例如-
genrule(
name = "hello-make",
srcs = ["hello"] + glob(["hello/**"]),
outs = ["hello/main"],
cmd = "(cd $(location :hello) && make)",
)
2.在执行genrule之前如何更新更新子模块?
添加cmd = "(git submodule init && git submodule update)"
给出fatal: Not a git repository (or any of the parent directories): .git
复制步骤:
-
git clone git@github.com:bazelbuild/examples.git
-
cd examples/ && git submodule add git@github.com:mucsi96/cpp-hello-world.git cpp-tutorial/stage1/main/hello
-
cd cpp-tutorial/stage1/ && bazel build //main:hello-world
git clone git@github.com:bazelbuild/examples.git
cd examples/ && git submodule add git@github.com:mucsi96/cpp-hello-world.git cpp-tutorial/stage1/main/hello
cd cpp-tutorial/stage1/ && bazel build //main:hello-world
此步骤之后,我想添加一条规则,该规则允许我进行初始化,更新并制作hello子模块.
After this step I want to add a rule that allows me init, update and make the hello submodule.
是否有比将其创建为git clone git@github.com:bazelbuild/examples.git
的子模块更好的方法来构建git@github.com:mucsi96/cpp-hello-world.git
?
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
?
实际项目更加复杂,并且无法为cpp-hello-world.git创建BUILD文件.
The actual projects are more complex and creating a BUILD file for cpp-hello-world.git is not feasible.
推荐答案
而不是滚动自己的genrule
来构建使用Makefile
的(大概是C ++)项目,请查看
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.
请参见 simple_make
示例.在其中,您首先要创建一个filegroup
来收集与要构建的项目相关的所有源和文件,包括Makefile
本身:
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__"],
)
然后,调用rules_foreign_cc
的 make
特定的属性,例如prefix
,make_env_vars
,甚至还有一种用make_commands
覆盖整个make
命令的方式.在此示例中,我们仅使用lib_source
和static_libraries
属性:
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"],
)
最后,运行bazel build //package/to:make_lib
调用make
规则.
在执行genrule之前如何更新更新子模块?
How do I update update submodule before executing genrule?
不要尝试执行此操作,尤其是当它更新就地项目源时. genrules
和其他规则不应在构建过程中修改源的状态,而应仅构建输出文件.考虑在构建之前运行单独的脚本来更新子模块.
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.
这篇关于使用bazel构建Makefile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!