2

我正在尝试使用外部模块构建 OpenCV,但无法提出一个干净的解决方案。

我已经使用 CMake (rules_foreign_cc) 成功构建了 OpenCV。但是为了能够使用外部模块进行构建,我需要下载一个单独的 repo (opencv_contrib),然后使用第二个 repo 的路径作为构建参数进行构建。

有没有一种方法可以做到这一点,而不必创建我自己的结合这两个 git 存储库的 git 存储库?

4

2 回答 2

2
# WORKSPACE file

http_archive(
   name = "rules_foreign_cc",
   strip_prefix = "rules_foreign_cc-4010620160e0df4d894b61496d3d3b6fc8323212",
    sha256 = "07e3414cc841b1f4d16e5231eb818e5c5e03e2045827f5306a55709e5045c7fd",
   url = "https://github.com/bazelbuild/rules_foreign_cc/archive/4010620160e0df4d894b61496d3d3b6fc8323212.zip",
)

load("@rules_foreign_cc//foreign_cc:repositories.bzl", "rules_foreign_cc_dependencies")
rules_foreign_cc_dependencies()


http_archive(
    name = "opencv_contrib",
    build_file="//:opencv_contrib.BUILD",
    sha256 = "9f52fd3114ac464cb4c9a2a6a485c729a223afb57b9c24848484e55cef0b5c2a",
    urls = ["https://github.com/opencv/opencv_contrib/archive/refs/tags/4.5.2.tar.gz"],
    strip_prefix = "opencv_contrib-4.5.2",
)

http_archive(
    name = "opencv",
    sha256="ae258ed50aa039279c3d36afdea5c6ecf762515836b27871a8957c610d0424f8",
    build_file="//:opencv.BUILD",
    urls = ["https://github.com/opencv/opencv/archive/refs/tags/4.5.2.tar.gz"],
    strip_prefix = "opencv-4.5.2",
)

# opencv_contrib.BUILD file

filegroup(
    name = "modules",
    srcs = glob(["modules/**"]),
    visibility = ["//visibility:public"],
)
# opencv.BUILD file

load("@rules_foreign_cc//foreign_cc:defs.bzl", "cmake")

filegroup(
    name = "srcs",
    srcs = glob(["**"]),
    visibility = ["//visibility:public"],
)
cmake(
    name = "opencv",
    generate_args = ["-GNinja"],
    additional_inputs=["@opencv_contrib//:modules"],
    cache_entries = {
        "BUILD_SHARED_LIBS": "OFF",
        "BUILD_opencv_world": "ON",
        "OPENCV_EXTRA_MODULES_PATH": "$$EXT_BUILD_ROOT$$/external/opencv_contrib/modules",
    },
    lib_source = ":srcs",
    out_static_libs = ["libopencv_world.a"],
)
于 2021-06-01T20:22:00.653 回答
0

您可以git_repository在文件中使用规则WORKSPACE让 bazel 在构建之前自动克隆 git 存储库。

将其添加到WORKSPACE文件后,您可以在自己的存储库中引用远程存储库的构建目标。

查看此 repo以获取示例。

于 2021-06-01T18:34:52.460 回答