Merge pull request #154 from keith:ks/move-apple-toolchain-setup-to-apple_support

PiperOrigin-RevId: 502796914
Change-Id: I2cd0481a244db416ec2abb06e05dee0f783b72b5
diff --git a/cc/private/toolchain/BUILD b/cc/private/toolchain/BUILD
index 6915d5b..557a6a7 100644
--- a/cc/private/toolchain/BUILD
+++ b/cc/private/toolchain/BUILD
@@ -81,11 +81,6 @@
 )
 
 filegroup(
-    name = "osx_wrapper",
-    srcs = ["osx_cc_wrapper.sh"],
-)
-
-filegroup(
     name = "link_dynamic_library",
     srcs = ["link_dynamic_library.sh"],
 )
diff --git a/cc/private/toolchain/cc_configure.bzl b/cc/private/toolchain/cc_configure.bzl
index c281198..c7b19de 100644
--- a/cc/private/toolchain/cc_configure.bzl
+++ b/cc/private/toolchain/cc_configure.bzl
@@ -13,68 +13,36 @@
 # limitations under the License.
 """Rules for configuring the C++ toolchain (experimental)."""
 
-load("@bazel_tools//tools/osx:xcode_configure.bzl", "run_xcode_locator")
 load(
     ":lib_cc_configure.bzl",
     "get_cpu_value",
     "resolve_labels",
 )
-load(":osx_cc_configure.bzl", "configure_osx_toolchain")
 load(":unix_cc_configure.bzl", "configure_unix_toolchain")
 load(":windows_cc_configure.bzl", "configure_windows_toolchain")
 
-def _generate_cpp_only_build_file(repository_ctx, cpu_value, paths):
-    repository_ctx.template(
-        "BUILD",
-        paths["@rules_cc//cc/private/toolchain:BUILD.toolchains.tpl"],
-        {"%{name}": cpu_value},
-    )
-
 def cc_autoconf_toolchains_impl(repository_ctx):
     """Generate BUILD file with 'toolchain' targets for the local host C++ toolchain.
 
     Args:
       repository_ctx: repository context
     """
-    paths = resolve_labels(repository_ctx, [
-        "@rules_cc//cc/private/toolchain:BUILD.toolchains.tpl",
-        "@bazel_tools//tools/osx/crosstool:BUILD.toolchains",
-        "@bazel_tools//tools/osx/crosstool:osx_archs.bzl",
-        "@bazel_tools//tools/osx:xcode_locator.m",
-    ])
     env = repository_ctx.os.environ
-    cpu_value = get_cpu_value(repository_ctx)
 
     # Should we try to find C++ toolchain at all? If not, we don't have to generate toolchains for C++ at all.
     should_detect_cpp_toolchain = "BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN" not in env or env["BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN"] != "1"
 
-    # Should we unconditionally *not* use xcode? If so, we don't have to run Xcode locator ever.
-    should_use_cpp_only_toolchain = "BAZEL_USE_CPP_ONLY_TOOLCHAIN" in env and env["BAZEL_USE_CPP_ONLY_TOOLCHAIN"] == "1"
-
-    # Should we unconditionally use xcode? If so, we don't have to run Xcode locator now.
-    should_use_xcode = "BAZEL_USE_XCODE_TOOLCHAIN" in env and env["BAZEL_USE_XCODE_TOOLCHAIN"] == "1"
-
-    if not should_detect_cpp_toolchain:
-        repository_ctx.file("BUILD", "# C++ toolchain autoconfiguration was disabled by BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN env variable.")
-    elif cpu_value == "darwin" and not should_use_cpp_only_toolchain:
-        xcode_toolchains = []
-
-        # Only detect xcode if the user didn't tell us it will be there.
-        if not should_use_xcode:
-            # TODO(#6926): Unify C++ and ObjC toolchains so we don't have to run xcode locator to generate toolchain targets.
-            # And also so we don't have to keep this code in sync with @rules_cc//cc/private/toolchain:osx_cc_configure.bzl.
-            (xcode_toolchains, _xcodeloc_err) = run_xcode_locator(
-                repository_ctx,
-                paths["@bazel_tools//tools/osx:xcode_locator.m"],
-            )
-
-        if should_use_xcode or xcode_toolchains:
-            repository_ctx.symlink(paths["@bazel_tools//tools/osx/crosstool:BUILD.toolchains"], "BUILD")
-            repository_ctx.symlink(paths["@bazel_tools//tools/osx/crosstool:osx_archs.bzl"], "osx_archs.bzl")
-        else:
-            _generate_cpp_only_build_file(repository_ctx, cpu_value, paths)
+    if should_detect_cpp_toolchain:
+        paths = resolve_labels(repository_ctx, [
+            "@rules_cc//cc/private/toolchain:BUILD.toolchains.tpl",
+        ])
+        repository_ctx.template(
+            "BUILD",
+            paths["@rules_cc//cc/private/toolchain:BUILD.toolchains.tpl"],
+            {"%{name}": get_cpu_value(repository_ctx)},
+        )
     else:
