Skip to content

Commit

Permalink
Actually support the C++ stdlib
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jpieper committed Apr 11, 2019
1 parent 267da15 commit 6e90762
Show file tree
Hide file tree
Showing 8 changed files with 153 additions and 16 deletions.
1 change: 1 addition & 0 deletions example/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@
cc_binary(
name = "example1",
srcs = ["example1.cc"],
deps = ["@org_llvm_libcxx//:libcxx"],
linkstatic = 1,
)
8 changes: 6 additions & 2 deletions example/example1.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include <stdio.h>
#include <iostream>
#include <string>

extern "C" {
int main(int, char**) {
printf("hello there!\n");
std::string stuff = "hello there!";

std::cout << "yo: " << stuff << "\n";

return 0;
}

Expand Down
4 changes: 3 additions & 1 deletion tools/cc_toolchain/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
)

Expand All @@ -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"],
Expand Down
31 changes: 21 additions & 10 deletions tools/cc_toolchain/CROSSTOOL
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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/"
Expand All @@ -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"
Expand Down
56 changes: 56 additions & 0 deletions tools/workspace/clang/libcxx.BUILD
Original file line number Diff line number Diff line change
@@ -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",
],
)
45 changes: 45 additions & 0 deletions tools/workspace/clang/libcxxabi.BUILD
Original file line number Diff line number Diff line change
@@ -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"],
)
22 changes: 20 additions & 2 deletions tools/workspace/clang/repository.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,31 @@
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",
],
sha256 = "69b85c833cd28ea04ce34002464f10a6ad9656dd2bba0f7133536a9927c660d2",
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"),
)
2 changes: 1 addition & 1 deletion tools/workspace/default.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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")

0 comments on commit 6e90762

Please sign in to comment.