<1> 创建本节实验工程目录,创建完成后,工程目录如下所示:
cyber_demo
|-- cyber_01
|-- demo_main
| |-- BUILD
| |-- main.cc
|–BUILD
|–cyberfile.xml
|–cyber_demo.BUILD
<2> 编写Apollo包管理相关的BUILD和cyberfile.xml文件文件
BUILD
load("//tools/install:install.bzl", "install", "install_src_files")
install(
name = "install",
data = [
"cyber_demo.BUILD",
"cyberfile.xml",
],
deps = [
"//cyber_demo/cyber_01/demo_main:install",
],
)
install_src_files(
name = "install_src",
src_dir = ["."],
dest = "cyber_demo/src",
filter = "*",
deps = [
"//cyber_demo/cyber_01/demo_main:install_src",
]
)
install 规则可以将模块 BUILD 文件中定义的 target安装到本地仓库;
install_src规则根据特定规则直接安装文件到本地仓库,并保留源码目录结构;
cyberfile.xml 是模块描述文件,用来描述一个模块制作成软件包的相关信息,比如包名、版本号、依赖的软件包。本示例cyberfile.xml 内容如下所示:
cyberfile.xml
<package>
<name>cyber_demo</name>
<version>1.0.0</version>
<description>
cyber_demo
</description>
<maintainer email="AD-platform">AD-platform@baidu.com</maintainer>
<type>module</type>
<src_path>//cyber_demo
<license>BSD</license>
<author>Apollo</author>
<depend type="binary" repo_name="cyber">cyber-dev</depend>
<builder>bazel</builder>
</package>
<3> 编写源文件及BUILD文件;
通过main.cc输出 “hello Apollo” 内容。
main.cc
#include
int main(int argc, char const *argv[])
{
apollo::cyber::Init(argv[0]);
AINFO << "hello Apollo";
AWARN << "hello Apollo";
AERROR << "hello Apollo";
AFATAL << "hello Apollo";
return 0;
}
编写完源文件后main.cc后,可以通过实例化 Bazel 的内置 cc_binary规则,将源文件main.cc构建一个独立的可执行二进制文件,BUILD文件内容如下
BUILD
load("//tools/install:install.bzl", "install", "install_src_files")
install(
name = "install",
data = [
"cyber_demo.BUILD",
"cyberfile.xml",
],
deps = [
"//cyber_demo/cyber_01/demo_main:install",
],
)
install_src_files(
name = "install_src",
src_dir = ["."],
dest = "cyber_demo/src",
filter = "*",
deps = [
"//cyber_demo/cyber_01/demo_main:install_src",
]
)
<4> 编译代码目录
在apollo_workspace目录下执行buildtool编译指令
buildtool build -p cyber_demo
编译完成后,系统会在/opt/apollo/neo/bin目录下生成可执行文件main,如下图所示:
为了能够看到结果,通过以下命令将输出结果打印到窗口,命令如下:
export GLOG_alsologtostderr=1
<5> 运行可执行文件
在/opt/apollo/neo/bin目录下,执行以下命令:
./main
执行完成后,可以看到命令行窗口打印出了"hello Apollo"的内容。