OpenHarmony开发——GN快速上手

背景

最近在研究鸿蒙操作系统的开源项目OpenHarmony,该项目使用了GN+Ninja工具链进行配置,编译,于是开始研究GN如何使用。
本文的所有信息均来自GN官网和本人个人体会。

GN快速入门

使用GN

GN的主要功能是根据配置文件(.gn, BUILD.gn等)生成build.ninja文件。build.ninja类似于Makefile,不同的是由Ninja负责执行编译过程。
获取GN可执行程序。
1)源码编译。可以到官网下载源码。也可以到我的GN源码(需要5积分)
2)鸿蒙源码提供的GN可执行程序。Ubuntu下路径为[源码路径]/prebuilts/build-tools/linux-x86/bin/
将可执行程序放入PATH路径下,或将可执行程序的路径放入PATH,便可在命令行中直接使用GN。

建立构建环境

使用官网示例代码examples/simple_build,可以到官网下载,或到simple_build下载。
进入simple_build代码目录下,将…/out/build作为构建目录。

simple_build$ tree
.
├── build
│   ├── BUILDCONFIG.gn
│   ├── BUILD.gn
│   └── toolchain
│       └── BUILD.gn
├── BUILD.gn
├── hello.cc
├── hello_shared.cc
├── hello_shared.h
├── hello_static.cc
├── hello_static.h
├── README.md
└── tutorial├── README.md└── tutorial.cc
simple_build$ gn gen ../out/build
Done. Made 3 targets from 4 files in 31ms
simple_build$ tree ../out/build
../out/build
├── args.gn
├── build.ninja
├── build.ninja.d
├── obj
│   ├── hello.ninja
│   ├── hello_shared.ninja
│   └── hello_static.ninja
└── toolchain.ninja

显示构建参数

simple_build$ gn args --list ../out/build
current_cpuCurrent value (from the default) = ""(Internally set; try `gn help current_cpu`.)
current_osCurrent value (from the default) = ""(Internally set; try `gn help current_os`.)

交叉编译

设置target_os=“android”,target_cpu = “arm”

simple_build$ gn args ../out/build
##在编辑器里输入
##    target_os=“android”
##    target_cpu = "arm"
##    保存,退出
Waiting for editor on "/media/hndz-dhliu/C7968B675F10B93B/download/gn/examples/out/build/args.gn"...
Generating files...
Done. Made 3 targets from 4 files in 32mssimple_build$ gn args ../out/build --list
current_cpuCurrent value (from the default) = ""(Internally set; try `gn help current_cpu`.)
......
target_cpuCurrent value = "arm"From /media/hndz-dhliu/C7968B675F10B93B/download/gn/examples/out/build/args.gn:4Overridden from the default = ""(Internally set; try `gn help target_cpu`.)
target_osCurrent value = "android"From /media/hndz-dhliu/C7968B675F10B93B/download/gn/examples/out/build/args.gn:3Overridden from the default = ""(Internally set; try `gn help target_os`.)

添加tutorial目标

在simple_build目录下有一个tutorial目录,其下有一个tutorial.cc文件。
在tutorial目录下,添加一个BUILD.gn文件

## tutorial/BUILD.gn
executable("tutorial") {sources = ["tutorial.cc",]
}

修改simple_build下BUILD.gn文件,使其引用tutorial目录下tutorial目标。

## simple_build/BUILD.gn
group("tools") { //虚目标节点deps = [# This will expand to the name "//tutorial:tutorial" which is the full name of our new target. Run "gn help labels" for more."//tutorial",]
}

测试验证。

simple_build$ gn gen ../out/build
Done. Made 5 targets from 5 files in 35ms
simple_build$ tree ../out/build
../out/build
├── args.gn
├── build.ninja
├── build.ninja.d
├── obj
│   ├── hello.ninja
│   ├── hello_shared.ninja
│   ├── hello_static.ninja
│   └── tutorial
│       └── tutorial.ninja
└── toolchain.ninja

目标由3更新为5,产生了tutorial/ tutorial.ninja文件。
编译验证

