From efef7cae1617b8cd59d5bfcc4320b0321c8eb840 Mon Sep 17 00:00:00 2001 From: Jon Meow <46229924+jonmeow@users.noreply.github.com> Date: Fri, 5 Feb 2021 15:15:51 -0800 Subject: [PATCH] Detect if there's a built version of clang within the workspace (#260) --- WORKSPACE | 7 ++++-- bazel/cc_toolchains/clang_detection.bzl | 30 ++++++++++++++----------- docs/project/contribution_tools.md | 13 +++++++---- 3 files changed, 31 insertions(+), 19 deletions(-) diff --git a/WORKSPACE b/WORKSPACE index 02304ff5cc20..4aeee0d11102 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -17,8 +17,8 @@ http_archive( # Add Bazel's python rules. http_archive( name = "rules_python", - url = "https://github.com/bazelbuild/rules_python/releases/download/0.1.0/rules_python-0.1.0.tar.gz", sha256 = "b6d46438523a3ec0f3cead544190ee13223a52f6a6765a29eae7b7cc24cc83a0", + url = "https://github.com/bazelbuild/rules_python/releases/download/0.1.0/rules_python-0.1.0.tar.gz", ) # Set up necessary dependencies for working with the foreign C++ rules. @@ -29,7 +29,10 @@ rules_foreign_cc_dependencies() # Detect and configure a Clang and LLVM based toolchain. load("//bazel/cc_toolchains:clang_detection.bzl", "detect_clang_toolchain") -detect_clang_toolchain(name = "bazel_cc_toolchain") +detect_clang_toolchain( + name = "bazel_cc_toolchain", + workspace_dir = __workspace_dir__, +) local_repository( name = "llvm_bazel", diff --git a/bazel/cc_toolchains/clang_detection.bzl b/bazel/cc_toolchains/clang_detection.bzl index 1ef719a81a1a..02339e60961e 100644 --- a/bazel/cc_toolchains/clang_detection.bzl +++ b/bazel/cc_toolchains/clang_detection.bzl @@ -37,19 +37,22 @@ def _find_clang(repository_ctx): This assumes the `CC` environment variable points to a Clang binary or looks for one on the path. """ - cc_env = repository_ctx.os.environ.get("CC") - if not cc_env: - # Without a specified `CC` name, simply look for `clang`. - clang = repository_ctx.which("clang") - elif "/" not in cc_env: - # Lookup relative `CC` names according to the system `PATH`. - clang = repository_ctx.which(cc_env) - else: - # An absolute `CC` path is simply be used directly. - clang = repository_ctx.path(cc_env) - if not clang.exists: - fail(("The `CC` environment variable is set to a path (`%s`) " + - "that doesn't exist.") % cc_env) + clang = repository_ctx.path("%s/third_party/llvm-project/build/bin/clang" % + repository_ctx.attr.workspace_dir) + if not clang.exists: + cc_env = repository_ctx.os.environ.get("CC") + if not cc_env: + # Without a specified `CC` name, simply look for `clang`. + clang = repository_ctx.which("clang") + elif "/" not in cc_env: + # Lookup relative `CC` names according to the system `PATH`. + clang = repository_ctx.which(cc_env) + else: + # An absolute `CC` path is simply be used directly. + clang = repository_ctx.path(cc_env) + if not clang.exists: + fail(("The `CC` environment variable is set to a path (`%s`) " + + "that doesn't exist.") % cc_env) # Check if either of the `which` invocations fail. if not clang: @@ -152,6 +155,7 @@ detect_clang_toolchain = repository_rule( configure = True, local = True, attrs = { + "workspace_dir": attr.string(mandatory = True), "_clang_toolchain_build": attr.label( default = Label("//bazel/cc_toolchains:clang_toolchain.BUILD"), allow_single_file = True, diff --git a/docs/project/contribution_tools.md b/docs/project/contribution_tools.md index efec1d7fef4d..486ea4f1e7ff 100644 --- a/docs/project/contribution_tools.md +++ b/docs/project/contribution_tools.md @@ -117,11 +117,16 @@ Our recommended way of installing is: ### Clang and LLVM [Clang](https://clang.llvm.org/) and [LLVM](https://llvm.org/) are used to -compile and link Carbon. It's currently recommended to build LLVM from head in -order to avoid compatibility issues in many distributions. +compile and link Carbon. -Please refer to -[LLVM's build instructions](https://llvm.org/docs/GettingStarted.html#getting-the-source-code-and-building-llvm). +If the `CC` environment variable is set to `clang`, or if `clang` is in the +path, it can be used without building LLVM from head. However, it's currently +recommended to build LLVM from head in order to avoid compatibility issues in +many distributions. Once `third_party/llvm-project/build/bin/clang` exists, it +will automatically be used instead of `CC`. + +In order to build `third_party/llvm-project/build/bin/clang`, please refer to +[LLVM's instructions](https://llvm.org/docs/GettingStarted.html#getting-the-source-code-and-building-llvm). Tips: - When `carbon-lang` is checked out with submodules, the LLVM submodule is at