From f1f24fc4b5aa83b7a4b872ec8f1c3a369799c081 Mon Sep 17 00:00:00 2001 From: Damien Martin-Guillerez Date: Wed, 4 May 2016 09:32:30 +0000 Subject: [PATCH] Ship the environment fixed at configure time to the C++ compiler Use a wrapper script on all platform to ship the environment to the C++ compiler. This should enable building with Homebrew special setup and will likely reduce the number of hard corner cases to solve. Should unblock #1177. -- Change-Id: I36f09edaf131f65c730bdd626ce521478ff587c7 Reviewed-on: https://bazel-review.googlesource.com/3560 MOS_MIGRATED_REVID=121462120 --- tools/cpp/cc_configure.bzl | 23 ++++++++++++++++------- tools/cpp/linux_cc_wrapper.sh.tpl | 25 +++++++++++++++++++++++++ tools/cpp/osx_cc_wrapper.sh.tpl | 3 +++ 3 files changed, 44 insertions(+), 7 deletions(-) create mode 100644 tools/cpp/linux_cc_wrapper.sh.tpl diff --git a/tools/cpp/cc_configure.bzl b/tools/cpp/cc_configure.bzl index 1250d2357db475..ff0f20efefbb23 100644 --- a/tools/cpp/cc_configure.bzl +++ b/tools/cpp/cc_configure.bzl @@ -50,7 +50,7 @@ def _which(repository_ctx, cmd, default): return default if result == None else str(result) -def _get_tool_paths(repository_ctx, darwin, cc): +def _get_tool_paths(repository_ctx, darwin): """Compute the path to the various tools.""" return {k: _which(repository_ctx, k, "/usr/bin/" + k) for k in [ @@ -63,7 +63,7 @@ def _get_tool_paths(repository_ctx, darwin, cc): "objdump", "strip", ]} + { - "gcc": cc, + "gcc": "cc_wrapper.sh", "ar": "/usr/bin/libtool" if darwin else _which(repository_ctx, "ar", "/usr/bin/ar") } @@ -289,13 +289,20 @@ def _find_cc(repository_ctx): return cc -def _tpl(repository_ctx, tpl, substitutions={}): +def _tpl(repository_ctx, tpl, substitutions={}, out=None): + if not out: + out = tpl repository_ctx.template( - tpl, + out, Label("@bazel_tools//tools/cpp:%s.tpl" % tpl), substitutions) +def _get_env(repository_ctx): + """Convert the environment in a list of export.""" + env = repository_ctx.os.environ + return "\n".join(["export %s='%s'" % (k, env[k]) for k in env]) + def _impl(repository_ctx): repository_ctx.file("tools/cpp/empty.cc") cpu_value = _get_cpu_value(repository_ctx) @@ -311,9 +318,8 @@ def _impl(repository_ctx): else: darwin = cpu_value == "darwin" cc = _find_cc(repository_ctx) - crosstool_cc = "osx_cc_wrapper.sh" if darwin else str(cc) darwin = cpu_value == "darwin" - tool_paths = _get_tool_paths(repository_ctx, darwin, crosstool_cc) + tool_paths = _get_tool_paths(repository_ctx, darwin) crosstool_content = _crosstool_content(repository_ctx, cc, cpu_value, darwin) opt_content = _opt_content(darwin) dbg_content = _dbg_content() @@ -321,7 +327,10 @@ def _impl(repository_ctx): "%{name}": cpu_value, "%{supports_param_files}": "0" if darwin else "1" }) - _tpl(repository_ctx, "osx_cc_wrapper.sh", {"%{cc}": str(cc)}) + _tpl(repository_ctx, + "osx_cc_wrapper.sh" if darwin else "linux_cc_wrapper.sh", + {"%{cc}": str(cc), "%{env}": _get_env(repository_ctx)}, + "cc_wrapper.sh") _tpl(repository_ctx, "CROSSTOOL", { "%{cpu}": cpu_value, "%{content}": _build_crosstool(crosstool_content) + "\n" + diff --git a/tools/cpp/linux_cc_wrapper.sh.tpl b/tools/cpp/linux_cc_wrapper.sh.tpl new file mode 100644 index 00000000000000..a83be50655c3d9 --- /dev/null +++ b/tools/cpp/linux_cc_wrapper.sh.tpl @@ -0,0 +1,25 @@ +#!/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. +# +# Ship the environment to the C++ action +# +set -eu + +# Set-up the environment +%{env} + +# Call the C++ compiler +%{cc} "$@" diff --git a/tools/cpp/osx_cc_wrapper.sh.tpl b/tools/cpp/osx_cc_wrapper.sh.tpl index 5e66680f6a7215..5ea4b52c1b5e69 100644 --- a/tools/cpp/osx_cc_wrapper.sh.tpl +++ b/tools/cpp/osx_cc_wrapper.sh.tpl @@ -52,6 +52,9 @@ for i in "$@"; do fi done +# Set-up the environment +%{env} + # Call the C++ compiler %{cc} "$@"