Bazel - a fast, scalable, multi-language and extensible build system
在使用 bazel
许久之后,开始做一下记录,以记录一些奇奇怪怪的处理方式(?)。
引入第三方源码库
bazel
支持引入第三方库,例如 c
/c++
引入 cmake
、make
、configure
等方式编译的库,或者已编译完成等动态库或静态库。
WORKSPACE.bazel:
1 | load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") |
sqlite 编译文件BUILD.bazel
:
1 | cc_library( |
openssl 编译文件BUILD.bazel
:
1 | load("@rules_foreign_cc//foreign_cc:defs.bzl", "configure_make") |
更多的例子参考: https://github.com/bazelbuild/rules_foreign_cc/tree/main/examples/third_party
bazel
支持远程编译
、测试
。
远程编译建议使用 bazel-buildfarm 友好地支持。
bazel-buildfarm
不支持账号密码,但是可以使用nginx
auth_basic
作为验证:
1 | server { |
配合 .netrc
自动输入账号密码。参考:The .netrc file
The .netrc file contains login and initialization information used by the auto-login process.
It generally resides in the user’s home directory,
but a location outside of the home directory can be set using the environment variable NETRC.
Both locations are overridden by the command line option -N.
The selected file must be a regular file, or access will be denied.
注意事项:
如果在macos编译需要GUI等库等情况下,无法执行,且提示:
Symbol not found: "_NSApp"
,则需要加入链接参数:linkopts = ["-framework Cocoa","-framework OpenGL"]
,便可以执行。如果使用
rules_foreign_cc
引入外部库,在进行编译的时候,出现大量std
相关未定义错误,则是未加入c++
链接导致,暂时不知道原因,但是可以通过maybe
/http_archive
的patch_cmds
功能,配合sed
自动插入-lstdc++
等所需链接,例如:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62all_content = """filegroup(name = "all", srcs = glob(["**"]), visibility = ["//visibility:public"])"""
maybe(
http_archive,
name = "mysql",
build_file_content = all_content,
patch_cmds = [
"find . -type f \\( -name 'WORKSPACE' -o -name '*.bzl' -o -name 'BUILD' \\) -exec rm -f {} +"
],
sha256 = "b5f1472a970897196bf18effb00e202086b0e11e2d15ab61932c3c24e943f864",
strip_prefix = "mysql-connector-cpp-8.0.25",
urls = [
"https://github.com/mysql/mysql-connector-cpp/archive/8.0.25.tar.gz",
],
)
maybe(
http_archive,
name = "mysql_ubuntu",
build_file_content = all_content,
patch_cmds = [
"find . -type f \\( -name 'WORKSPACE' -o -name '*.bzl' -o -name 'BUILD' \\) -exec rm -f {} +",
"""sed -i'' -e '120a \\
TARGET_LINK_LIBRARIES(save_linker_opts -lstdc++ )
' cmake/libutils.cmake""",
"sed -i.bak 's/protobuf protobuf-lite/protobuf protobuf-lite -lstdc++/g' cdk/protobuf/CMakeLists.txt",
"sed -i.bak 's/protoclib protobuf/protoclib protobuf -lstdc++/g' cdk/protobuf/CMakeLists.txt",
"sed -i.bak 's/protoc protoclib/protoc protoclib -lstdc++/g' cdk/protobuf/CMakeLists.txt",
"sed -ibuildinfo.cmake 's/NOT EXISTS INFO_SRC/NOT EXISTS \"\\$\\{CMAKE_BINARY_DIR\\}\\/INFO_SRC\"/g' buildinfo.cmake",
"sed -ibuildinfo.cmake 's/CMAKE_SOURCE_DIR/CMAKE_BINARY_DIR/g' buildinfo.cmake",
],
sha256 = "b5f1472a970897196bf18effb00e202086b0e11e2d15ab61932c3c24e943f864",
strip_prefix = "mysql-connector-cpp-8.0.25",
urls = [
"https://github.com/mysql/mysql-connector-cpp/archive/8.0.25.tar.gz",
],
)
maybe(
http_archive,
name = "mysql_mingw",
build_file_content = all_content,
patch_cmds = [
"find . -type f \\( -name 'WORKSPACE' -o -name '*.bzl' -o -name 'BUILD' \\) -exec rm -f {} +",
"""sed -i'' -e '120a \\
TARGET_LINK_LIBRARIES(save_linker_opts -static-libgcc -static-libstdc++ -Wl,-Bstatic -lstdc++ -lpthread -Wl,-Bdynamic)
' cmake/libutils.cmake""",
"sed -i.bak 's/protobuf protobuf-lite/protobuf protobuf-lite -lstdc++/g' cdk/protobuf/CMakeLists.txt",
"sed -i.bak 's/COMMAND Protobuf::protoc/COMMAND wine #Protobuf::protoc/g' cdk/cmake/DepFindProtobuf.cmake",
"sed -i.bak 's/ARGS --cpp_out \"/ARGS ..\\/..\\/protobuf\\/protoc.exe --cpp_out \"Z:/g' cdk/cmake/DepFindProtobuf.cmake",
"sed -i.bak 's/-I \\${ABS_PATH} \\${ABS_FIL}/-I Z:\\${ABS_PATH} Z:\\${ABS_FIL}/g' cdk/cmake/DepFindProtobuf.cmake",
"sed -i.bak 's/--proto_path=/--proto_path=Z:/g' cdk/cmake/DepFindProtobuf.cmake",
"sed -i.bak 's/RULE_LAUNCH_LINK \"/RULE_LAUNCH_LINK \"wine /g' cmake/libutils.cmake",
"sed -i.bak 's/protoclib protobuf/protoclib protobuf -lstdc++/g' cdk/protobuf/CMakeLists.txt",
"sed -i.bak 's/protoc protoclib/protoc protoclib -lstdc++/g' cdk/protobuf/CMakeLists.txt",
"sed -ibuildinfo.cmake 's/NOT EXISTS INFO_SRC/NOT EXISTS \"\\$\\{CMAKE_BINARY_DIR\\}\\/INFO_SRC\"/g' buildinfo.cmake",
"sed -ibuildinfo.cmake 's/CMAKE_SOURCE_DIR/CMAKE_BINARY_DIR/g' buildinfo.cmake",
"sed -i.bak 's/file(STRINGS \"${BUILD_LOG}\" DEPS)/file(STRINGS \"${BUILD_LOG}\" DEPS)\\n set(DEPS \"\\$\\{DEPS\\};xapi\\/libxapi.a;devapi\\/libdevapi.a;common\\/libcommon.a;cdk\\/core\\/libcdk.a;cdk\\/mysqlx\\/libcdk_mysqlx.a;cdk\\/protocol\\/mysqlx\\/libcdk_proto_mysqlx.a;cdk\\/extra\\/zlib\\/libz.a;cdk\\/extra\\/liblz4.a;cdk\\/extra\\/zstd\\/libzstd.a;cdk\\/parser\\/libcdk_parser.a;cdk\\/foundation\\/libcdk_foundation.a;cdk\\/protobuf\\/libprotobuf-lite.a;\"\\)/g' cmake/libutils/merge_archives.cmake.in",
],
sha256 = "b5f1472a970897196bf18effb00e202086b0e11e2d15ab61932c3c24e943f864",
strip_prefix = "mysql-connector-cpp-8.0.25",
urls = [
"https://github.com/mysql/mysql-connector-cpp/archive/8.0.25.tar.gz",
],
)