【问题标题】:Build Makefile Target in Bazel在 Bazel 中构建 Makefile 目标
【发布时间】:2021-06-26 06:13:04
【问题描述】:

我正在尝试使用 bazel 构建 openssl。这是我目前的设置

在我的项目根目录中的/WORKSPACE

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
    name = "openssl",
    urls = ["https://www.openssl.org/source/openssl-1.1.1c.tar.gz"],
    sha256 = "f6fb3079ad15076154eda9413fed42877d668e7069d9b87396d0804fdb3f4c90",
    strip_prefix = "openssl-1.1.1c",
    build_file = "@//:BUILD.openssl",
)

在我的/BUILD.openssl 文件中

genrule(
    name = "build",
    visibility = ["//visibility:public"],
    srcs = glob(["**"]),
    cmd = '\n'.join([
        './Configure darwin64-x86_64-cc -mmacosx-version-min="10.14" --prefix=$@ --openssldir=$@',
        'make',
        'make install',
    ]),
    outs = ["openssl"],
)

我不太明白 genrule 运行时我所在的文件夹,因为它会抱怨

/bin/bash: ./Configure: No such file or directory

另外,我在 makefile 目标中为 srcsouts 指定什么? 在这种情况下,我将为 openssls --prefix--openssldir 指定什么目录?

考虑到这可能是最重要的用例,Bazel 中未配置的集成目标的文档记录如此之差,这让我有点惊讶。

【问题讨论】:

    标签: bazel


    【解决方案1】:

    使用rules_foreign_cc 与基于make 的项目集成。根据this bug, 中的信息,我得到了一个基本的 openssl 构建工作:

    在您的WORKSPACE 中,添加:

    load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
    
    # Group the sources of the library so that rules in rules_foreign_cc have access to it
    all_content = """filegroup(name = "all", srcs = glob(["**"]), visibility = ["//visibility:public"])"""
    
    http_archive(
        name = "openssl",
        build_file_content = all_content,
        sha256 = "f6fb3079ad15076154eda9413fed42877d668e7069d9b87396d0804fdb3f4c90",
        strip_prefix = "openssl-1.1.1c",
        urls = ["https://www.openssl.org/source/openssl-1.1.1c.tar.gz"],
    )
    
    # Rule repository
    http_archive(
        name = "rules_foreign_cc",
        sha256 = "7b350ba8b2ef203626fda7572506111e3d5286db92de3ecafdcbc99c6c271265",
        strip_prefix = "rules_foreign_cc-16ddc00bd4e1b3daf3faee1605a168f5283326fa",
        url = "https://github.com/bazelbuild/rules_foreign_cc/archive/rules_foreign_cc-16ddc00bd4e1b3daf3faee1605a168f5283326fa.zip",
    )
    
    load("@rules_foreign_cc//:workspace_definitions.bzl", "rules_foreign_cc_dependencies")
    
    rules_foreign_cc_dependencies()
    

    在您的 BUILD 文件中,添加:

    load("@rules_foreign_cc//tools/build_defs:configure.bzl", "configure_make")
    
    configure_make(
        name = "openssl",
        configure_command = "config",
        configure_options = [
            "no-shared",
        ],
        lib_source = "@openssl//:all",
        static_libraries = [
            "libssl.a",
            "libcrypto.a",
        ],
    )
    

    最后,运行bazel build

    $ bazel build :all
    INFO: Analyzed target //:openssl (1 packages loaded, 1 target configured).
    INFO: Found 1 target...
    INFO: From CcConfigureMakeRule openssl/include:
    Target //:openssl up-to-date:
      bazel-bin/openssl/include
      bazel-bin/openssl/lib/libcrypto.a
      bazel-bin/openssl/lib/libssl.a
      bazel-bin/copy_openssl/openssl
      bazel-bin/openssl/logs/Configure_script.sh
      bazel-bin/openssl/logs/Configure.log
      bazel-bin/openssl/logs/wrapper_script.sh
    

    【讨论】:

    • 我需要能够在构建期间导出环境变量 (export OSX_DEPLOYMENT_TARGET="10.14"),否则生成的库没有所需的 LC_VERSION_ 集。我现在用自定义genrule 解决了它,是否可以在configure_make 中导出一个变量?
    • 也许是configure_env_vars 属性? github.com/bazelbuild/rules_foreign_cc/blob/…
    • 不,这行不通。试过了。我回到了正常的genrule
    【解决方案2】:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多