After downloading an archive throug http_archive I'd like to run a script to generate a BUILD file from the folder structure and Cmake files in it (I currently do that by hand and it is easy enough that it could be scripted). I don't find anything on how to open, read and write files in the starlark documentation but since http_archive itself is loaded from a bzl file (haven't found the source of that file yet though...) and generates BUILD files (by unpacking them from archives) I guess it must be possible to write a wrapper for http_archive that also generates the BUILD file?
2 Answers
This is a perfect use case for a custom repository rule. That lets you run arbitrary commands to generate the files for the repository, along with some helpers for common operations like downloading a file over HTTP using the repository cache (if configured). A repository rule conceptually similar to a normal rule, but with much less infrastructure because it's running during the loading phase when most of the Bazel infrastructure doesn't apply yet.
The starlark implementation of http_archive
is in http.bzl. The core of it is a single call to ctx.download_and_extract
. Your custom rule should do that too. http_archive
then calls workspace_and_buildfile
and patch
from util.bzl
, which do what they sound like. Instead of workspace_and_buildfile
, you should call ctx.execute
to run your command to generate the BUILD file. You could call patch
if you want, or skip that functionality if you're not going to use it.
The repository_ctx page in the documentation is the top-level reference for everything your repository rule's implementation function can do, if you want to extend it further.

- 3,085
- 11
- 13
When using http_archive
, you can use the build_file
argument to create a BUILD file. To generate it dynamically, I think you can use the patch_cmds
argument to run external commands.

- 2,951
- 16
- 19