diff --git a/utils/bazel/MODULE.bazel b/utils/bazel/MODULE.bazel new file mode 100644 --- /dev/null +++ b/utils/bazel/MODULE.bazel @@ -0,0 +1,44 @@ +module( + name = "llvm-project-overlay", + version = "16.0.0-bcr.0", + compatibility_level = 0, +) + +bazel_dep(name = "bazel_skylib", version = "1.3.0") +bazel_dep(name = "platforms", version = "0.0.4") +bazel_dep(name = "rules_cc", version = "0.0.2") +bazel_dep(name = "zlib", version = "1.2.12") + +llvm_project_overlay = use_extension( + "//:extensions.bzl", + "llvm_project_overlay", +) + +llvm_project_overlay.configure( + targets = [ + "AArch64", + "AMDGPU", + "ARM", + "AVR", + "BPF", + "Hexagon", + "Lanai", + "Mips", + "MSP430", + "NVPTX", + "PowerPC", + "RISCV", + "Sparc", + "SystemZ", + "VE", + "WebAssembly", + "X86", + "XCore", + ], +) + +use_repo( + llvm_project_overlay, + "llvm-project", + "llvm-raw", +) diff --git a/utils/bazel/bzlmod_compatibility_patch.patch b/utils/bazel/bzlmod_compatibility_patch.patch new file mode 100644 --- /dev/null +++ b/utils/bazel/bzlmod_compatibility_patch.patch @@ -0,0 +1,43 @@ +diff --git a/BUILD.bazel b/BUILD.bazel +new file mode 100755 +index 000000000000..996cee9ad835 +--- /dev/null ++++ b/BUILD.bazel +@@ -0,0 +1 @@ ++# Empty. +diff --git a/MODULE.bazel b/MODULE.bazel +new file mode 100644 +index 000000000000..d2c45773034f +--- /dev/null ++++ b/MODULE.bazel +@@ -0,0 +1,23 @@ ++module( ++ name = "llvm-project-overlay", ++ version = "16.0.0-bcr.0", ++ compatibility_level = 0, ++) ++ ++bazel_dep(name = "bazel_skylib", version = "1.3.0") ++bazel_dep(name = "platforms", version = "0.0.4") ++bazel_dep(name = "rules_cc", version = "0.0.2") ++bazel_dep(name = "zlib", version = "1.2.12") ++ ++llvm_project_overlay = use_extension( ++ "//utils/bazel:extensions.bzl", ++ "llvm_project_overlay", ++) ++ ++llvm_project_overlay.configure() ++ ++use_repo( ++ llvm_project_overlay, ++ "llvm-project", ++ "llvm-raw", ++) +diff --git a/WORKSPACE b/WORKSPACE +new file mode 100755 +index 000000000000..996cee9ad835 +--- /dev/null ++++ b/WORKSPACE +@@ -0,0 +1 @@ ++# Empty. diff --git a/utils/bazel/examples/bzlmod/.bazelrc b/utils/bazel/examples/bzlmod/.bazelrc new file mode 100644 --- /dev/null +++ b/utils/bazel/examples/bzlmod/.bazelrc @@ -0,0 +1,5 @@ +common --repo_env=BAZEL_CXXOPTS='-std=c++17:-O3' +common --experimental_enable_bzlmod + +# TODO(aaronmondal): Only for testing during review. Remove before merging this patch. +common --registry=https://raw.githubusercontent.com/eomii/bazel-eomii-registry/llvm-project-overlay diff --git a/utils/bazel/examples/bzlmod/.bazelversion b/utils/bazel/examples/bzlmod/.bazelversion new file mode 100644 --- /dev/null +++ b/utils/bazel/examples/bzlmod/.bazelversion @@ -0,0 +1 @@ +5.3.2 diff --git a/utils/bazel/examples/bzlmod/MODULE.bazel b/utils/bazel/examples/bzlmod/MODULE.bazel new file mode 100644 --- /dev/null +++ b/utils/bazel/examples/bzlmod/MODULE.bazel @@ -0,0 +1,8 @@ +bazel_dep(name = "llvm-project-overlay", version = "16.0.0-bcr.0") + +llvm_project_overlay = use_extension( + "@llvm-project-overlay//utils/bazel:extensions.bzl", + "llvm_project_overlay", +) + +use_repo(llvm_project_overlay, "llvm-project") diff --git a/utils/bazel/examples/bzlmod/WORKSPACE.bazel b/utils/bazel/examples/bzlmod/WORKSPACE.bazel new file mode 100644 --- /dev/null +++ b/utils/bazel/examples/bzlmod/WORKSPACE.bazel @@ -0,0 +1 @@ +# Empty. diff --git a/utils/bazel/extensions.bzl b/utils/bazel/extensions.bzl new file mode 100644 --- /dev/null +++ b/utils/bazel/extensions.bzl @@ -0,0 +1,134 @@ +load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") +load(":vulkan_sdk.bzl", "vulkan_sdk_setup") +load(":configure.bzl", "llvm_configure", "DEFAULT_TARGETS") +load(":terminfo.bzl", "llvm_terminfo_disable") +load(":zlib.bzl", "llvm_zlib_external") + +def _llvm_in_tree_sources_impl(repository_ctx): + """Create a non-overlaid version of the llvm-project repository. + + Adds MODULE.bazel, WORKSPACE.bazel and BUILD.bazel to the top level + directory, similar to `native.local_repository` which cannot be called from + within a module extension implementation. + """ + repo_name = repository_ctx.path( + Label("@llvm-project-overlay//:MODULE.bazel"), + ).dirname.basename + + # If we are building from within the /utils/bazel subdirectory in the LLVM + # repo, symlink to "llvm-raw". Otherwise, keep using the dynamically + # determined llvm-project-overlay module version and workspace to get the + # correct path to the sources. + if repo_name == "bazel": + repo_name = "llvm-raw" + + repository_ctx.symlink("../../external/{}".format(repo_name), "") + + # repository_ctx.file("WORKSPACE", content = "# Empty.",) + + # repository_ctx.file("BUILD.bazel", content = "# Empty.") + + +llvm_in_tree_sources = repository_rule( + implementation = _llvm_in_tree_sources_impl, + attrs = { + "patches": attr.label_list(), + }, +) + +def _llvm_configure_extension_impl(ctx): + targets = [] + patches = [] + commit = "" + sha256 = "" + + for module in ctx.modules: + + # Aggregate targets and patches across imports. + for config in module.tags.configure: + for target in config.targets: + if target not in targets: + targets.append(target) + for patch in config.patches: + if patch not in patches: + patches.append(patch) + + # Use the commit/sha configuration from the top-level import. + if module.tags.configure != []: + commit = module.tags.configure[0].commit + sha256 = module.tags.configure[0].sha256 + + if commit == "": + if patches != []: + fail("""Cannot apply patches when `commit` is unspecified. Patches + would modify the original LLVM sources, making the build + irreproducible. + """, + ) + llvm_in_tree_sources(name = "llvm-raw") + else: + http_archive( + name = "llvm-raw", + build_file_content = "# Empty.", + sha256 = sha256, + strip_prefix = "llvm-project-" + commit, + urls = [ + "https://github.com/llvm/llvm-project/archive/{}.tar.gz".format( + commit, + ), + ], + patches = patches + [ + ":bzlmod_compatibility_patch.patch", + ], + patch_args = ["-p1"], + ) + + # Fall back to the default targets if all configurations of this extension + # omit the `target` attribute. + if targets == []: + targets = DEFAULT_TARGETS + + llvm_configure(name = "llvm-project", targets = targets) + + llvm_zlib_external(name = "llvm_zlib", external_zlib = "@zlib") + + llvm_terminfo_disable(name = "llvm_terminfo") + + http_archive( + name = "vulkan_headers", + build_file = "@llvm-raw//utils/bazel/third_party_build:vulkan_headers.BUILD", + sha256 = "19f491784ef0bc73caff877d11c96a48b946b5a1c805079d9006e3fbaa5c1895", + strip_prefix = "Vulkan-Headers-9bd3f561bcee3f01d22912de10bb07ce4e23d378", + urls = [ + "https://github.com/KhronosGroup/Vulkan-Headers/archive/9bd3f561bcee3f01d22912de10bb07ce4e23d378.tar.gz", + ], + ) + + vulkan_sdk_setup(name = "vulkan_sdk") + +llvm_project_overlay = module_extension( + doc = """Configure the llvm-project. + + Tags: + targets: List of targets which Clang should support. + commit: An optional LLVM commit. If specified, the LLVM project will be + redownloaded from that commit (i.e.) it is downloaded once for the + bzlmod dependency and once for the specified commit. + Defaults to None, in which case the sources specified in the BCR + module are used. + sha256: Hash for verifying the custom archive if `commit` is not `None`. + patches: Optional list of patches to apply to the LLVM sources. May not + be used if `commit` is unspecified. + """, + implementation = _llvm_configure_extension_impl, + tag_classes = { + "configure": tag_class( + attrs = { + "commit": attr.string(), + "sha256": attr.string(), + "targets": attr.string_list(), + "patches": attr.label_list(), + }, + ), + }, +)