当前位置:   article > 正文

【Openharmony】【4.0R】hello程序之“ohos.gni”模板使用方法_import("//build/ohos.gni")

import("//build/ohos.gni")

【Openharmony】【4.0R】hello程序之“ohos.gni”模板使用方法

lite_component.gni虽然好用,但是毕竟是个“轻量-组件”模板。
这里记录下较为常用的“ohos.gni”模板使用方法。
OH版本:4.0Release
内核版本:LiteOS-A
产品版本:qemu_small_system_demo

ohos.gni模板

相比lite_component.gni模板,ohos.gni模板要复杂多了,关于该模板教程也很多。
首先依然是要先import才能使用模板方法

import("//build/ohos.gni")
  • 1

在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")
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

//build/templates/cxx/cxx.gni

在cxx.gni里面,才真正定义了我们要使用的模板,有兴趣的可以仔细阅读cxx.gni内容

template(“ohos_executable”)

可以看到ohos_executable内部使用的是executable,在lite_component.gni那个例子中用的也是executable生成可执行文件。如果熟悉gn,应该知道executable是gn原本就支持的功能,这里ohos_executable就是对executable进行了封装。

template("ohos_executable") {
...
  executable("${target_name}") {
  ...
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
template(“ohos_shared_library”)

同上,ohos_shared_library对shared_library进行了封装。

template("ohos_shared_library") {
...
  shared_library("${target_name}") {
  ...
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
template(“ohos_static_library”)
template("ohos_static_library") {
...
  static_library(target_name) {
  ...
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
template(“ohos_source_set”)
template("ohos_source_set") {
...
  source_set(target_name) {
  ...
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

实际上使用ohos_executable和ohos_shared_library就够了。

准备工作

使用ohos.gni模板需要提供两部分信息:子系统,部件。为了和原有子系统、部件区分,针对hello程序可以新建单独的子系统和部件。

子系统

在"build/subsystem_config.json"中新加一个“hellotestsystem”子系统

   "hellotestsystem":{
    "path":"testhello",
    "name":"hellotestsystem"
  },
  • 1
  • 2
  • 3
  • 4

部件

在项目目录中新建bundle.json
文件结构:

testhello/
└── hello_ohos
    ├── BUILD.gn
    ├── bundle.json
    ├── include
    │   └── hello.h
    ├── src
    │   └── hello.cpp
    └── test
        └── test_hello.cpp
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

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": []
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

添加编译选项

在"vendor/ohemu/qemu_small_system_demo/config.json"中将子系统和部件添加到编译选项里

      {
        "subsystem": "hellotestsystem",
        "components": [
          { "component": "hellotestpart", "features":[] }
        ]
      },
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

头文件

//hello.h
#include <iostream>
void helloTest();
  • 1
  • 2
  • 3

动态库源文件

//hello.cpp
#include "hello.h"
void helloTest()
{
    std::cout << "Hello, lyxqg, this is ohos!" << std::endl;
    return;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

主函数源文件

//test_hello.cpp
#include "hello.h"
int main()
{
    helloTest();
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

BUILD.gn

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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39

注意事项

1.ohos.gni模板内禁止修改output_dirs,所以不要在BUILD.gn中设置output_dirs选项。
2.使用ohos.gni模板时可以使用deps去依赖lite_component模板中的库,测试发现lite_component里也不要写output_dirs,不然编译虽然通过,但是进入系统执行命令发现找不到库文件。

最后
在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/452850?site
推荐阅读
相关标签
  

闽ICP备14008679号