How does C++20 build with Bazel & Clang?
This article will cover:
- Installation of the Bazel build system
Installation of LLVM compilation system
- Clang is an "LLVM native" C/C++/Objective-C compiler
- Configuration of the Bazel Clang toolchain
- Building C++20 Libraries and Applications
Examples of this article can be found at: https://github.com/ikuokuo/start-cpp20
This article is a practice on Ubuntu 20, Windows can prepare the environment with WSL.
Install Bazel, in binary mode
Bazelisk is the recommended way to install Bazel, we just install its binary distribution :
cd ~
wget https://github.com/bazelbuild/bazelisk/releases/download/v1.12.0/bazelisk-linux-amd64 -O bazelisk-1.12.0-linux-amd64
chmod a+x bazelisk-*
sudo ln -s $(pwd)/bazelisk-1.12.0-linux-amd64 /usr/local/bin/bazel
touch WORKSPACE
# 国内下载 Bazel 可能遇到如下问题,配置 .bazeliskrc 解决
# could not resolve the version 'latest' to an actual version number
# https://github.com/bazelbuild/bazelisk/issues/220
cat <<-EOF > .bazeliskrc
BAZELISK_BASE_URL=https://github.com/bazelbuild/bazel/releases/download
USE_BAZEL_VERSION=5.2.0
EOF
bazel version
For more ways, see the official documentation . Further, it is recommended to install buildtools and soft-link it after downloading:
sudo ln -s $(pwd)/buildifier-5.1.0-linux-amd64 /usr/local/bin/buildifier
sudo ln -s $(pwd)/buildozer-5.1.0-linux-amd64 /usr/local/bin/buildozer
How Bazel builds C++ projects, see my Start Bazel notes.
Install LLVM from source
Clang's std::fromat
text formatting feature is not enabled by default:
The paper is implemented but still marked as an incomplete feature (the feature-test macro is not set and the libary is only available when built with LIBCXX_ENABLE_INCOMPLETE_FEATURES). Not yet implemented LWG-issues will cause API and ABI breakage.
C++20 features, compiler support:
Therefore, to install LLVM from source, you need to build Clang & libc++:
git clone -b llvmorg-14.0.6 --depth 1 https://github.com/llvm/llvm-project.git
cd llvm-project
mkdir _build
cd _build
# llvm install path, such as /usr/local/llvm
LLVM_PREFIX=$HOME/Apps/llvm-14.0.6
cmake -DCMAKE_BUILD_TYPE=Release \
-DCMAKE_INSTALL_PREFIX=$LLVM_PREFIX \
-DLLVM_ENABLE_PROJECTS=clang \
-DLLVM_ENABLE_RUNTIMES="libcxx;libcxxabi" \
-DLIBCXX_ENABLE_INCOMPLETE_FEATURES=ON \
../llvm
make -j`nproc`
make install
sudo ln -s $LLVM_PREFIX /usr/local/llvm
cat <<-EOF >> ~/.bashrc
# llvm
export LLVM_HOME=/usr/local/llvm
export PATH=\$LLVM_HOME/bin:\$PATH
export LD_LIBRARY_PATH=\$LLVM_HOME/lib/x86_64-unknown-linux-gnu:\$LD_LIBRARY_PATH
EOF
llvm-config --version
clang --version
LLVM_PREFIX
installation path is determined by yourself. Finally, compile the test:
cat <<-EOF > hello.cc
#include <format>
#include <iostream>
int main() {
std::string message = std::format("The answer is {}.", 42);
std::cout << message << std::endl;
}
EOF
clang++ -std=c++20 -stdlib=libc++ hello.cc -o hello
./hello
Install LLVM, in binary mode
This section can be omitted. This method is not used in this practice because I want to open more C++20 features. This is for the record only and can be used for reference if necessary.
Method 1. Install the binary distribution :
cd ~
wget https://github.com/llvm/llvm-project/releases/download/llvmorg-13.0.0/clang+llvm-13.0.0-x86_64-linux-gnu-ubuntu-20.04.tar.xz
tar -xf clang+llvm-*.tar.xz
sudo ln -s $(pwd)/clang+llvm-13.0.0-x86_64-linux-gnu-ubuntu-20.04 /usr/local/llvm
cat <<-EOF >> ~/.bashrc
# llvm
export LLVM_HOME=/usr/local/llvm
export PATH=\$LLVM_HOME/bin:\$PATH
EOF
llvm-config --version
clang --version
Method 2. Install with apt
: https://apt.llvm.org/
Method 3. Use the configured toolchain: LLVM toolchain for Bazel
Configure the Clang toolchain
In this article, the Clang toolchain is configured according to the steps of Bazel Tutorial: Configure C++ Toolchains . In the end, the root directory of the project will have the following files:
WORKSPACE
Indicates the Bazel workspace with empty content.
.bazelrc
allow --config=clang_config
enable the Clang toolchain:
# Use our custom-configured c++ toolchain.
build:clang_config --crosstool_top=//toolchain:clang_suite
# Use --cpu as a differentiator.
build:clang_config --cpu=linux_x86_64
# Use the default Bazel C++ toolchain to build the tools used during the build.
build:clang_config --host_crosstool_top=@bazel_tools//tools/cpp:toolchain
toolchain/BUILD
Configure Clang toolchain information:
load(":cc_toolchain_config.bzl", "cc_toolchain_config")
package(default_visibility = ["//visibility:public"])
#filegroup(name = "clang_suite")
cc_toolchain_suite(
name = "clang_suite",
toolchains = {
"linux_x86_64": ":linux_x86_64_toolchain",
},
)
filegroup(name = "empty")
cc_toolchain(
name = "linux_x86_64_toolchain",
toolchain_identifier = "linux_x86_64-toolchain",
toolchain_config = ":linux_x86_64_toolchain_config",
all_files = ":empty",
compiler_files = ":empty",
dwp_files = ":empty",
linker_files = ":empty",
objcopy_files = ":empty",
strip_files = ":empty",
supports_param_files = 0,
)
#filegroup(name = "linux_x86_64_toolchain_config")
cc_toolchain_config(name = "linux_x86_64_toolchain_config")
toolchain/cc_toolchain_config.bzl
Configure Clang toolchain rules:
# C++ Toolchain Configuration
# https://bazel.build/docs/cc-toolchain-config-reference
# https://github.com/bazelbuild/bazel/blob/master/tools/build_defs/cc/action_names.bzl
load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES")
load(
"@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl",
"feature",
"flag_group",
"flag_set",
"tool_path",
)
all_compile_actions = [
ACTION_NAMES.c_compile,
ACTION_NAMES.cpp_compile,
ACTION_NAMES.linkstamp_compile,
ACTION_NAMES.assemble,
ACTION_NAMES.preprocess_assemble,
ACTION_NAMES.cpp_header_parsing,
ACTION_NAMES.cpp_module_compile,
ACTION_NAMES.cpp_module_codegen,
]
all_link_actions = [
ACTION_NAMES.cpp_link_executable,
ACTION_NAMES.cpp_link_dynamic_library,
ACTION_NAMES.cpp_link_nodeps_dynamic_library,
]
def _impl(ctx):
llvm_version = "14.0.6"
llvm_prefix = "/home/john/Apps/llvm-{}".format(llvm_version)
llvm_bindir = llvm_prefix + "/bin"
tool_paths = [
tool_path(
name = "gcc",
path = llvm_bindir + "/clang",
),
tool_path(
name = "ld",
path = llvm_bindir + "/ld.lld",
),
tool_path(
name = "ar",
path = llvm_bindir + "/llvm-ar",
),
tool_path(
name = "cpp",
path = llvm_bindir + "/clang-cpp",
),
tool_path(
name = "gcov",
path = llvm_bindir + "/llvm-cov",
),
tool_path(
name = "nm",
path = llvm_bindir + "/llvm-nm",
),
tool_path(
name = "objdump",
path = llvm_bindir + "/llvm-objdump",
),
tool_path(
name = "strip",
path = llvm_bindir + "/llvm-strip",
),
]
features = [
feature(
name = "default_compiler_flags",
enabled = True,
flag_sets = [
flag_set(
actions = all_compile_actions,
flag_groups = ([
flag_group(
flags = [
"-O2", "-DNDEBUG",
"-Wall", "-Wextra", "-Wpedantic", "-fPIC",
"-std=c++20", "-stdlib=libc++",
],
),
]),
),
],
),
feature(
name = "default_linker_flags",
enabled = True,
flag_sets = [
flag_set(
actions = all_link_actions,
flag_groups = ([
flag_group(
flags = [
"-lc++", "-lc++abi",
"-lm", "-ldl", "-lpthread",
],
),
]),
),
],
),
]
return cc_common.create_cc_toolchain_config_info(
ctx = ctx,
features = features,
cxx_builtin_include_directories = [
llvm_prefix + "/lib/clang/{}/include".format(llvm_version),
llvm_prefix + "/include/x86_64-unknown-linux-gnu/c++/v1",
llvm_prefix + "/include/c++/v1",
"/usr/local/include",
"/usr/include/x86_64-linux-gnu",
"/usr/include",
],
toolchain_identifier = "local",
host_system_name = "local",
target_system_name = "local",
target_cpu = "linux_x86_64",
target_libc = "unknown",
compiler = "clang",
abi_version = "unknown",
abi_libc_version = "unknown",
tool_paths = tool_paths,
)
cc_toolchain_config = rule(
implementation = _impl,
attrs = {},
provides = [CcToolchainConfigInfo],
)
llvm_prefix
Give to your own LLVM installation path.
Building C++20 Libraries and Applications
C++20 libraries and applications are prepared under the code/00/ path of the example in this article:
code/00/
├── BUILD
├── greet
│ ├── BUILD
│ ├── greet.cc
│ └── greet.h
└── main.cc
write binary
main.cc
:
#include <format>
#include <iostream>
#include <string>
#include <string_view>
#include "greet/greet.h"
template <typename... Args>
std::string dyna_print(std::string_view rt_fmt_str, Args&&... args) {
return std::vformat(rt_fmt_str, std::make_format_args(args...));
}
int main() {
std::cout << greet::hello("world") << std::endl;
std::string fmt;
for (int i{}; i != 3; ++i) {
fmt += "{} "; // constructs the formatting string
std::cout << fmt << " : ";
std::cout << dyna_print(fmt, "alpha", 'Z', 3.14, "unused");
std::cout << '\n';
}
}
BUILD
:
load("@rules_cc//cc:defs.bzl", "cc_binary")
cc_binary(
name = "main",
srcs = ["main.cc"],
deps = [
"//code/00/greet:greet",
],
)
write library
greet.h
:
#pragma once
#include <string>
#include <string_view>
namespace greet {
std::string hello(std::string_view who);
} // namespace greet
greet.cc
:
#include "greet.h"
#include <format>
#include <utility>
namespace greet {
std::string hello(std::string_view who) {
return std::format("Hello {}!", std::move(who));
}
} // namespace greet
BUILD
:
load("@rules_cc//cc:defs.bzl", "cc_library")
package(default_visibility = ["//visibility:public"])
cc_library(
name = "greet",
srcs = [
"greet.cc",
],
hdrs = [
"greet.h",
],
)
Bazel build
bazel build --config=clang_config //code/00:main
run the test
$ bazel-bin/code/00/main
Hello world!
{} : alpha
{} {} : alpha Z
{} {} {} : alpha Z 3.14
View dependencies
sudo apt update && sudo apt install graphviz xdot -y
# view
xdot <(bazel query --notool_deps --noimplicit_deps "deps(//code/00:main)" --output graph)
# to svg
dot -Tsvg <(bazel query --notool_deps --noimplicit_deps "deps(//code/00:main)" --output graph) -o 00_main.svg
more references
Bazel Tutorial
Bazel Issue
Project Example
GoCoding personal practice experience sharing, you can pay attention to the public number!
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。