4/29/2016

How To Port a CMake build project to Bazel

For large project, say OpenCV, the CMake build can be very complicated, which makes it difficult to create a BUILD file directly for Bazel. This article will describe an easy way to add a BUILD file.

1. cmake and make the project to create pre-compiled .a files.


2. Find all the header files in the project packages. Usually they're included in folders named with "include". A command to find all "include" folders.
find . -iname include

3. Create a new git repository on github.com. Check in all the .a and header files to the repository. The repository can be 'projectname'-'version'-precompiled, e.g. opencv-3.1.0-precompiled. Also, a BUILD file should be checked in with them. An example of the BUILD file: https://github.com/zoukyle/opencv-3.1.0-precompiled/blob/master/BUILD

4. Then the project can be depended by your own project. In your Bazel WORKSPACE file, you'll need to add:
git_repository(
    name   = "precompiled_opencv_git",
    commit = "cac2317cde1d4c9b68c6c4425d7171322d52aaed", # The latest commit id
    remote = "https://github.com/zoukyle/opencv-3.1.0-precompiled.git"
)

bind(
    name   = "opencv",
    actual = "@precompiled_opencv_git//:opencv_lib",
)

 
5. Say you want to bazel build a binary. In the BUILD file, you can add the dependency:
deps = [
    "//external:opencv",
], 

2 comments:

Unknown said...

Hi Liang,

The link to the BUILD file is broken: https://github.com/zoukyle/opencv-3.1.0-precompiled/blob/master/BUILD

Do you still have the repo anywhere or have you deleted it? Could you maybe just write out the BUILD file if its not too long?

Thanks!
- Nathan

Liang Zou said...

git Restored.