blob: bac3c0dfc9f87ea4c6a30e683d7789b9689307cd [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")
18
19load(
20 "@bazel_tools//tools/cpp:lib_cc_configure.bzl",
21 "escape_string",
22)
23
24load(
25 "@bazel_tools//tools/cpp:unix_cc_configure.bzl",
26 "get_escaped_cxx_inc_directories",
27 "tpl",
28 "get_env",
29 "find_cc",
30 "configure_unix_toolchain"
31)
32
33
34def _get_escaped_xcode_cxx_inc_directories(repository_ctx, cc, xcode_toolchains):
35 """Compute the list of default C++ include paths on Xcode-enabled darwin.
36
37 Args:
38 repository_ctx: The repository context.
39 cc: The default C++ compiler on the local system.
40 xcode_toolchains: A list containing the xcode toolchains available
41 Returns:
42 include_paths: A list of builtin include paths.
43 """
44
45 # TODO(cparsons): Falling back to the default C++ compiler builtin include
46 # paths shouldn't be unnecessary once all actions are using xcrun.
47 include_dirs = get_escaped_cxx_inc_directories(repository_ctx, cc)
48 for toolchain in xcode_toolchains:
49 include_dirs.append(escape_string(toolchain.developer_dir))
50 return include_dirs
51
52
53def configure_osx_toolchain(repository_ctx):
54 """Configure C++ toolchain on macOS."""
55 xcode_toolchains = []
56 (xcode_toolchains, xcodeloc_err) = run_xcode_locator(
57 repository_ctx,
58 Label("@bazel_tools//tools/osx:xcode_locator.m"))
59 if xcode_toolchains:
60 cc = find_cc(repository_ctx)
61 tpl(repository_ctx, "osx_cc_wrapper.sh", {
62 "%{cc}": escape_string(str(cc)),
63 "%{env}": escape_string(get_env(repository_ctx))
64 }, "cc_wrapper.sh")
65 repository_ctx.symlink(
66 Label("@bazel_tools//tools/objc:xcrunwrapper.sh"), "xcrunwrapper.sh")
67 repository_ctx.symlink(
68 Label("@bazel_tools//tools/objc:libtool.sh"), "libtool")
69 repository_ctx.symlink(
70 Label("@bazel_tools//tools/objc:make_hashed_objlist.py"),
71 "make_hashed_objlist.py")
72 repository_ctx.symlink(
73 Label("@bazel_tools//tools/osx/crosstool:wrapped_ar.tpl"),
74 "wrapped_ar")
75 repository_ctx.symlink(
76 Label("@bazel_tools//tools/osx/crosstool:wrapped_clang.tpl"),
77 "wrapped_clang")
78 repository_ctx.symlink(
79 Label("@bazel_tools//tools/osx/crosstool:wrapped_clang_pp.tpl"),
80 "wrapped_clang_pp")
81 repository_ctx.symlink(
82 Label("@bazel_tools//tools/osx/crosstool:BUILD.tpl"),
83 "BUILD")
84 repository_ctx.symlink(
85 Label("@bazel_tools//tools/osx/crosstool:osx_archs.bzl"),
86 "osx_archs.bzl")
87 escaped_include_paths = _get_escaped_xcode_cxx_inc_directories(repository_ctx, cc, xcode_toolchains)
88 escaped_cxx_include_directories = []
89 for path in escaped_include_paths:
90 escaped_cxx_include_directories.append(("cxx_builtin_include_directory: \"%s\"" % path))
91 if xcodeloc_err:
92 escaped_cxx_include_directories.append("# Error: " + xcodeloc_err + "\n")
93 repository_ctx.template(
94 "CROSSTOOL",
95 Label("@bazel_tools//tools/osx/crosstool:CROSSTOOL.tpl"),
96 {"%{cxx_builtin_include_directory}": "\n".join(escaped_cxx_include_directories)})
97 else:
98 configure_unix_toolchain(repository_ctx, cpu_value = "darwin")