从多个仓库构建 Bazel 包
Build Bazel package from multiple repos
我正在尝试使用外部模块构建 OpenCV,但未能想出一个干净的解决方案。
我已经使用 CMake 成功构建了 OpenCV (rules_foreign_cc)。但是为了能够使用外部模块进行构建,我需要下载一个单独的 repo (opencv_contrib),然后使用第二个 repo 的路径作为构建参数进行构建。
有没有一种方法可以做到这一点,而无需创建自己的 git 存储库来组合这两个 git 存储库?
您可以在 WORKSPACE
文件中使用 git_repository
规则让 bazel 在构建之前自动克隆 git 存储库。
将其添加到 WORKSPACE
文件后,您就可以在自己的存储库中引用远程存储库的构建目标。
查看 this repo 示例。
# 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"],
)
我正在尝试使用外部模块构建 OpenCV,但未能想出一个干净的解决方案。
我已经使用 CMake 成功构建了 OpenCV (rules_foreign_cc)。但是为了能够使用外部模块进行构建,我需要下载一个单独的 repo (opencv_contrib),然后使用第二个 repo 的路径作为构建参数进行构建。
有没有一种方法可以做到这一点,而无需创建自己的 git 存储库来组合这两个 git 存储库?
您可以在 WORKSPACE
文件中使用 git_repository
规则让 bazel 在构建之前自动克隆 git 存储库。
将其添加到 WORKSPACE
文件后,您就可以在自己的存储库中引用远程存储库的构建目标。
查看 this repo 示例。
# 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"],
)