0

I have a following rule in WORKSPACE:

new_local_repository(
    name = "llvm",
    path = "/opt/local/libexec/llvm-4.0",
    build_file= "llvm.BUILD")

I would like to now use hardcoded path for llvm. llvm-config --prefix can give me the directory for llvm. What is the correct way to get this? Can I just use standard python commands (e.g. subprocess package)?

gruszczy
  • 40,948
  • 31
  • 128
  • 181

2 Answers2

4

As @abergmeier said, you can create a custom repository rule, run the command, create a symlink pointing to its output, and create a BUILD file for the repository:

WORKSPACE file:

workspace(name = "io_bazel")

load("//foo:repo.bzl", "llvm_configure")

llvm_configure(name = "local_config_llvm")

foo/repo.bzl:

def _impl(repository_ctx):
  result = repository_ctx.execute(["echo", "/tmp/dummy/path"])
  llvm_path = result.stdout.splitlines()[0]
  repository_ctx.symlink(llvm_path, "llvm-4.0")
  repository_ctx.file("BUILD", """
filegroup(
    name = "llvm_files",
    srcs = glob(["llvm-4.0/**"]),
    visibility = ["//visibility:public"],
)
""")

llvm_configure = repository_rule(
    implementation=_impl,
    local = True,
    environ = [])

Targets in the repo:

$ bazel query @local_config_llvm//:*
@local_config_llvm//:llvm_files
@local_config_llvm//:llvm-4.0/a.txt
@local_config_llvm//:BUILD

Generated files:

$ ls -la $(bazel info output_base)/external/local_config_llvm
total 16
drwxr-x--- 2 laszlocsomor eng 4096 May 12 13:06 .
drwxr-x--- 6 laszlocsomor eng 4096 May 12 13:06 ..
-rwxr-x--x 1 laszlocsomor eng  115 May 12 13:06 BUILD
lrwxrwxrwx 1 laszlocsomor eng   15 May 12 13:06 llvm-4.0 -> /tmp/dummy/path
-rw-r----- 1 laszlocsomor eng  116 May 12 13:06 WORKSPACE


$ cat $(bazel info output_base)/external/local_config_llvm/BUILD

filegroup(
    name = "llvm_files",
    srcs = glob(["llvm-4.0/**"]),
    visibility = ["//visibility:public"],
)


$ cat $(bazel info output_base)/external/local_config_llvm/WORKSPACE
# DO NOT EDIT: automatically generated WORKSPACE file for llvm_configure rule
workspace(name = "local_config_llvm")
László
  • 3,973
  • 1
  • 13
  • 26
  • Thanks a lot, I will try that out. – gruszczy May 12 '17 at 20:16
  • Thanks for this idea! I tried something very much like this but my `glob` expression keeps returning an empty list - it appears that somehow the glob gets executed before the `symlink` completes. I have verified that if I run `bazel cquery --output=build` on the target from my main repo the generated `BUILD` returned the empty list. However, if I `cd` into the generated repository and run the same query the glob returns the expected files. Any thoughts? – Oliver Dain Dec 10 '19 at 21:07
  • I think there's a Bazel bug that keeps this from working. I just filed the bug here: https://github.com/bazelbuild/bazel/issues/10400 – Oliver Dain Dec 11 '19 at 01:55
2

You can create a custom repository_rule, in which you can call repository_ctx.execute("llvm-config --prefix") in and make that value available in your package.

I'd suggest you first read about repository_rule and if anything is not clear, do not hesitate to ask.

abergmeier
  • 13,224
  • 13
  • 64
  • 120