自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

[gn+ninja學(xué)習(xí) 0x02] GN入門示例

系統(tǒng) OpenHarmony
作為開源軟件,可以自己編譯,也可以直接使用現(xiàn)有的二進(jìn)制文件,官方下載地址如下。通常外網(wǎng)速度慢,甚至打不開。還好,gn、ninja相關(guān)的源代碼可以在OpenHarmony的third party倉庫下找到。

??想了解更多關(guān)于開源的內(nèi)容,請?jiān)L問:??

??51CTO 開源基礎(chǔ)軟件社區(qū)??

??https://ost.51cto.com??

OpenHarmony使用gn+ninja來維護(hù)開源項(xiàng)目的構(gòu)建。之前沒有接觸過gn+ninja,是時(shí)候系統(tǒng)性的來學(xué)習(xí)下了。邊學(xué)邊記錄下學(xué)習(xí)過程,希望對同樣需要學(xué)習(xí)gn+ninja的朋友有所幫助。

這一篇,我們通過示例來學(xué)習(xí)GN的入門知識。

1、環(huán)境配置

作為開源軟件,可以自己編譯,也可以直接使用現(xiàn)有的二進(jìn)制文件,官方下載地址如下。通常外網(wǎng)速度慢,甚至打不開。還好,gn、ninja相關(guān)的源代碼可以在OpenHarmony的third party倉庫下找到,見參考資料。

https://github.com/ninja-build/ninja/releases
https://chrome-infra-packages.appspot.com/p/gn/gn

搭建OpenHarmony環(huán)境后,也會自動配置gn和ninja的構(gòu)建環(huán)境,這就很方便了。下載openharmony源碼,使用hb編譯時(shí)會自動下載gn+ninja工具時(shí),在目錄prebuilts/build-tools/linux-x86/bin下可以找到gn+ninja。更多信息請參考OpenHarmony的環(huán)境搭建文檔吧。

2、GN Quick Start guide快速入門

快速入門參考了官方文檔,讀者也可以去閱讀下原文https://gitee.com/openharmony/third_party_gn/blob/master/docs/quick_start.md。

(1)Running GN

搭建好環(huán)境后,就可以命令行運(yùn)行g(shù)n命令。如下:

zhushangyuan@DESKTOP-RPE9R4O:~/openharmony$ gn help help
gn help <anything>:
Yo dawg, I heard you like help on your help so I put help on the help in the
help.
You can also use "all" as the parameter to get all help at once.
Switches
--markdown
Format output in markdown syntax.
Example
gn help --markdown all
Dump all help to stdout in markdown format.

(2)Setting up a build

GN允許你設(shè)置專有的構(gòu)建目錄,不同的構(gòu)建目錄使用不同的參數(shù)設(shè)置。設(shè)置構(gòu)建目錄后,當(dāng)配置文件更新時(shí),Ninja文件會自動重新生成,而不需要重新運(yùn)行GN。設(shè)置構(gòu)建目錄使用下述命令:

gn gen out/my_build

不是隨便一個(gè)目錄就可以運(yùn)行上述命令的,否則會報(bào)錯(cuò):

ERROR Can't find source root.
I could not find a ".gn" file in the current directory or any parent,
and the --root command-line argument was not specified.

如果想練習(xí)下設(shè)置構(gòu)建目錄,可以切換到gn的example目錄下進(jìn)行嘗試,如下:

cd ~/openharmony/third_party/gn/examples/simple_build/
gn gen out/my_build

(3)Passing build arguments傳遞構(gòu)建參數(shù)

執(zhí)行命令gn args --list out/my_build ,可以查詢可用的參數(shù)列表及其他們的默認(rèn)值。必須指定構(gòu)建目錄,可用參數(shù)在不同的構(gòu)建目錄下是不一樣的。

執(zhí)行命令gn args out/my_build可以為構(gòu)建目錄設(shè)置構(gòu)建參數(shù),該命令執(zhí)行后,會打開一個(gè)編輯器,輸入構(gòu)建參數(shù),類似下面:

