Generate BUILD files _within_ WORKSPACE

277 views
Skip to first unread message

Gunnar Wagenknecht

unread,
Oct 11, 2021, 1:12:49 AM10/11/21
to bazel-discuss
Hi,

I'm investigating a repository rule to generate BUILD files. So far it looks like this would only work with a repository named "something", i.e. all references to labels within the generated BUILD files would need to include the repository name (@somthing://some/label).

Is it possible to create BUILD files for the host WORKSPACE? I need to be able to refer to labels without repo prefix as if they would be within the same WORKSPACE.

Thanks!

-Gunnar

--
Gunnar Wagenknecht
gun...@wagenknecht.org, http://guw.io/



Brian Silverman

unread,
Oct 11, 2021, 5:38:07 AM10/11/21
to Gunnar Wagenknecht, bazel-discuss
Hi,

You can generate a .bzl file with a macro in a separate repository. Then you can load and call that macro from a BUILD file in the main repository. I think that's completely equivalent to writing the same text in that BUILD file directly, after transforming rule names to the native. versions and loading other rules in the .bzl file instead of the BUILD file.

The one exception I can think of is that if you use Label, it gets resolved relative to the repository of the .bzl file. I can't think of any use for that in a BUILD file though, so if you're only doing things which could be in a BUILD file it shouldn't matter.

Brian

--
You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/55CC10B0-3996-4F56-801D-68D25E80FC3F%40wagenknecht.org.

Gunnar Wagenknecht

unread,
Oct 11, 2021, 7:52:37 AM10/11/21
to bazel-discuss
On Oct 11, 2021, at 11:37, Brian Silverman <bsilve...@gmail.com> wrote:
The one exception I can think of is that if you use Label, it gets resolved relative to the repository of the .bzl file. I can't think of any use for that in a BUILD file though, so if you're only doing things which could be in a BUILD file it shouldn't matter.

I want to generate BUILD files for actually compiling code in the main workspace.

To some extend I am looking at rebuilding something similar to Maven polyglot (1). We do have a Maven extension that let developers write a simplified yaml file for dependency management. The Maven extension translates that into a full blown Maven module POM at Maven runtime. The generated pom.xml only exists temporarily on disc and is deleted when the Maven process exits.


For Bazel I can only think of two separate processes right now:

a) Run a tool to generate BUILD files
b) Bazel build //...

That leaves users with the issues of cleanup and accidental commits, etc. Ideally I would like to avoid having users run two different processes. Hence my investigation of a repository rule. But when defining this a repository I would loose the ability to reference dependencies between Bazel packages generated from the repository rule and the main workspace easily, i.e. users hand writing BUILD files would need to know which packages come from the repository and which don't. 

To some extend it's similar to dynamic dependencies, which is not supported in Bazel yet.

Konstantin

unread,
Nov 5, 2021, 2:35:16 PM11/5/21
to bazel-discuss
First of all in the build files generated by your repository rule you can use "@//..." syntax to refer to the primary workspace where Bazel is executed.

But I would imagine your repository rule generate .bzl file with the macro(s) defining rule invocations and then I would load those macros somewhere in the primary workspace to let them generate those rules in the primary workspace.

Konstantin

Reply all
Reply to author
Forward
0 new messages