【问题标题】:Bazel: Referencing a file inside an output directory in a genruleBazel:在 genrule 中引用输出目录中的文件
【发布时间】:2020-04-24 12:42:51
【问题描述】:

我正在尝试引用嵌套在 genrule 中另一个规则的输出目录中的规则的输出。

比如我用rules_foreign_cc来构建boost:

boost_build(
    name = "boost",
    lib_source = "@boost//:all",
    linkopts = [
        "-lpthread",
    ],
    shared_libraries = [
        "libboost_chrono.so.1.72.0",
        "libboost_program_options.so.1.72.0",
        "libboost_filesystem.so.1.72.0",
        "libboost_system.so.1.72.0",
        "libboost_thread.so.1.72.0",
        "libboost_timer.so.1.72.0",
    ],
    user_options = [
        "cxxstd=17",
        "--with-chrono",
        "--with-filesystem",
        "--with-program_options",
        "--with-system",
        "--with-thread",
        "--with-timer",
        "-j4",
    ],
)

当我构建它时,我会看到输出:

bazel build //:boost
INFO: Invocation ID: 36440de3-15f2-4ca0-8802-0a95f75ed926
INFO: Analyzed target //:boost (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
Target //:boost up-to-date:
  bazel-bin/boost/include
  bazel-bin/boost/lib/libboost_chrono.so.1.72.0
  bazel-bin/boost/lib/libboost_program_options.so.1.72.0
  bazel-bin/boost/lib/libboost_filesystem.so.1.72.0
  bazel-bin/boost/lib/libboost_system.so.1.72.0
  bazel-bin/boost/lib/libboost_thread.so.1.72.0
  bazel-bin/boost/lib/libboost_timer.so.1.72.0
  bazel-bin/copy_boost/boost
  bazel-bin/boost/logs/BuildBoost_script.sh
  bazel-bin/boost/logs/BuildBoost.log
  bazel-bin/boost/logs/wrapper_script.sh
INFO: Elapsed time: 0.758s, Critical Path: 0.00s
INFO: 0 processes.
INFO: Build completed successfully, 1 total action

Boost 工作正常,我可以在 cc_library 目标中引用它,并且二进制文件运行良好。

现在,我想引用 genrule 中的一个输出。我要引用的文件嵌套在boost/lib/ 目录中。我期望类似:$(location :boost/lib/libboost_program_options.so.1.72.0),但这不起作用。

在目录中引用输出的正确方法是什么?

【问题讨论】:

    标签: bazel bazel-rules starlark


    【解决方案1】:

    这就是我所做的,希望它有所帮助。 在genrule 中,创建一个变量来保存提升规则的输出数组,使用$(locations) 而不是$(location)。然后循环遍历数组,找到以后要使用的路径。

    BOOST_OUTPUTS=( $(locations boost) )
    LIBBOOST_CHRONO_SO=
    
    for i in "$${{BOOST_OUTPUTS[@]}}"
    do
        if [[ "$$i" == *"libboost_chrono.so.1.72.0"* ]]; then
            LIBBOOST_CHRONO_SO="$$i"
        fi
    done
    
    # Do something with LIBBOOST_CHRONO_SO
    

    请注意 $${{}} 用于转义特殊字符。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-31
      • 1970-01-01
      • 1970-01-01
      • 2022-11-23
      相关资源
      最近更新 更多