Thanks, but I don't see how to make that work. I did not state the problem very clearly. Here's some more detail.
I have a cc_binary rule that builds the tool (opam_bootstrap) I need. It lives in repo @tools_obazl, which is installed using `http_archive`. To build it: `bazel build @tools_obazl//bootstrap:opam_bootstrap`.
The job of the `opam_bootstrap` tool is to configure repository @opam. It writes some BUILD.bazel files into the repo (i.e. `external/opam`). To run it, I have a custom repository rule, `opam_configuration`. It uses `repo_ctx.execute` to run the tool.
(In principle the logic of `opam_bootstrap` could be implemented in starlark, but it parses config files and I don't know of a starlark lex/yacc library. Actually I haven't even looked, I just used re2c and lemon since they are fast and portable.)
So after `opam_configuration` is run, we have a collection of targets like @opam//lib/zarith, @opam//lib/foo, etc. These are used by the OCaml rules, e.g. `ocaml_module(name = "foo", deps = ["@opam//lib/zarith"], ...)`
So when an OCaml build target that depends on an @opam target is built, the `opam_configuration` rule is run (and cached). For it to succeed, the `opam_bootstrap` executable must be in the path, so that `repo_ctx.execute` can find it.
So the following procedure works just fine:
1. $ bazel build @tools_obazl//bootstrap:opam_bootstrap
2. sudo cp bazel-bin/external/tools_obazl/bootstrap/opam_bootstrap
3. $ bazel build src/some/ocaml:target ## target that depends on @opam//lib/foo
After step 2, the `opam_bootstrap` executable is in the path, so `repo_ctx.executable` will find it and run it.
I want to automate steps 1 and 2, so that the user need only use step 3. I can get part way there by using a custom env variable, OBAZL_BOOTSTRAP, setting it to `bazel-bin/external/tools_obazl/bootstrap/opam_bootstrap`, and passing it to `opam_confugration`, which adds it to the path in the env passed to `repo_ctx.execute`.
What I have not been able to figure out is how to get the opam_configuration rule to trigger a build of the opam_bootstrap tool, and then run it.
I wonder if there is some way to make this work by putting opam_bootstrap in a toolchain.
Note that I have two external repos installed using http_archive: tools_obazl, and rules_ocaml. The latter contains the `opam_configuration` rule that creates @opam (by running tool `opam_bootstrap` contained in the former).