buildbazelbazel-rules

bazel debian package as a dependency for my project


I need to build my project with bazel and it uses libcap-dev as a dependency. how to use that inside bazel? I tried used the genrule to install it but i can't use sudo inside the cmd section. I don't want to rebuild the package inside my code but if there is no other option please tell me how.


Solution

  • Using packages from apt is not the most "bazelist" way to do manage dependencies. It would depend a lot on how your sources.list is configured and would not be hermetic at all.

    Build time dependency

    I guess you need headers files as build-time dependencies.

    You can grab tarballs from https://packages.debian.org/sid/libcap-dev with https://bazel.build/rules/lib/repo/http#http_archive or directly use the source repository https://salsa.debian.org/debian/libcap2 with the https://bazel.build/rules/lib/repo/git#new_git_repository rule.

    Then you can import the lib with https://bazel.build/reference/be/c-cpp#cc_import

    Runtime dependency

    It depends a lot on what you are building. If you intend to run your project through bazel run with dynamically linked lib you will have to include the .so files in the cc_library for the matching cc_binary to find them.

    If you intend to build a debian package that can be distributed you will have to add libcap as a dependency of this package.

    Example

    helpers.bzl

    def _impl(repository_ctx):
        repository_ctx.download_and_extract(repository_ctx.attr.url, sha256=repository_ctx.attr.sha256)
        repository_ctx.extract("data.tar.xz", rename_files={"./usr/include/sys/capability.h":"./sys/capability.h"}) # Maybe use glob for archive extension
        repository_ctx.symlink(repository_ctx.attr.build_file, "BUILD")
    
    deb_package = repository_rule(
        implementation=_impl,
        attrs={
            "url": attr.string(mandatory=True),
            "sha256": attr.string(mandatory=True),
            "build_file": attr.label(mandatory=True),
        },
    )
    

    WORKSPACE

    load("@//:helpers.bzl", "deb_package")
    
    deb_package(
        name = "libcap2-dev",
        url = "http://ftp.fr.debian.org/debian/pool/main/libc/libcap2/libcap-dev_2.66-4_amd64.deb",
        sha256 = "da67132dba6eb1c452b999d9ac77c10ce4a5ad230ccbde1312868744b149f879",
        build_file = "@//:libcap2-dev.BUILD",
    )
    

    libcap2-dev.BUILD

    cc_import(
        name = "lib",
        hdrs = ["sys/capability.h"],
        shared_library = "usr/lib/x86_64-linux-gnu/libcap.so",
        static_library = "usr/lib/x86_64-linux-gnu/libcap.a",
        visibility = [
            "//visibility:public"
        ]
    )
    

    example.cc

    #include "sys/capability.h"
    
    int main(void)
    {
            cap_t cap;
            cap = cap_get_proc();
    
            return 0;
    }
    

    BUILD

    cc_binary(
        name = "example_libcap2",
        srcs = [
            "example.cc",
        ],
        deps = ["@libcap2-dev//:lib"],
    )