My project depends on some external libraries which I have to bazelfy myself. Thus, my WORKSPACE
:
http_archive(
name = "external_lib_component1",
build_file = "//third_party:external_lib_component1.BUILD",
sha256 = "xxx",
urls = ["https://example.org/external_lib_component1.tar.gz"],
)
http_archive(
name = "external_lib_component2",
build_file = "//third_party:external_lib_component2.BUILD",
sha256 = "yyy",
urls = ["https://example.org/external_lib_component2.tar.gz"],
)
...
The two entries above are similar, and external_lib_component{1, 2}.BUILD
share a lot of code.
What is the best way to share code (macros) between them?
Just putting a shared_macros.bzl
file into third_party/
won't work, because it will not be copied into
the archive location on build (only the build_file
is copied).
If you place a bzl
file such a In your./third_party/shared_macros.bzl
into your tree as you've mentioned.
Then in the //third_party:external_lib_component1.BUILD
and //third_party:external_lib_component2.BUILD
you provide for your external dependencies, you can load symbols from that shared file using:
load("@//third_party:shared_macros.bzl", ...)
Labels starting with @//
refer to packages from the main repository, even when used in an external dependency (as they would otherwise be rooted when starting with //
. You can for check docs on labels, in particular the last paragraph.
Alternatively you can also refer to the "parent" project by its name. If in your WORKSPACE
file you've had:
workspace(name = "parent")
You could say:
load("@parent//third_party:shared_macros.bzl", ...)
Note: in versions prior to 2.0.0 you might want to add --incompatible_remap_main_repo
if you mixed both of above approaches in your project.