is_component_build = true
is_debug = false

3、Step-by-step示例學(xué)習(xí)

(1)Adding a build file添加構(gòu)建文件

切換到目錄~/openharmony/third_party/gn/examples/simple_build/,這是最小化的GN構(gòu)建示例目錄。在目錄下有個(gè)tutorial 目錄,內(nèi)有一個(gè)源文件tutorial.cc,但是有利于構(gòu)建之外。在tutorial 目錄下創(chuàng)建一個(gè)BUILD.gn文件,用于構(gòu)建可執(zhí)行文件target:

executable("tutorial") {
sources = [
"tutorial.cc",
]
}

然后,把構(gòu)建target目標(biāo)告知構(gòu)建系統(tǒng)。simple_build目錄下的BUILD.gn文件,是GN構(gòu)建的入口根文件,從這里加載啟動后,然后加載依賴。因此,我們在此根文件中添加引用到我們上述創(chuàng)建的構(gòu)建target。

通常,把一個(gè)可執(zhí)行文件作為另外一個(gè)可執(zhí)行文件的依賴,沒有意義,無法鏈接。我們創(chuàng)建一個(gè)tools組group。在GN中,group用于收集維護(hù)不會編譯或者鏈接的依賴。如下:

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

(2)Testing your addition驗(yàn)證編譯

命令行進(jìn)入simple_build命令,執(zhí)行如下命令,可以看到會有打印輸出“Hello from the tutorial.”。

gn gen out
ninja -C out tutorial
out/tutorial

(3)Declaring dependencies 聲明依賴

我們再來看下examples/simple_build/BUILD.gn中的構(gòu)建target的定義,⑴處定義的可執(zhí)行文件構(gòu)建target依賴hello_shared和hello_static,前面的冒號表示這些依賴的target在當(dāng)前文件中定義,可以在下文找到。⑵處定義了一個(gè)共享庫的構(gòu)建target,它定義了一個(gè)函數(shù)GetSharedText(),也演示了如何使用預(yù)處理宏。⑶處定義了一個(gè)靜態(tài)庫的構(gòu)建target,它定義了一個(gè)函數(shù)GetStaticText()。

  executable("hello") {
sources = [ "hello.cc" ]
deps = [
":hello_shared",
":hello_static",
]
}
shared_library("hello_shared") {
sources = [
"hello_shared.cc",
"hello_shared.h",
]
defines = [ "HELLO_SHARED_IMPLEMENTATION" ]
}
static_library("hello_static") {
sources = [
"hello_static.cc",
"hello_static.h",
]
}

(4)Test the binary測試運(yùn)行二進(jìn)制

命令行進(jìn)入simple_build命令,執(zhí)行如下命令,可以看到會有打印輸出“Hello, world”。命令中-C指定構(gòu)建目錄,后面的hello也可以構(gòu)建的target名稱。使用命令gn ls out可以輸出所有的構(gòu)建target。

ninja -C out hello
out/hello

(5)Putting settings in a config使用config配置設(shè)置

使用library庫的時(shí)候,經(jīng)常需要編譯選項(xiàng)、宏定義、include頭文件包含等??梢园堰@些設(shè)置放到一個(gè)config的命名設(shè)置集里,不能放置源文件或依賴。如下:

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

為了把一個(gè)config里面的設(shè)置應(yīng)用到target里,可以加到configs列表里,如注釋所示,使用+=,這樣默認(rèn)的設(shè)置不會被覆蓋。

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

一個(gè)config也能應(yīng)用到所有依賴當(dāng)前target的target上,只需要把config添加到public_configs 列表里,如下。public_configs 里面的設(shè)置也會應(yīng)用到當(dāng)前的target,不需要重復(fù)指定。

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

