上文我们使用了git_repository,Bazel可以直接从github下载指定的外部依赖库,非常方便。不过,需要你的系统里安装git,这个要求对于那些使用其他版本控制软件,如svn的人来说该怎么办呢?Bazel提供了http_archive,你可以把某个Bazel项目repository打包压缩成一个文件。
咱们先创建这个Bazel项目repository,还是老规矩,hello-world-lib。
hello-world-lib/BUILD
load("@rules_cc//cc:defs.bzl", "cc_library")
cc_library(
name = "hello-world-lib",
srcs = ["src/hello-world.cc"],
hdrs = ["include/hello-world.h"],
visibility = ["//visibility:public"],
)
hello-world-lib/include/hello-world.h
#pragma once
#include
std::string hello_world();
hello-world-lib/src/hello-world.cc
#include "include/hello-world.h"
std::string hello_world() { return "Hello, world!"; }
打开终端,将当前目录切换到hello-world-lib,运行:tar -zcvf hello-world.tar.gz *
这样我们将hello-world-lib中的两个文件和两个文件夹打包压缩成了一个文件,然后我把它上传到了我的个人网站上(bazel-learning.zhouxd.com),不过你可能不一定能够访问到这个url,你可以放到你自己的网站上。Bazel会自动解析url,下载并解压这个文件,将里面的内容构建成一个repository。
好了,我们再次建立一个Bazel项目来使用它。
hello-world-http-archive/WORKSPACE
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name = "hello-world-repo",
urls = ["file:/home/zhouxd/temp/hello-world-archive/hello-world.tar.gz",
"https://bazel-learning.zhouxd.com/hello-world.tar.gz"],
)
这里urls我给了两个,防止万一我的网站挂了,还可以从本地的压缩包中完成构建。多说一点,如果你得到的某个外部项目的压缩包文件的目录结构,WORKSPACE并不在顶级目录中,而是在某个目录下,比如说放在了hello-world-lib-1.2.3里面,你可以使用strip_prefix属性来指定略过这个目录名。
http_archive(
name = "hello-world-repo",
urls = ["file:/home/zhouxd/temp/hello-world-archive/hello-world.tar.gz",
"https://bazel-learning.zhouxd.com/hello-world.tar.gz"],
strip_prefix = "hello-world-lib.1.2.3",
)
为了确保压缩包的数据完整性,还可以通过sha256这个属性来指定压缩包的签名。如果你懒得用openssl来生成这个签名,用Bazel构建时它会告诉你这个压缩包的签名,你把它复制下来,然后添加sha256属性,其值设置为这个字符串就可以了。
hello-world-http-archive/src/BUILD
load("@rules_cc//cc:defs.bzl", "cc_binary")
cc_binary(
name = "hello-world-main",
srcs = ["hello-world-main.cc"],
deps = [
"@hello-world-repo//:hello-world-lib",
],
)
hello-world-http-archive/src/hello-world-main.cc
#include "include/hello-world.h"
#include
int main() {
std::cout << hello_world() << std::endl;
return 0;
}
构建,运行!
源码获取:https://github.com/zhouxindong/bazel-learning.git
页面更新:2024-03-31
本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828
© CopyRight 2020-2024 All Rights Reserved. Powered By 71396.com 闽ICP备11008920号-4
闽公网安备35020302034903号