1、为BehaviorTree.CPP编写BUILD.gn进行Rom集成
- Rom集成笔者开发环境:
- wsl2+ubuntu18.04
- OpenHarmony 3.2 release 源码
- 润和大禹200开发板
2、修改build/subsystem_config.json,新增子系统behaviortree定义
在源码/build/subsystem_config.json中增加子系统behaviortree。
"behaviortree": {
"path": "third_party/behaviortree",
"name": "behaviortree"
}
3、修改vendor/hihope/rk3568/config.json文件将behaviortree添加至rk3568开发板
{
"subsystem": "behaviortree",
"components": [
{
"component": "behaviortree",
"features": []
}
]
}
4、在OpenHarmony标准系统源码下third_party下放置BehaviorTree.CPP源码
- BehaviorTree.CPP 源码版本为4.1.1 :https://github.com/BehaviorTree/BehaviorTree.CPP/tree/4.1.1。
- 并且文件夹名称修改为behaviortree。
5、third_party/behaviortree目录下添加bundle.json文件
- 特别说明:ohos.build不再使用,OpenHarmony源码中全部使用bundle.json。
- bundle.json文件:
{
"name": "@ohos/behaviortree",
"description": "",
"version": "",
"license": "",
"publishAs": "",
"segment": {
"destPath": "third_party/behaviortree"
},
"dirs": {},
"scripts": {},
"readmePath": {
},
"component": {
"name": "behaviortree",
"subsystem": "behaviortree",
"syscap": [],
"features": [],
"adapted_system_type": [],
"rom": "",
"ram": "",
"deps": {
"components": [],
"third_party": []
},
"build": {
"sub_component": [
"//third_party/behaviortree:lexy_file",
"//third_party/behaviortree:bt_sample_nodes",
"//third_party/behaviortree:behaviortree_cpp",
"//third_party/behaviortree:dummy_nodes_dyn",
"//third_party/behaviortree:crossdoor_nodes_dyn",
"//third_party/behaviortree:movebase_node_dyn",
"//third_party/behaviortree:t01_build_your_first_tree",
"//third_party/behaviortree:t02_basic_ports",
"//third_party/behaviortree:t03_generic_ports",
"//third_party/behaviortree:t05_crossdoor",
"//third_party/behaviortree:t04_reactive_sequence",
"//third_party/behaviortree:t06_subtree_port_remapping",
"//third_party/behaviortree:t07_load_multiple_xml",
"//third_party/behaviortree:t08_additional_node_args",
"//third_party/behaviortree:t09_scripting",
"//third_party/behaviortree:t10_observer",
"//third_party/behaviortree:t11_replace_rules",
"//third_party/behaviortree:ex01_wrap_legacy",
"//third_party/behaviortree:ex02_runtime_ports",
"//third_party/behaviortree:ex03_ncurses_manual_selector",
"//third_party/behaviortree:ex04_waypoints"
],
"inner_kits": [],
"test": []
}
}
}
6、BehaviorTree.CPP编译gn化,在third_party/behaviortree下添加BUILD.gn脚本文件
- third_party/behaviortree/BUILD.gn配置的模块有
- so
- libbehaviortree_cpp.z.so
- libmovebase_node_dyn.z.so
- libcrossdoor_nodes_dyn.z.so
- libdummy_nodes_dyn.z.so
- .a
- liblexy_file.a
- libbt_sample_nodes.a
- 可执行文件
- t01_build_your_first_tree
- t02_basic_ports
- t03_generic_ports
- t04_reactive_sequence
- t05_crossdoor
- t06_subtree_port_remapping
- t07_load_multiple_xml
- t08_additional_node_args
- t09_scripting
- t10_observer
- t11_replace_rules
- ex01_wrap_legacy
- ex02_runtime_ports
- ex03_ncurses_manual_selector
- ex04_waypoints
- 模块之间的依赖关系
- libbehaviortree_cpp.z.so依赖lexy_file.a
- libcrossdoor_nodes_dyn.z.so依赖libbehaviortree_cpp.z.so
- libmovebase_node_dyn.z.so依赖libbehaviortree_cpp.z.so
- 所有的可执行文件都依赖libbt_sample_nodes.a和libbehaviortree_cpp.z.so
- third_party/behaviortree/BUILD.gn文件如下:
import("//build/ohos.gni")
##############################################################################
# 公共配置
config("public_config"){
ldflags = [
#"-lstdc++",
#用-lc++替代-lstdc++
"-lc++",
"-Wl",
"-lm",
"-lc",
"-lpthread",
]
}
##############################################################################
# liblexy_file.a
config("lexy_file_config"){
#cflags_cc是用来存储专门针对 C++ 语言编译器的选项,只会被 C++ 编译器使用。
cflags_cc = [
"-O3",
"-DNDEBUG",
"-Wpedantic",
"-pedantic-errors",
"-Werror",
"-Wall",
"-Wextra",
"-Wconversion",
"-Wsign-conversion",
"-Wno-parentheses",
"-Wno-unused-local-typedefs",
"-Wno-array-bounds",
"-Wno-maybe-uninitialized",
"-Wno-restrict",
"-std=gnu++20",
]
}
ohos_static_library("lexy_file") {
output_name = "lexy_file" # 可选,模块输出名
sources = [
"//third_party/behaviortree/3rdparty/lexy/src/input/file.cpp",
]
defines = [
]
configs = [
":lexy_file_config",
":public_config",
]
include_dirs = [
"3rdparty/lexy/include",
]
part_name = "behaviortree"
subsystem_name = "behaviortree"
}
##############################################################################
# libbt_sample_nodes.a
config("bt_sample_nodes_config"){
#cflags_cc是用来存储专门针对 C++ 语言编译器的选项,只会被 C++ 编译器使用。
cflags_cc = [
"-O3",
"-DNDEBUG",
"-Wpedantic",
"-std=gnu++17",
# 为了消除编译报错添加的
"-fexceptions",
"-frtti",
"-Wno-unused-function",
]
}
ohos_static_library("bt_sample_nodes") {
sources = [
"sample_nodes/crossdoor_nodes.cpp",
"sample_nodes/dummy_nodes.cpp",
"sample_nodes/movebase_node.cpp",
]
defines = [
]
configs = [
":bt_sample_nodes_config",
":public_config",
]
include_dirs = [
"include",
"sample_nodes"
]
part_name = "behaviortree"
subsystem_name = "behaviortree"
}
##############################################################################
# libbehaviortree_cpp.so
config("behaviortreecpp_config"){
cflags_cc = [
# 为了编译libbehaviortree_cpp.so原生库中添加的编译器标志
"-O3",
"-DNDEBUG",
"-fPIC",
"-Wpedantic",
"-Wall",
"-Wextra",
"-std=gnu++20",
# 为了消除shared_library.cpp编译报错添加-fexceptions
"-fexceptions",
"-frtti",
"-Wno-deprecated-volatile",
"-Wno-unused-lambda-capture",
]
include_dirs = [
".",
"include",
"3rdparty",
"3rdparty/lexy/include",
]
}
ohos_shared_library("behaviortree_cpp") {
output_name = "behaviortree_cpp" # 可选,模块输出名
sources = [
"src/action_node.cpp",
"src/basic_types.cpp",
"src/behavior_tree.cpp",
"src/blackboard.cpp",
"src/bt_factory.cpp",
"src/decorator_node.cpp",
"src/condition_node.cpp",
"src/control_node.cpp",
"src/shared_library.cpp",
"src/tree_node.cpp",
"src/script_parser.cpp",
"src/json_export.cpp",
"src/xml_parsing.cpp",
"src/actions/test_node.cpp",
"src/decorators/inverter_node.cpp",
"src/decorators/repeat_node.cpp",
"src/decorators/retry_node.cpp",
"src/decorators/subtree_node.cpp",
"src/decorators/delay_node.cpp",
"src/controls/if_then_else_node.cpp",
"src/controls/fallback_node.cpp",
"src/controls/parallel_node.cpp",
"src/controls/reactive_sequence.cpp",
"src/controls/reactive_fallback.cpp",
"src/controls/sequence_node.cpp",
"src/controls/sequence_star_node.cpp",
"src/controls/switch_node.cpp",
"src/controls/while_do_else_node.cpp",
"src/loggers/bt_cout_logger.cpp",
"src/loggers/bt_file_logger.cpp",
"src/loggers/bt_minitrace_logger.cpp",
"src/loggers/bt_observer.cpp",
"3rdparty/tinyxml2/tinyxml2.cpp",
"3rdparty/minitrace/minitrace.cpp",
"src/shared_library_UNIX.cpp",
]
defines = [
"LEXY_HAS_UNICODE_DATABASE=1",
"behaviortree_cpp_EXPORTS",
]
configs = [
":behaviortreecpp_config",
":public_config",
]
deps = [
"//third_party/behaviortree:lexy_file",
]
install_enable = true
install_images = [
"system",
"ramdisk",
"updater",
]
part_name = "behaviortree"
subsystem_name = "behaviortree"
}
##############################################################################
# libdummy_nodes_dyn.so
config("dummy_nodes_dyn_config"){
cflags_cc = [
# 为了编译libbehaviortree_cpp.so原生库中添加的编译器标志
"-O3",
"-DNDEBUG",
"-fPIC",
"-Wpedantic",
"-std=gnu++17",
# 为了消除shared_library.cpp编译报错添加-fexceptions
"-fexceptions",
"-frtti",
"-Wno-deprecated-volatile",
"-Wno-unused-lambda-capture",
]
include_dirs = [
"include",
"sample_nodes"
]
}
ohos_shared_library("dummy_nodes_dyn") {
output_name = "dummy_nodes_dyn" # 可选,模块输出名
sources = [
"sample_nodes/dummy_nodes.cpp",
]
defines = [
"BT_PLUGIN_EXPORT",
"dummy_nodes_dyn_EXPORTS",
]
configs = [
":dummy_nodes_dyn_config",
":public_config",
]
deps = [
"//third_party/behaviortree:behaviortree_cpp",
]
install_enable = true
install_images = [
"system",
"ramdisk",
"updater",
]
part_name = "behaviortree"
subsystem_name = "behaviortree"
}
##############################################################################
# libcrossdoor_nodes_dyn.so
config("crossdoor_nodes_dyn_config"){
cflags_cc = [
# 为了编译libbehaviortree_cpp.so原生库中添加的编译器标志
"-O3",
"-DNDEBUG",
"-fPIC",
"-Wpedantic",
"-std=gnu++17",
# 为了消除shared_library.cpp编译报错添加-fexceptions
"-fexceptions",
"-frtti",
"-Wno-deprecated-volatile",
"-Wno-unused-lambda-capture",
]
include_dirs = [
"include",
"sample_nodes"
]
}
ohos_shared_library("crossdoor_nodes_dyn") {
output_name = "crossdoor_nodes_dyn" # 可选,模块输出名
sources = [
"sample_nodes/crossdoor_nodes.cpp",
]
defines = [
"BT_PLUGIN_EXPORT",
"crossdoor_nodes_dyn_EXPORTS",
]
configs = [
":crossdoor_nodes_dyn_config",
":public_config",
]
deps = [
"//third_party/behaviortree:behaviortree_cpp",
]
install_enable = true
install_images = [
"system",
"ramdisk",
"updater",
]
part_name = "behaviortree"
subsystem_name = "behaviortree"
}
##############################################################################
# libmovebase_node_dyn.so
config("movebase_node_dyn_config"){
cflags_cc = [
# 为了编译libbehaviortree_cpp.so原生库中添加的编译器标志
"-O3",
"-DNDEBUG",
"-fPIC",
"-Wpedantic",
"-std=gnu++17",
# 为了消除shared_library.cpp编译报错添加-fexceptions
"-fexceptions",
"-frtti",
"-Wno-deprecated-volatile",
"-Wno-unused-lambda-capture",
]
include_dirs = [
"include",
"sample_nodes"
]
}
ohos_shared_library("movebase_node_dyn") {
output_name = "movebase_node_dyn" # 可选,模块输出名
sources = [
"sample_nodes/movebase_node.cpp",
]
defines = [
"BT_PLUGIN_EXPORT",
"movebase_node_dyn_EXPORTS",
]
configs = [
":movebase_node_dyn_config",
":public_config",
]
deps = [
"//third_party/behaviortree:behaviortree_cpp",
]
install_enable = true
install_images = [
"system",
"ramdisk",
"updater",
]
part_name = "behaviortree"
subsystem_name = "behaviortree"
}
##############################################################################
# t01_build_your_first_tree
config("executable_public_config"){
cflags_cc = [
# 为了编译libbehaviortree_cpp.so原生库中添加的编译器标志
"-O3",
"-DNDEBUG",
"-Wpedantic",
"-std=gnu++17",
# 为了消除shared_library.cpp编译报错添加-fexceptions
"-fexceptions",
"-frtti",
"-Wno-deprecated-volatile",
"-Wno-unused-lambda-capture",
]
include_dirs = [
"include",
"sample_nodes"
]
}
ohos_executable("t01_build_your_first_tree") {
output_name = "t01_build_your_first_tree" # 可选,模块输出名
sources = [
"examples/t01_build_your_first_tree.cpp",
]
configs = [
":executable_public_config",
":public_config",
]
deps = [
"//third_party/behaviortree:behaviortree_cpp",
"//third_party/behaviortree:bt_sample_nodes",
]
install_enable = true
install_images = [
"system",
"ramdisk",
"updater",
]
part_name = "behaviortree"
subsystem_name = "behaviortree"
}
##############################################################################
# t02_basic_ports
ohos_executable("t02_basic_ports") {
output_name = "t02_basic_ports" # 可选,模块输出名
sources = [
"examples/t02_basic_ports.cpp",
]
configs = [
":executable_public_config",
":public_config",
]
deps = [
"//third_party/behaviortree:behaviortree_cpp",
"//third_party/behaviortree:bt_sample_nodes",
]
install_enable = true
install_images = [
"system",
"ramdisk",
"updater",
]
part_name = "behaviortree"
subsystem_name = "behaviortree"
}
##############################################################################
# t03_generic_ports
ohos_executable("t03_generic_ports") {
output_name = "t03_generic_ports" # 可选,模块输出名
sources = [
"examples/t03_generic_ports.cpp",
]
configs = [
":executable_public_config",
":public_config",
]
deps = [
"//third_party/behaviortree:behaviortree_cpp",
"//third_party/behaviortree:bt_sample_nodes",
]
install_enable = true
install_images = [
"system",
"ramdisk",
"updater",
]
part_name = "behaviortree"
subsystem_name = "behaviortree"
}
##############################################################################
# t04_reactive_sequence
ohos_executable("t04_reactive_sequence") {
output_name = "t04_reactive_sequence" # 可选,模块输出名
sources = [
"examples/t04_reactive_sequence.cpp",
]
configs = [
":executable_public_config",
":public_config",
]
deps = [
"//third_party/behaviortree:behaviortree_cpp",
"//third_party/behaviortree:bt_sample_nodes",
]
install_enable = true
install_images = [
"system",
"ramdisk",
"updater",
]
part_name = "behaviortree"
subsystem_name = "behaviortree"
}
##############################################################################
# t05_crossdoor
ohos_executable("t05_crossdoor") {
output_name = "t05_crossdoor" # 可选,模块输出名
sources = [
"examples/t05_crossdoor.cpp",
]
configs = [
":executable_public_config",
":public_config",
]
deps = [
"//third_party/behaviortree:behaviortree_cpp",
"//third_party/behaviortree:bt_sample_nodes",
]
install_enable = true
install_images = [
"system",
"ramdisk",
"updater",
]
part_name = "behaviortree"
subsystem_name = "behaviortree"
}
##############################################################################
# t06_subtree_port_remapping
ohos_executable("t06_subtree_port_remapping") {
output_name = "t06_subtree_port_remapping" # 可选,模块输出名
sources = [
"examples/t06_subtree_port_remapping.cpp",
]
configs = [
":executable_public_config",
":public_config",
]
deps = [
"//third_party/behaviortree:behaviortree_cpp",
"//third_party/behaviortree:bt_sample_nodes",
]
install_enable = true
install_images = [
"system",
"ramdisk",
"updater",
]
part_name = "behaviortree"
subsystem_name = "behaviortree"
}
##############################################################################
# t07_load_multiple_xml
ohos_executable("t07_load_multiple_xml") {
output_name = "t07_load_multiple_xml" # 可选,模块输出名
sources = [
"examples/t07_load_multiple_xml.cpp",
]
configs = [
":executable_public_config",
":public_config",
]
deps = [
"//third_party/behaviortree:behaviortree_cpp",
"//third_party/behaviortree:bt_sample_nodes",
]
install_enable = true
install_images = [
"system",
"ramdisk",
"updater",
]
part_name = "behaviortree"
subsystem_name = "behaviortree"
}
##############################################################################
# t08_additional_node_args
ohos_executable("t08_additional_node_args") {
output_name = "t08_additional_node_args" # 可选,模块输出名
sources = [
"examples/t08_additional_node_args.cpp",
]
configs = [
":executable_public_config",
":public_config",
]
deps = [
"//third_party/behaviortree:behaviortree_cpp",
"//third_party/behaviortree:bt_sample_nodes",
]
install_enable = true
install_images = [
"system",
"ramdisk",
"updater",
]
part_name = "behaviortree"
subsystem_name = "behaviortree"
}
##############################################################################
# t09_scripting
ohos_executable("t09_scripting") {
output_name = "t09_scripting" # 可选,模块输出名
sources = [
"examples/t09_scripting.cpp",
]
configs = [
":executable_public_config",
":public_config",
]
deps = [
"//third_party/behaviortree:behaviortree_cpp",
"//third_party/behaviortree:bt_sample_nodes",
]
install_enable = true
install_images = [
"system",
"ramdisk",
"updater",
]
part_name = "behaviortree"
subsystem_name = "behaviortree"
}
##############################################################################
# t10_observer
config("t10_observer_config"){
cflags_cc = [
# 为了编译libbehaviortree_cpp.so原生库中添加的编译器标志
"-O3",
"-DNDEBUG",
"-Wpedantic",
"-std=gnu++17",
# 为了消除shared_library.cpp编译报错添加-fexceptions
"-fexceptions",
"-frtti",
"-Wno-deprecated-volatile",
"-Wno-unused-lambda-capture",
"-Wno-unused-variable",
]
include_dirs = [
"include",
"sample_nodes"
]
}
ohos_executable("t10_observer") {
output_name = "t10_observer" # 可选,模块输出名
sources = [
"examples/t10_observer.cpp",
]
configs = [
":t10_observer_config",
":public_config",
]
deps = [
"//third_party/behaviortree:behaviortree_cpp",
"//third_party/behaviortree:bt_sample_nodes",
]
install_enable = true
install_images = [
"system",
"ramdisk",
"updater",
]
part_name = "behaviortree"
subsystem_name = "behaviortree"
}
##############################################################################
# t11_replace_rules
ohos_executable("t11_replace_rules") {
output_name = "t11_replace_rules" # 可选,模块输出名
sources = [
"examples/t11_replace_rules.cpp",
]
configs = [
":executable_public_config",
":public_config",
]
deps = [
"//third_party/behaviortree:behaviortree_cpp",
"//third_party/behaviortree:bt_sample_nodes",
]
install_enable = true
install_images = [
"system",
"ramdisk",
"updater",
]
part_name = "behaviortree"
subsystem_name = "behaviortree"
}
##############################################################################
# ex01_wrap_legacy
ohos_executable("ex01_wrap_legacy") {
output_name = "ex01_wrap_legacy" # 可选,模块输出名
sources = [
"examples/ex01_wrap_legacy.cpp",
]
configs = [
":executable_public_config",
":public_config",
]
deps = [
"//third_party/behaviortree:behaviortree_cpp",
"//third_party/behaviortree:bt_sample_nodes",
]
install_enable = true
install_images = [
"system",
"ramdisk",
"updater",
]
part_name = "behaviortree"
subsystem_name = "behaviortree"
}
##############################################################################
# ex02_runtime_ports
ohos_executable("ex02_runtime_ports") {
output_name = "ex02_runtime_ports" # 可选,模块输出名
sources = [
"examples/ex02_runtime_ports.cpp",
]
configs = [
":executable_public_config",
":public_config",
]
deps = [
"//third_party/behaviortree:behaviortree_cpp",
"//third_party/behaviortree:bt_sample_nodes",
]
install_enable = true
install_images = [
"system",
"ramdisk",
"updater",
]
part_name = "behaviortree"
subsystem_name = "behaviortree"
}
##############################################################################
# ex03_ncurses_manual_selector
ohos_executable("ex03_ncurses_manual_selector") {
output_name = "ex03_ncurses_manual_selector" # 可选,模块输出名
sources = [
"examples/ex03_ncurses_manual_selector.cpp",
]
configs = [
":executable_public_config",
":public_config",
]
deps = [
"//third_party/behaviortree:behaviortree_cpp",
"//third_party/behaviortree:bt_sample_nodes",
]
install_enable = true
install_images = [
"system",
"ramdisk",
"updater",
]
part_name = "behaviortree"
subsystem_name = "behaviortree"
}
##############################################################################
# ex04_waypoints
ohos_executable("ex04_waypoints") {
output_name = "ex04_waypoints" # 可选,模块输出名
sources = [
"examples/ex04_waypoints.cpp",
]
configs = [
":executable_public_config",
":public_config",
]
deps = [
"//third_party/behaviortree:behaviortree_cpp",
"//third_party/behaviortree:bt_sample_nodes",
]
install_enable = true
install_images = [
"system",
"ramdisk",
"updater",
]
part_name = "behaviortree"
subsystem_name = "behaviortree"
}
##############################################################################
7、对源码进行增量编译,推送编译生成BehaviorTree.CPP的so以及可执行文件到开发板上,验证编译结果
(1)对源码进行增量编译
- 推荐使用如下命令对对源码进行增量编译,编译生成BehaviorTree.CPP的so以及可执行文件
./build.sh --product-name rk3568 --ccache --build-target=behaviortree --disable-post-build --disable-package-image --gn-args enable_notice_collection=false --gn-args load_test_config=false
- 默认编译的是32位,添加–target-cpu arm64参数编译64位
--product-name rk3568 :表示编译的产品是rk3568 (润和大禹200)
--build-target=behaviortree :编译子系统behaviortree
以下这些都是加快编译速度的选项
--ccache --build-target=behaviortree --disable-post-build --disable-package-image --gn-args enable_notice_collection=false --gn-args load_test_config=false
(2)推送编译生成BehaviorTree.CPP的so以及可执行文件到开发板上,验证编译结果
so和可执行文件在out\rk3568\behaviortree目录下:
liblexy_file.a、libbt_sample_nodes.a等静态库文件在out\rk3568\obj\third_party\behaviortree目录下。
1、通过与ohos版本匹配的hdc_std工具,将编译生成的库以及测试用的可执行文件推送到开发板system/lib (lib64)
- 推送到开发板system/lib (lib64),是因为运行需要链接该目录下的libc++.so
- 注意,不再是用hdc_std,改成了hdc
hdc shell
mount -o remount,rw / ## 重新加载系统为可读写
chmod 777 t02_basic_ports
./t02_basic_ports
8、对源码进行增量编译全量编译,烧录固件验证编译结果。
- 如果有将编译生成BehaviorTree.CPP的so以及可执行文件打包到固件,随固件烧录到开发板的需求。推荐进行全量编译,执行 ./build.sh --product-name rk3568 --ccache ,然后编译烧录固件到开发板上即可。
- 编译烧录好固件到开发板后,so文件会在开发板system/lib(64位系统的话在system/lib64),可执行文件会在system/bin。
- hdc shell进入开发板后,在任意目录层级下执行可执行文件都可以。
- 将编译生成BehaviorTree.CPP的so以及可执行文件打包到固件,在上文third_party/behaviortree/BUILD.gn中已经添加相关代码。
install_enable = true
install_images = [
"system",
"ramdisk",
"updater",
]