構(gòu)建配置一般設(shè)置一些配置項(xiàng),可以默認(rèn)應(yīng)用到所有的targe里。例如,在文件examples\simple_build\build\BUILDCONFIG.gn中,定義了如下默認(rèn)configs??梢栽贐UILD.gn的target里使用語句“print(configs)”打印輸出默認(rèn)配置。

# Apply that default list to the binary target types.
set_defaults("executable") {
configs = _shared_binary_target_configs
# Executables get this additional configuration.
configs += [ "http://build:executable_ldconfig" ]
}
set_defaults("static_library") {
configs = _shared_binary_target_configs
}
set_defaults("shared_library") {
configs = _shared_binary_target_configs
}
set_defaults("source_set") {
configs = _shared_binary_target_configs
}

(6)Add a new build argument添加構(gòu)建參數(shù)

可以使用declare_args聲明可以接受的構(gòu)建參數(shù),并提供默認(rèn)值??梢允褂妹畈榭礃?gòu)建參數(shù)的使用幫助gn help buildargs。

declare_args() {
enable_teleporter = true
enable_doom_melon = false
}

聲明參數(shù)后,在生成構(gòu)建目錄時(shí),可以在命令行指定構(gòu)建參數(shù):

gn gen out/FooBar --args="enable_doom_melon=true os=\"android\""

4、gn常用命令

  • gn gen out/dir [–args=“…”]:創(chuàng)建新的編譯目錄,會自動創(chuàng)建args.gn文件作為編譯參數(shù)。
  • gn args --list out/dir:列出可選的編譯參數(shù)。
  • gn ls out/dir:列出所有的target。
  • gn ls out/dir “//:hello_word*”:列出匹配的target。
  • gn desc out/dir “//:hello_word”:查看指定target的描述信息,包括src源碼文件、依賴的lib、編譯選項(xiàng)等。
  • gn refs out/dir 文件:查看依賴該文件的target。
  • gn refs out/dir //:hello_word:查看依賴該target的target。

注意//代表從項(xiàng)目根目錄開始。

5、小結(jié)

我們初步了解了下構(gòu)建系統(tǒng)、元構(gòu)建系統(tǒng)是做什么的,了解到gn是個(gè)元構(gòu)建系統(tǒng),類似cmake。接下來的一篇會介紹gn入門知識、具體如何使用等等。

??想了解更多關(guān)于開源的內(nèi)容,請?jiān)L問:??

??51CTO 開源基礎(chǔ)軟件社區(qū)??

??https://ost.51cto.com??。

責(zé)任編輯:jianghua 來源: 51CTO開源基礎(chǔ)軟件社區(qū)
相關(guān)推薦

2022-10-26 15:33:27

GN鴻蒙

2022-10-24 14:43:31

GN語法GN

2024-01-18 15:24:06

Rust開發(fā)鴻蒙OH4.0

2021-02-02 10:13:56

鴻蒙HarmonyOS應(yīng)用開發(fā)

2017-06-19 09:59:32

Python調(diào)試

2012-05-16 15:03:53

WA2110-GN

2012-05-10 15:35:29

WA2110-GNH3C

2020-12-01 15:54:08

鴻蒙構(gòu)建系統(tǒng)

2022-06-10 14:37:24

鴻蒙操作系統(tǒng)

2016-12-27 14:59:50

得分函數(shù)參數(shù)

2024-01-11 16:02:38

OHOS依賴關(guān)系檢查編譯構(gòu)建系統(tǒng)

2023-06-12 15:43:44

鴻蒙智能家居開發(fā)

2020-12-10 12:12:32

鴻蒙開發(fā)板init_lite

2017-09-13 10:06:36

人工智能阿里云

2023-03-17 16:38:17

gn文件圖譜查詢

2021-04-07 08:43:09

SpringBootRocketMQ開發(fā)技術(shù)

2022-05-07 15:54:56

小熊派鴻蒙

2017-06-22 09:45:58

阿里云GN5實(shí)例深度學(xué)習(xí)

2024-04-11 13:13:27

2011-07-01 17:12:44

Qt OpenGL
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號