bazel 条件编译选择不同的库

WORKSPACE文件

load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository", "new_git_repository")
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

git_repository(
    name = "com_google_protobuf",
    remote = "https://github.com/protocolbuffers/protobuf.git",
    tag = "v3.9.0",
)

http_archive(
    name = "com_github_brpc_brpc",
    urls = [
        "https://github.com/apache/incubator-brpc/archive/1.0.0.tar.gz",
    ],
    strip_prefix = "brpc-1.0.0",
)

http_archive(
    name = "com_github_grpc_grpc",
    urls = [
        "https://github.com/grpc/grpc/archive/v1.56.0.tar.gz",
    ],
    strip_prefix = "grpc-1.56.0",
)
load("@com_github_grpc_grpc//bazel:grpc_deps.bzl", "grpc_deps")
grpc_deps()
load("@com_github_grpc_grpc//bazel:grpc_extra_deps.bzl", "grpc_extra_deps")
grpc_extra_deps()

BUILD文件

package(default_visibility = ["//visibility:public"])

#设置条件
config_setting(
    name = "with_brpc_client",
    define_values = {"client_channel": "brpc"},
    visibility = ["//visibility:public"],
)

config_setting(
    name = "with_grpc_client",
    define_values = {"client_channel": "grpc"},
    visibility = ["//visibility:public"],
)

cc_binary(
    name = "memory_leak",
    linkopts = ["-ltcmalloc",],
    srcs = ["memory_leak.cpp"],
    deps = [
        "@com_google_protobuf//:protobuf",
    ] + select({
        ":with_brpc_client": ["@com_github_brpc_brpc//:brpc"],
        ":with_grpc_client": ["@com_github_grpc_grpc//:grpc"],
        "//conditions:default": ["@com_github_brpc_brpc//:brpc"], #默认brpc
    }),
)

.bazelrc文件

build --copt=-O2
build --copt=-g
build --cxxopt=-std=c++17
build --define=client_channel=grpc

通过define选择用grpc还是brpc

memory_leak.cpp文件

#include <iostream>
  
int func() {
    int *p = new int(10);
    return 0;
}

int main() {
    std::cout << "memory leak test" << std::endl;
    return func();
}

注意这个只是模版,由于缺少好多库,导致brpc编译不过

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在x86机器上使用Bazel进行交叉编译,需要进行以下几个步骤: 1. 安装交叉编译工具链。例如对于armhf架构,可以使用`sudo apt-get install crossbuild-essential-armhf`命令进行安装。 2. 在WORKSPACE文件中添加以下内容来指定交叉编译工具链路径: ``` new_local_repository( name = "armhf_compiler", path = "/usr/bin", build_file = "compiler.BUILD", exec_paths = [ "arm-linux-gnueabihf-gcc", "arm-linux-gnueabihf-g++", "arm-linux-gnueabihf-ld", "arm-linux-gnueabihf-ar", "arm-linux-gnueabihf-objdump", "arm-linux-gnueabihf-objcopy", "arm-linux-gnueabihf-nm", "arm-linux-gnueabihf-strip", ], ) ``` 然后在`compiler.BUILD`文件中定义这些工具的路径: ``` package(default_visibility = ["//visibility:public"]) filegroup( name = "binaries", srcs = [ "@armhf_compiler//arm-linux-gnueabihf-gcc", "@armhf_compiler//arm-linux-gnueabihf-g++", "@armhf_compiler//arm-linux-gnueabihf-ld", "@armhf_compiler//arm-linux-gnueabihf-ar", "@armhf_compiler//arm-linux-gnueabihf-objdump", "@armhf_compiler//arm-linux-gnueabihf-objcopy", "@armhf_compiler//arm-linux-gnueabihf-nm", "@armhf_compiler//arm-linux-gnueabihf-strip", ], ) ``` 注意将`path`指定为交叉编译工具链的路径。 3. 在`WORKSPACE`文件中添加以下内容来指定交叉编译的平台和架构: ``` load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") http_archive( name = "rules_cc", sha256 = "d8b4d9d6a99a3c5c13b6e82e1f5f3c380e6c764c7a13ecb3e9d753cdeae7c1c1", strip_prefix = "rules_cc-0.13.1", urls = ["https://github.com/bazelbuild/rules_cc/archive/0.13.1.tar.gz"], ) load("@rules_cc//cc:repositories.bzl", "cc_repositories") cc_repositories() load("@rules_cc//cc:toolchains.bzl", "register_toolchains") register_toolchains( "@rules_cc//cc/toolchain:cross_armhf", ) ``` 这里指定了交叉编译的平台为`armhf`。 4. 在`BUILD`文件中指定编译选项,例如: ``` cc_binary( name = "hello-world", srcs = ["hello-world.cc"], copts = [ "-march=armv7-a", "-mfpu=neon-vfpv4", "-mfloat-abi=hard", ], linkopts = [ "-static-libgcc", "-static-libstdc++", ], target_compatible_with = ["@rules_cc//cc:toolchain_type=//cc/toolchain:cross_armhf"], ) ``` 这里指定了编译选项为`-march=armv7-a`,`-mfpu=neon-vfpv4`和`-mfloat-abi=hard`,并且链接选项为`-static-libgcc`和`-static-libstdc++`。 5. 使用以下命令进行交叉编译: ``` bazel build --platforms=@io_bazel_rules_cc//cc/platform:linux_armhf //path/to:target ``` 这里的`//path/to:target`是你要构建的目标文件的路径。在上面的例子中,如果你要编译`hello-world`程序,那么目标就是`//:hello-world`。 希望以上步骤可以帮助你在x86机器上使用Bazel进行交叉编译。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值