From 6e9076206b43a35533cadc0d2b713721750d9499 Mon Sep 17 00:00:00 2001 From: Josh Pieper Date: Thu, 11 Apr 2019 14:57:41 -0400 Subject: [PATCH] Actually support the C++ stdlib Now that we're downloading arbitrary versions of clang, if we want to actually use the C++ standard library in cross-compilation environments, we need to compile it too. bazel doesn't make that super easy for now, as there is no facility for transitioning between a no-stdlib-C++ compiler and a with-stdlib-C++ compiler automatically. Thus we currently require cc_binary nodes to manually link in the libcxx label. --- example/BUILD | 1 + example/example1.cc | 8 +++- tools/cc_toolchain/BUILD | 4 +- tools/cc_toolchain/CROSSTOOL | 31 ++++++++++----- tools/workspace/clang/libcxx.BUILD | 56 +++++++++++++++++++++++++++ tools/workspace/clang/libcxxabi.BUILD | 45 +++++++++++++++++++++ tools/workspace/clang/repository.bzl | 22 ++++++++++- tools/workspace/default.bzl | 2 +- 8 files changed, 153 insertions(+), 16 deletions(-) create mode 100644 tools/workspace/clang/libcxx.BUILD create mode 100644 tools/workspace/clang/libcxxabi.BUILD diff --git a/example/BUILD b/example/BUILD index ca36a23..7590849 100644 --- a/example/BUILD +++ b/example/BUILD @@ -17,5 +17,6 @@ cc_binary( name = "example1", srcs = ["example1.cc"], + deps = ["@org_llvm_libcxx//:libcxx"], linkstatic = 1, ) diff --git a/example/example1.cc b/example/example1.cc index d08a006..1135773 100644 --- a/example/example1.cc +++ b/example/example1.cc @@ -12,11 +12,15 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include +#include +#include extern "C" { int main(int, char**) { - printf("hello there!\n"); + std::string stuff = "hello there!"; + + std::cout << "yo: " << stuff << "\n"; + return 0; } diff --git a/tools/cc_toolchain/BUILD b/tools/cc_toolchain/BUILD index 633e1d5..60f64ac 100644 --- a/tools/cc_toolchain/BUILD +++ b/tools/cc_toolchain/BUILD @@ -50,6 +50,8 @@ filegroup( "@org_llvm_clang//:includes", "@org_llvm_clang//:runtime_libs", "@org_llvm_clang//:static_libs", + "@org_llvm_libcxx//:raw_headers", + "@org_llvm_libcxxabi//:raw_headers", ], ) @@ -58,7 +60,7 @@ cc_toolchain( name = "cc_toolchain_linux", toolchain_identifier = "clang-x86_64", all_files = ":clang_all", - compiler_files = ":clang_wrappers", + compiler_files = ":clang_all", cpu = "k8", dwp_files = ":clang_wrappers", dynamic_runtime_libs = ["@org_llvm_clang//:runtime_libs"], diff --git a/tools/cc_toolchain/CROSSTOOL b/tools/cc_toolchain/CROSSTOOL index fb63d26..e4d4d4c 100644 --- a/tools/cc_toolchain/CROSSTOOL +++ b/tools/cc_toolchain/CROSSTOOL @@ -33,7 +33,7 @@ toolchain { abi_version: "local" abi_libc_version: "local" builtin_sysroot: "" - compiler: "compiler" + compiler: "clang" host_system_name: "local" target_libc: "local" target_cpu: "k8" @@ -69,8 +69,16 @@ toolchain { linker_flag: "-Wno-unused-command-line-argument" cxx_flag: "-std=c++17" - cxx_flag: "-stdlib=libc++" - linker_flag: "-stdlib=libc++" + + # We rely on a compiled version of libcxx, which for now users must + # manually specify when linking applications (but not compiling). + cxx_flag: "-nostdinc++" + linker_flag: "-nostdinc++" + linker_flag: "-nostdlib++" + cxx_flag: "-isystem" + cxx_flag: "external/org_llvm_libcxx/include" + cxx_flag: "-isystem" + cxx_flag: "external/org_llvm_libcxxabi/include" linker_flag: "-fuse-ld=lld" linker_flag: "-rtlib=compiler-rt" @@ -201,11 +209,6 @@ toolchain { needsPic: true - cxx_builtin_include_directory: "external/raspberry_pi/sysroot/usr/include/c++/5" - cxx_builtin_include_directory: "external/raspberry_pi/sysroot/usr/include/arm-linux-gnueabihf/c++/5" - cxx_builtin_include_directory: "external/raspberry_pi/sysroot/usr/include/c++/5/backward" - cxx_builtin_include_directory: "external/raspberry_pi/sysroot/usr/lib/gcc/arm-linux-gnueabihf/5/include" - cxx_builtin_include_directory: "external/raspberry_pi/sysroot/usr/lib/gcc/arm-linux-gnueabihf/5/include-fixed" cxx_builtin_include_directory: "external/raspberry_pi/sysroot/usr/include/arm-linux-gnueabihf" cxx_builtin_include_directory: "external/raspberry_pi/sysroot/usr/include" cxx_builtin_include_directory: "external/org_llvm_clang/include/" @@ -217,8 +220,16 @@ toolchain { linker_flag: "-Wno-unused-command-line-argument" cxx_flag: "-std=c++17" - cxx_flag: "-stdlib=libc++" - linker_flag: "-stdlib=libc++" + + # We rely on a compiled version of libcxx, which for now users must + # manually specify when linking applications (but not compiling). + cxx_flag: "-nostdinc++" + linker_flag: "-nostdinc++" + linker_flag: "-nostdlib++" + cxx_flag: "-isystem" + cxx_flag: "external/org_llvm_libcxx/include" + cxx_flag: "-isystem" + cxx_flag: "external/org_llvm_libcxxabi/include" linker_flag: "-fuse-ld=lld" linker_flag: "-lm" diff --git a/tools/workspace/clang/libcxx.BUILD b/tools/workspace/clang/libcxx.BUILD new file mode 100644 index 0000000..38201ab --- /dev/null +++ b/tools/workspace/clang/libcxx.BUILD @@ -0,0 +1,56 @@ +# -*- python -*- + +# Copyright 2019 Josh Pieper, jjp@pobox.com. +# +# 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. + +package(default_visibility = ["//visibility:public"]) + +filegroup( + name = "raw_headers", + srcs = glob(["include/**"]), +) + +cc_library( + name = "headers", + hdrs = [":raw_headers"], +) + +cc_library( + name = "libcxx", + hdrs = glob(["include/**"]), + srcs = glob([ + "src/*.cpp", + "src/include/*.h", + "filesystem/*.cpp", + "filesystem/*.h", + ]), + textual_hdrs = glob([ + "src/support/runtime/**", + ]), + copts = [ + "-Iexternal/org_llvm_libcxx/src/include", + "-Iexternal/org_llvm_libcxx/src", + "-D_LIBCPP_BUILDING_LIBRARY", + "-D_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER", + "-DLIBCXX_BUILDING_LIBCXXABI", + "-DLIBCXX_CXX_ABI=libstdc++", + "-DNDEBUG", + "-fvisibility-inlines-hidden", + ], + includes = ["include"], + deps = [ + ":headers", + "@org_llvm_libcxxabi//:libcxxabi", + ], +) diff --git a/tools/workspace/clang/libcxxabi.BUILD b/tools/workspace/clang/libcxxabi.BUILD new file mode 100644 index 0000000..d8d33bc --- /dev/null +++ b/tools/workspace/clang/libcxxabi.BUILD @@ -0,0 +1,45 @@ +# -*- python -*- + +# Copyright 2019 Josh Pieper, jjp@pobox.com. +# +# 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. + +package(default_visibility = ["//visibility:public"]) + +filegroup( + name = "raw_headers", + srcs = glob(["include/**"]), +) + +cc_library( + name = "libcxxabi", + hdrs = [":raw_headers"], + srcs = glob([ + "src/*.cpp", + "src/*.hpp", + "src/*.h", + "src/include/*.h", + "src/demangle/*.h", + ], exclude = [ + "src/stdlib_new_delete.cpp", + "src/cxa_noexception.cpp", + ]), + copts = [ + "-Iexternal/org_llvm_libcxx/include", + "-D_LIBCPP_BUILDING_LIBRARY", + "-DNDEBUG", + "-fvisibility-inlines-hidden", + ], + includes = ["include"], + deps = ["@org_llvm_libcxx//:headers"], +) diff --git a/tools/workspace/clang/repository.bzl b/tools/workspace/clang/repository.bzl index 52b70b5..832b68d 100644 --- a/tools/workspace/clang/repository.bzl +++ b/tools/workspace/clang/repository.bzl @@ -17,9 +17,9 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") -def clang_repository(name): +def clang_repository(): http_archive( - name = name, + name = "org_llvm_clang", urls = [ "http://releases.llvm.org/7.0.0/clang+llvm-7.0.0-x86_64-linux-gnu-ubuntu-16.04.tar.xz", ], @@ -27,3 +27,21 @@ def clang_repository(name): strip_prefix = "clang+llvm-7.0.0-x86_64-linux-gnu-ubuntu-16.04", build_file = Label("//tools/workspace/clang:package.BUILD"), ) + http_archive( + name = "org_llvm_libcxx", + urls = [ + "http://releases.llvm.org/7.0.0/libcxx-7.0.0.src.tar.xz", + ], + sha256 = "9b342625ba2f4e65b52764ab2061e116c0337db2179c6bce7f9a0d70c52134f0", + strip_prefix = "libcxx-7.0.0.src", + build_file = Label("//tools/workspace/clang:libcxx.BUILD"), + ) + http_archive( + name = "org_llvm_libcxxabi", + urls = [ + "http://releases.llvm.org/7.0.1/libcxxabi-7.0.1.src.tar.xz", + ], + sha256 = "9b45c759ff397512eae4d938ff82827b1bd7ccba49920777e5b5e460baeb245f", + strip_prefix = "libcxxabi-7.0.0.src", + build_file = Label("//tools/workspace/clang:libcxxabi.BUILD"), + ) diff --git a/tools/workspace/default.bzl b/tools/workspace/default.bzl index aee14d0..45b206d 100644 --- a/tools/workspace/default.bzl +++ b/tools/workspace/default.bzl @@ -19,6 +19,6 @@ load("//tools/workspace/raspberry_pi:repository.bzl", "raspberry_pi_repository") def add_default_repositories(excludes = []): if "clang" not in excludes: - clang_repository(name = "org_llvm_clang") + clang_repository() if "raspberry_pi" not in excludes: raspberry_pi_repository(name = "raspberry_pi")