【问题标题】:Building Makefile using bazel使用 bazel 构建 Makefile
【发布时间】:2019-09-20 22:24:16
【问题描述】:

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

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

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

重现步骤:

  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 的子模块更好的方法来构建git@github.com:mucsi96/cpp-hello-world.git?

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

【问题讨论】:

  • 也许this 是一个半有用的例子——有些东西$(location ...)?
  • 谢谢,是的,我希望逐步解决这个问题。我得到$(location) expression is not a declared prerequisite of this rule 使用cd ./$(location hello)
  • 您需要引用已声明的名称之一——例如如果您有srcs = ["foo.txt"] + glob(["*.json]),,那么您可以参考$(location :foo.txt) 等。也许明确列出您的来源,或者至少列出其中一个(Makefile)?

标签: c++ git makefile bazel


【解决方案1】:

与其滚动您自己的genrule 来构建使用Makefile 的(可能是C++)项目,不如查看rules_foreign_cc。 rules_foreign_cc 被envoy 和其他各种大型 C++ 程序用于构建外部 CMake 和基于 Make 的依赖项。

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

filegroup(
    name = "sources",
    srcs = glob(["**"]),
    visibility = ["//simple_make:__subpackages__"],
)

然后,调用rules_foreign_cc 的make rule,它还带有其他make 特定属性,如prefix、make_env_vars,甚至还有一种用@987654342 覆盖整个make 命令的方法@。在这个例子中,我们只使用了lib_source 和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"],
)

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

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

尽量不要这样做,尤其是当它就地更新项目的源代码时。 genrules 和其他规则不应在构建期间修改源的状态,而应仅构建输出文件。考虑在构建之前运行一个单独的脚本来更新子模块。

【讨论】:

  • 谢谢,这是一个非常详细和有用的答案! :) 假设我没有对我正在尝试构建的子项目的写入权限。我的例子,我不能在code 目录中有BUILD 文件,只有父目录。有没有办法在simple_make/BUILD 文件中创建文件组,而不是simple_make/code/BUILD?
  • 您可以在父目录中拥有 BUILD 文件 - 不用担心。另一种方法是在该子项目上创建一个new_local_repository,并在其上应用您自己的BUILD 文件,就好像您正在“写入”子项目一样,但我建议您只在父目录中编写一个BUILD 文件.您可以在这里了解更多信息:docs.bazel.build/versions/master/be/…
  • 改为在堆栈帖子中跟进 - stackoverflow.com/questions/58211438/…
  • 另一个关于linking this so to cc_library rule的后续行动
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-10
  • 1970-01-01
  • 2018-05-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多