blob: 072c17525fd17f44ea0500db0d2d0d126ae30602 [file] [log] [blame]
Damien Martin-Guillerez8fa5ae62016-03-02 16:24:13 +00001# 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 Peng65cda4f2017-06-22 11:06:11 +020016load("@bazel_tools//tools/cpp:windows_cc_configure.bzl", "configure_windows_toolchain")
17load("@bazel_tools//tools/cpp:osx_cc_configure.bzl", "configure_osx_toolchain")
18load("@bazel_tools//tools/cpp:unix_cc_configure.bzl", "configure_unix_toolchain")
jmmv5b025592018-05-29 12:03:21 -070019load(
20 "@bazel_tools//tools/cpp:lib_cc_configure.bzl",
21 "get_cpu_value",
22 "resolve_labels",
23)
Marcel Hlopko7984a152019-05-29 10:31:42 -070024load("@bazel_tools//tools/osx:xcode_configure.bzl", "run_xcode_locator")
25
26def _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 )
hlopko0f0ccc42017-05-10 04:15:26 -040032
Marcel Hlopkob4c0fb02019-05-27 03:54:06 -070033def 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 Hlopko7984a152019-05-29 10:31:42 -070043 "@bazel_tools//tools/osx:xcode_locator.m",
Marcel Hlopkob4c0fb02019-05-27 03:54:06 -070044 ])
45 env = repository_ctx.os.environ
46 cpu_value = get_cpu_value(repository_ctx)
Marcel Hlopko7984a152019-05-29 10:31:42 -070047
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 Hlopkob4c0fb02019-05-27 03:54:06 -070058 repository_ctx.file("BUILD", "# C++ toolchain autoconfiguration was disabled by BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN env variable.")
Marcel Hlopko7984a152019-05-29 10:31:42 -070059 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 Hlopkob4c0fb02019-05-27 03:54:06 -070076 else:
Marcel Hlopko7984a152019-05-29 10:31:42 -070077 _generate_cpp_only_build_file(repository_ctx, cpu_value, paths)
Marcel Hlopkob4c0fb02019-05-27 03:54:06 -070078
79cc_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 Aehlige1a68772019-08-29 02:54:41 -070085 configure = True,
Marcel Hlopkob4c0fb02019-05-27 03:54:06 -070086)
87
Ilya Biryukov12471a72017-12-20 05:46:44 -080088def cc_autoconf_impl(repository_ctx, overriden_tools = dict()):
Marcel Hlopkob4c0fb02019-05-27 03:54:06 -070089 """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 """
jmmv5b025592018-05-29 12:03:21 -070095
vladmos20a042f2018-06-01 04:51:21 -070096 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":
rosica3fd51292019-08-12 10:24:34 -070099 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")
vladmos20a042f2018-06-01 04:51:21 -0700105 elif cpu_value == "freebsd":
rosica5ee6d352019-08-09 07:24:36 -0700106 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
vladmos20a042f2018-06-01 04:51:21 -0700112 # 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.
rosica5ee6d352019-08-09 07:24:36 -0700115 repository_ctx.symlink(paths["@bazel_tools//tools/cpp:freebsd_cc_toolchain_config.bzl"], "cc_toolchain_config.bzl")
vladmos20a042f2018-06-01 04:51:21 -0700116 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-Guillerez8fa5ae62016-03-02 16:24:13 +0000126
Laszlo Csomor07853cd2019-07-16 06:14:30 -0700127MSVC_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-Guillerezac29b782017-02-15 14:50:39 +0000143cc_autoconf = repository_rule(
Damien Martin-Guillerezac29b782017-02-15 14:50:39 +0000144 environ = [
Nicolas Lopezbcbdb872017-03-28 08:08:50 +0000145 "ABI_LIBC_VERSION",
146 "ABI_VERSION",
147 "BAZEL_COMPILER",
148 "BAZEL_HOST_SYSTEM",
Piotr Sikoraa89c7ef2019-01-21 09:28:39 -0800149 "BAZEL_CXXOPTS",
hlopko19c64282018-02-27 07:28:40 -0800150 "BAZEL_LINKOPTS",
Marcel Hlopkoab9c1f52019-06-19 00:45:58 -0700151 "BAZEL_LINKLIBS",
Nicolas Lopezbcbdb872017-03-28 08:08:50 +0000152 "BAZEL_PYTHON",
153 "BAZEL_SH",
154 "BAZEL_TARGET_CPU",
155 "BAZEL_TARGET_LIBC",
156 "BAZEL_TARGET_SYSTEM",
hlopko209a9752017-12-12 04:34:41 -0800157 "BAZEL_USE_CPP_ONLY_TOOLCHAIN",
Marcel Hlopko7984a152019-05-29 10:31:42 -0700158 "BAZEL_USE_XCODE_TOOLCHAIN",
hlopko57bc2012018-05-16 02:15:04 -0700159 "BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN",
Ulf Adams1441a3a2018-04-26 00:39:43 -0700160 "BAZEL_USE_LLVM_NATIVE_COVERAGE",
Yun Pengf445af52018-11-02 07:55:01 -0700161 "BAZEL_LLVM",
Marcel Hlopko8b0bfaf2019-09-02 08:13:18 -0700162 "BAZEL_IGNORE_SYSTEM_HEADERS_VERSIONS",
Yun Pengf445af52018-11-02 07:55:01 -0700163 "USE_CLANG_CL",
Nicolas Lopezbcbdb872017-03-28 08:08:50 +0000164 "CC",
Yun Penga6f0f132017-06-28 14:55:37 +0200165 "CC_CONFIGURE_DEBUG",
Nicolas Lopezbcbdb872017-03-28 08:08:50 +0000166 "CC_TOOLCHAIN_NAME",
Nicolas Lopez6326f982017-04-04 16:22:30 +0000167 "CPLUS_INCLUDE_PATH",
Ulf Adams9566f672018-04-19 02:55:15 -0700168 "GCOV",
Nicolas Lopez6326f982017-04-04 16:22:30 +0000169 "HOMEBREW_RUBY_PATH",
Ulf Adams1441a3a2018-04-26 00:39:43 -0700170 "SYSTEMROOT",
Laszlo Csomor07853cd2019-07-16 06:14:30 -0700171 ] + MSVC_ENVVARS,
jcaterf5c8c0b2018-03-27 07:22:35 -0700172 implementation = cc_autoconf_impl,
Klaus Aehlige1a68772019-08-29 02:54:41 -0700173 configure = True,
jcaterf5c8c0b2018-03-27 07:22:35 -0700174)
Damien Martin-Guillerez8fa5ae62016-03-02 16:24:13 +0000175
176def cc_configure():
vladmos20a042f2018-06-01 04:51:21 -0700177 """A C++ configuration rules that generate the crosstool file."""
Marcel Hlopkob4c0fb02019-05-27 03:54:06 -0700178 cc_autoconf_toolchains(name = "local_config_cc_toolchains")
vladmos20a042f2018-06-01 04:51:21 -0700179 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 Hlopkob4c0fb02019-05-27 03:54:06 -0700183 "@local_config_cc_toolchains//:all",
vladmos20a042f2018-06-01 04:51:21 -0700184 )