blob: 4643406302ec5a14b9049f05af32fa8d66b2fcba [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,
85)
86
Ilya Biryukov12471a72017-12-20 05:46:44 -080087def cc_autoconf_impl(repository_ctx, overriden_tools = dict()):
Marcel Hlopkob4c0fb02019-05-27 03:54:06 -070088 """Generate BUILD file with 'cc_toolchain' targets for the local host C++ toolchain.
89
90 Args:
91 repository_ctx: repository context
92 overriden_tools: dict of tool paths to use instead of autoconfigured tools
93 """
jmmv5b025592018-05-29 12:03:21 -070094
vladmos20a042f2018-06-01 04:51:21 -070095 env = repository_ctx.os.environ
96 cpu_value = get_cpu_value(repository_ctx)
97 if "BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN" in env and env["BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN"] == "1":
rosica3fd51292019-08-12 10:24:34 -070098 paths = resolve_labels(repository_ctx, [
99 "@bazel_tools//tools/cpp:BUILD.empty",
100 "@bazel_tools//tools/cpp:empty_cc_toolchain_config.bzl",
101 ])
102 repository_ctx.symlink(paths["@bazel_tools//tools/cpp:empty_cc_toolchain_config.bzl"], "cc_toolchain_config.bzl")
103 repository_ctx.symlink(paths("@bazel_tools//tools/cpp:BUILD.empty"), "BUILD")
vladmos20a042f2018-06-01 04:51:21 -0700104 elif cpu_value == "freebsd":
rosica5ee6d352019-08-09 07:24:36 -0700105 paths = resolve_labels(repository_ctx, [
106 "@bazel_tools//tools/cpp:BUILD.static.freebsd",
107 "@bazel_tools//tools/cpp:freebsd_cc_toolchain_config.bzl",
108 ])
109
110 # This is defaulting to a static crosstool, we should eventually
vladmos20a042f2018-06-01 04:51:21 -0700111 # autoconfigure this platform too. Theorically, FreeBSD should be
112 # straightforward to add but we cannot run it in a docker container so
113 # skipping until we have proper tests for FreeBSD.
rosica5ee6d352019-08-09 07:24:36 -0700114 repository_ctx.symlink(paths["@bazel_tools//tools/cpp:freebsd_cc_toolchain_config.bzl"], "cc_toolchain_config.bzl")
vladmos20a042f2018-06-01 04:51:21 -0700115 repository_ctx.symlink(paths["@bazel_tools//tools/cpp:BUILD.static.freebsd"], "BUILD")
116 elif cpu_value == "x64_windows":
117 # TODO(ibiryukov): overriden_tools are only supported in configure_unix_toolchain.
118 # We might want to add that to Windows too(at least for msys toolchain).
119 configure_windows_toolchain(repository_ctx)
120 elif (cpu_value == "darwin" and
121 ("BAZEL_USE_CPP_ONLY_TOOLCHAIN" not in env or env["BAZEL_USE_CPP_ONLY_TOOLCHAIN"] != "1")):
122 configure_osx_toolchain(repository_ctx, overriden_tools)
123 else:
124 configure_unix_toolchain(repository_ctx, cpu_value, overriden_tools)
Damien Martin-Guillerez8fa5ae62016-03-02 16:24:13 +0000125
Laszlo Csomor07853cd2019-07-16 06:14:30 -0700126MSVC_ENVVARS = [
127 "BAZEL_VC",
128 "BAZEL_VC_FULL_VERSION",
129 "BAZEL_VS",
130 "BAZEL_WINSDK_FULL_VERSION",
131 "VS90COMNTOOLS",
132 "VS100COMNTOOLS",
133 "VS110COMNTOOLS",
134 "VS120COMNTOOLS",
135 "VS140COMNTOOLS",
136 "VS150COMNTOOLS",
137 "VS160COMNTOOLS",
138 "TMP",
139 "TEMP",
140]
141
Damien Martin-Guillerezac29b782017-02-15 14:50:39 +0000142cc_autoconf = repository_rule(
Damien Martin-Guillerezac29b782017-02-15 14:50:39 +0000143 environ = [
Nicolas Lopezbcbdb872017-03-28 08:08:50 +0000144 "ABI_LIBC_VERSION",
145 "ABI_VERSION",
146 "BAZEL_COMPILER",
147 "BAZEL_HOST_SYSTEM",
Piotr Sikoraa89c7ef2019-01-21 09:28:39 -0800148 "BAZEL_CXXOPTS",
hlopko19c64282018-02-27 07:28:40 -0800149 "BAZEL_LINKOPTS",
Marcel Hlopkoab9c1f52019-06-19 00:45:58 -0700150 "BAZEL_LINKLIBS",
Nicolas Lopezbcbdb872017-03-28 08:08:50 +0000151 "BAZEL_PYTHON",
152 "BAZEL_SH",
153 "BAZEL_TARGET_CPU",
154 "BAZEL_TARGET_LIBC",
155 "BAZEL_TARGET_SYSTEM",
hlopko209a9752017-12-12 04:34:41 -0800156 "BAZEL_USE_CPP_ONLY_TOOLCHAIN",
Marcel Hlopko7984a152019-05-29 10:31:42 -0700157 "BAZEL_USE_XCODE_TOOLCHAIN",
hlopko57bc2012018-05-16 02:15:04 -0700158 "BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN",
Ulf Adams1441a3a2018-04-26 00:39:43 -0700159 "BAZEL_USE_LLVM_NATIVE_COVERAGE",
Yun Pengf445af52018-11-02 07:55:01 -0700160 "BAZEL_LLVM",
161 "USE_CLANG_CL",
Nicolas Lopezbcbdb872017-03-28 08:08:50 +0000162 "CC",
Yun Penga6f0f132017-06-28 14:55:37 +0200163 "CC_CONFIGURE_DEBUG",
Nicolas Lopezbcbdb872017-03-28 08:08:50 +0000164 "CC_TOOLCHAIN_NAME",
Nicolas Lopez6326f982017-04-04 16:22:30 +0000165 "CPLUS_INCLUDE_PATH",
Ulf Adams9566f672018-04-19 02:55:15 -0700166 "GCOV",
Nicolas Lopez6326f982017-04-04 16:22:30 +0000167 "HOMEBREW_RUBY_PATH",
Ulf Adams1441a3a2018-04-26 00:39:43 -0700168 "SYSTEMROOT",
Laszlo Csomor07853cd2019-07-16 06:14:30 -0700169 ] + MSVC_ENVVARS,
jcaterf5c8c0b2018-03-27 07:22:35 -0700170 implementation = cc_autoconf_impl,
171)
Damien Martin-Guillerez8fa5ae62016-03-02 16:24:13 +0000172
173def cc_configure():
vladmos20a042f2018-06-01 04:51:21 -0700174 """A C++ configuration rules that generate the crosstool file."""
Marcel Hlopkob4c0fb02019-05-27 03:54:06 -0700175 cc_autoconf_toolchains(name = "local_config_cc_toolchains")
vladmos20a042f2018-06-01 04:51:21 -0700176 cc_autoconf(name = "local_config_cc")
177 native.bind(name = "cc_toolchain", actual = "@local_config_cc//:toolchain")
178 native.register_toolchains(
179 # Use register_toolchain's target pattern expansion to register all toolchains in the package.
Marcel Hlopkob4c0fb02019-05-27 03:54:06 -0700180 "@local_config_cc_toolchains//:all",
vladmos20a042f2018-06-01 04:51:21 -0700181 )