在Bazel中构建Makefile目标

3

我正在尝试使用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指定什么?

在这种情况下,我应该为openssl的--prefix--openssldir指定什么目录?

我有点惊讶于没有很好地记录集成在Bazel中未配置的目标,考虑到这可能是最重要的用例。

2个回答

5

使用 rules_foreign_cc 来集成基于 make 的项目。根据这个错误信息中的信息,我实现了基本的 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 中是否可以导出变量呢? - user187676
也许是 configure_env_vars 属性?https://github.com/bazelbuild/rules_foreign_cc/blob/16ddc00bd4e1b3daf3faee1605a168f5283326fa/examples/cc_configure_make/BUILD#L18 - Jin
不行,那样做不起作用。我试过了。我回到了普通的“genrule”。 - user187676

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接