赞
踩
笔者最近将BehaviorTree.CPP移植到OpenHarmony标准系统上,并且将移植成果开源。相关成果已经合并至
OpenHarmony-SIG仓库tpc_c_cplusplus:https://gitee.com/openharmony-sig/tpc_c_cplusplus/pulls/313
笔者去年分享过如何移植C/C++三方库到OpenHarmony标准系统,在此篇文章将不再重复分享相关内容。想学习移植细节的可以前往查看一下内容:
将分享两种移植方式
在源码/build/subsystem_config.json中增加子系统behaviortree
"behaviortree": {
"path": "third_party/behaviortree",
"name": "behaviortree"
}
{
"subsystem": "behaviortree",
"components": [
{
"component": "behaviortree",
"features": []
}
]
}
特别说明: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": [] } } }
third_party/behaviortree/BUILD.gn配置的模块有
模块之间的依赖关系
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" } ##############################################################################
./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
--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
so和可执行文件在out\rk3568\behaviortree目录下
liblexy_file.a、libbt_sample_nodes.a等静态库文件在out\rk3568\obj\third_party\behaviortree目录下
1、通过与ohos版本匹配的hdc_std工具,将编译生成的库以及测试用的可执行文件推送到开发板system/lib (lib64)
hdc shell
mount -o remount,rw / ## 重新加载系统为可读写
chmod 777 t02_basic_ports
./t02_basic_ports
如果有将编译生成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",
]
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。