This is what I did for OpenCV 2.4.13.2, core/ only. This approach goes from the opencv source, which is adapted from the accepted answer above by @kristina.
The first thing is to add the http_archive for the opencv 2.4 release:
# OpenCV 2.4.13.2
new_http_archive(
name = "opencv2",
url = "https://github.com/opencv/opencv/archive/2.4.13.2.zip",
build_file = "third_party/opencv2.BUILD",
strip_prefix = "opencv-2.4.13.2",
)
And then, add the file third_party/opencv2.BUILD as:
cc_library(
name = "dynamicuda",
hdrs = glob([
"modules/dynamicuda/include/**/*.hpp",
]),
includes = [
"modules/dynamicuda/include"
],
)
cc_library(
name = "core",
visibility = ["//visibility:public"],
srcs = glob(["modules/core/src/**/*.cpp"]),
hdrs = glob([
"modules/core/src/**/*.hpp",
"modules/core/include/**/*.hpp",
]) + [
":module_includes",
":cvconfig",
":version_string",
],
copts = [
"-Imodules/dynamicuda/include",
],
# Note that opencv core requires zlib and pthread to build.
linkopts = ["-pthread", "-lz"],
includes = [
"modules/core/include",
],
deps = [
":dynamicuda",
],
)
genrule(
name = "module_includes",
cmd = "echo '#define HAVE_OPENCV_CORE' > $@",
outs = ["opencv2/opencv_modules.hpp"],
)
genrule(
name = "cvconfig",
outs = ["cvconfig.h"],
cmd = """
cat > $@ <<"EOF"
// JPEG-2000
#define HAVE_JASPER
// IJG JPEG
#define HAVE_JPEG
// PNG
#define HAVE_PNG
// TIFF
#define HAVE_TIFF
// Compile for 'real' NVIDIA GPU architectures
#define CUDA_ARCH_BIN ""
// NVIDIA GPU features are used
#define CUDA_ARCH_FEATURES ""
// Compile for 'virtual' NVIDIA PTX architectures
#define CUDA_ARCH_PTX ""
EOF"""
)
genrule(
name = "version_string",
outs = ["version_string.inc"],
cmd = """
cat > $@ <<"EOF"
"\\n"
)
Note that I did not put anything in the version_string.inc. It is just a C++ string literal which does not affect the functionality of OpenCV. If you are really interested in this file see this example.
After this you should be able to add target with dependencies on @opencv2//:core.