在 Bazel 中构建 Makefile 目标

3 bazel

我正在尝试openssl用 bazel 进行构建。这是我当前的设置

在我的/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",
)
Run Code Online (Sandbox Code Playgroud)

在我的/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"],
)
Run Code Online (Sandbox Code Playgroud)

我不太明白genrule运行时我在哪个文件夹中,因为它抱怨

/bin/bash: ./Configure: No such file or directory
Run Code Online (Sandbox Code Playgroud)

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

我有点惊讶的是,集成未在 Bazel 中配置的目标的文档记录如此之少,因为这可能是最重要的用例。

Jin*_*Jin 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()
Run Code Online (Sandbox Code Playgroud)

在您的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",
    ],
)
Run Code Online (Sandbox Code Playgroud)

最后,运行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
Run Code Online (Sandbox Code Playgroud)