Yun Peng | 65cda4f | 2017-06-22 11:06:11 +0200 | [diff] [blame] | 1 | # 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 | |
| 17 | load("@bazel_tools//tools/osx:xcode_configure.bzl", "run_xcode_locator") |
Yun Peng | 65cda4f | 2017-06-22 11:06:11 +0200 | [diff] [blame] | 18 | load( |
| 19 | "@bazel_tools//tools/cpp:lib_cc_configure.bzl", |
| 20 | "escape_string", |
jmmv | 5b02559 | 2018-05-29 12:03:21 -0700 | [diff] [blame] | 21 | "resolve_labels", |
Marcel Hlopko | 8b0bfaf | 2019-09-02 08:13:18 -0700 | [diff] [blame] | 22 | "write_builtin_include_directory_paths", |
Yun Peng | 65cda4f | 2017-06-22 11:06:11 +0200 | [diff] [blame] | 23 | ) |
Yun Peng | 65cda4f | 2017-06-22 11:06:11 +0200 | [diff] [blame] | 24 | load( |
| 25 | "@bazel_tools//tools/cpp:unix_cc_configure.bzl", |
vladmos | 20a042f | 2018-06-01 04:51:21 -0700 | [diff] [blame] | 26 | "configure_unix_toolchain", |
vladmos | 20a042f | 2018-06-01 04:51:21 -0700 | [diff] [blame] | 27 | "get_env", |
| 28 | "get_escaped_cxx_inc_directories", |
Yun Peng | 65cda4f | 2017-06-22 11:06:11 +0200 | [diff] [blame] | 29 | ) |
| 30 | |
Yun Peng | 65cda4f | 2017-06-22 11:06:11 +0200 | [diff] [blame] | 31 | def _get_escaped_xcode_cxx_inc_directories(repository_ctx, cc, xcode_toolchains): |
vladmos | 20a042f | 2018-06-01 04:51:21 -0700 | [diff] [blame] | 32 | """Compute the list of default C++ include paths on Xcode-enabled darwin. |
Yun Peng | 65cda4f | 2017-06-22 11:06:11 +0200 | [diff] [blame] | 33 | |
vladmos | 20a042f | 2018-06-01 04:51:21 -0700 | [diff] [blame] | 34 | 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 Peng | 65cda4f | 2017-06-22 11:06:11 +0200 | [diff] [blame] | 41 | |
vladmos | 20a042f | 2018-06-01 04:51:21 -0700 | [diff] [blame] | 42 | # 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 Smiley | 8ba86a0 | 2019-03-28 12:42:10 -0700 | [diff] [blame] | 47 | |
| 48 | # Assume that all paths that point to /Applications/ are built in include paths |
| 49 | include_dirs.append("/Applications/") |
vladmos | 20a042f | 2018-06-01 04:51:21 -0700 | [diff] [blame] | 50 | return include_dirs |
Yun Peng | 65cda4f | 2017-06-22 11:06:11 +0200 | [diff] [blame] | 51 | |
Ilya Biryukov | 12471a7 | 2017-12-20 05:46:44 -0800 | [diff] [blame] | 52 | def configure_osx_toolchain(repository_ctx, overriden_tools): |
vladmos | 20a042f | 2018-06-01 04:51:21 -0700 | [diff] [blame] | 53 | """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", |
rosica | ac3d06b | 2019-06-25 04:08:00 -0700 | [diff] [blame] | 60 | "@bazel_tools//tools/osx/crosstool:cc_toolchain_config.bzl", |
vladmos | 20a042f | 2018-06-01 04:51:21 -0700 | [diff] [blame] | 61 | "@bazel_tools//tools/osx/crosstool:wrapped_clang.cc", |
vladmos | 20a042f | 2018-06-01 04:51:21 -0700 | [diff] [blame] | 62 | "@bazel_tools//tools/osx:xcode_locator.m", |
| 63 | ]) |
jmmv | 5b02559 | 2018-05-29 12:03:21 -0700 | [diff] [blame] | 64 | |
Marcel Hlopko | 7984a15 | 2019-05-29 10:31:42 -0700 | [diff] [blame] | 65 | env = repository_ctx.os.environ |
| 66 | should_use_xcode = "BAZEL_USE_XCODE_TOOLCHAIN" in env and env["BAZEL_USE_XCODE_TOOLCHAIN"] == "1" |
vladmos | 20a042f | 2018-06-01 04:51:21 -0700 | [diff] [blame] | 67 | xcode_toolchains = [] |
Marcel Hlopko | 7984a15 | 2019-05-29 10:31:42 -0700 | [diff] [blame] | 68 | |
| 69 | # Make the following logic in sync with //tools/cpp:cc_configure.bzl#cc_autoconf_toolchains_impl |
Keith Smiley | c6c9b58 | 2019-02-28 11:27:07 -0800 | [diff] [blame] | 70 | (xcode_toolchains, xcodeloc_err) = run_xcode_locator( |
vladmos | 20a042f | 2018-06-01 04:51:21 -0700 | [diff] [blame] | 71 | repository_ctx, |
| 72 | paths["@bazel_tools//tools/osx:xcode_locator.m"], |
| 73 | ) |
Marcel Hlopko | 7984a15 | 2019-05-29 10:31:42 -0700 | [diff] [blame] | 74 | 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.") |
vladmos | 20a042f | 2018-06-01 04:51:21 -0700 | [diff] [blame] | 77 | if xcode_toolchains: |
Tony Allevato | 2682688 | 2019-09-24 07:23:01 -0700 | [diff] [blame] | 78 | # 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 Smiley | 63332eb5 | 2019-11-13 05:43:27 -0800 | [diff] [blame] | 86 | cc_path = '"$(/usr/bin/dirname "$0")"/wrapped_clang' |
vladmos | 20a042f | 2018-06-01 04:51:21 -0700 | [diff] [blame] | 87 | repository_ctx.template( |
| 88 | "cc_wrapper.sh", |
| 89 | paths["@bazel_tools//tools/cpp:osx_cc_wrapper.sh.tpl"], |
| 90 | { |
Keith Smiley | c76c3e5 | 2019-11-12 07:10:35 -0800 | [diff] [blame] | 91 | "%{cc}": escape_string(cc_path), |
vladmos | 20a042f | 2018-06-01 04:51:21 -0700 | [diff] [blame] | 92 | "%{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( |
rosica | ac3d06b | 2019-06-25 04:08:00 -0700 | [diff] [blame] | 108 | paths["@bazel_tools//tools/osx/crosstool:cc_toolchain_config.bzl"], |
| 109 | "cc_toolchain_config.bzl", |
vladmos | 20a042f | 2018-06-01 04:51:21 -0700 | [diff] [blame] | 110 | ) |
vladmos | 20a042f | 2018-06-01 04:51:21 -0700 | [diff] [blame] | 111 | 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 Smiley | ada2c55 | 2019-09-13 08:09:31 -0700 | [diff] [blame] | 118 | "--sdk", |
| 119 | "macosx", |
vladmos | 20a042f | 2018-06-01 04:51:21 -0700 | [diff] [blame] | 120 | "clang", |
Keith Smiley | 4ffb36f | 2019-09-18 09:01:05 -0700 | [diff] [blame] | 121 | "-mmacosx-version-min=10.9", |
vladmos | 20a042f | 2018-06-01 04:51:21 -0700 | [diff] [blame] | 122 | "-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: |
vladmos | 20a042f | 2018-06-01 04:51:21 -0700 | [diff] [blame] | 131 | 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 Smiley | 992eb17 | 2019-02-25 12:43:31 -0800 | [diff] [blame] | 138 | 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) |
vladmos | 20a042f | 2018-06-01 04:51:21 -0700 | [diff] [blame] | 141 | |
plf | ba2ccb1 | 2020-03-12 11:07:56 -0700 | [diff] [blame] | 142 | 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 | |
vladmos | 20a042f | 2018-06-01 04:51:21 -0700 | [diff] [blame] | 149 | escaped_include_paths = _get_escaped_xcode_cxx_inc_directories(repository_ctx, cc, xcode_toolchains) |
Marcel Hlopko | 8b0bfaf | 2019-09-02 08:13:18 -0700 | [diff] [blame] | 150 | write_builtin_include_directory_paths(repository_ctx, cc, escaped_include_paths) |
vladmos | 20a042f | 2018-06-01 04:51:21 -0700 | [diff] [blame] | 151 | escaped_cxx_include_directories = [] |
| 152 | for path in escaped_include_paths: |
rosica | ac3d06b | 2019-06-25 04:08:00 -0700 | [diff] [blame] | 153 | escaped_cxx_include_directories.append((" \"%s\"," % path)) |
Keith Smiley | c6c9b58 | 2019-02-28 11:27:07 -0800 | [diff] [blame] | 154 | if xcodeloc_err: |
| 155 | escaped_cxx_include_directories.append("# Error: " + xcodeloc_err + "\n") |
vladmos | 20a042f | 2018-06-01 04:51:21 -0700 | [diff] [blame] | 156 | repository_ctx.template( |
rosica | ac3d06b | 2019-06-25 04:08:00 -0700 | [diff] [blame] | 157 | "BUILD", |
| 158 | paths["@bazel_tools//tools/osx/crosstool:BUILD.tpl"], |
plf | ba2ccb1 | 2020-03-12 11:07:56 -0700 | [diff] [blame] | 159 | { |
| 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 | }, |
vladmos | 20a042f | 2018-06-01 04:51:21 -0700 | [diff] [blame] | 165 | ) |
kaipi | 149e95b | 2018-02-06 08:56:05 -0800 | [diff] [blame] | 166 | else: |
vladmos | 20a042f | 2018-06-01 04:51:21 -0700 | [diff] [blame] | 167 | configure_unix_toolchain(repository_ctx, cpu_value = "darwin", overriden_tools = overriden_tools) |