前后端编程语言杂谈
参考文献链接
https://mp.weixin.qq.com/s/NfkPxf-3gJURE2TIbM6InA
https://mp.weixin.qq.com/s/jjT0x99ht8xtfWmzL-0R1A
https://mp.weixin.qq.com/s/YAhX5895meSHENZSAe184w
https://mp.weixin.qq.com/s/DJzKXDYz4fD8zc1Ln_dK7g
C++20 以 Bazel & Clang 开始
C++20 如何以 Bazel & Clang 进行构建呢?
本节将介绍:
• Bazel[1] 构建系统的安装
• LLVM[2] 编译系统的安装
Clang[3] is an “LLVM native” C/C++/Objective-C compiler
• Bazel Clang 工具链的配置
• C++20 库与应用的构建
示例可见:https://github.com/ikuokuo/start-cpp20
Ubuntu 20 上进行的实践,Windows 可以用 WSL 准备环境。
安装 Bazel,以二进制方式
Bazelisk[4] 是安装 Bazel 的推荐方式,安装二进制发布[5]即可:
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
更多方式,可见官方文档[6]。进一步,推荐安装 buildtools[7],下载后软链一下:
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
Bazel 如何构建 C++ 项目,可见 Start Bazel[8] 笔记。
安装 LLVM,以源码方式
Clang 有关 std::fromat 文本格式化的特性,默认未开启:
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 特性,编译器支持情况:
• C++ compiler support[9]
• libc++ C++20 Status[10]
因此,这里以源码方式安装 LLVM,需要构建 Clang & libc++:
• Building Clang[11]
• Building libc++[12]
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 -jnproc
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 安装路径自己决定。最后,编译测试:
cat <<-EOF > hello.cc
#include
#include
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
安装 LLVM,以二进制方式
可省略该节。本节实践未用此方式,因为想开启更多 C++20 特性。这里仅作记录,有需要可参考。
方式 1. 安装二进制发布[13]:
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
方式 2. 用 apt 进行安装:https://apt.llvm.org/
方式 3. 用已配好的工具链:LLVM toolchain for Bazel[14]
配置 Clang 工具链
本节依照 Bazel Tutorial: Configure C++ Toolchains[15] 步骤配置的 Clang 工具链,最后项目根目录会多如下文件:
• WORKSPACE[16]
• .bazelrc[17]
• toolchain/BUILD[18]
• toolchain/cc_toolchain_config.bzl[19]
WORKSPACE 表示 Bazel 工作区,内容空。
.bazelrc 允许 --config=clang_config 启用 Clang 工具链:
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 配置 Clang 工具链信息:
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 配置 Clang 工具链规则:
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_in