Damien Martin-Guillerez | 8fa5ae6 | 2016-03-02 16:24:13 +0000 | [diff] [blame] | 1 | # Copyright 2016 The Bazel Authors. All rights reserved. |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | """Rules for configuring the C++ toolchain (experimental).""" |
| 15 | |
Yun Peng | 65cda4f | 2017-06-22 11:06:11 +0200 | [diff] [blame] | 16 | load("@bazel_tools//tools/cpp:windows_cc_configure.bzl", "configure_windows_toolchain") |
| 17 | load("@bazel_tools//tools/cpp:osx_cc_configure.bzl", "configure_osx_toolchain") |
| 18 | load("@bazel_tools//tools/cpp:unix_cc_configure.bzl", "configure_unix_toolchain") |
jmmv | 5b02559 | 2018-05-29 12:03:21 -0700 | [diff] [blame] | 19 | load( |
| 20 | "@bazel_tools//tools/cpp:lib_cc_configure.bzl", |
| 21 | "get_cpu_value", |
| 22 | "resolve_labels", |
| 23 | ) |
Marcel Hlopko | 7984a15 | 2019-05-29 10:31:42 -0700 | [diff] [blame] | 24 | load("@bazel_tools//tools/osx:xcode_configure.bzl", "run_xcode_locator") |
| 25 | |
| 26 | def _generate_cpp_only_build_file(repository_ctx, cpu_value, paths): |
| 27 | repository_ctx.template( |
| 28 | "BUILD", |
| 29 | paths["@bazel_tools//tools/cpp:BUILD.toolchains.tpl"], |
| 30 | {"%{name}": cpu_value}, |
| 31 | ) |
hlopko | 0f0ccc4 | 2017-05-10 04:15:26 -0400 | [diff] [blame] | 32 | |
Marcel Hlopko | b4c0fb0 | 2019-05-27 03:54:06 -0700 | [diff] [blame] | 33 | def cc_autoconf_toolchains_impl(repository_ctx): |
| 34 | """Generate BUILD file with 'toolchain' targets for the local host C++ toolchain. |
| 35 | |
| 36 | Args: |
| 37 | repository_ctx: repository context |
| 38 | """ |
| 39 | paths = resolve_labels(repository_ctx, [ |
| 40 | "@bazel_tools//tools/cpp:BUILD.toolchains.tpl", |
| 41 | "@bazel_tools//tools/osx/crosstool:BUILD.toolchains", |
| 42 | "@bazel_tools//tools/osx/crosstool:osx_archs.bzl", |
Marcel Hlopko | 7984a15 | 2019-05-29 10:31:42 -0700 | [diff] [blame] | 43 | "@bazel_tools//tools/osx:xcode_locator.m", |
Marcel Hlopko | b4c0fb0 | 2019-05-27 03:54:06 -0700 | [diff] [blame] | 44 | ]) |
| 45 | env = repository_ctx.os.environ |
| 46 | cpu_value = get_cpu_value(repository_ctx) |
Marcel Hlopko | 7984a15 | 2019-05-29 10:31:42 -0700 | [diff] [blame] | 47 | |
| 48 | # Should we try to find C++ toolchain at all? If not, we don't have to generate toolchains for C++ at all. |
| 49 | should_detect_cpp_toolchain = "BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN" not in env or env["BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN"] != "1" |
| 50 | |
| 51 | # Should we unconditionally *not* use xcode? If so, we don't have to run Xcode locator ever. |
| 52 | should_use_cpp_only_toolchain = "BAZEL_USE_CPP_ONLY_TOOLCHAIN" in env and env["BAZEL_USE_CPP_ONLY_TOOLCHAIN"] == "1" |
| 53 | |
| 54 | # Should we unconditionally use xcode? If so, we don't have to run Xcode locator now. |
| 55 | should_use_xcode = "BAZEL_USE_XCODE_TOOLCHAIN" in env and env["BAZEL_USE_XCODE_TOOLCHAIN"] == "1" |
| 56 | |
| 57 | if not should_detect_cpp_toolchain: |
Marcel Hlopko | b4c0fb0 | 2019-05-27 03:54:06 -0700 | [diff] [blame] | 58 | repository_ctx.file("BUILD", "# C++ toolchain autoconfiguration was disabled by BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN env variable.") |
Marcel Hlopko | 7984a15 | 2019-05-29 10:31:42 -0700 | [diff] [blame] | 59 | elif cpu_value == "darwin" and not should_use_cpp_only_toolchain: |
| 60 | xcode_toolchains = [] |
| 61 | |
| 62 | # Only detect xcode if the user didn't tell us it will be there. |
| 63 | if not should_use_xcode: |
| 64 | # TODO(#6926): Unify C++ and ObjC toolchains so we don't have to run xcode locator to generate toolchain targets. |
| 65 | # And also so we don't have to keep this code in sync with //tools/cpp:osx_cc_configure.bzl. |
| 66 | (xcode_toolchains, _xcodeloc_err) = run_xcode_locator( |
| 67 | repository_ctx, |
| 68 | paths["@bazel_tools//tools/osx:xcode_locator.m"], |
| 69 | ) |
| 70 | |
| 71 | if should_use_xcode or xcode_toolchains: |
| 72 | repository_ctx.symlink(paths["@bazel_tools//tools/osx/crosstool:BUILD.toolchains"], "BUILD") |
| 73 | repository_ctx.symlink(paths["@bazel_tools//tools/osx/crosstool:osx_archs.bzl"], "osx_archs.bzl") |
| 74 | else: |
| 75 | _generate_cpp_only_build_file(repository_ctx, cpu_value, paths) |
Marcel Hlopko | b4c0fb0 | 2019-05-27 03:54:06 -0700 | [diff] [blame] | 76 | else: |
Marcel Hlopko | 7984a15 | 2019-05-29 10:31:42 -0700 | [diff] [blame] | 77 | _generate_cpp_only_build_file(repository_ctx, cpu_value, paths) |
Marcel Hlopko | b4c0fb0 | 2019-05-27 03:54:06 -0700 | [diff] [blame] | 78 | |
| 79 | cc_autoconf_toolchains = repository_rule( |
| 80 | environ = [ |
| 81 | "BAZEL_USE_CPP_ONLY_TOOLCHAIN", |
| 82 | "BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN", |
| 83 | ], |
| 84 | implementation = cc_autoconf_toolchains_impl, |
Klaus Aehlig | e1a6877 | 2019-08-29 02:54:41 -0700 | [diff] [blame] | 85 | configure = True, |
Marcel Hlopko | b4c0fb0 | 2019-05-27 03:54:06 -0700 | [diff] [blame] | 86 | ) |
| 87 | |
Ilya Biryukov | 12471a7 | 2017-12-20 05:46:44 -0800 | [diff] [blame] | 88 | def cc_autoconf_impl(repository_ctx, overriden_tools = dict()): |
Marcel Hlopko | b4c0fb0 | 2019-05-27 03:54:06 -0700 | [diff] [blame] | 89 | """Generate BUILD file with 'cc_toolchain' targets for the local host C++ toolchain. |
| 90 | |
| 91 | Args: |
| 92 | repository_ctx: repository context |
| 93 | overriden_tools: dict of tool paths to use instead of autoconfigured tools |
| 94 | """ |
jmmv | 5b02559 | 2018-05-29 12:03:21 -0700 | [diff] [blame] | 95 | |
vladmos | 20a042f | 2018-06-01 04:51:21 -0700 | [diff] [blame] | 96 | env = repository_ctx.os.environ |
| 97 | cpu_value = get_cpu_value(repository_ctx) |
| 98 | if "BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN" in env and env["BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN"] == "1": |
rosica | 3fd5129 | 2019-08-12 10:24:34 -0700 | [diff] [blame] | 99 | paths = resolve_labels(repository_ctx, [ |
| 100 | "@bazel_tools//tools/cpp:BUILD.empty", |
| 101 | "@bazel_tools//tools/cpp:empty_cc_toolchain_config.bzl", |
| 102 | ]) |
| 103 | repository_ctx.symlink(paths["@bazel_tools//tools/cpp:empty_cc_toolchain_config.bzl"], "cc_toolchain_config.bzl") |
| 104 | repository_ctx.symlink(paths("@bazel_tools//tools/cpp:BUILD.empty"), "BUILD") |
vladmos | 20a042f | 2018-06-01 04:51:21 -0700 | [diff] [blame] | 105 | elif cpu_value == "freebsd": |
rosica | 5ee6d35 | 2019-08-09 07:24:36 -0700 | [diff] [blame] | 106 | paths = resolve_labels(repository_ctx, [ |
| 107 | "@bazel_tools//tools/cpp:BUILD.static.freebsd", |
| 108 | "@bazel_tools//tools/cpp:freebsd_cc_toolchain_config.bzl", |
| 109 | ]) |
| 110 | |
| 111 | # This is defaulting to a static crosstool, we should eventually |
vladmos | 20a042f | 2018-06-01 04:51:21 -0700 | [diff] [blame] | 112 | # autoconfigure this platform too. Theorically, FreeBSD should be |
| 113 | # straightforward to add but we cannot run it in a docker container so |
| 114 | # skipping until we have proper tests for FreeBSD. |
rosica | 5ee6d35 | 2019-08-09 07:24:36 -0700 | [diff] [blame] | 115 | repository_ctx.symlink(paths["@bazel_tools//tools/cpp:freebsd_cc_toolchain_config.bzl"], "cc_toolchain_config.bzl") |
vladmos | 20a042f | 2018-06-01 04:51:21 -0700 | [diff] [blame] | 116 | repository_ctx.symlink(paths["@bazel_tools//tools/cpp:BUILD.static.freebsd"], "BUILD") |
| 117 | elif cpu_value == "x64_windows": |
| 118 | # TODO(ibiryukov): overriden_tools are only supported in configure_unix_toolchain. |
| 119 | # We might want to add that to Windows too(at least for msys toolchain). |
| 120 | configure_windows_toolchain(repository_ctx) |
| 121 | elif (cpu_value == "darwin" and |
| 122 | ("BAZEL_USE_CPP_ONLY_TOOLCHAIN" not in env or env["BAZEL_USE_CPP_ONLY_TOOLCHAIN"] != "1")): |
| 123 | configure_osx_toolchain(repository_ctx, overriden_tools) |
| 124 | else: |
| 125 | configure_unix_toolchain(repository_ctx, cpu_value, overriden_tools) |
Damien Martin-Guillerez | 8fa5ae6 | 2016-03-02 16:24:13 +0000 | [diff] [blame] | 126 | |
Laszlo Csomor | 07853cd | 2019-07-16 06:14:30 -0700 | [diff] [blame] | 127 | MSVC_ENVVARS = [ |
| 128 | "BAZEL_VC", |
| 129 | "BAZEL_VC_FULL_VERSION", |
| 130 | "BAZEL_VS", |
| 131 | "BAZEL_WINSDK_FULL_VERSION", |
| 132 | "VS90COMNTOOLS", |
| 133 | "VS100COMNTOOLS", |
| 134 | "VS110COMNTOOLS", |
| 135 | "VS120COMNTOOLS", |
| 136 | "VS140COMNTOOLS", |
| 137 | "VS150COMNTOOLS", |
| 138 | "VS160COMNTOOLS", |
| 139 | "TMP", |
| 140 | "TEMP", |
| 141 | ] |
| 142 | |
Damien Martin-Guillerez | ac29b78 | 2017-02-15 14:50:39 +0000 | [diff] [blame] | 143 | cc_autoconf = repository_rule( |
Damien Martin-Guillerez | ac29b78 | 2017-02-15 14:50:39 +0000 | [diff] [blame] | 144 | environ = [ |
Nicolas Lopez | bcbdb87 | 2017-03-28 08:08:50 +0000 | [diff] [blame] | 145 | "ABI_LIBC_VERSION", |
| 146 | "ABI_VERSION", |
| 147 | "BAZEL_COMPILER", |
| 148 | "BAZEL_HOST_SYSTEM", |
Piotr Sikora | a89c7ef | 2019-01-21 09:28:39 -0800 | [diff] [blame] | 149 | "BAZEL_CXXOPTS", |
hlopko | 19c6428 | 2018-02-27 07:28:40 -0800 | [diff] [blame] | 150 | "BAZEL_LINKOPTS", |
Marcel Hlopko | ab9c1f5 | 2019-06-19 00:45:58 -0700 | [diff] [blame] | 151 | "BAZEL_LINKLIBS", |
Nicolas Lopez | bcbdb87 | 2017-03-28 08:08:50 +0000 | [diff] [blame] | 152 | "BAZEL_PYTHON", |
| 153 | "BAZEL_SH", |
| 154 | "BAZEL_TARGET_CPU", |
| 155 | "BAZEL_TARGET_LIBC", |
| 156 | "BAZEL_TARGET_SYSTEM", |
hlopko | 209a975 | 2017-12-12 04:34:41 -0800 | [diff] [blame] | 157 | "BAZEL_USE_CPP_ONLY_TOOLCHAIN", |
Marcel Hlopko | 7984a15 | 2019-05-29 10:31:42 -0700 | [diff] [blame] | 158 | "BAZEL_USE_XCODE_TOOLCHAIN", |
hlopko | 57bc201 | 2018-05-16 02:15:04 -0700 | [diff] [blame] | 159 | "BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN", |
Ulf Adams | 1441a3a | 2018-04-26 00:39:43 -0700 | [diff] [blame] | 160 | "BAZEL_USE_LLVM_NATIVE_COVERAGE", |
Yun Peng | f445af5 | 2018-11-02 07:55:01 -0700 | [diff] [blame] | 161 | "BAZEL_LLVM", |
Marcel Hlopko | 8b0bfaf | 2019-09-02 08:13:18 -0700 | [diff] [blame] | 162 | "BAZEL_IGNORE_SYSTEM_HEADERS_VERSIONS", |
Yun Peng | f445af5 | 2018-11-02 07:55:01 -0700 | [diff] [blame] | 163 | "USE_CLANG_CL", |
Nicolas Lopez | bcbdb87 | 2017-03-28 08:08:50 +0000 | [diff] [blame] | 164 | "CC", |
Yun Peng | a6f0f13 | 2017-06-28 14:55:37 +0200 | [diff] [blame] | 165 | "CC_CONFIGURE_DEBUG", |
Nicolas Lopez | bcbdb87 | 2017-03-28 08:08:50 +0000 | [diff] [blame] | 166 | "CC_TOOLCHAIN_NAME", |
Nicolas Lopez | 6326f98 | 2017-04-04 16:22:30 +0000 | [diff] [blame] | 167 | "CPLUS_INCLUDE_PATH", |
Ulf Adams | 9566f67 | 2018-04-19 02:55:15 -0700 | [diff] [blame] | 168 | "GCOV", |
Nicolas Lopez | 6326f98 | 2017-04-04 16:22:30 +0000 | [diff] [blame] | 169 | "HOMEBREW_RUBY_PATH", |
Ulf Adams | 1441a3a | 2018-04-26 00:39:43 -0700 | [diff] [blame] | 170 | "SYSTEMROOT", |
Laszlo Csomor | 07853cd | 2019-07-16 06:14:30 -0700 | [diff] [blame] | 171 | ] + MSVC_ENVVARS, |
jcater | f5c8c0b | 2018-03-27 07:22:35 -0700 | [diff] [blame] | 172 | implementation = cc_autoconf_impl, |
Klaus Aehlig | e1a6877 | 2019-08-29 02:54:41 -0700 | [diff] [blame] | 173 | configure = True, |
jcater | f5c8c0b | 2018-03-27 07:22:35 -0700 | [diff] [blame] | 174 | ) |
Damien Martin-Guillerez | 8fa5ae6 | 2016-03-02 16:24:13 +0000 | [diff] [blame] | 175 | |
| 176 | def cc_configure(): |
vladmos | 20a042f | 2018-06-01 04:51:21 -0700 | [diff] [blame] | 177 | """A C++ configuration rules that generate the crosstool file.""" |
Marcel Hlopko | b4c0fb0 | 2019-05-27 03:54:06 -0700 | [diff] [blame] | 178 | cc_autoconf_toolchains(name = "local_config_cc_toolchains") |
vladmos | 20a042f | 2018-06-01 04:51:21 -0700 | [diff] [blame] | 179 | cc_autoconf(name = "local_config_cc") |
| 180 | native.bind(name = "cc_toolchain", actual = "@local_config_cc//:toolchain") |
| 181 | native.register_toolchains( |
| 182 | # Use register_toolchain's target pattern expansion to register all toolchains in the package. |
Marcel Hlopko | b4c0fb0 | 2019-05-27 03:54:06 -0700 | [diff] [blame] | 183 | "@local_config_cc_toolchains//:all", |
vladmos | 20a042f | 2018-06-01 04:51:21 -0700 | [diff] [blame] | 184 | ) |