RobotLocomotion / drake

Model-based design and verification for robotics.
https://drake.mit.edu
Other
3.35k stars 1.27k forks source link

CLion on macOS no longer works #9347

Closed jwnimmer-tri closed 6 years ago

jwnimmer-tri commented 6 years ago

Discussed at length in slack; https://drakedevelopers.slack.com/messages/C36L0D7D4/convo/C36L0D7D4-1535410503.000100/. Exploratory testing looks like #8881 is what broke it. The default macOS toolchain is thought to not be a C toolchain in the IDE.

jwnimmer-tri commented 6 years ago

See also bazelbuild/intellij#109 and bazelbuild/intellij#397.

jwnimmer-tri commented 6 years ago

\CC @RussTedrake

jwnimmer-tri commented 6 years ago

The commit on https://github.com/jwnimmer-tri/drake/commits/macos-clion-workaround appears to work around the problem for now. Users have to merge it into their local copy, but not commit it to master.

Edit: I have since removed that commit, but here is the patch for the record:

From daca23091e40bde66bd89f8f206894513fdc7ed6 Mon Sep 17 00:00:00 2001
From: Jeremy Nimmer <jeremy.nimmer@tri.global>
Date: Fri, 31 Aug 2018 16:53:25 -0400
Subject: [PATCH] Revert portions of "Use default autogenerated toolchain"

Revert "Remove CROSSTOOL"

This reverts commit a2618a504ebd6e061b3fd14d5e4f59c7aa6dee0f.

Revert "Move C++14 flags from CROSSTOOL to bazel.rc"

This reverts commit 51a4ee89c36cde25fb74d98263125a5dee2b0c61.

Revert "Use cpp constraint to identify compiler family"

This reverts commit 3f0e6dc58b2296395302b06c3461787ca36e8dea.
---
 third_party/BUILD.bazel                       |    9 +
 .../com_github_bazelbuild_bazel/LICENSE       |  202 +++
 .../tools/cpp/osx_cc_wrapper.sh               |  103 ++
 .../com_github_bazelbuild_bazel/vendor.txt    |    6 +
 tools/cc_toolchain/BUILD                      |    1 +
 tools/cc_toolchain/BUILD.bazel                |   73 +-
 tools/cc_toolchain/CROSSTOOL                  | 1372 +++++++++++++++++
 tools/cc_toolchain/bazel.rc                   |    8 +-
 tools/cc_toolchain/osx_ar_wrapper.sh          |    7 +
 tools/cc_toolchain/osx_cc_wrapper.sh          |    2 +
 10 files changed, 1773 insertions(+), 10 deletions(-)
 create mode 100644 third_party/com_github_bazelbuild_bazel/LICENSE
 create mode 100755 third_party/com_github_bazelbuild_bazel/tools/cpp/osx_cc_wrapper.sh
 create mode 100644 third_party/com_github_bazelbuild_bazel/vendor.txt
 create mode 120000 tools/cc_toolchain/BUILD
 create mode 100644 tools/cc_toolchain/CROSSTOOL
 create mode 100755 tools/cc_toolchain/osx_ar_wrapper.sh
 create mode 100755 tools/cc_toolchain/osx_cc_wrapper.sh

diff --git a/third_party/BUILD.bazel b/third_party/BUILD.bazel
index eacea904184..c2780d8f190 100644
--- a/third_party/BUILD.bazel
+++ b/third_party/BUILD.bazel
@@ -4,6 +4,15 @@ load("//tools/lint:lint.bzl", "add_lint_tests")

 package(default_visibility = ["//visibility:public"])

