|
| 1 | +# Relevant documentation: |
| 2 | +# - https://bazel.build/reference/be/platforms-and-toolchains |
| 3 | +# - https://bazel.build/rules/lib/builtins/path.html |
| 4 | +# - https://bazel.build/rules/lib/builtins/repository_ctx |
| 5 | +# - https://bazel.build/rules/lib/globals/bzl.html#repository_rule |
| 6 | +def _file_detector_impl(repository_ctx): |
| 7 | + root_build_file = 'package(default_visibility = ["//visibility:public"])\n\n' |
| 8 | + for (filename, constraint_setting) in repository_ctx.attr.files.items(): |
| 9 | + file_path = repository_ctx.path(filename) |
| 10 | + repository_ctx.watch(file_path) |
| 11 | + if file_path.exists: |
| 12 | + exists_str = "present" |
| 13 | + else: |
| 14 | + exists_str = "absent" |
| 15 | + build_fragment = """ |
| 16 | +constraint_setting( |
| 17 | + name = "{0}", |
| 18 | + default_constraint_value = ":{0}_{1}" |
| 19 | +) |
| 20 | +constraint_value( |
| 21 | + name = "{0}_present", |
| 22 | + constraint_setting = ":{0}", |
| 23 | +) |
| 24 | +constraint_value( |
| 25 | + name = "{0}_absent", |
| 26 | + constraint_setting = ":{0}", |
| 27 | +) |
| 28 | + """.strip().format(constraint_setting, exists_str) |
| 29 | + root_build_file += "\n" + build_fragment + "\n" |
| 30 | + build_file = repository_ctx.path("BUILD.bazel") |
| 31 | + repository_ctx.file(build_file, root_build_file, False) |
| 32 | + |
| 33 | +# Creates a repository that defines `constraint_setting`s and |
| 34 | +# `constraint_value`s based on the existence of some files on the host. |
| 35 | +# |
| 36 | +# Example usage: |
| 37 | +# ``` |
| 38 | +# file_detector = use_repo_rule("//toolchain:file_detector.bzl", "file_detector") |
| 39 | +# |
| 40 | +# file_detector( |
| 41 | +# name = "clang_detector", |
| 42 | +# files = { |
| 43 | +# "/usr/bin/clang-11": "clang_11", |
| 44 | +# }, |
| 45 | +# ), |
| 46 | +# ``` |
| 47 | +# This expands to a repository `@clang_detector` defining the following |
| 48 | +# targets: |
| 49 | +# ``` |
| 50 | +# constraint_setting( |
| 51 | +# name = "clang_11", |
| 52 | +# default_constraint_value = ":clang_11_present", |
| 53 | +# ) |
| 54 | +# constraint_value( |
| 55 | +# name = "clang_11_present", |
| 56 | +# constraint_setting = ":clang_11", |
| 57 | +# ) |
| 58 | +# constraint_value( |
| 59 | +# name = "clang_11_absent", |
| 60 | +# constraint_setting = ":clang_11", |
| 61 | +# ) |
| 62 | +# ``` |
| 63 | +# The default constraint value of the "@clang_detector//:clang_11" setting is |
| 64 | +# set to either "@clang_detector//:clang_11_present" or |
| 65 | +# "@clang_detector//:clang_11_absent" depending on whether the file |
| 66 | +# /usr/bin/clang-11 is present on the host. |
| 67 | +# |
| 68 | +# This is helpful for toolchain resolution. A toolchain relying on host |
| 69 | +# executables can reference these constraints in its `exec_compatible_with` |
| 70 | +# list, which is used to report availability. |
| 71 | +# |
| 72 | +# Relevant documentation: |
| 73 | +# - https://bazel.build/extending/toolchains |
| 74 | +# - https://bazel.build/reference/be/platforms-and-toolchains |
| 75 | +# - https://bazel.build/rules/lib/globals/bzl.html#repository_rule |
| 76 | +# - https://bazel.build/rules/lib/toplevel/attr#string_dict |
| 77 | +file_detector = repository_rule( |
| 78 | + implementation = _file_detector_impl, |
| 79 | + attrs = { |
| 80 | + "files": attr.string_dict( |
| 81 | + allow_empty = False, |
| 82 | + configurable = False, |
| 83 | + mandatory = True, |
| 84 | + ), |
| 85 | + }, |
| 86 | +) |
0 commit comments