simple_build$ ninja -C ../out/build tutorial ##表示转到../out/build tutorial目录下编译
ninja: Entering directory `../out/build'
[2/2] LINK tutorial
simple_build$ ../out/build/tutorial 
Hello from the tutorial.

BUILD.gn配置说明

simple_build/BUILD.gn
静态库hello_static配置:用static_library声明静态库,用sources声明所用的源文件。

static_library("hello_static") {sources = ["hello_static.cc","hello_static.h",]
}

可用 gn help static_library 获取static_library的详细用法。
动态库hello_shared配置:用shared_library声明动态库,用sources声明所用的源文件,用defines声明所需要的宏定义。

shared_library("hello_shared") {sources = ["hello_shared.cc","hello_shared.h",]defines = [ "HELLO_SHARED_IMPLEMENTATION" ]
}

可用 gn help shared_library 获取shared_library的详细用法。
可执行程序配置:用executable声明一个可执行程序,用sources声明该可执行程序的源文件,用deps指示所用的库文件。

executable("hello") {sources = ["hello.cc",]deps = [":hello_shared",":hello_static",]
}

测试可执行程序。

simple_build$ ninja -C ../out/build
ninja: Entering directory `../out/build'
[7/7] LINK hello
simple_build$ ../out/build/hello 
Hello, world

验证删除…/out/build/tutorial,再次编译是否重新生成tutorial

