فهرست منبع

Detect if there's a built version of clang within the workspace (#260)

Jon Meow 5 سال پیش
والد
کامیت
efef7cae16
3فایلهای تغییر یافته به همراه31 افزوده شده و 19 حذف شده
  1. 5 2
      WORKSPACE
  2. 17 13
      bazel/cc_toolchains/clang_detection.bzl
  3. 9 4
      docs/project/contribution_tools.md

+ 5 - 2
WORKSPACE

@@ -17,8 +17,8 @@ http_archive(
 # Add Bazel's python rules.
 # Add Bazel's python rules.
 http_archive(
 http_archive(
     name = "rules_python",
     name = "rules_python",
-    url = "https://github.com/bazelbuild/rules_python/releases/download/0.1.0/rules_python-0.1.0.tar.gz",
     sha256 = "b6d46438523a3ec0f3cead544190ee13223a52f6a6765a29eae7b7cc24cc83a0",
     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.
 # 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.
 # Detect and configure a Clang and LLVM based toolchain.
 load("//bazel/cc_toolchains:clang_detection.bzl", "detect_clang_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(
 local_repository(
     name = "llvm_bazel",
     name = "llvm_bazel",

+ 17 - 13
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
     This assumes the `CC` environment variable points to a Clang binary or
     looks for one on the path.
     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.
     # Check if either of the `which` invocations fail.
     if not clang:
     if not clang:
@@ -152,6 +155,7 @@ detect_clang_toolchain = repository_rule(
     configure = True,
     configure = True,
     local = True,
     local = True,
     attrs = {
     attrs = {
+        "workspace_dir": attr.string(mandatory = True),
         "_clang_toolchain_build": attr.label(
         "_clang_toolchain_build": attr.label(
             default = Label("//bazel/cc_toolchains:clang_toolchain.BUILD"),
             default = Label("//bazel/cc_toolchains:clang_toolchain.BUILD"),
             allow_single_file = True,
             allow_single_file = True,

+ 9 - 4
docs/project/contribution_tools.md

@@ -117,11 +117,16 @@ Our recommended way of installing is:
 ### Clang and LLVM
 ### Clang and LLVM
 
 
 [Clang](https://clang.llvm.org/) and [LLVM](https://llvm.org/) are used to
 [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:
 Tips:
 
 
 -   When `carbon-lang` is checked out with submodules, the LLVM submodule is at
 -   When `carbon-lang` is checked out with submodules, the LLVM submodule is at