0%

bazel 笔记 01

Bazel - a fast, scalable, multi-language and extensible build system

在使用 bazel 许久之后,开始做一下记录,以记录一些奇奇怪怪的处理方式(?)。

引入第三方源码库

bazel 支持引入第三方库,例如 c/c++ 引入 cmakemakeconfigure等方式编译的库,或者已编译完成等动态库或静态库。

WORKSPACE.bazel:

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
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")

# 使用 rules_foreign_cc 进行第三方库引入
http_archive(
name = "rules_foreign_cc",
sha256 = "e14a159c452a68a97a7c59fa458033cc91edb8224516295b047a95555140af5f",
strip_prefix = "rules_foreign_cc-0.4.0",
url = "https://github.com/bazelbuild/rules_foreign_cc/archive/0.4.0.tar.gz",
)
load("@rules_foreign_cc//foreign_cc:repositories.bzl", "rules_foreign_cc_dependencies")
# https://docs.aspect.dev/bazelbuild/rules_foreign_cc/0.3.0/readme#rules_foreign_cc_dependencies
rules_foreign_cc_dependencies(
# macos 下远程编译需要使用该选项
register_default_tools = False,
# macos 下远程编译需要使用该选项
register_built_tools = False,
)

# 引入 sqlite
sqlite_all_content = """filegroup(name = "all", srcs = ["sqlite3.c","sqlite3.h"], visibility = ["//visibility:public"])"""
maybe(
http_archive,
name = "sqlite",
build_file_content = sqlite_all_content,
sha256 = "bd90c3eb96bee996206b83be7065c9ce19aef38c3f4fb53073ada0d0b69bbce3",
strip_prefix = "sqlite-autoconf-3360000",
urls = ["https://sqlite.org/2021/sqlite-autoconf-3360000.tar.gz"],
)

# 引入 openssl
all_content = """filegroup(name = "all", srcs = glob(["**"]), visibility = ["//visibility:public"])"""
maybe(
http_archive,
name = "openssl",
build_file_content = all_content,
sha256 = "b92f9d3d12043c02860e5e602e50a73ed21a69947bcc74d391f41148e9f6aa95",
strip_prefix = "openssl-OpenSSL_1_1_1k",
urls = [
"https://github.com/openssl/openssl/archive/OpenSSL_1_1_1k.tar.gz",
],
)

sqlite 编译文件BUILD.bazel:

1
2
3
4
5
6
7
cc_library(
name = "sqlite3",
srcs = ["@sqlite//:all"],
hdrs = ["@sqlite//:all"],
visibility = ["//visibility:public"],
deps = [],
)

openssl 编译文件BUILD.bazel:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
load("@rules_foreign_cc//foreign_cc:defs.bzl", "configure_make")
CONFIGURE_OPTIONS = [
"no-shared",
"no-sse2",
"-static",
]
configure_make(
name = "openssl",
# 根据实际情况选择 Configure 还是 config
configure_command = "config",
configure_options = CONFIGURE_OPTIONS,
# 根据情况引入 pthread
linkopts = ["-lpthread"],
lib_source = "@openssl//:all",
out_static_libs = [
"libssl.a",
"libcrypto.a",
],
targets = [
"build_libs",
"install_dev",
],
visibility = ["//visibility:public"],
)

更多的例子参考: https://github.com/bazelbuild/rules_foreign_cc/tree/main/examples/third_party

bazel 支持远程编译测试

远程编译建议使用 bazel-buildfarm 友好地支持。

bazel-buildfarm 不支持账号密码,但是可以使用nginx auth_basic作为验证:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
server {
listen 801 http2;
server_name _;
client_max_body_size 512M;
grpc_buffer_size 100M;
grpc_read_timeout 1d;
grpc_send_timeout 1d;
sendfile on;

location / {
auth_basic "Please enter your username and password";
auth_basic_user_file /etc/bazel-buildfarm/buildfarm.htpasswd;
grpc_pass grpc://localhost:8980;
proxy_buffering off;
proxy_send_timeout 1200;
proxy_read_timeout 1200;
}
}

配合 .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_archivepatch_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
    62
    all_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",
    ],
    )