simple_build$ rm ../out/build/tutorial 
simple_build$ ninja -C ../out/build
ninja: Entering directory `../out/build'
[2/2] STAMP obj/tools.stamp
simple_build$ ls ../out/build/tutorial -la
-rwxrwxrwx 1 hndz-dhliu hndz-dhliu 8304 3月   9 13:45 ../out/build/tutorial

使用config

库使用者常常需要一些编译选项,宏定义,头文件包含路径,可以将这些内容封装到一个config变量中。
示例如下

config("my_lib_config") {defines = [ "ENABLE_DOOM_MELON" ]include_dirs = [ "//third_party/something" ]
}

使用config,将config变量添加到某个目标的configs列表中。

static_library("hello_shared") {...# Note "+=" here is usually required, see "default configs" below.configs += [":my_lib_config",]
}

如果将config变量应用到所有目标中,则将config变量添加到public_configs列表中。

static_library("hello_shared") {...public_configs = [":my_lib_config",]
}

使用默认配置

默认配置将被用到所有目标中。
可通过print函数打印默认配置。

executable("hello") {print(configs)
}

运行gn,可能打印出如下信息。

$ gn gen out
["//build:compiler_defaults", "//build:executable_ldconfig"]
Done. Made 5 targets from 5 files in 9ms

修改配置。
示例,关闭//build:no_exceptions,启用//build:exceptions

executable("hello") {...configs -= [ "//build:no_exceptions" ]  # Remove global default.configs += [ "//build:exceptions" ]  # Replace with a different one.print("The configs for the target $target_name are $configs")##打印,用来检查
}

使用参数

通过 gn help buildargs可以学习参数是如何设置的。其加载过程如下,加载系统默认参数(),加载//.gn中的default_args,加载–args命令行参数,加载工具链的参数。使用参数首先要用declare_args声明参数,并赋默认值。如果加载顺序上没被赋值,则使用默认值。

声明参数

declare_args() {enable_teleporter = trueenable_doom_melon = false
}

可通过gn help declare_args 了解declare_args详细信息。

了解GN构建过程

使用 -v 可以了解GN的详细执行流程。

simple_build$ gn gen -v ../out/build
Using source root /media/hndz-dhliu/C7968B675F10B93B/download/gn/examples/simple_build
Got dotfile /media/hndz-dhliu/C7968B675F10B93B/download/gn/examples/simple_build/.gn
Using build dir /media/hndz-dhliu/C7968B675F10B93B/download/gn/examples/out/build/
Loading //build/BUILDCONFIG.gn
Loading //BUILD.gn
Running //BUILD.gn with toolchain //build/toolchain:gcc
Defining target //:hello(//build/toolchain:gcc)
Defining target //:hello_shared(//build/toolchain:gcc)
Defining target //:hello_static(//build/toolchain:gcc)
Defining target //:tools(//build/toolchain:gcc)
Loading //build/BUILD.gn (referenced from //build/BUILDCONFIG.gn:22)
Loading //build/toolchain/BUILD.gn (referenced from //BUILD.gn:5)
Loading //tutorial/BUILD.gn (referenced from //BUILD.gn:33)
Running //build/BUILD.gn with toolchain //build/toolchain:gcc
Defining config //build:compiler_defaults(//build/toolchain:gcc)
Defining config //build:executable_ldconfig(//build/toolchain:gcc)
Running //build/toolchain/BUILD.gn with toolchain //build/toolchain:gcc
Defining toolchain //build/toolchain:gcc
Computing //:hello_static(//build/toolchain:gcc)
Computing //:hello_shared(//build/toolchain:gcc)
Computing //:hello(//build/toolchain:gcc)
Running //tutorial/BUILD.gn with toolchain //build/toolchain:gcc
Defining target //tutorial:tutorial(//build/toolchain:gcc)
Computing //tutorial:tutorial(//build/toolchain:gcc)
Computing //:tools(//build/toolchain:gcc)
Done. Made 5 targets from 5 files in 30ms

构建过程如下
1)在当前目录及其父目录查找.gn文件(即dotfile),以此作为source root
2).gn文件中buildconfig变量指示build config file路径(该文件常用来配置相关编译工具链),root变量指示source root(不指定时则为.gn所在的目录)。
3)加载build config file
4)加载source root下的BUILD.gn文件以及其引用的相关文件。
可通过gn help dotfile了解其构建过程。

查找依赖

通过 gn desc <build_dir> 可以了解一个目标的详细信息。通过 gn desc <build_dir> deps --tree 可以查找一个目标的依赖信息。

simple_build$ gn desc ../out/build //:hello
Target //:hello
type: executable
toolchain: //build/toolchain:gccvisibility*metadata{}testonlyfalsecheck_includestrueallow_circular_includes_fromsources//hello.ccpublic[All headers listed in the sources are public.]configs (in order applying, try also --tree)//build:compiler_defaults//build:executable_ldconfigoutputs/media/hndz-dhliu/C7968B675F10B93B/download/gn/examples/out/build/helloldflags-Wl,-rpath=$ORIGIN/-Wl,-rpath-link=Direct dependencies (try also "--all", "--tree", or even "--all --tree")//:hello_shared//:hello_staticexternssimple_build$ gn desc ../out/build //:hello deps --tree
//:hello_shared
//:hello_static

通过gn help desc 了解desc的更多用法。

GN文件执行脚本
参照官方文档language.md
有两种方式:
1)使用action目标类型
2)使用exec_script函数

action("run_this_guy_once") {script = "doprocessing.py"sources = [ "my_configuration.txt" ]outputs = [ "$target_gen_dir/insightful_output.txt" ]# Our script imports this Python file so we want to rebuild if it changes.inputs = [ "helper_library.py" ]# Note that we have to manually pass the sources to our script if the# script needs them as inputs.args = [ "--out", rebase_path(target_gen_dir, root_build_dir) ] +rebase_path(sources, root_build_dir)}

exec_script(filename,arguments = [],input_conversion = "",file_dependencies = [])The default script interpreter is Python ("python" on POSIX, "python.exe" or "python.bat" on Windows). This can be configured by the script_executable variable, see "gn help dotfile".
Arguments:filename:File name of script to execute. Non-absolute names will be treated as relative to the current build file.arguments: A list of strings to be passed to the script as arguments. May be unspecified or the empty list which means no arguments.input_conversion: Controls how the file is read and parsed. See "gn help io_conversion".If unspecified, defaults to the empty string which causes the script result to be discarded. exec script will return None.dependencies: (Optional) A list of files that this script reads or otherwise depends on. These dependencies will be added to the build result such that if any of them change, the build will be regenerated and the script will be re-run.Exampleall_lines = exec_script("myscript.py", [some_input], "list lines",[ rebase_path("data_file.txt", root_build_dir) ])# This example just calls the script with no arguments and discards the# result.exec_script("//foo/bar/myscript.py")

为了能让大家更好的学习鸿蒙 (OpenHarmony) 开发技术,这边特意整理了《鸿蒙 (OpenHarmony)开发学习手册》(共计890页),希望对大家有所帮助:https://qr21.cn/FV7h05

《鸿蒙 (OpenHarmony)开发学习手册》

入门必看:https://qr21.cn/FV7h05

  1. 应用开发导读(ArkTS)
  2. ……

HarmonyOS 概念:https://qr21.cn/FV7h05

  1. 系统定义
  2. 技术架构
  3. 技术特性
  4. 系统安全

如何快速入门?:https://qr21.cn/FV7h05

  1. 基本概念
  2. 构建第一个ArkTS应用
  3. 构建第一个JS应用
  4. ……

开发基础知识:https://qr21.cn/FV7h05

  1. 应用基础知识
  2. 配置文件
  3. 应用数据管理
  4. 应用安全管理
  5. 应用隐私保护
  6. 三方应用调用管控机制
  7. 资源分类与访问
  8. 学习ArkTS语言
  9. ……

基于ArkTS 开发:https://qr21.cn/FV7h05

1.Ability开发
2.UI开发
3.公共事件与通知
4.窗口管理
5.媒体
6.安全
7.网络与链接
8.电话服务
9.数据管理
10.后台任务(Background Task)管理
11.设备管理
12.设备使用信息统计
13.DFX
14.国际化开发
15.折叠屏系列
16.……

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/348239.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

Android开发--状态栏布局隐藏的方法

1.问题如下&#xff0c;安卓布局很不协调 2.先将ActionBar设置为NoActionBar 先打开styles.xml 3.使用工具类 package com.afison.newfault.utils;import android.annotation.TargetApi; import android.app.Activity; import android.content.Context; import android.graph…

字符串匹配(BF KMP)详解 + 刷题

目录 &#x1f33c;前言 BF 算法 KMP 算法 &#xff08;1&#xff09;前缀函数 -- O(n^3) &#xff08;2&#xff09;前缀函数 -- O(n^2) &#xff08;3&#xff09;前缀函数 -- O(n) &#xff08;4&#xff09;辅助理解 &#x1f40b;P1308 -- 统计单词数 …

【深度学习】线性回归模型与梯度下降法

线性回归模型与梯度下降法 线性回归模型与枚举法 线性回归模型定义: w:权重b:偏置#mermaid-svg-ZAxF27Mw5dXNQgw2 {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-ZAxF27Mw5dXNQgw2 .error-icon{fill:#552222;}…

pyecharts模块的下载方法以及介绍,折线图的创立

目录 1.pyecharts是什么 2.pyecharts下载方法 1.在屏幕左下角搜索这里输入cmd&#xff0c;找到命令提示符并且打开 2.输入pip install pyecharts 然后回车进行下载 3.检查是否下载完成 4.另一个方法 3.pyecharts入门 4.pyecharts的配置选项 set_global_opts全局配置选…

[docker] Docker资源管理

一、docker资源控制 Docker通过Cgroup 来控制容器使用的资源配额&#xff0c;包括CPU、内存、磁盘三大方面&#xff0c;基本覆盖了常见的资源配额和使用量控制。Caroup 是ControlGroups的缩写&#xff0c;是Linux 内核提供的一种可以限制、记录、隔离进程组所使用的物理资源(如…

OpenKruise :Kubernetes背后的托底

一、 诞生背景 Kubernetes 自身提供的应用部署管理功能&#xff0c;无法满足大规模应用场景的需求&#xff0c;例如应用发布时的原地升级策略&#xff0c;流式扩容&#xff0c;缩容顺序控制等等。所以OpenKruise的出现弥补了 Kubernetes 在应用部署、升级、防护、运维等领域的不…

剪映声音克隆;多位滴滴前中高层加入小红书提速商业化;中国和新加坡互免签证

今日精选 • 剪映推出 AI 音色克隆功能&#xff0c;录制 5 秒声音即可完成克隆• 商业化全面提速&#xff0c;多位滴滴前中高层加入小红书• 2 月 9 日起&#xff0c;中国和新加坡互免签证 科技动态 • 夸克上线大模型新产品“AI PPT”&#xff0c;可一键生成提纲、创作 PPT…

Unity - gamma space下还原linear space效果

文章目录 环境目的环境问题实践结果处理要点处理细节【OnPostProcessTexture 实现 sRGB 2 Linear 编码】 - 预处理【封装个简单的 *.cginc】 - shader runtime【shader需要gamma space下还原记得 #define _RECOVERY_LINEAR_IN_GAMMA】【颜色参数应用前 和 颜色贴图采样后】【灯…

接口自动化测试实践

众所周知&#xff0c;接口自动化测试有着如下特点&#xff1a; 低投入&#xff0c;高产出。 比较容易实现自动化。 和UI自动化测试相比更加稳定。 如何做好一个接口自动化测试项目呢&#xff1f; 我认为&#xff0c;一个“好的”自动化测试项目&#xff0c;需要从“时间”…

【算法练习Day51】柱状图中最大的矩形

​&#x1f4dd;个人主页&#xff1a;Sherry的成长之路 &#x1f3e0;学习社区&#xff1a;Sherry的成长之路&#xff08;个人社区&#xff09; &#x1f4d6;专栏链接&#xff1a;练题 &#x1f3af;长路漫漫浩浩&#xff0c;万事皆有期待 文章目录 柱状图中最大的矩形思路动态…

HTML+CSS:飞翔按钮

效果演示 实现了一个按钮的动画效果&#xff0c;当鼠标悬停在按钮上时&#xff0c;按钮的背景颜色和图标会发生变化&#xff0c;并且图标会旋转45度并向右移动1.2em&#xff0c;同时按钮中的文字也会向右移动5em。当鼠标点击按钮时&#xff0c;按钮会变小并向下移动0.1em。整个…

软考复习之软件工程篇

软件生命周期 问题定义&#xff1a;要示系统分析员与用户进行交流&#xff0c;弄清”用户需要计算机解决什么问题”然后提出关于“系统目标与范围的说明”&#xff0c;提交用户审查和确认 可行性研究&#xff1a;一方面在于把待开发的系统的目标以明确的语言描述出来&#xf…

LINUX服务之YUM仓库

1. YUM概述 YUM基于RPM包构建的软件更新机制 可以自动解决依赖关系 所有软件包由集中的YUM软件仓库提供 YUM支持软件源 搭建yum支持的的软件源主要有以下三种&#xff1a; 本地yum&#xff1a;file&#xff1a;//… 网络yum&#xff0c;又分为HTTP服务器&#xff1a;http…

Vue3 watch与watchEffect区别

✨ 专栏介绍 在当今Web开发领域中&#xff0c;构建交互性强、可复用且易于维护的用户界面是至关重要的。而Vue.js作为一款现代化且流行的JavaScript框架&#xff0c;正是为了满足这些需求而诞生。它采用了MVVM架构模式&#xff0c;并通过数据驱动和组件化的方式&#xff0c;使…

从全流程的角度来了解python包的使用,也许你会有不一样的认识

在python中&#xff0c;只要我们一谈到包或模块&#xff0c;基本默认说的就是包的导入和使用。也就是说只要我们知道包的名字&#xff0c;导入后知道怎么使用基本就可以了&#xff0c;但本人认为&#xff0c;我们仅仅了解的是包的一部分&#xff0c;若想对包有个整体的认识&…

376. 摆动序列 - 力扣(LeetCode)

题目描述 如果连续数字之间的差严格地在正数和负数之间交替&#xff0c;则数字序列称为摆动序列。第一个差&#xff08;如果存在的话&#xff09;可能是正数或负数。少于两个元素的序列也是摆动序列。 例如&#xff0c; [1,7,4,9,2,5] 是一个摆动序列&#xff0c;因为差值 (6,…

【机器学习300问】15、什么是逻辑回归模型?

一、逻辑回归模型是为了解决什么问题&#xff1f; 逻辑回归&#xff08;Logistic Regression&#xff09;是一种广义线性回归分析模型&#xff0c;尤其适用于解决二分类问题&#xff08;输出为两个类别&#xff09;。 &#xff08;1&#xff09;二分类举例 邮件过滤&#xff…

详解BLDC和PMSM的特点

文章目录 前言BLDC和PMSM的优点基础架构前言 在电机领域中,有刷电机和无刷电机代表着两种不同的技术路径。有刷电机的绕组通常位于转子,即电机的旋转部分。 而无刷电机则采用一种更为先进的设计,其绕组安置在定子,即电机的静止部分。 这样的设计理念在于将绕组固定在电机的…

深入理解stress/stress-ng

文章目录 一、概述二、安装2.1、源码编译安装2.2、命令行安装2.3、安装确认 三、重要参数详解3.1、查询支持的参数3.2、重要参数说明 四、实例4.1、压测CPU4.2、压测内存4.3、压测IO4.4、压测磁盘及IO4.5、压测磁盘及CPU 团队博客: 汽车电子社区 一、概述 stress是一种工作负载…

【AIGC】Diffusers:AutoPipeline自动化扩散生图管道

前言 &#x1f917; 扩散器能够完成许多不同的任务&#xff0c;并且您通常可以将相同的预训练权重用于多个任务&#xff0c;例如文本到图像、图像到图像和修复。但是&#xff0c;如果您不熟悉库和扩散模型&#xff0c;可能很难知道将哪个管道用于任务。例如&#xff0c;如果您…
最新文章