在Bazel中构建Makefile目标

时间:2019-09-25 08:48:11

标签: bazel

我正在尝试使用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中未配置的目标的文档很少,因为这可能是最重要的用例。

2 个答案:

答案 0 :(得分:1)

使用rules_foreign_cc与基于make的项目集成。根据{{​​3}}中的信息,我得到了一个基本的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 = [
        "libcrypto.a",
        "libssl.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

答案 1 :(得分:0)

有关更多详细信息,请参见https://github.com/bazelbuild/rules_foreign_cc/issues/337 ...