user9092688
user9092688

Reputation: 171

Bazel get location of external dependency as command line arg for py_binary

I need the path to external (or internal) dependency to pass it as an argument to a function inside. We need the location to the folder, not specific files. Also, sometimes, we need the path to the folder where a shared library, generated by cc_library.

Python file

import cppyy
cppyy.add_include_path('path/to/external/dependency/1')
cppyy.add_library_path('path/to/another/external/dependency/2')
cppyy.add_include_path('path/to/another/internal/dependency')
cppyy.include('file/in/external/dependency')

BUILD file

py_binary(
    name = "sample",
    srcs = ["sample.py"],
    deps = [
        "@cppyy_archive//:cppyy",
    ],
    data = [
        "@external-dependency//location:target",
        "//internal-dependency/location:target2"
    ]
)

Upvotes: 3

Views: 2932

Answers (1)

Laurenz
Laurenz

Reputation: 1940

From https://docs.bazel.build/versions/master/external.html#layout:

You can see the external directory by running:

ls $(bazel info output_base)/external

How the paths in external actually look like really depends on the rule used for the archive. For example, if it's declared using an http_file in the WORKSPACE file:

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_file")
http_file(
    name = "fenix",
    urls = ["https://github.com/mozilla-mobile/fenix/archive/v76.0.0-beta.2.tar.gz"],
    sha256 = "94050c664e5ec5b66cd2ca9f6a8b898987ab63d9602090533217df1a3f2dc5a9"
)

You will find that v76.0.0-beta.2.tar.gz file as external/fenix/file/downloaded:

user@host:~$ file $(bazel info output_base)/external/fenix/file/downloaded                 
/home/user/.cache/bazel/_bazel_user/761044447e04744e746cd54d0b4b5056/external/fenix/file/downloaded: gzip compressed data, from Unix, original size modulo 2^32 15759360

Upvotes: 2

Related Questions