以下是一种适用于当前版本
bazel
(v3.1.0)的最新解决方案。在此小项目中,我想构建一个依赖于最新的
openCV
版本(4.3.0)的C++程序,但只选择了一组模块(core,highgui,imgcodecs,imgproc)。
无需本地安装
openCV
,
bazel
从github加载所需文件(即使已安装旧版本的
openCV
也可以正常工作):
/WORKSPACE
文件的内容:
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
all_content = """filegroup(name = "all", srcs = glob(["**"]), visibility = ["//visibility:public"])"""
http_archive(
name = "opencv",
build_file_content = all_content,
strip_prefix = "opencv-4.3.0",
urls = ["https://github.com/opencv/opencv/archive/4.3.0.zip"],
)
http_archive(
name = "rules_foreign_cc",
strip_prefix = "rules_foreign_cc-master",
url = "https://github.com/bazelbuild/rules_foreign_cc/archive/master.zip",
)
load("@rules_foreign_cc//:workspace_definitions.bzl", "rules_foreign_cc_dependencies")
rules_foreign_cc_dependencies()
/BUILD
文件的内容:
load("@rules_foreign_cc//tools/build_defs:cmake.bzl", "cmake_external")
cmake_external(
name = "opencv",
cmake_options = [
"-GNinja",
"-DBUILD_LIST=core,highgui,imgcodecs,imgproc",
],
lib_source = "@opencv//:all",
make_commands = [
"ninja",
"ninja install",
],
out_include_dir = "include/opencv4",
shared_libraries = [
"libopencv_core.so",
"libopencv_highgui.so",
"libopencv_imgcodecs.so",
"libopencv_imgproc.so",
],
visibility = ["//visibility:public"],
)
最后,你需要依赖于opencv
的目标文件,在我的情况下是文件/opencv/BUILD
:
cc_binary(
name = "opencv",
srcs = ["opencv.cpp"],
data = [
"LinuxLogo.jpg",
"WindowsLogo.jpg",
],
deps = ["//:opencv"],
)
如果您想尝试它,请转到以下链接: blackliner/automata
git clone https://github.com/blackliner/automata.git
cd automata
bazel build ...