-        _generate_cpp_only_build_file(repository_ctx, cpu_value, paths)
+        repository_ctx.file("BUILD", "# C++ toolchain autoconfiguration was disabled by BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN env variable.")
 
 cc_autoconf_toolchains = repository_rule(
     environ = [
@@ -118,9 +86,6 @@
         # TODO(ibiryukov): overriden_tools are only supported in configure_unix_toolchain.
         # We might want to add that to Windows too(at least for msys toolchain).
         configure_windows_toolchain(repository_ctx)
-    elif (cpu_value == "darwin" and
-          ("BAZEL_USE_CPP_ONLY_TOOLCHAIN" not in env or env["BAZEL_USE_CPP_ONLY_TOOLCHAIN"] != "1")):
-        configure_osx_toolchain(repository_ctx, overriden_tools)
     else:
         configure_unix_toolchain(repository_ctx, cpu_value, overriden_tools)
 
diff --git a/cc/private/toolchain/libtool_check_unique.cc b/cc/private/toolchain/libtool_check_unique.cc
deleted file mode 100644
index 340e9a3..0000000
--- a/cc/private/toolchain/libtool_check_unique.cc
+++ /dev/null
@@ -1,109 +0,0 @@
-// Copyright 2020 The Bazel Authors. All rights reserved.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-//    http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-#include <cstdlib>
-#include <fstream>
-#include <iostream>
-#include <regex>  // NOLINT
-#include <unordered_set>
-
-using std::ifstream;
-using std::regex;
-using std::string;
-using std::unordered_set;
-using std::vector;
-
-const regex libRegex = regex(".*\\.a$");
-const regex noArgFlags =
-    regex("-static|-s|-a|-c|-L|-T|-D|-no_warning_for_no_symbols");
-const regex singleArgFlags = regex("-arch_only|-syslibroot|-o");
-
-string getBasename(const string &path) {
-  // Assumes we're on an OS with "/" as the path separator
-  auto idx = path.find_last_of("/");
-  if (idx == string::npos) {
-    return path;
-  }
-  return path.substr(idx + 1);
-}
-
-vector<string> readFile(const string path) {
-  vector<string> lines;
-  ifstream file(path);
-  string line;
-  while (std::getline(file, line)) {
-    if (!line.empty()) {
-      lines.push_back(line);
-    }
-  }
-
-  return lines;
-}
-
-unordered_set<string> parseArgs(vector<string> args) {
-  unordered_set<string> basenames;
-  for (auto it = args.begin(); it != args.end(); ++it) {
-    const string arg = *it;
-    if (arg == "-filelist") {
-      ++it;
-      ifstream list(*it);
-      for (string line; getline(list, line);) {
-        const string basename = getBasename(line);
-        const auto pair = basenames.insert(basename);
-        if (!pair.second) {
-          exit(EXIT_FAILURE);
-        }
-      }
-      list.close();
-    } else if (arg[0] == '@') {
-      string paramsFilePath(arg.substr(1));
-      auto newBasenames = parseArgs(readFile(paramsFilePath));
-      for (auto newBasename : newBasenames) {
-        const auto pair = basenames.insert(newBasename);
-        if (!pair.second) {
-          exit(EXIT_FAILURE);
-        }
-      }
-    } else if (regex_match(arg, noArgFlags)) {
-    } else if (regex_match(arg, singleArgFlags)) {
-      ++it;
-    } else if (arg[0] == '-') {
-      exit(EXIT_FAILURE);
-      // Unrecognized flag, let the wrapper deal with it, any flags added to
-      // libtool.sh should also be added here.
-    } else if (regex_match(arg, libRegex)) {
-      // Archive inputs can remain untouched, as they come from other targets.
-    } else {
-      const string basename = getBasename(arg);
-      const auto pair = basenames.insert(basename);
-      if (!pair.second) {
-        exit(EXIT_FAILURE);
-      }
-    }
-  }
-
-  return basenames;
-}
-
-// Returns 0 if there are no duplicate basenames in the object files (via
-// -filelist, params files, and shell args), 1 otherwise
-int main(int argc, const char *argv[]) {
-  vector<string> args;
-  // Set i to 1 to skip executable path
-  for (int i = 1; argv[i] != nullptr; i++) {
-    args.push_back(argv[i]);
-  }
-  parseArgs(args);
-  return EXIT_SUCCESS;
-}
diff --git a/cc/private/toolchain/osx_cc_configure.bzl b/cc/private/toolchain/osx_cc_configure.bzl
deleted file mode 100644
index 6d45e04..0000000
--- a/cc/private/toolchain/osx_cc_configure.bzl
+++ /dev/null
@@ -1,184 +0,0 @@
-# pylint: disable=g-bad-file-header
-# Copyright 2016 The Bazel Authors. All rights reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-#    http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-"""Configuring the C++ toolchain on macOS."""
-
-load("@bazel_tools//tools/osx:xcode_configure.bzl", "run_xcode_locator")
-load(
-    ":lib_cc_configure.bzl",
-    "escape_string",
-    "resolve_labels",
-    "write_builtin_include_directory_paths",
-)
-load(
-    ":unix_cc_configure.bzl",
-    "configure_unix_toolchain",
-    "get_env",
-    "get_escaped_cxx_inc_directories",
-)
-
-def _get_escaped_xcode_cxx_inc_directories(repository_ctx, cc, xcode_toolchains):
-    """Compute the list of default C++ include paths on Xcode-enabled darwin.
-
-    Args:
-      repository_ctx: The repository context.
-      cc: The default C++ compiler on the local system.
-      xcode_toolchains: A list containing the xcode toolchains available
-    Returns:
-      include_paths: A list of builtin include paths.
-    """
-
-    # TODO(cparsons): Falling back to the default C++ compiler builtin include
-    # paths shouldn't be unnecessary once all actions are using xcrun.
-    include_dirs = get_escaped_cxx_inc_directories(repository_ctx, cc, "-xc++")
-    for toolchain in xcode_toolchains:
-        include_dirs.append(escape_string(toolchain.developer_dir))
-
-    # Assume that all paths that point to /Applications/ are built in include paths
-    include_dirs.append("/Applications/")
-    return include_dirs
-
-def compile_cc_file(repository_ctx, src_name, out_name):
-    xcrun_result = repository_ctx.execute([
-        "env",
-        "-i",
-        "xcrun",
-        "--sdk",
-        "macosx",
-        "clang",
-        "-mmacosx-version-min=10.9",
-        "-std=c++11",
-        "-lc++",
-        "-o",
-        out_name,
-        src_name,
-    ], 30)
-    if (xcrun_result.return_code != 0):
-        error_msg = (
-            "return code {code}, stderr: {err}, stdout: {out}"
-        ).format(
-            code = xcrun_result.return_code,
-            err = xcrun_result.stderr,
-            out = xcrun_result.stdout,
-        )
-        fail(out_name + " failed to generate. Please file an issue at " +
-             "https://github.com/bazelbuild/bazel/issues with the following:\n" +
-             error_msg)
-
-def configure_osx_toolchain(repository_ctx, overriden_tools):
-    """Configure C++ toolchain on macOS.
-
-    Args:
-      repository_ctx: The repository context.
-      overriden_tools: dictionary of overriden tools.
-    """
-    paths = resolve_labels(repository_ctx, [
-        "@rules_cc//cc/private/toolchain:armeabi_cc_toolchain_config.bzl",
-        "@rules_cc//cc/private/toolchain:osx_cc_wrapper.sh.tpl",
-        "@rules_cc//cc/private/toolchain:libtool_check_unique.cc",
-        "@bazel_tools//tools/objc:libtool.sh",
-        "@bazel_tools//tools/objc:make_hashed_objlist.py",
-        "@bazel_tools//tools/objc:xcrunwrapper.sh",
-        "@bazel_tools//tools/osx/crosstool:BUILD.tpl",
-        "@bazel_tools//tools/osx/crosstool:cc_toolchain_config.bzl",
-        "@bazel_tools//tools/osx/crosstool:wrapped_clang.cc",
-        "@bazel_tools//tools/osx:xcode_locator.m",
-    ])
-
-    env = repository_ctx.os.environ
-    should_use_xcode = "BAZEL_USE_XCODE_TOOLCHAIN" in env and env["BAZEL_USE_XCODE_TOOLCHAIN"] == "1"
-    xcode_toolchains = []
-
-    # Make the following logic in sync with @rules_cc//cc/private/toolchain:cc_configure.bzl#cc_autoconf_toolchains_impl
-    (xcode_toolchains, xcodeloc_err) = run_xcode_locator(
-        repository_ctx,
-        paths["@bazel_tools//tools/osx:xcode_locator.m"],
-    )
-    if should_use_xcode and not xcode_toolchains:
-        fail("BAZEL_USE_XCODE_TOOLCHAIN is set to 1 but Bazel couldn't find Xcode installed on the " +
-             "system. Verify that 'xcode-select -p' is correct.")
-    if xcode_toolchains:
-        # For Xcode toolchains, there's no reason to use anything other than
-        # wrapped_clang, so that we still get the Bazel Xcode placeholder
-        # substitution and other behavior for actions that invoke this
-        # cc_wrapper.sh script. The wrapped_clang binary is already hardcoded
-        # into the Objective-C crosstool actions, anyway, so this ensures that
-        # the C++ actions behave consistently.
-        cc = repository_ctx.path("wrapped_clang")
-
-        cc_path = '"$(/usr/bin/dirname "$0")"/wrapped_clang'
-        repository_ctx.template(
-            "cc_wrapper.sh",
-            paths["@rules_cc//cc/private/toolchain:osx_cc_wrapper.sh.tpl"],
-            {
-                "%{cc}": escape_string(cc_path),
-                "%{env}": escape_string(get_env(repository_ctx)),
-            },
-        )
-        repository_ctx.symlink(
-            paths["@rules_cc//cc/private/toolchain:armeabi_cc_toolchain_config.bzl"],
-            "armeabi_cc_toolchain_config.bzl",
-        )
-        repository_ctx.symlink(
-            paths["@bazel_tools//tools/objc:xcrunwrapper.sh"],
-            "xcrunwrapper.sh",
-        )
-        repository_ctx.symlink(
-            paths["@bazel_tools//tools/objc:libtool.sh"],
-            "libtool",
-        )
-        repository_ctx.symlink(
-            paths["@bazel_tools//tools/objc:make_hashed_objlist.py"],
-            "make_hashed_objlist.py",
-        )
-        repository_ctx.symlink(
-            paths["@bazel_tools//tools/osx/crosstool:cc_toolchain_config.bzl"],
-            "cc_toolchain_config.bzl",
-        )
-        libtool_check_unique_src_path = str(repository_ctx.path(
-            paths["@rules_cc//cc/private/toolchain:libtool_check_unique.cc"],
-        ))
-        compile_cc_file(repository_ctx, libtool_check_unique_src_path, "libtool_check_unique")
-        wrapped_clang_src_path = str(repository_ctx.path(
-            paths["@bazel_tools//tools/osx/crosstool:wrapped_clang.cc"],
-        ))
-        compile_cc_file(repository_ctx, wrapped_clang_src_path, "wrapped_clang")
-        repository_ctx.symlink("wrapped_clang", "wrapped_clang_pp")
-
-        tool_paths = {}
-        gcov_path = repository_ctx.os.environ.get("GCOV")
-        if gcov_path != None:
-            if not gcov_path.startswith("/"):
-                gcov_path = repository_ctx.which(gcov_path)
-            tool_paths["gcov"] = gcov_path
-
-        escaped_include_paths = _get_escaped_xcode_cxx_inc_directories(repository_ctx, cc, xcode_toolchains)
-        write_builtin_include_directory_paths(repository_ctx, cc, escaped_include_paths)
-        escaped_cxx_include_directories = []
-        for path in escaped_include_paths:
-            escaped_cxx_include_directories.append(("        \"%s\"," % path))
-        if xcodeloc_err:
-            escaped_cxx_include_directories.append("# Error: " + xcodeloc_err + "\n")
-        repository_ctx.template(
-            "BUILD",
-            paths["@bazel_tools//tools/osx/crosstool:BUILD.tpl"],
-            {
-                "%{cxx_builtin_include_directories}": "\n".join(escaped_cxx_include_directories),
-                "%{tool_paths_overrides}": ",\n        ".join(
-                    ['"%s": "%s"' % (k, v) for k, v in tool_paths.items()],
-                ),
-            },
-        )
-    else:
-        configure_unix_toolchain(repository_ctx, cpu_value = "darwin", overriden_tools = overriden_tools)
diff --git a/cc/private/toolchain/osx_cc_wrapper.sh b/cc/private/toolchain/osx_cc_wrapper.sh
deleted file mode 100755
index 8c9c111..0000000
--- a/cc/private/toolchain/osx_cc_wrapper.sh
+++ /dev/null
@@ -1,116 +0,0 @@
-#!/bin/bash
-#
-# Copyright 2015 The Bazel Authors. All rights reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-#    http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-# OS X relpath is not really working. This is a wrapper script around gcc
-# to simulate relpath behavior.
-#
-# This wrapper uses install_name_tool to replace all paths in the binary
-# (bazel-out/.../path/to/original/library.so) by the paths relative to
-# the binary. It parses the command line to behave as rpath is supposed
-# to work.
-#
-# See https://blogs.oracle.com/dipol/entry/dynamic_libraries_rpath_and_mac
-# on how to set those paths for Mach-O binaries.
-#
-set -eu
-
-GCC=/usr/bin/gcc
-INSTALL_NAME_TOOL="/usr/bin/install_name_tool"
-
-LIBS=
-LIB_DIRS=
-RPATHS=
-OUTPUT=
-
-function parse_option() {
-    local -r opt="$1"
-    if [[ "${OUTPUT}" = "1" ]]; then
-        OUTPUT=$opt
-    elif [[ "$opt" =~ ^-l(.*)$ ]]; then
-        LIBS="${BASH_REMATCH[1]} $LIBS"
-    elif [[ "$opt" =~ ^-L(.*)$ ]]; then
-        LIB_DIRS="${BASH_REMATCH[1]} $LIB_DIRS"
-    elif [[ "$opt" =~ ^-Wl,-rpath,\@loader_path/(.*)$ ]]; then
-        RPATHS="${BASH_REMATCH[1]} ${RPATHS}"
-    elif [[ "$opt" = "-o" ]]; then
-        # output is coming
-        OUTPUT=1
-    fi
-}
-
-# let parse the option list
-for i in "$@"; do
-    if [[ "$i" = @* ]]; then
-        while IFS= read -r opt
-        do
-            parse_option "$opt"
-        done < "${i:1}" || exit 1
-    else
-        parse_option "$i"
-    fi
-done
-
-# Call gcc
-${GCC} "$@"
-
-function get_library_path() {
-    for libdir in ${LIB_DIRS}; do
-        if [ -f ${libdir}/lib$1.so ]; then
-            echo "${libdir}/lib$1.so"
-        elif [ -f ${libdir}/lib$1.dylib ]; then
-            echo "${libdir}/lib$1.dylib"
-        fi
-    done
-}
-
-# A convenient method to return the actual path even for non symlinks
-# and multi-level symlinks.
-function get_realpath() {
-    local previous="$1"
-    local next=$(readlink "${previous}")
-    while [ -n "${next}" ]; do
-        previous="${next}"
-        next=$(readlink "${previous}")
-    done
-    echo "${previous}"
-}
-
-# Get the path of a lib inside a tool
-function get_otool_path() {
-    # the lib path is the path of the original lib relative to the workspace
-    get_realpath $1 | sed 's|^.*/bazel-out/|bazel-out/|'
-}
-
-# Do replacements in the output
-for rpath in ${RPATHS}; do
-    for lib in ${LIBS}; do
-        unset libname
-        if [ -f "$(dirname ${OUTPUT})/${rpath}/lib${lib}.so" ]; then
-            libname="lib${lib}.so"
-        elif [ -f "$(dirname ${OUTPUT})/${rpath}/lib${lib}.dylib" ]; then
-            libname="lib${lib}.dylib"
-        fi
-        # ${libname-} --> return $libname if defined, or undefined otherwise. This is to make
-        # this set -e friendly
-        if [[ -n "${libname-}" ]]; then
-            libpath=$(get_library_path ${lib})
-            if [ -n "${libpath}" ]; then
-                ${INSTALL_NAME_TOOL} -change $(get_otool_path "${libpath}") \
-                    "@loader_path/${rpath}/${libname}" "${OUTPUT}"
-            fi
-        fi
-    done
-done