blob: a3728044ec685a1b6a3600fa501e5f7d66fa8bae [file] [log] [blame]
Yun Peng65cda4f2017-06-22 11:06:11 +02001# pylint: disable=g-bad-file-header
2# Copyright 2016 The Bazel Authors. All rights reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15"""Configuring the C++ toolchain on macOS."""
16
17load("@bazel_tools//tools/osx:xcode_configure.bzl", "run_xcode_locator")
Yun Peng65cda4f2017-06-22 11:06:11 +020018load(
19 "@bazel_tools//tools/cpp:lib_cc_configure.bzl",
20 "escape_string",
jmmv5b025592018-05-29 12:03:21 -070021 "resolve_labels",
Marcel Hlopko8b0bfaf2019-09-02 08:13:18 -070022 "write_builtin_include_directory_paths",
Yun Peng65cda4f2017-06-22 11:06:11 +020023)
Yun Peng65cda4f2017-06-22 11:06:11 +020024load(
25 "@bazel_tools//tools/cpp:unix_cc_configure.bzl",
vladmos20a042f2018-06-01 04:51:21 -070026 "configure_unix_toolchain",
vladmos20a042f2018-06-01 04:51:21 -070027 "get_env",
28 "get_escaped_cxx_inc_directories",
Yun Peng65cda4f2017-06-22 11:06:11 +020029)
30
Yun Peng65cda4f2017-06-22 11:06:11 +020031def _get_escaped_xcode_cxx_inc_directories(repository_ctx, cc, xcode_toolchains):
vladmos20a042f2018-06-01 04:51:21 -070032 """Compute the list of default C++ include paths on Xcode-enabled darwin.
Yun Peng65cda4f2017-06-22 11:06:11 +020033
vladmos20a042f2018-06-01 04:51:21 -070034 Args:
35 repository_ctx: The repository context.
36 cc: The default C++ compiler on the local system.
37 xcode_toolchains: A list containing the xcode toolchains available
38 Returns:
39 include_paths: A list of builtin include paths.
40 """
Yun Peng65cda4f2017-06-22 11:06:11 +020041
vladmos20a042f2018-06-01 04:51:21 -070042 # TODO(cparsons): Falling back to the default C++ compiler builtin include
43 # paths shouldn't be unnecessary once all actions are using xcrun.
44 include_dirs = get_escaped_cxx_inc_directories(repository_ctx, cc, "-xc++")
45 for toolchain in xcode_toolchains:
46 include_dirs.append(escape_string(toolchain.developer_dir))
Keith Smiley8ba86a02019-03-28 12:42:10 -070047
48 # Assume that all paths that point to /Applications/ are built in include paths
49 include_dirs.append("/Applications/")
vladmos20a042f2018-06-01 04:51:21 -070050 return include_dirs
Yun Peng65cda4f2017-06-22 11:06:11 +020051
Ilya Biryukov12471a72017-12-20 05:46:44 -080052def configure_osx_toolchain(repository_ctx, overriden_tools):
vladmos20a042f2018-06-01 04:51:21 -070053 """Configure C++ toolchain on macOS."""
54 paths = resolve_labels(repository_ctx, [
55 "@bazel_tools//tools/cpp:osx_cc_wrapper.sh.tpl",
56 "@bazel_tools//tools/objc:libtool.sh",
57 "@bazel_tools//tools/objc:make_hashed_objlist.py",
58 "@bazel_tools//tools/objc:xcrunwrapper.sh",
59 "@bazel_tools//tools/osx/crosstool:BUILD.tpl",
rosicaac3d06b2019-06-25 04:08:00 -070060 "@bazel_tools//tools/osx/crosstool:cc_toolchain_config.bzl",
vladmos20a042f2018-06-01 04:51:21 -070061 "@bazel_tools//tools/osx/crosstool:wrapped_clang.cc",
vladmos20a042f2018-06-01 04:51:21 -070062 "@bazel_tools//tools/osx:xcode_locator.m",
63 ])
jmmv5b025592018-05-29 12:03:21 -070064
Marcel Hlopko7984a152019-05-29 10:31:42 -070065 env = repository_ctx.os.environ
66 should_use_xcode = "BAZEL_USE_XCODE_TOOLCHAIN" in env and env["BAZEL_USE_XCODE_TOOLCHAIN"] == "1"
vladmos20a042f2018-06-01 04:51:21 -070067 xcode_toolchains = []
Marcel Hlopko7984a152019-05-29 10:31:42 -070068
69 # Make the following logic in sync with //tools/cpp:cc_configure.bzl#cc_autoconf_toolchains_impl
Keith Smileyc6c9b582019-02-28 11:27:07 -080070 (xcode_toolchains, xcodeloc_err) = run_xcode_locator(
vladmos20a042f2018-06-01 04:51:21 -070071 repository_ctx,
72 paths["@bazel_tools//tools/osx:xcode_locator.m"],
73 )
Marcel Hlopko7984a152019-05-29 10:31:42 -070074 if should_use_xcode and not xcode_toolchains:
75 fail("BAZEL_USE_XCODE_TOOLCHAIN is set to 1 but Bazel couldn't find Xcode installed on the " +
76 "system. Verify that 'xcode-select -p' is correct.")
vladmos20a042f2018-06-01 04:51:21 -070077 if xcode_toolchains:
Tony Allevato26826882019-09-24 07:23:01 -070078 # For Xcode toolchains, there's no reason to use anything other than
79 # wrapped_clang, so that we still get the Bazel Xcode placeholder
80 # substitution and other behavior for actions that invoke this
81 # cc_wrapper.sh script. The wrapped_clang binary is already hardcoded
82 # into the Objective-C crosstool actions, anyway, so this ensures that
83 # the C++ actions behave consistently.
84 cc = repository_ctx.path("wrapped_clang")
85
Keith Smiley63332eb52019-11-13 05:43:27 -080086 cc_path = '"$(/usr/bin/dirname "$0")"/wrapped_clang'
vladmos20a042f2018-06-01 04:51:21 -070087 repository_ctx.template(
88 "cc_wrapper.sh",
89 paths["@bazel_tools//tools/cpp:osx_cc_wrapper.sh.tpl"],
90 {
Keith Smileyc76c3e52019-11-12 07:10:35 -080091 "%{cc}": escape_string(cc_path),
vladmos20a042f2018-06-01 04:51:21 -070092 "%{env}": escape_string(get_env(repository_ctx)),
93 },
94 )
95 repository_ctx.symlink(
96 paths["@bazel_tools//tools/objc:xcrunwrapper.sh"],
97 "xcrunwrapper.sh",
98 )
99 repository_ctx.symlink(
100 paths["@bazel_tools//tools/objc:libtool.sh"],
101 "libtool",
102 )
103 repository_ctx.symlink(
104 paths["@bazel_tools//tools/objc:make_hashed_objlist.py"],
105 "make_hashed_objlist.py",
106 )
107 repository_ctx.symlink(
rosicaac3d06b2019-06-25 04:08:00 -0700108 paths["@bazel_tools//tools/osx/crosstool:cc_toolchain_config.bzl"],
109 "cc_toolchain_config.bzl",
vladmos20a042f2018-06-01 04:51:21 -0700110 )
vladmos20a042f2018-06-01 04:51:21 -0700111 wrapped_clang_src_path = str(repository_ctx.path(
112 paths["@bazel_tools//tools/osx/crosstool:wrapped_clang.cc"],
113 ))
114 xcrun_result = repository_ctx.execute([
115 "env",
116 "-i",
117 "xcrun",
Keith Smileyada2c552019-09-13 08:09:31 -0700118 "--sdk",
119 "macosx",
vladmos20a042f2018-06-01 04:51:21 -0700120 "clang",
Keith Smiley4ffb36f2019-09-18 09:01:05 -0700121 "-mmacosx-version-min=10.9",
vladmos20a042f2018-06-01 04:51:21 -0700122 "-std=c++11",
123 "-lc++",
124 "-o",
125 "wrapped_clang",
126 wrapped_clang_src_path,
127 ], 30)
128 if (xcrun_result.return_code == 0):
129 repository_ctx.symlink("wrapped_clang", "wrapped_clang_pp")
130 else:
vladmos20a042f2018-06-01 04:51:21 -0700131 error_msg = (
132 "return code {code}, stderr: {err}, stdout: {out}"
133 ).format(
134 code = xcrun_result.return_code,
135 err = xcrun_result.stderr,
136 out = xcrun_result.stdout,
137 )
Keith Smiley992eb172019-02-25 12:43:31 -0800138 fail("wrapped_clang failed to generate. Please file an issue at " +
139 "https://github.com/bazelbuild/bazel/issues with the following:\n" +
140 error_msg)
vladmos20a042f2018-06-01 04:51:21 -0700141
plfba2ccb12020-03-12 11:07:56 -0700142 tool_paths = {}
143 gcov_path = repository_ctx.os.environ.get("GCOV")
144 if gcov_path != None:
145 if not gcov_path.startswith("/"):
146 gcov_path = repository_ctx.which(gcov_path)
147 tool_paths["gcov"] = gcov_path
148
vladmos20a042f2018-06-01 04:51:21 -0700149 escaped_include_paths = _get_escaped_xcode_cxx_inc_directories(repository_ctx, cc, xcode_toolchains)
Marcel Hlopko8b0bfaf2019-09-02 08:13:18 -0700150 write_builtin_include_directory_paths(repository_ctx, cc, escaped_include_paths)
vladmos20a042f2018-06-01 04:51:21 -0700151 escaped_cxx_include_directories = []
152 for path in escaped_include_paths:
rosicaac3d06b2019-06-25 04:08:00 -0700153 escaped_cxx_include_directories.append((" \"%s\"," % path))
Keith Smileyc6c9b582019-02-28 11:27:07 -0800154 if xcodeloc_err:
155 escaped_cxx_include_directories.append("# Error: " + xcodeloc_err + "\n")
vladmos20a042f2018-06-01 04:51:21 -0700156 repository_ctx.template(
rosicaac3d06b2019-06-25 04:08:00 -0700157 "BUILD",
158 paths["@bazel_tools//tools/osx/crosstool:BUILD.tpl"],
plfba2ccb12020-03-12 11:07:56 -0700159 {
160 "%{cxx_builtin_include_directories}": "\n".join(escaped_cxx_include_directories),
161 "%{tool_paths_overrides}": ",\n ".join(
162 ['"%s": "%s"' % (k, v) for k, v in tool_paths.items()],
163 ),
164 },
vladmos20a042f2018-06-01 04:51:21 -0700165 )
kaipi149e95b2018-02-06 08:56:05 -0800166 else:
vladmos20a042f2018-06-01 04:51:21 -0700167 configure_unix_toolchain(repository_ctx, cpu_value = "darwin", overriden_tools = overriden_tools)