Skip to content

Latest commit

 

History

History
91 lines (66 loc) · 2.47 KB

how_to_add_an_external_dependency.md

File metadata and controls

91 lines (66 loc) · 2.47 KB

How to Add a New External Dependency

1. Library that supports bazel

For example,

Simply import it into WORKSPACE:

git_repository(
  name = "com_google_absl",
  remote = "https://github.com/abseil/abseil-cpp",
  tag = "20190808",
)

2. Library that has third-party bazel rule

Some popular libraries don't support bazel natively, but may be resolved by other developers.

Try boost as an example:

git_repository(
    name = "com_github_nelhage_rules_boost",
    commit = "9f9fb8b2f0213989247c9d5c0e814a8451d18d7f",
    remote = "https://github.com/nelhage/rules_boost",
    shallow_since = "1570056263 -0700",
)

load("@com_github_nelhage_rules_boost//:boost/boost.bzl", "boost_deps")
boost_deps()

Now you can refer to it in BUILD files with @boost//:system, etc.

3. Library with handcrafted BUILD file

It's pretty common to do so. But it needs very solid knowledge with bazel.

new_http_archive(
    name = "civetweb",
    url = "https://github.com/civetweb/civetweb/archive/v1.11.tar.gz",
    sha256 = "de7d5e7a2d9551d325898c71e41d437d5f7b51e754b242af897f7be96e713a42",
    build_file = "third_party/civetweb.BUILD",
    strip_prefix = "civetweb-1.11",
)

4. Library which is pre-installed into the operating system

It's NOT recommended, as it breaks the rule of a self-contained bazel WORKSPACE. However, some libraries are very complicated to build with bazel, while the operating system, such as Ubuntu, provides easy installation.

For example,

Please do raise a discussion before doing so. Then we can add it to the docker image:

sudo apt install libopencv-dev libpoco-dev

Then add it as a third_party library in third_party/BUILD.

cc_library(
    name = "PocoFoundation",
    linkopts = ["-lPocoFoundation"],
)

Note that in such case, you should include the headers like #include <Poco/SharedLibrary.h> instead of #include "Poco/SharedLibrary.h" as they are in the system path.

References

For a detailed description on adding a dependency with Bazel, refer to the following: