上文我们使用了gitrepository,Bazel可以直接从github下载指定的外部依赖库,非常方便。不过,需要你的系统里安装git,这个要求对于那些使用其他版本控制软件,如svn的人来说该怎么办呢?Bazel提供了httparchive,你可以把某个Bazel项目repository打包压缩成一个文件。 咱们先创建这个Bazel项目repository,还是老规矩,helloworldlib。 helloworldlibBUILDload(rulescccc:defs。bzl,cclibrary)cclibrary(namehelloworldlib,srcs〔srchelloworld。cc〕,hdrs〔includehelloworld。h〕,visibility〔visibility:public〕,) helloworldlibincludehelloworld。hpragmaonceincludestringstd::stringhelloworld(); helloworldlibsrchelloworld。ccincludeincludehelloworld。hstd::stringhelloworld(){returnHello,world!;} 打开终端,将当前目录切换到helloworldlib,运行:tarzcvfhelloworld。tar。gz 这样我们将helloworldlib中的两个文件和两个文件夹打包压缩成了一个文件,然后我把它上传到了我的个人网站上(bazellearning。zhouxd。com),不过你可能不一定能够访问到这个url,你可以放到你自己的网站上。Bazel会自动解析url,下载并解压这个文件,将里面的内容构建成一个repository。 好了,我们再次建立一个Bazel项目来使用它。 helloworldhttparchiveWORKSPACEload(bazeltoolstoolsbuilddefsrepo:http。bzl,httparchive)httparchive(namehelloworldrepo,urls〔file:homezhouxdtemphelloworldarchivehelloworld。tar。gz,https:bazellearning。zhouxd。comhelloworld。tar。gz〕,) 这里urls我给了两个,防止万一我的网站挂了,还可以从本地的压缩包中完成构建。多说一点,如果你得到的某个外部项目的压缩包文件的目录结构,WORKSPACE并不在顶级目录中,而是在某个目录下,比如说放在了helloworldlib1。2。3里面,你可以使用stripprefix属性来指定略过这个目录名。httparchive(namehelloworldrepo,urls〔file:homezhouxdtemphelloworldarchivehelloworld。tar。gz,https:bazellearning。zhouxd。comhelloworld。tar。gz〕,stripprefixhelloworldlib。1。2。3,) 为了确保压缩包的数据完整性,还可以通过sha256这个属性来指定压缩包的签名。如果你懒得用openssl来生成这个签名,用Bazel构建时它会告诉你这个压缩包的签名,你把它复制下来,然后添加sha256属性,其值设置为这个字符串就可以了。 helloworldhttparchivesrcBUILDload(rulescccc:defs。bzl,ccbinary)ccbinary(namehelloworldmain,srcs〔helloworldmain。cc〕,deps〔helloworldrepo:helloworldlib,〕,) helloworldhttparchivesrchelloworldmain。ccincludeincludehelloworld。hincludeiostreamintmain(){std::couthelloworld()std::endl;return0;} 构建,运行! 源码获取:https:github。comzhouxindongbazellearning。git