赞
踩
lite_component.gni虽然好用,但是毕竟是个“轻量-组件”模板。
这里记录下较为常用的“ohos.gni”模板使用方法。
OH版本:4.0Release
内核版本:LiteOS-A
产品版本:qemu_small_system_demo
相比lite_component.gni模板,ohos.gni模板要复杂多了,关于该模板教程也很多。
首先依然是要先import才能使用模板方法
import("//build/ohos.gni")
在Openharmony看ohos.gni会发现,这个模板实际上是一堆模板通过import结合起来的,内容还是较多的,但是我们要用的实际在import(“//build/templates/cxx/cxx.gni”)这里。
//ohos.gni import("//build/config/sanitizers/sanitizers.gni") import("//build/ohos/ndk/ndk.gni") import("//build/ohos/notice/notice.gni") import("//build/ohos/sa_profile/sa_profile.gni") import("//build/ohos_var.gni") import("//build/toolchain/toolchain.gni") # import cxx base templates import("//build/templates/cxx/cxx.gni") if (support_jsapi) { import("//build/ohos/ace/ace.gni") import("//build/ohos/app/app.gni") } import("//build/templates/common/ohos_templates.gni") # import prebuilt templates import("//build/templates/cxx/prebuilt.gni") if (build_cross_platform_version) { import("//build_plugins/templates/java/rules.gni") } else { import("//build/templates/bpf/ohos_bpf.gni") import("//build/templates/rust/ohos_cargo_crate.gni") import("//build/templates/rust/rust_bindgen.gni") import("//build/templates/rust/rust_cxx.gni") import("//build/templates/rust/rust_template.gni") } import("//build/templates/idl/ohos_idl.gni")
在cxx.gni里面,才真正定义了我们要使用的模板,有兴趣的可以仔细阅读cxx.gni内容
可以看到ohos_executable内部使用的是executable,在lite_component.gni那个例子中用的也是executable生成可执行文件。如果熟悉gn,应该知道executable是gn原本就支持的功能,这里ohos_executable就是对executable进行了封装。
template("ohos_executable") {
...
executable("${target_name}") {
...
}
}
同上,ohos_shared_library对shared_library进行了封装。
template("ohos_shared_library") {
...
shared_library("${target_name}") {
...
}
}
template("ohos_static_library") {
...
static_library(target_name) {
...
}
}
template("ohos_source_set") {
...
source_set(target_name) {
...
}
}
实际上使用ohos_executable和ohos_shared_library就够了。
使用ohos.gni模板需要提供两部分信息:子系统,部件。为了和原有子系统、部件区分,针对hello程序可以新建单独的子系统和部件。
在"build/subsystem_config.json"中新加一个“hellotestsystem”子系统
"hellotestsystem":{
"path":"testhello",
"name":"hellotestsystem"
},
在项目目录中新建bundle.json
文件结构:
testhello/
└── hello_ohos
├── BUILD.gn
├── bundle.json
├── include
│ └── hello.h
├── src
│ └── hello.cpp
└── test
└── test_hello.cpp
bundle.json内容如下,设置名为hellotestpart的部件
重点看“component”:的内容,
name 部件名
subsystem 部件所属子系统
deps 部件依赖
sub_componrnt 部件中组件编译入口
{ "name": "@ohos/hellotestpart", "description": "Hello example.", "version": "4.0", "license": "Apache License 2.0", "publishAs": "code-segment", "segment": { "destPath": "testhello/hello_ohos" }, "dirs": {}, "scripts": {}, "component": { "name": "hellotestpart", "subsystem": "hellotestsystem", "syscap": [], "features": [], "adapted_system_type": [ "mini", "small", "standard" ], "rom": "10KB", "ram": "10KB", "deps": { "components": [], "third_party": [] }, "build": { "sub_component": [ "//testhello/hello_ohos:test_hello" ], "inner_kits": [], "test": [] } } }
在"vendor/ohemu/qemu_small_system_demo/config.json"中将子系统和部件添加到编译选项里
{
"subsystem": "hellotestsystem",
"components": [
{ "component": "hellotestpart", "features":[] }
]
},
//hello.h
#include <iostream>
void helloTest();
//hello.cpp
#include "hello.h"
void helloTest()
{
std::cout << "Hello, lyxqg, this is ohos!" << std::endl;
return;
}
//test_hello.cpp
#include "hello.h"
int main()
{
helloTest();
return 0;
}
import("//build/ohos.gni") libsources = [ "src/hello.cpp", ] config("testhelloliteinclude") { include_dirs = [ "include" ] cflags = [ "-Wall" ] cflags_cc = cflags ldflags = [ "-Wl,-rpath-link=$ohos_root_path/$root_out_dir" ] } ohos_shared_library("hellotest") { sources = libsources configs = [ ":testhelloliteinclude" ] output_name = "hellotestv2.0" part_name = "hellotestpart" subsystem_name = "hellotestsystem" } exesources = [ "test/test_hello.cpp" ] ohos_executable("test_hello") { sources = exesources configs = [":testhelloliteinclude"] deps = [ ":hellotest", ] output_name = "test_hello" part_name = "hellotestpart" subsystem_name = "hellotestsystem" }
1.ohos.gni模板内禁止修改output_dirs,所以不要在BUILD.gn中设置output_dirs选项。
2.使用ohos.gni模板时可以使用deps去依赖lite_component模板中的库,测试发现lite_component里也不要写output_dirs,不然编译虽然通过,但是进入系统执行命令发现找不到库文件。
最后
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。