+exports_files(
+    ["com_github_bazelbuild_bazel/tools/cpp/osx_cc_wrapper.sh"],
+    licenses = ["notice"],  # Apache-2.0
+    visibility = [
+        "//common:__pkg__",
+        "//tools/cc_toolchain:__pkg__",
+    ],
+)
+
 exports_files(
     ["net_sf_jchart2d/LICENSE"],
     licenses = ["restricted"],  # LGPL-3.0+
diff --git a/third_party/com_github_bazelbuild_bazel/LICENSE b/third_party/com_github_bazelbuild_bazel/LICENSE
new file mode 100644
index 00000000000..7a4a3ea2424
--- /dev/null
+++ b/third_party/com_github_bazelbuild_bazel/LICENSE
@@ -0,0 +1,202 @@
+
+                                 Apache License
+                           Version 2.0, January 2004
+                        http://www.apache.org/licenses/
+
+   TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+   1. Definitions.
+
+      "License" shall mean the terms and conditions for use, reproduction,
+      and distribution as defined by Sections 1 through 9 of this document.
+
+      "Licensor" shall mean the copyright owner or entity authorized by
+      the copyright owner that is granting the License.
+
+      "Legal Entity" shall mean the union of the acting entity and all
+      other entities that control, are controlled by, or are under common
+      control with that entity. For the purposes of this definition,
+      "control" means (i) the power, direct or indirect, to cause the
+      direction or management of such entity, whether by contract or
+      otherwise, or (ii) ownership of fifty percent (50%) or more of the
+      outstanding shares, or (iii) beneficial ownership of such entity.
+
+      "You" (or "Your") shall mean an individual or Legal Entity
+      exercising permissions granted by this License.
+
+      "Source" form shall mean the preferred form for making modifications,
+      including but not limited to software source code, documentation
+      source, and configuration files.
+
+      "Object" form shall mean any form resulting from mechanical
+      transformation or translation of a Source form, including but
+      not limited to compiled object code, generated documentation,
+      and conversions to other media types.
+
+      "Work" shall mean the work of authorship, whether in Source or
+      Object form, made available under the License, as indicated by a
+      copyright notice that is included in or attached to the work
+      (an example is provided in the Appendix below).
+
+      "Derivative Works" shall mean any work, whether in Source or Object
+      form, that is based on (or derived from) the Work and for which the
+      editorial revisions, annotations, elaborations, or other modifications
+      represent, as a whole, an original work of authorship. For the purposes
+      of this License, Derivative Works shall not include works that remain
+      separable from, or merely link (or bind by name) to the interfaces of,
+      the Work and Derivative Works thereof.
+
+      "Contribution" shall mean any work of authorship, including
+      the original version of the Work and any modifications or additions
+      to that Work or Derivative Works thereof, that is intentionally
+      submitted to Licensor for inclusion in the Work by the copyright owner
+      or by an individual or Legal Entity authorized to submit on behalf of
+      the copyright owner. For the purposes of this definition, "submitted"
+      means any form of electronic, verbal, or written communication sent
+      to the Licensor or its representatives, including but not limited to
+      communication on electronic mailing lists, source code control systems,
+      and issue tracking systems that are managed by, or on behalf of, the
+      Licensor for the purpose of discussing and improving the Work, but
+      excluding communication that is conspicuously marked or otherwise
+      designated in writing by the copyright owner as "Not a Contribution."
+
+      "Contributor" shall mean Licensor and any individual or Legal Entity
+      on behalf of whom a Contribution has been received by Licensor and
+      subsequently incorporated within the Work.
+
+   2. Grant of Copyright License. Subject to the terms and conditions of
+      this License, each Contributor hereby grants to You a perpetual,
+      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+      copyright license to reproduce, prepare Derivative Works of,
+      publicly display, publicly perform, sublicense, and distribute the
+      Work and such Derivative Works in Source or Object form.
+
+   3. Grant of Patent License. Subject to the terms and conditions of
+      this License, each Contributor hereby grants to You a perpetual,
+      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+      (except as stated in this section) patent license to make, have made,
+      use, offer to sell, sell, import, and otherwise transfer the Work,
+      where such license applies only to those patent claims licensable
+      by such Contributor that are necessarily infringed by their
+      Contribution(s) alone or by combination of their Contribution(s)
+      with the Work to which such Contribution(s) was submitted. If You
+      institute patent litigation against any entity (including a
+      cross-claim or counterclaim in a lawsuit) alleging that the Work
+      or a Contribution incorporated within the Work constitutes direct
+      or contributory patent infringement, then any patent licenses
+      granted to You under this License for that Work shall terminate
+      as of the date such litigation is filed.
+
+   4. Redistribution. You may reproduce and distribute copies of the
+      Work or Derivative Works thereof in any medium, with or without
+      modifications, and in Source or Object form, provided that You
+      meet the following conditions:
+
+      (a) You must give any other recipients of the Work or
+          Derivative Works a copy of this License; and
+
+      (b) You must cause any modified files to carry prominent notices
+          stating that You changed the files; and
+
+      (c) You must retain, in the Source form of any Derivative Works
+          that You distribute, all copyright, patent, trademark, and
+          attribution notices from the Source form of the Work,
+          excluding those notices that do not pertain to any part of
+          the Derivative Works; and
+
+      (d) If the Work includes a "NOTICE" text file as part of its
+          distribution, then any Derivative Works that You distribute must
+          include a readable copy of the attribution notices contained
+          within such NOTICE file, excluding those notices that do not
+          pertain to any part of the Derivative Works, in at least one
+          of the following places: within a NOTICE text file distributed
+          as part of the Derivative Works; within the Source form or
+          documentation, if provided along with the Derivative Works; or,
+          within a display generated by the Derivative Works, if and
+          wherever such third-party notices normally appear. The contents
+          of the NOTICE file are for informational purposes only and
+          do not modify the License. You may add Your own attribution
+          notices within Derivative Works that You distribute, alongside
+          or as an addendum to the NOTICE text from the Work, provided
+          that such additional attribution notices cannot be construed
+          as modifying the License.
+
+      You may add Your own copyright statement to Your modifications and
+      may provide additional or different license terms and conditions
+      for use, reproduction, or distribution of Your modifications, or
+      for any such Derivative Works as a whole, provided Your use,
+      reproduction, and distribution of the Work otherwise complies with
+      the conditions stated in this License.
+
+   5. Submission of Contributions. Unless You explicitly state otherwise,
+      any Contribution intentionally submitted for inclusion in the Work
+      by You to the Licensor shall be under the terms and conditions of
+      this License, without any additional terms or conditions.
+      Notwithstanding the above, nothing herein shall supersede or modify
+      the terms of any separate license agreement you may have executed
+      with Licensor regarding such Contributions.
+
+   6. Trademarks. This License does not grant permission to use the trade
+      names, trademarks, service marks, or product names of the Licensor,
+      except as required for reasonable and customary use in describing the
+      origin of the Work and reproducing the content of the NOTICE file.
+
+   7. Disclaimer of Warranty. Unless required by applicable law or
+      agreed to in writing, Licensor provides the Work (and each
+      Contributor provides its Contributions) on an "AS IS" BASIS,
+      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+      implied, including, without limitation, any warranties or conditions
+      of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+      PARTICULAR PURPOSE. You are solely responsible for determining the
+      appropriateness of using or redistributing the Work and assume any
+      risks associated with Your exercise of permissions under this License.
+
+   8. Limitation of Liability. In no event and under no legal theory,
+      whether in tort (including negligence), contract, or otherwise,
+      unless required by applicable law (such as deliberate and grossly
+      negligent acts) or agreed to in writing, shall any Contributor be
+      liable to You for damages, including any direct, indirect, special,
+      incidental, or consequential damages of any character arising as a
+      result of this License or out of the use or inability to use the
+      Work (including but not limited to damages for loss of goodwill,
+      work stoppage, computer failure or malfunction, or any and all
+      other commercial damages or losses), even if such Contributor
+      has been advised of the possibility of such damages.
+
+   9. Accepting Warranty or Additional Liability. While redistributing
+      the Work or Derivative Works thereof, You may choose to offer,
+      and charge a fee for, acceptance of support, warranty, indemnity,
+      or other liability obligations and/or rights consistent with this
+      License. However, in accepting such obligations, You may act only
+      on Your own behalf and on Your sole responsibility, not on behalf
+      of any other Contributor, and only if You agree to indemnify,
+      defend, and hold each Contributor harmless for any liability
+      incurred by, or claims asserted against, such Contributor by reason
+      of your accepting any such warranty or additional liability.
+
+   END OF TERMS AND CONDITIONS
+
+   APPENDIX: How to apply the Apache License to your work.
+
+      To apply the Apache License to your work, attach the following
+      boilerplate notice, with the fields enclosed by brackets "[]"
+      replaced with your own identifying information. (Don't include
+      the brackets!)  The text should be enclosed in the appropriate
+      comment syntax for the file format. We also recommend that a
+      file or class name and description of purpose be included on the
+      same "printed page" as the copyright notice for easier
+      identification within third-party archives.
+
+   Copyright [yyyy] [name of copyright owner]
+
+   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.
\ No newline at end of file
diff --git a/third_party/com_github_bazelbuild_bazel/tools/cpp/osx_cc_wrapper.sh b/third_party/com_github_bazelbuild_bazel/tools/cpp/osx_cc_wrapper.sh
new file mode 100755
index 00000000000..bbb5d7e5390
--- /dev/null
+++ b/third_party/com_github_bazelbuild_bazel/tools/cpp/osx_cc_wrapper.sh
@@ -0,0 +1,103 @@
+#!/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=
+# let parse the option list
+for i in "$@"; do
+    if [[ "${OUTPUT}" = "1" ]]; then
+        OUTPUT=$i
+    elif [[ "$i" =~ ^-l(.*)$ ]]; then
+        LIBS="${BASH_REMATCH[1]} $LIBS"
+    elif [[ "$i" =~ ^-L(.*)$ ]]; then
+        LIB_DIRS="${BASH_REMATCH[1]} $LIB_DIRS"
+    elif [[ "$i" =~ ^-Wl,-rpath,\@loader_path/(.*)$ ]]; then
+        RPATHS="${BASH_REMATCH[1]} ${RPATHS}"
+    elif [[ "$i" = "-o" ]]; then
+        # output is coming
+        OUTPUT=1
+    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
diff --git a/third_party/com_github_bazelbuild_bazel/vendor.txt b/third_party/com_github_bazelbuild_bazel/vendor.txt
new file mode 100644
index 00000000000..8184ca16d6a
--- /dev/null
+++ b/third_party/com_github_bazelbuild_bazel/vendor.txt
@@ -0,0 +1,6 @@
+The files in this directory tree (other than this file) are derived from Bazel:
+
+  remote = "https://github.com/bazelbuild/bazel.git"
+  sha = "f98a7a2fedb3e714cef1038dcb85f83731150246"
+
+Please update this metadata when copying new versions from upstream.
diff --git a/tools/cc_toolchain/BUILD b/tools/cc_toolchain/BUILD
new file mode 120000
index 00000000000..a1f39501865
--- /dev/null
+++ b/tools/cc_toolchain/BUILD
@@ -0,0 +1 @@
+BUILD.bazel
\ No newline at end of file
diff --git a/tools/cc_toolchain/BUILD.bazel b/tools/cc_toolchain/BUILD.bazel
index 734f1e8ba30..f921ba20e33 100644
--- a/tools/cc_toolchain/BUILD.bazel
+++ b/tools/cc_toolchain/BUILD.bazel
@@ -4,6 +4,72 @@ load("//tools/lint:lint.bzl", "add_lint_tests")

 package(default_visibility = ["//visibility:public"])

+# The default toolchain selector for Drake.
+cc_toolchain_suite(
+    name = "default-toolchain",
+    toolchains = {
+        "darwin_x86_64|compiler": "cc_toolchain_apple",
+    },
+)
+
+filegroup(
+    name = "empty",
+    srcs = [],
+    visibility = ["//visibility:private"],
+)
+
+# This filegroup contains the files on which every toolchain component should
+# always depend, regardless of platform. It is a useful place for the outputs
+# of rules that check preconditions for the entire build.  (At the moment, we
+# do not have any such files, although we once did.)
+filegroup(
+    name = "universal_toolchain_deps",
+    srcs = [
+        "//tools/dynamic_analysis:sanitizer_blacklists",
+    ],
+    visibility = ["//visibility:private"],
+)
+
+# A compiler wrapper script that adjusts linker paths on macOS.
+filegroup(
+    name = "osx_cc_wrapper",
+    srcs = [
+        "osx_cc_wrapper.sh",
+        "//third_party:com_github_bazelbuild_bazel/tools/cpp/osx_cc_wrapper.sh",  # noqa
+    ],
+    visibility = ["//visibility:private"],
+)
+
+filegroup(
+    name = "osx_ar_wrapper",
+    srcs = ["osx_ar_wrapper.sh"],
+    visibility = ["//visibility:private"],
+)
+
+filegroup(
+    name = "apple_toolchain_deps",
+    srcs = [
+        ":osx_ar_wrapper",
+        ":osx_cc_wrapper",
+        ":universal_toolchain_deps",
+    ],
+    visibility = ["//visibility:private"],
+)
+
+cc_toolchain(
+    name = "cc_toolchain_apple",
+    all_files = ":apple_toolchain_deps",
+    compiler_files = ":apple_toolchain_deps",
+    cpu = "darwin_x86_64",
+    dwp_files = ":apple_toolchain_deps",
+    dynamic_runtime_libs = [":empty"],
+    linker_files = ":apple_toolchain_deps",
+    objcopy_files = ":apple_toolchain_deps",
+    static_runtime_libs = [":empty"],
+    strip_files = ":apple_toolchain_deps",
+    supports_param_files = 0,
+)
+
 config_setting(
     name = "apple",
     constraint_values = ["@bazel_tools//platforms:osx"],
@@ -27,10 +93,9 @@ config_setting(

 filegroup(
     name = "toolchain_deps",
-    data = select({
-        ":apple": ["@local_config_cc//:cc_wrapper"],
-        "//conditions:default": [],
-    }),
+    data = [
+        ":apple_toolchain_deps",
+    ],
     visibility = ["//common:__pkg__"],
 )

diff --git a/tools/cc_toolchain/CROSSTOOL b/tools/cc_toolchain/CROSSTOOL
new file mode 100644
index 00000000000..3485b90731d
--- /dev/null
+++ b/tools/cc_toolchain/CROSSTOOL
@@ -0,0 +1,1372 @@
+# This CROSSTOOL was produced by copying the Bazel auto-generated CROSSTOOL for
+# our supported platforms, and then making some Drake-specific tweaks.
+#
+# To extract the Bazel auto-generated toolchain stanza for a given platform:
+# - Run bazel build --crosstool_top=//external:cc_toolchain ...
+# - Inspect ./bazel-drake/external/local_config_cc/CROSSTOOL
+# - Find the default_toolchain stanza for the cpu (k8 on Linux, darwin on OS X)
+# - Find the toolchain stanza with a target_cpu and toolchain_identifer that
+#   match the default_toolchain values.
+
+major_version: "local"
+minor_version: ""
+default_target_cpu: "same_as_host"
+
+default_toolchain {
+  cpu: "darwin"
+  toolchain_identifier: "darwin_x86_64"
+}
+
+default_toolchain {
+  cpu: "darwin_x86_64"
+  toolchain_identifier: "darwin_x86_64"
+}
+
+# We use Clang on Ubuntu by default. To use gcc, specify --compiler=gcc-5.
+default_toolchain {
+  cpu: "k8"
+  toolchain_identifier: "clang-4.0-linux"
+}
+
+# GCC 6.x on Linux
+toolchain {
+  toolchain_identifier: "gcc-6-linux"
+  abi_libc_version: "local"
+  abi_version: "local"
+  builtin_sysroot: ""
+  compiler: "gcc-6" # Modified for Drake.
+  compiler_flag: "-U_FORTIFY_SOURCE"
+  compiler_flag: "-D_FORTIFY_SOURCE=1"
+  compiler_flag: "-fstack-protector"
+  compiler_flag: "-Wall"
+  compiler_flag: "-Wl,-z,-relro,-z,now"
+  compiler_flag: "-B/usr/bin"
+  compiler_flag: "-B/usr/bin"
+  compiler_flag: "-Wunused-but-set-parameter"
+  compiler_flag: "-Wno-free-nonheap-object"
+  compiler_flag: "-fno-canonical-system-headers"
+  compiler_flag: "-fno-omit-frame-pointer"
+  cxx_builtin_include_directory: "/usr/include/c++/6"
+  cxx_builtin_include_directory: "/usr/include/x86_64-linux-gnu/c++/6"
+  cxx_builtin_include_directory: "/usr/include/c++/6/backward"
+  cxx_builtin_include_directory: "/usr/lib/gcc/x86_64-linux-gnu/6/include"
+  cxx_builtin_include_directory: "/usr/local/include"
+  cxx_builtin_include_directory: "/usr/lib/gcc/x86_64-linux-gnu/6/include-fixed"
+  cxx_builtin_include_directory: "/usr/include/x86_64-linux-gnu"
+  cxx_builtin_include_directory: "/usr/include"
+  cxx_flag: "-std=c++1y" # Modified for Drake.
+  host_system_name: "local"
+  linker_flag: "-lstdc++"
+  linker_flag: "-lm"
+  linker_flag: "-Wl,-no-as-needed"
+  linker_flag: "-B/usr/bin"
+  linker_flag: "-B/usr/bin"
+  linker_flag: "-pass-exit-codes"
+  linker_flag: "-Wl,--build-id=md5"
+  linker_flag: "-Wl,--hash-style=gnu"
+  needsPic: true
+  objcopy_embed_flag: "-I"
+  objcopy_embed_flag: "binary"
+  supports_fission: false
+  supports_gold_linker: false
+  supports_incremental_linker: false
+  supports_interface_shared_objects: false
+  supports_normalizing_ar: false
+  supports_start_end_lib: false
+  target_cpu: "k8"
+  target_libc: "local"
+  target_system_name: "local"
+  unfiltered_cxx_flag: "-fno-canonical-system-headers"
+  unfiltered_cxx_flag: "-Wno-builtin-macro-redefined"
+  unfiltered_cxx_flag: "-D__DATE__=\"redacted\""
+  unfiltered_cxx_flag: "-D__TIMESTAMP__=\"redacted\""
+  unfiltered_cxx_flag: "-D__TIME__=\"redacted\""
+  tool_path {name: "ar" path: "/usr/bin/ar" }
+  tool_path {name: "cpp" path: "/usr/bin/cpp" }
+  tool_path {name: "dwp" path: "/usr/bin/dwp" }
+  tool_path {name: "gcc" path: "/usr/bin/gcc-6" }
+  tool_path {name: "gcov" path: "/usr/bin/gcov" }
+  tool_path {name: "ld" path: "/usr/bin/ld" }
+  tool_path {name: "nm" path: "/usr/bin/nm" }
+  tool_path {name: "objcopy" path: "/usr/bin/objcopy" }
+  tool_path {name: "objdump" path: "/usr/bin/objdump" }
+  tool_path {name: "strip" path: "/usr/bin/strip" }
+
+  compilation_mode_flags {
+    mode: DBG
+    compiler_flag: "-g"
+  }
+  compilation_mode_flags {
+    mode: OPT
+    compiler_flag: "-g0"
+    compiler_flag: "-O2"
+    compiler_flag: "-DNDEBUG"
+    compiler_flag: "-ffunction-sections"
+    compiler_flag: "-fdata-sections"
+    linker_flag: "-Wl,--gc-sections"
+  }
+  linking_mode_flags { mode: DYNAMIC }
+}
+
+# GCC 5.x on Linux
+toolchain {
+  toolchain_identifier: "gcc-5-linux"
+  abi_libc_version: "local"
+  abi_version: "local"
+  builtin_sysroot: ""
+  compiler: "gcc-5" # Modified for Drake.
+  compiler_flag: "-U_FORTIFY_SOURCE"
+  compiler_flag: "-D_FORTIFY_SOURCE=1"
+  compiler_flag: "-fstack-protector"
+  compiler_flag: "-Wall"
+  compiler_flag: "-Wl,-z,-relro,-z,now"
+  compiler_flag: "-B/usr/bin"
+  compiler_flag: "-B/usr/bin"
+  compiler_flag: "-Wunused-but-set-parameter"
+  compiler_flag: "-Wno-free-nonheap-object"
+  compiler_flag: "-fno-canonical-system-headers"
+  compiler_flag: "-fno-omit-frame-pointer"
+  cxx_builtin_include_directory: "/usr/include/c++/5"
+  cxx_builtin_include_directory: "/usr/include/x86_64-linux-gnu/c++/5"
+  cxx_builtin_include_directory: "/usr/include/c++/5/backward"
+  cxx_builtin_include_directory: "/usr/lib/gcc/x86_64-linux-gnu/5/include"
+  cxx_builtin_include_directory: "/usr/local/include"
+  cxx_builtin_include_directory: "/usr/lib/gcc/x86_64-linux-gnu/5/include-fixed"
+  cxx_builtin_include_directory: "/usr/include/x86_64-linux-gnu"
+  cxx_builtin_include_directory: "/usr/include"
+  cxx_flag: "-std=c++1y" # Modified for Drake.
+  host_system_name: "local"
+  linker_flag: "-lstdc++"
+  linker_flag: "-lm"
+  linker_flag: "-Wl,-no-as-needed"
+  linker_flag: "-B/usr/bin"
+  linker_flag: "-B/usr/bin"
+  linker_flag: "-pass-exit-codes"
+  linker_flag: "-Wl,--build-id=md5"
+  linker_flag: "-Wl,--hash-style=gnu"
+  needsPic: true
+  objcopy_embed_flag: "-I"
+  objcopy_embed_flag: "binary"
+  supports_fission: false
+  supports_gold_linker: false
+  supports_incremental_linker: false
+  supports_interface_shared_objects: false
+  supports_normalizing_ar: false
+  supports_start_end_lib: false
+  target_cpu: "k8"
+  target_libc: "local"
+  target_system_name: "local"
+  unfiltered_cxx_flag: "-fno-canonical-system-headers"
+  unfiltered_cxx_flag: "-Wno-builtin-macro-redefined"
+  unfiltered_cxx_flag: "-D__DATE__=\"redacted\""
+  unfiltered_cxx_flag: "-D__TIMESTAMP__=\"redacted\""
+  unfiltered_cxx_flag: "-D__TIME__=\"redacted\""
+  tool_path {name: "ar" path: "/usr/bin/ar" }
+  tool_path {name: "cpp" path: "/usr/bin/cpp" }
+  tool_path {name: "dwp" path: "/usr/bin/dwp" }
+  tool_path {name: "gcc" path: "/usr/bin/gcc-5" }
+  tool_path {name: "gcov" path: "/usr/bin/gcov" }
+  tool_path {name: "ld" path: "/usr/bin/ld" }
+  tool_path {name: "nm" path: "/usr/bin/nm" }
+  tool_path {name: "objcopy" path: "/usr/bin/objcopy" }
+  tool_path {name: "objdump" path: "/usr/bin/objdump" }
+  tool_path {name: "strip" path: "/usr/bin/strip" }
+
+  compilation_mode_flags {
+    mode: DBG
+    compiler_flag: "-g"
+  }
+  compilation_mode_flags {
+    mode: OPT
+    compiler_flag: "-g0"
+    compiler_flag: "-O2"
+    compiler_flag: "-DNDEBUG"
+    compiler_flag: "-ffunction-sections"
+    compiler_flag: "-fdata-sections"
+    linker_flag: "-Wl,--gc-sections"
+  }
+  linking_mode_flags { mode: DYNAMIC }
+}
+
+# Clang 4.0 on Linux
+toolchain {
+  toolchain_identifier: "clang-4.0-linux"
+  abi_libc_version: "local"
+  abi_version: "local"
+  builtin_sysroot: ""
+  compiler: "clang-4.0"
+  compiler_flag: "-U_FORTIFY_SOURCE"
+  compiler_flag: "-D_FORTIFY_SOURCE=1"
+  compiler_flag: "-fstack-protector"
+  compiler_flag: "-Wall"
+  compiler_flag: "-B/usr/bin"
+  compiler_flag: "-B/usr/bin"
+  compiler_flag: "-fcolor-diagnostics"
+  compiler_flag: "-fno-omit-frame-pointer"
+  cxx_builtin_include_directory: "/usr/include/c++/5.4.0"
+  cxx_builtin_include_directory: "/usr/include/x86_64-linux-gnu/c++/5.4.0"
+  cxx_builtin_include_directory: "/usr/include/c++/5.4.0/backward"
+  cxx_builtin_include_directory: "/usr/local/include"
+  cxx_builtin_include_directory: "/usr/include/clang/4.0.0/include"
+  cxx_builtin_include_directory: "/usr/include/clang/4.0.1/include"
+  cxx_builtin_include_directory: "/usr/include/x86_64-linux-gnu"
+  cxx_builtin_include_directory: "/usr/include"
+  # Next two lines are a Drake fix for https://github.com/bazelbuild/bazel/issues/3977.
+  cxx_builtin_include_directory: "/usr/lib/llvm-4.0/lib/clang/4.0.1/include"
+  cxx_builtin_include_directory: "/usr/lib/llvm-4.0/lib/clang/4.0.0/include"
+  cxx_flag: "-std=c++1y" # Modified for Drake.
+  host_system_name: "local"
+  linker_flag: "-lstdc++"
+  linker_flag: "-lm"
+  linker_flag: "-fuse-ld=gold"
+  linker_flag: "-B/usr/bin"
+  linker_flag: "-B/usr/bin"
+  needsPic: true
+  objcopy_embed_flag: "-I"
+  objcopy_embed_flag: "binary"
+  supports_fission: false
+  supports_gold_linker: true
+  supports_incremental_linker: false
+  supports_interface_shared_objects: false
+  supports_normalizing_ar: false
+  supports_start_end_lib: true
+  target_cpu: "k8"
+  target_libc: "local"
+  target_system_name: "local"
+  unfiltered_cxx_flag: "-Wno-builtin-macro-redefined"
+  unfiltered_cxx_flag: "-D__DATE__=\"redacted\""
+  unfiltered_cxx_flag: "-D__TIMESTAMP__=\"redacted\""
+  unfiltered_cxx_flag: "-D__TIME__=\"redacted\""
+  tool_path {name: "ar" path: "/usr/bin/ar" }
+  tool_path {name: "cpp" path: "/usr/bin/cpp" }
+  tool_path {name: "dwp" path: "/usr/bin/dwp" }
+  tool_path {name: "gcc" path: "/usr/bin/clang-4.0" }
+  tool_path {name: "gcov" path: "/usr/bin/gcov" }
+  tool_path {name: "ld" path: "/usr/bin/ld" }
+  tool_path {name: "nm" path: "/usr/bin/nm" }
+  tool_path {name: "objcopy" path: "/usr/bin/objcopy" }
+  tool_path {name: "objdump" path: "/usr/bin/objdump" }
+  tool_path {name: "strip" path: "/usr/bin/strip" }
+
+  compilation_mode_flags {
+    mode: DBG
+    compiler_flag: "-g"
+  }
+  compilation_mode_flags {
+    mode: OPT
+    compiler_flag: "-g0"
+    compiler_flag: "-O2"
+    compiler_flag: "-DNDEBUG"
+    compiler_flag: "-ffunction-sections"
+    compiler_flag: "-fdata-sections"
+    linker_flag: "-Wl,--gc-sections"
+  }
+  linking_mode_flags { mode: DYNAMIC }
+}
+
+# Clang on macOS. To update, extract the C/C++ parts (https://git.io/vNCSg) of
+# the configured version of CROSSTOOL.tpl (https://git.io/vNCSb) which is
+# created in external/local_config_cc/CROSSTOOL for a local build.
+# TODO(jamiesnape): Write a script to extract the necessary fields
+# automatically.
+toolchain {
+  toolchain_identifier: "darwin_x86_64"
+  cxx_builtin_include_directory: "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1"
+  cxx_builtin_include_directory: "/usr/local/include"
+  cxx_builtin_include_directory: "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang"
+  cxx_builtin_include_directory: "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include"
+  cxx_builtin_include_directory: "/usr/include"
+  cxx_builtin_include_directory: "/System/Library/Frameworks"
+  cxx_builtin_include_directory: "/Library/Frameworks"
+  cxx_builtin_include_directory: "/Applications/Xcode.app/Contents/Developer"
+  cxx_builtin_include_directory: "/Library/Developer/CommandLineTools"  # Added for Drake.
+  host_system_name: "local"
+  target_system_name: "local"
+  target_cpu: "darwin_x86_64"
+  target_libc: "local"
+  compiler: "compiler"
+  abi_version: "local"
+  abi_libc_version: "local"
+  tool_path {
+    name: "ar"
+    path: "osx_ar_wrapper.sh"  # Changed from "wrapped_ar" for Drake.
+  }
+  tool_path {
+    name: "compat-ld"
+    path: "/usr/bin/ld"
+  }
+  tool_path {
+    name: "cpp"
+    path: "/usr/bin/cpp"
+  }
+  tool_path {
+    name: "dwp"
+    path: "/usr/bin/dwp"
+  }
+  tool_path {
+    name: "gcc"
+    path: "osx_cc_wrapper.sh"  # Changed from "cc_wrapper.sh" for Drake.
+  }
+  tool_path {
+    name: "gcov"
+    path: "/usr/bin/gcov"
+  }
+  tool_path {
+    name: "ld"
+    path: "/usr/bin/ld"
+  }
+  tool_path {
+    name: "nm"
+    path: "/usr/bin/nm"
+  }
+  tool_path {
+    name: "objcopy"
+    path: "/usr/bin/objcopy"
+  }
+  tool_path {
+    name: "objdump"
+    path: "/usr/bin/objdump"
+  }
+  tool_path {
+    name: "strip"
+    path: "/usr/bin/strip"
+  }
+  needsPic: false
+  compiler_flag: "-U_FORTIFY_SOURCE"  # Added for Drake.
+  compiler_flag: "-D_FORTIFY_SOURCE=1"
+  compiler_flag: "-fstack-protector"
+  compiler_flag: "-fcolor-diagnostics"
+  compiler_flag: "-Wall"
+  compiler_flag: "-Wthread-safety"
+  compiler_flag: "-Wself-assign"
+  compiler_flag: "-fno-omit-frame-pointer"
+  cxx_flag: "-std=c++14"  # Changed from "-std=c++11" for Drake.
+  linker_flag: "-headerpad_max_install_names"
+  linker_flag: "-lc++"
+  linker_flag: "-no-canonical-prefixes"
+  objcopy_embed_flag: "-I"
+  objcopy_embed_flag: "binary"
+  compilation_mode_flags {
+    mode: FASTBUILD
+    compiler_flag: "-O0"
+    compiler_flag: "-DDEBUG"
+  }
+  compilation_mode_flags {
+    mode: OPT
+    compiler_flag: "-g0"
+    compiler_flag: "-O2"
+    compiler_flag: "-U_FORTIFY_SOURCE"  # Added for Drake.
+    compiler_flag: "-D_FORTIFY_SOURCE=1"
+    compiler_flag: "-DNDEBUG"
+    compiler_flag: "-ffunction-sections"
+    compiler_flag: "-fdata-sections"
+  }
+  compilation_mode_flags {
+    mode: DBG
+    compiler_flag: "-g"
+  }
+  linking_mode_flags {
+    mode: DYNAMIC
+    linker_flag: "-undefined"
+    linker_flag: "dynamic_lookup"
+  }
+  builtin_sysroot: ""
+  unfiltered_cxx_flag: "-no-canonical-prefixes"
+  unfiltered_cxx_flag: "-Wno-builtin-macro-redefined"
+  unfiltered_cxx_flag: "-D__DATE__=\"redacted\""
+  unfiltered_cxx_flag: "-D__TIMESTAMP__=\"redacted\""
+  unfiltered_cxx_flag: "-D__TIME__=\"redacted\""
+  default_python_version: "python2.7"
+  feature {
+    name: "fastbuild"
+  }
+  feature {
+    name: "no_legacy_features"
+  }
+  feature {
+    name: "opt"
+  }
+  feature {
+    name: "dbg"
+  }
+  feature {
+    name: "compile_all_modules"
+  }
+  feature {
+    name: "exclude_private_headers_in_module_maps"
+  }
+  feature {
+    name: "has_configured_linker_path"
+  }
+  feature {
+    name: "only_doth_headers_in_module_maps"
+  }
+  feature {
+    name: "legacy_compile_flags"
+    flag_set {
+      action: "assemble"
+      action: "preprocess-assemble"
+      action: "c-compile"
+      action: "c++-compile"
+      action: "c++-header-parsing"
+      action: "c++-header-preprocessing"
+      action: "c++-module-compile"
+      action: "c++-module-codegen"
+      flag_group {
+        flag: "%{legacy_compile_flags}"
+        iterate_over: "legacy_compile_flags"
+      }
+      expand_if_all_available: "legacy_compile_flags"
+    }
+  }
+  feature {
+    name: "generate_dsym_file"
+    flag_set {
+      action: "c-compile"
+      action: "c++-compile"
+      flag_group {
+        flag: "-g"
+      }
+    }
+    flag_set {
+      flag_group {
+        flag: "DSYM_HINT_LINKED_BINARY=%{linked_binary}"
+        flag: "DSYM_HINT_DSYM_PATH=%{dsym_path}"
+        flag: "DSYM_HINT_DSYM_BUNDLE_ZIP=%{dsym_bundle_zip}"
+      }
+    }
+  }
+  feature {
+    name: "strip_debug_symbols"
+    flag_set {
+      action: "c++-link-executable"
+      action: "c++-link-dynamic-library"
+      action: "c++-link-nodeps-dynamic-library"
+      flag_group {
+        flag: "-Wl,-S"
+        expand_if_all_available: "strip_debug_symbols"
+      }
+    }
+  }
+  feature {
+    name: "symbol_counts"
+    flag_set {
+      action: "c++-link-executable"
+      action: "c++-link-dynamic-library"
+      action: "c++-link-nodeps-dynamic-library"
+      flag_group {
+        flag: "-Wl,--print-symbol-counts=%{symbol_counts_output}"
+      }
+      expand_if_all_available: "symbol_counts_output"
+    }
+  }
+  feature {
+    name: "shared_flag"
+    flag_set {
+      action: "c++-link-dynamic-library"
+      action: "c++-link-nodeps-dynamic-library"
+      flag_group {
+        flag: "-shared"
+      }
+    }
+  }
+  feature {
+    name: "linkstamps"
+    flag_set {
+      action: "c++-link-executable"
+      action: "c++-link-dynamic-library"
+      action: "c++-link-nodeps-dynamic-library"
+      flag_group {
+        flag: "%{linkstamp_paths}"
+        iterate_over: "linkstamp_paths"
+      }
+      expand_if_all_available: "linkstamp_paths"
+    }
+  }
+  feature {
+    name: "output_execpath_flags"
+    flag_set {
+      action: "c++-link-executable"
+      action: "c++-link-dynamic-library"
+      action: "c++-link-nodeps-dynamic-library"
+      flag_group {
+        flag: "-o"
+        flag: "%{output_execpath}"
+      }
+      expand_if_all_available: "output_execpath"
+    }
+  }
+  feature {
+    name: "archiver_flags"
+    flag_set {
+      action: "c++-link-static-library"
+      flag_group {
+        flag: "rcs"
+        flag: "%{output_execpath}"
+      }
+      expand_if_all_available: "output_execpath"
+    }
+  }
+  feature {
+    name: "runtime_root_flags"
+    flag_set {
+      action: "c++-link-executable"
+      action: "c++-link-dynamic-library"
+      action: "c++-link-nodeps-dynamic-library"
+      action: "c++-link-static-library"
+      flag_group {
+        flag: "-Wl,-rpath,@loader_path/%{runtime_library_search_directories}"
+        iterate_over: "runtime_library_search_directories"
+      }
+      expand_if_all_available: "runtime_library_search_directories"
+    }
+    flag_set {
+      action: "c++-link-executable"
+      action: "c++-link-dynamic-library"
+      action: "c++-link-nodeps-dynamic-library"
+      action: "c++-link-static-library"
+      flag_group {
+        flag: "%{runtime_root_flags}"
+        iterate_over: "runtime_root_flags"
+      }
+      expand_if_all_available: "runtime_root_flags"
+    }
+    flag_set {
+      action: "c++-link-executable"
+      action: "c++-link-dynamic-library"
+      action: "c++-link-nodeps-dynamic-library"
+      action: "c++-link-static-library"
+      flag_group {
+        flag: "%{runtime_root_entries}"
+        iterate_over: "runtime_root_entries"
+      }
+      expand_if_all_available: "runtime_root_entries"
+    }
+  }
+  feature {
+    name: "input_param_flags"
+    flag_set {
+      action: "c++-link-executable"
+      action: "c++-link-dynamic-library"
+      action: "c++-link-nodeps-dynamic-library"
+      action: "c++-link-static-library"
+      flag_group {
+        flag: "-L%{library_search_directories}"
+        iterate_over: "library_search_directories"
+      }
+      expand_if_all_available: "library_search_directories"
+    }
+    flag_set {
+      action: "c++-link-executable"
+      action: "c++-link-dynamic-library"
+      action: "c++-link-nodeps-dynamic-library"
+      action: "c++-link-static-library"
+      flag_group {
+        flag: "%{libopts}"
+        iterate_over: "libopts"
+      }
+      expand_if_all_available: "libopts"
+    }
+    flag_set {
+      action: "c++-link-executable"
+      action: "c++-link-dynamic-library"
+      action: "c++-link-nodeps-dynamic-library"
+      action: "c++-link-static-library"
+      flag_group {
+        flag: "-Wl,-force_load,%{whole_archive_linker_params}"
+        iterate_over: "whole_archive_linker_params"
+      }
+      expand_if_all_available: "whole_archive_linker_params"
+    }
+    flag_set {
+      action: "c++-link-executable"
+      action: "c++-link-dynamic-library"
+      action: "c++-link-nodeps-dynamic-library"
+      action: "c++-link-static-library"
+      flag_group {
+        flag: "%{linker_input_params}"
+        iterate_over: "linker_input_params"
+      }
+      expand_if_all_available: "linker_input_params"
+    }
+    flag_set {
+      action: "c++-link-executable"
+      action: "c++-link-dynamic-library"
+      action: "c++-link-nodeps-dynamic-library"
+      action: "c++-link-static-library"
+      flag_group {
+        flag_group {
+          flag: "-Wl,--start-lib"
+          expand_if_equal {
+            variable: "libraries_to_link.type"
+            value: "object_file_group"
+          }
+        }
+        flag_group {
+          flag_group {
+            flag: "%{libraries_to_link.object_files}"
+            expand_if_false: "libraries_to_link.is_whole_archive"
+          }
+          flag_group {
+            flag: "-Wl,-force_load,%{libraries_to_link.object_files}"
+            expand_if_true: "libraries_to_link.is_whole_archive"
+          }
+          iterate_over: "libraries_to_link.object_files"
+          expand_if_equal {
+            variable: "libraries_to_link.type"
+            value: "object_file_group"
+          }
+        }
+        flag_group {
+          flag: "-Wl,--end-lib"
+          expand_if_equal {
+            variable: "libraries_to_link.type"
+            value: "object_file_group"
+          }
+        }
+        flag_group {
+          flag_group {
+            flag: "%{libraries_to_link.name}"
+            expand_if_false: "libraries_to_link.is_whole_archive"
+          }
+          flag_group {
+            flag: "-Wl,-force_load,%{libraries_to_link.name}"
+            expand_if_true: "libraries_to_link.is_whole_archive"
+          }
+          expand_if_equal {
+            variable: "libraries_to_link.type"
+            value: "object_file"
+          }
+        }
+        flag_group {
+          flag_group {
+            flag: "%{libraries_to_link.name}"
+            expand_if_false: "libraries_to_link.is_whole_archive"
+          }
+          flag_group {
+            flag: "-Wl,-force_load,%{libraries_to_link.name}"
+            expand_if_true: "libraries_to_link.is_whole_archive"
+          }
+          expand_if_equal {
+            variable: "libraries_to_link.type"
+            value: "interface_library"
+          }
+        }
+        flag_group {
+          flag_group {
+            flag: "%{libraries_to_link.name}"
+            expand_if_false: "libraries_to_link.is_whole_archive"
+          }
+          flag_group {
+            flag: "-Wl,-force_load,%{libraries_to_link.name}"
+            expand_if_true: "libraries_to_link.is_whole_archive"
+          }
+          expand_if_equal {
+            variable: "libraries_to_link.type"
+            value: "static_library"
+          }
+        }
+        flag_group {
+          flag_group {
+            flag: "-l%{libraries_to_link.name}"
+            expand_if_false: "libraries_to_link.is_whole_archive"
+          }
+          flag_group {
+            flag: "-Wl,-force_load,-l%{libraries_to_link.name}"
+            expand_if_true: "libraries_to_link.is_whole_archive"
+          }
+          expand_if_equal {
+            variable: "libraries_to_link.type"
+            value: "dynamic_library"
+          }
+        }
+        flag_group {
+          flag_group {
+            flag: "-l:%{libraries_to_link.name}"
+            expand_if_false: "libraries_to_link.is_whole_archive"
+          }
+          flag_group {
+            flag: "-Wl,-force_load,-l:%{libraries_to_link.name}"
+            expand_if_true: "libraries_to_link.is_whole_archive"
+          }
+          expand_if_equal {
+            variable: "libraries_to_link.type"
+            value: "versioned_dynamic_library"
+          }
+        }
+        iterate_over: "libraries_to_link"
+      }
+      expand_if_all_available: "libraries_to_link"
+    }
+  }
+  feature {
+    name: "force_pic_flags"
+    flag_set {
+      action: "c++-link-executable"
+      flag_group {
+        flag: "-Wl,-pie"  # Changed from "-pie" for Drake.
+      }
+      expand_if_all_available: "force_pic"
+    }
+  }
+  feature {
+    name: "pch"
+    flag_set {
+      action: "c-compile"
+      action: "c++-compile"
+      flag_group {
+        flag: "-include"
+        flag: "%{pch_file}"
+      }
+    }
+  }
+  feature {
+    name: "apply_default_warnings"
+    flag_set {
+      flag_group {
+        flag: "-Wshorten-64-to-32"
+        flag: "-Wbool-conversion"
+        flag: "-Wconstant-conversion"
+        flag: "-Wduplicate-method-match"
+        flag: "-Wempty-body"
+        flag: "-Wenum-conversion"
+        flag: "-Wint-conversion"
+        flag: "-Wunreachable-code"
+        flag: "-Wmismatched-return-types"
+        flag: "-Wundeclared-selector"
+        flag: "-Wuninitialized"
+        flag: "-Wunused-function"
+        flag: "-Wunused-variable"
+      }
+    }
+  }
+  feature {
+    name: "includes"
+    flag_set {
+      action: "preprocess-assemble"
+      action: "linkstamp-compile"
+      action: "c-compile"
+      action: "c++-compile"
+      action: "c++-header-parsing"
+      action: "c++-header-preprocessing"
+      action: "c++-module-compile"
+      flag_group {
+        flag: "-include"
+        flag: "%{includes}"
+        iterate_over: "includes"
+        expand_if_all_available: "includes"
+      }
+    }
+    enabled: true
+  }
+  feature {
+    name: "include_paths"
+    flag_set {
+      action: "preprocess-assemble"
+      action: "c-compile"
+      action: "c++-compile"
+      action: "c++-header-parsing"
+      action: "c++-header-preprocessing"
+      action: "c++-module-compile"
+      action: "linkstamp-compile"
+      flag_group {
+        flag: "-iquote"
+        flag: "%{quote_include_paths}"
+        iterate_over: "quote_include_paths"
+      }
+      flag_group {
+        flag: "-I%{include_paths}"
+        iterate_over: "include_paths"
+      }
+      flag_group {
+        flag: "-isystem"
+        flag: "%{system_include_paths}"
+        iterate_over: "system_include_paths"
+      }
+    }
+  }
+  feature {
+    name: "sysroot"
+    flag_set {
+      action: "assemble"
+      action: "preprocess-assemble"
+      action: "c-compile"
+      action: "c++-compile"
+      action: "c++-module-compile"
+      action: "c++-header-preprocessing"
+      action: "c++-header-parsing"
+      action: "c++-link-executable"
+      action: "c++-link-dynamic-library"
+      action: "c++-link-nodeps-dynamic-library"
+      action: "linkstamp-compile"
+      flag_group {
+        flag: "--sysroot=%{sysroot}"
+      }
+      expand_if_all_available: "sysroot"
+    }
+  }
+  feature {
+    name: "dependency_file"
+    flag_set {
+      action: "assemble"
+      action: "preprocess-assemble"
+      action: "c-compile"
+      action: "c++-compile"
+      action: "c++-module-compile"
+      action: "c++-header-preprocessing"
+      action: "c++-header-parsing"
+      flag_group {
+        flag: "-MD"
+        flag: "-MF"
+        flag: "%{dependency_file}"
+      }
+      expand_if_all_available: "dependency_file"
+    }
+  }
+  feature {
+    name: "random_seed"
+    flag_set {
+      action: "c++-compile"
+      action: "c++-module-codegen"
+      action: "c++-module-compile"
+      flag_group {
+        flag: "-frandom-seed=%{output_file}"
+      }
+    }
+  }
+  feature {
+    name: "pic"
+    flag_set {
+      action: "c-compile"
+      action: "c++-compile"
+      action: "c++-module-codegen"
+      action: "c++-module-compile"
+      action: "linkstamp-compile"
+      action: "preprocess-assemble"
+      flag_group {
+        flag: "-fPIC"
+      }
+      expand_if_all_available: "pic"
+    }
+  }
+  feature {
+    name: "per_object_debug_info"
+    flag_set {
+      action: "c-compile"
+      action: "c++-compile"
+      action: "c++-module-codegen"
+      action: "assemble"
+      action: "preprocess-assemble"
+      flag_group {
+        flag: "-gsplit-dwarf"
+      }
+      expand_if_all_available: "per_object_debug_info_file"
+    }
+  }
+  feature {
+    name: "preprocessor_defines"
+    flag_set {
+      action: "preprocess-assemble"
+      action: "c-compile"
+      action: "c++-compile"
+      action: "c++-header-parsing"
+      action: "c++-header-preprocessing"
+      action: "c++-module-compile"
+      action: "linkstamp-compile"
+      flag_group {
+        flag: "-D%{preprocessor_defines}"
+        iterate_over: "preprocessor_defines"
+      }
+    }
+  }
+  feature {
+    name: "fdo_instrument"
+    flag_set {
+      action: "c-compile"
+      action: "c++-compile"
+      action: "c++-link-dynamic-library"
+      action: "c++-link-nodeps-dynamic-library"
+      action: "c++-link-executable"
+      flag_group {
+        flag: "-fprofile-generate=%{fdo_instrument_path}"
+        flag: "-fno-data-sections"
+      }
+    }
+    provides: "profile"
+  }
+  feature {
+    name: "fdo_optimize"
+    flag_set {
+      action: "c-compile"
+      action: "c++-compile"
+      flag_group {
+        flag: "-fprofile-use=%{fdo_profile_path}"
+        flag: "-Wno-profile-instr-unprofiled"
+        flag: "-Wno-profile-instr-out-of-date"
+        flag: "-fprofile-correction"
+      }
+      expand_if_all_available: "fdo_profile_path"
+    }
+    provides: "profile"
+  }
+  feature {
+    name: "autofdo"
+    flag_set {
+      action: "c-compile"
+      action: "c++-compile"
+      flag_group {
+        flag: "-fauto-profile=%{fdo_profile_path}"
+        flag: "-fprofile-correction"
+      }
+      expand_if_all_available: "fdo_profile_path"
+    }
+    provides: "profile"
+  }
+  feature {
+    name: "lipo"
+    flag_set {
+      action: "c-compile"
+      action: "c++-compile"
+      flag_group {
+        flag: "-fripa"
+      }
+    }
+    requires {
+      feature: "autofdo"
+    }
+    requires {
+      feature: "fdo_optimize"
+    }
+    requires {
+      feature: "fdo_instrument"
+    }
+  }
+  feature {
+    name: "coverage"
+  }
+  feature {
+    name: "llvm_coverage_map_format"
+    flag_set {
+      action: "preprocess-assemble"
+      action: "c-compile"
+      action: "c++-compile"
+      action: "c++-module-compile"
+      flag_group {
+        flag: "-fprofile-instr-generate"
+        flag: "-fcoverage-mapping"
+        flag: "-g"
+      }
+    }
+    flag_set {
+      action: "c++-link-dynamic-library"
+      action: "c++-link-nodeps-dynamic-library"
+      action: "c++-link-executable"
+      flag_group {
+        flag: "-fprofile-instr-generate"
+      }
+    }
+    requires {
+      feature: "coverage"
+    }
+  }
+  feature {
+    name: "gcc_coverage_map_format"
+    flag_set {
+      action: "preprocess-assemble"
+      action: "c-compile"
+      action: "c++-compile"
+      action: "c++-module-compile"
+      flag_group {
+        flag: "-fprofile-arcs"
+        flag: "-ftest-coverage"
+        flag: "-g"
+      }
+    }
+    flag_set {
+      action: "c++-link-dynamic-library"
+      action: "c++-link-nodeps-dynamic-library"
+      action: "c++-link-executable"
+      flag_group {
+        flag: "-lgcov"
+      }
+    }
+    requires {
+      feature: "coverage"
+    }
+  }
+  feature {
+    name: "apply_default_compiler_flags"
+    flag_set {
+      flag_group {
+        flag: "-DOS_MACOSX"
+        flag: "-fno-autolink"
+      }
+    }
+  }
+  feature {
+    name: "bitcode_embedded"
+  }
+  feature {
+    name: "bitcode_embedded_markers"
+  }
+  feature {
+    name: "user_link_flags"
+    flag_set {
+      action: "c++-link-executable"
+      action: "c++-link-dynamic-library"
+      action: "c++-link-nodeps-dynamic-library"
+      flag_group {
+        flag: "%{user_link_flags}"
+        iterate_over: "user_link_flags"
+      }
+      expand_if_all_available: "user_link_flags"
+    }
+    enabled: true
+  }
+  feature {
+    name: "legacy_link_flags"
+    flag_set {
+      action: "c++-link-executable"
+      action: "c++-link-dynamic-library"
+      action: "c++-link-nodeps-dynamic-library"
+      flag_group {
+        flag: "%{legacy_link_flags}"
+        iterate_over: "legacy_link_flags"
+      }
+      expand_if_all_available: "legacy_link_flags"
+    }
+  }
+  feature {
+    name: "dead_strip"
+    flag_set {
+      action: "c++-link-executable"
+      action: "c++-link-dynamic-library"
+      action: "c++-link-nodeps-dynamic-library"
+      flag_group {
+        flag: "--dead_strip"
+        flag: "--no_dead_strip_inits_and_terms"
+      }
+    }
+    requires {
+      feature: "opt"
+    }
+  }
+  feature {
+    name: "cpp_linker_flags"
+  }
+  feature {
+    name: "unfiltered_cxx_flags"
+  }
+  feature {
+    name: "user_compile_flags"
+    flag_set {
+      action: "assemble"
+      action: "preprocess-assemble"
+      action: "c-compile"
+      action: "c++-compile"
+      action: "c++-header-parsing"
+      action: "c++-header-preprocessing"
+      action: "c++-module-compile"
+      action: "c++-module-codegen"
+      action: "linkstamp-compile"
+      flag_group {
+        flag: "%{user_compile_flags}"
+        iterate_over: "user_compile_flags"
+      }
+      expand_if_all_available: "user_compile_flags"
+    }
+  }
+  feature {
+    name: "unfiltered_compile_flags"
+    flag_set {
+      action: "assemble"
+      action: "preprocess-assemble"
+      action: "c-compile"
+      action: "c++-compile"
+      action: "c++-header-parsing"
+      action: "c++-header-preprocessing"
+      action: "c++-module-compile"
+      action: "c++-module-codegen"
+      action: "linkstamp-compile"
+      flag_group {
+        flag: "%{unfiltered_compile_flags}"
+        iterate_over: "unfiltered_compile_flags"
+      }
+      expand_if_all_available: "unfiltered_compile_flags"
+    }
+  }
+  feature {
+    name: "linker_param_file"
+    flag_set {
+      action: "c++-link-executable"
+      action: "c++-link-dynamic-library"
+      action: "c++-link-nodeps-dynamic-library"
+      flag_group {
+        flag: "-Wl,@%{linker_param_file}"
+      }
+      expand_if_all_available: "linker_param_file"
+    }
+    flag_set {
+      action: "c++-link-static-library"
+      flag_group {
+        flag: "@%{linker_param_file}"
+      }
+      expand_if_all_available: "linker_param_file"
+    }
+  }
+  feature {
+    name: "compiler_input_flags"
+    flag_set {
+      action: "assemble"
+      action: "preprocess-assemble"
+      action: "c-compile"
+      action: "c++-compile"
+      action: "linkstamp-compile"
+      action: "c++-header-parsing"
+      action: "c++-header-preprocessing"
+      action: "c++-module-compile"
+      action: "c++-module-codegen"
+      flag_group {
+        flag: "-c"
+        flag: "%{source_file}"
+      }
+      expand_if_all_available: "source_file"
+    }
+  }
+  feature {
+    name: "compiler_output_flags"
+    flag_set {
+      action: "assemble"
+      action: "preprocess-assemble"
+      action: "c-compile"
+      action: "c++-compile"
+      action: "linkstamp-compile"
+      action: "c++-header-parsing"
+      action: "c++-header-preprocessing"
+      action: "c++-module-compile"
+      action: "c++-module-codegen"
+      flag_group {
+        flag: "-S"
+        expand_if_all_available: "output_assembly_file"
+      }
+      flag_group {
+        flag: "-E"
+        expand_if_all_available: "output_preprocess_file"
+      }
+      flag_group {
+        flag: "-o"
+        flag: "%{output_file}"
+        expand_if_all_available: "output_file"
+      }
+    }
+  }
+  action_config {
+    config_name: "strip"
+    action_name: "strip"
+    tool {
+      tool_path: "/usr/bin/strip"
+    }
+    flag_set {
+      flag_group {
+        flag: "-S"
+        flag: "-o"
+        flag: "%{output_file}"
+      }
+      flag_group {
+        flag: "%{stripopts}"
+        iterate_over: "stripopts"
+      }
+      flag_group {
+        flag: "%{input_file}"
+      }
+    }
+  }
+  action_config {
+    config_name: "c-compile"
+    action_name: "c-compile"
+    tool {
+      tool_path: "/usr/bin/clang"  # Changed from "wrapped_clang" for Drake.
+      execution_requirement: "requires-darwin"
+    }
+    implies: "preprocessor_defines"
+    implies: "legacy_compile_flags"
+    implies: "user_compile_flags"
+    implies: "sysroot"
+    implies: "unfiltered_compile_flags"
+    implies: "compiler_input_flags"
+    implies: "compiler_output_flags"
+  }
+  action_config {
+    config_name: "c++-compile"
+    action_name: "c++-compile"
+    tool {
+      tool_path: "/usr/bin/clang"  # Changed from "wrapped_clang" for Drake.
+      execution_requirement: "requires-darwin"
+    }
+    implies: "preprocessor_defines"
+    implies: "legacy_compile_flags"
+    implies: "user_compile_flags"
+    implies: "sysroot"
+    implies: "unfiltered_compile_flags"
+    implies: "compiler_input_flags"
+    implies: "compiler_output_flags"
+  }
+  action_config {
+    config_name: "linkstamp-compile"
+    action_name: "linkstamp-compile"
+    tool {
+      tool_path: "/usr/bin/clang"  # Changed from "wrapped_clang" for Drake.
+      execution_requirement: "requires-darwin"
+    }
+    implies: "preprocessor_defines"
+    implies: "legacy_compile_flags"
+    implies: "user_compile_flags"
+    implies: "sysroot"
+    implies: "unfiltered_compile_flags"
+    implies: "compiler_input_flags"
+    implies: "compiler_output_flags"
+  }
+  action_config {
+    config_name: "c++-module-compile"
+    action_name: "c++-module-compile"
+    tool {
+      tool_path: "/usr/bin/clang"  # Changed from "wrapped_clang" for Drake.
+      execution_requirement: "requires-darwin"
+    }
+    implies: "preprocessor_defines"
+    implies: "legacy_compile_flags"
+    implies: "user_compile_flags"
+    implies: "sysroot"
+    implies: "unfiltered_compile_flags"
+    implies: "compiler_input_flags"
+    implies: "compiler_output_flags"
+  }
+  action_config {
+    config_name: "c++-header-parsing"
+    action_name: "c++-header-parsing"
+    tool {
+      tool_path: "/usr/bin/clang"  # Changed from "wrapped_clang" for Drake.
+      execution_requirement: "requires-darwin"
+    }
+    implies: "preprocessor_defines"
+    implies: "legacy_compile_flags"
+    implies: "user_compile_flags"
+    implies: "sysroot"
+    implies: "unfiltered_compile_flags"
+    implies: "compiler_input_flags"
+    implies: "compiler_output_flags"
+  }
+  action_config {
+    config_name: "c++-header-preprocessing"
+    action_name: "c++-header-preprocessing"
+    tool {
+      tool_path: "/usr/bin/clang"  # Changed from "wrapped_clang" for Drake.
+      execution_requirement: "requires-darwin"
+    }
+    implies: "preprocessor_defines"
+    implies: "legacy_compile_flags"
+    implies: "user_compile_flags"
+    implies: "sysroot"
+    implies: "unfiltered_compile_flags"
+    implies: "compiler_input_flags"
+    implies: "compiler_output_flags"
+  }
+  action_config {
+    config_name: "assemble"
+    action_name: "assemble"
+    tool {
+      tool_path: "/usr/bin/clang"  # Changed from "wrapped_clang" for Drake.
+      execution_requirement: "requires-darwin"
+    }
+    implies: "legacy_compile_flags"
+    implies: "user_compile_flags"
+    implies: "sysroot"
+    implies: "unfiltered_compile_flags"
+    implies: "compiler_input_flags"
+    implies: "compiler_output_flags"
+  }
+  action_config {
+    config_name: "preprocess-assemble"
+    action_name: "preprocess-assemble"
+    tool {
+      tool_path: "/usr/bin/clang"  # Changed from "wrapped_clang" for Drake.
+      execution_requirement: "requires-darwin"
+    }
+    implies: "preprocessor_defines"
+    implies: "legacy_compile_flags"
+    implies: "user_compile_flags"
+    implies: "sysroot"
+    implies: "unfiltered_compile_flags"
+    implies: "compiler_input_flags"
+    implies: "compiler_output_flags"
+  }
+  action_config {
+    config_name: "c++-link-executable"
+    action_name: "c++-link-executable"
+    tool {
+      tool_path: "osx_cc_wrapper.sh"  # Changed from "cc_wrapper.sh" for Drake.
+      execution_requirement: "requires-darwin"
+    }
+    implies: "symbol_counts"
+    implies: "linkstamps"
+    implies: "output_execpath_flags"
+    implies: "runtime_root_flags"
+    implies: "input_param_flags"
+    implies: "force_pic_flags"
+    implies: "legacy_link_flags"
+    implies: "strip_debug_symbols"
+    implies: "linker_param_file"
+    implies: "sysroot"
+  }
+  action_config {
+    config_name: "c++-link-dynamic-library"
+    action_name: "c++-link-dynamic-library"
+    tool {
+      tool_path: "osx_cc_wrapper.sh"  # Changed from "cc_wrapper.sh" for Drake.
+      execution_requirement: "requires-darwin"
+    }
+    implies: "has_configured_linker_path"
+    implies: "symbol_counts"
+    implies: "shared_flag"
+    implies: "linkstamps"
+    implies: "output_execpath_flags"
+    implies: "runtime_root_flags"
+    implies: "input_param_flags"
+    implies: "legacy_link_flags"
+    implies: "strip_debug_symbols"
+    implies: "linker_param_file"
+    implies: "sysroot"
+  }
+  action_config {
+    config_name: "c++-link-nodeps-dynamic-library"
+    action_name: "c++-link-nodeps-dynamic-library"
+    tool {
+      tool_path: "osx_cc_wrapper.sh"  # Changed from "cc_wrapper.sh" for Drake.
+      execution_requirement: "requires-darwin"
+    }
+    implies: "has_configured_linker_path"
+    implies: "symbol_counts"
+    implies: "shared_flag"
+    implies: "linkstamps"
+    implies: "output_execpath_flags"
+    implies: "runtime_root_flags"
+    implies: "input_param_flags"
+    implies: "legacy_link_flags"
+    implies: "strip_debug_symbols"
+    implies: "linker_param_file"
+    implies: "sysroot"
+  }
+  action_config {
+    config_name: "c++-link-static-library"
+    action_name: "c++-link-static-library"
+    tool {
+      tool_path: "osx_ar_wrapper.sh"  # Changed from "wrapped_ar" for Drake.
+      execution_requirement: "requires-darwin"
+    }
+    implies: "runtime_root_flags"
+    implies: "archiver_flags"
+    implies: "input_param_flags"
+    implies: "linker_param_file"
+  }
+  cc_target_os: "apple"
+}
diff --git a/tools/cc_toolchain/bazel.rc b/tools/cc_toolchain/bazel.rc
index e8592e7aea5..94a7efe0025 100644
--- a/tools/cc_toolchain/bazel.rc
+++ b/tools/cc_toolchain/bazel.rc
@@ -1,9 +1,5 @@
-# Disable ccache due to incompatibility with Bazel.
-build --action_env=CCACHE_DISABLE=1
-
-# Add C++14 compiler flags.
-build --cxxopt=-std=c++14
-build --host_cxxopt=-std=c++14
+# Use one of Drake's pre-selected compilers.
+build --crosstool_top=//tools/cc_toolchain:default-toolchain

 ### Debug symbols on OS X. ###
 # See https://github.com/bazelbuild/bazel/issues/2537
diff --git a/tools/cc_toolchain/osx_ar_wrapper.sh b/tools/cc_toolchain/osx_ar_wrapper.sh
new file mode 100755
index 00000000000..14ba6cac63f
--- /dev/null
+++ b/tools/cc_toolchain/osx_ar_wrapper.sh
@@ -0,0 +1,7 @@
+#!/bin/bash
+
+set -euo pipefail
+
+# Ensure zero timestamping for hermetic results.
+export ZERO_AR_DATE=1
+/usr/bin/ar "$@"
diff --git a/tools/cc_toolchain/osx_cc_wrapper.sh b/tools/cc_toolchain/osx_cc_wrapper.sh
new file mode 100755
index 00000000000..64f074a59a8
--- /dev/null
+++ b/tools/cc_toolchain/osx_cc_wrapper.sh
@@ -0,0 +1,2 @@
+#!/bin/bash
+exec third_party/com_github_bazelbuild_bazel/tools/cpp/osx_cc_wrapper.sh "$@"
RussTedrake commented 6 years ago

Here's the one-liner

git fetch https://github.com/jwnimmer-tri/drake macos-clion-workaround && git cherry-pick daca23091e40bde66bd89f8f206894513fdc7ed6
jwnimmer-tri commented 6 years ago

It is possible that, instead of the above patch, setting BAZEL_USE_CPP_ONLY_TOOLCHAIN=1 in the environment (before launching CLion) would also get things working on macOS.

RussTedrake commented 6 years ago

Tested the environment variable patch, and confirmed that it works on my mac. Woohoo!