使用bazel构建Makefile

Adi*_*tya 4 c++ git makefile bazel

我正在尝试在bazel项目中构建子模块的Makefile。我看到bazel确实提供了执行bash命令的规则。我目前面临两个问题-

1.如何在执行命令之前将CD插入目录,例如-

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

2.在执行genrule之前如何更新更新子模块?

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

复制步骤:

  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

在此步骤之后,我想添加一条规则,该规则允许我进行初始化,更新并制作hello子模块。

git@github.com:mucsi96/cpp-hello-world.git比将其创建为的子模块更好的构建方法git clone git@github.com:bazelbuild/examples.git吗?

实际的项目更加复杂,为cpp-hello-world.git创建一个BUILD文件是不可行的。

Jin*_*Jin 5

不用自己动手genrule来构建使用的(大概是C ++)项目,而是Makefile签出rules_foreign_ccrules_foreign_cc所使用的envoy和其他各种大型C ++程序建立外部CMake的,并根据依赖关系。

参见simple_make示例。在其中,您首先filegroup要收集与要构建的项目相关的所有源和文件,包括其Makefile本身:

filegroup(
    name = "sources",
    srcs = glob(["**"]),
    visibility = ["//simple_make:__subpackages__"],
)
Run Code Online (Sandbox Code Playgroud)

然后,调用rules_foreign_ccmake规则,其还带有其他make类似特异性的属性prefixmake_env_vars甚至一种覆盖整个make用命令make_commands。在此示例中,我们仅使用lib_sourceand static_libraries属性:

load("@rules_foreign_cc//tools/build_defs:make.bzl", "make")

make(
    name = "make_lib",
    lib_source = "//simple_make/code:sources",
    static_libraries = ["liba.a"],
)
Run Code Online (Sandbox Code Playgroud)

最后,运行bazel build //package/to:make_lib以调用make规则。

执行genrule之前如何更新更新子模块?

尝试不要执行此操作,尤其是当它就地更新项目源时。genrules和其他规则不应在构建过程中修改源的状态,而应仅构建输出文件。考虑在构建之前运行单独的脚本来更新子模块。