介绍一下Openharmony如何在庞大的编译构建系统中,增添自己想编译的内容。不定期更新~🐸
gn官方文档:
- https://gn.googlesource.com/gn/+/main/docs/quick_start.md
- https://gn.googlesource.com/gn/+/master/docs/reference.md
openharmony官方build文档:
- build: Compilation and building framework and scripts | 编译构建框架和脚本
- zh-cn/device-dev/subsystems/subsys-build-module.md · OpenHarmony/docs - Gitee.com
Author | Date | Version | Description |
---|---|---|---|
陈梓归、唐凯文 | 2023-05-15 | V1.0 | 第一个版本 |
1. 编译自带libdrm/modeset程序
OH的源码环境下third_party/libdrm/tests下是有测试程序的,但是不会编译,需要自己写BUILD.gn来进行编译;
1.1 modeset添加BUILD.gn
- 注意:只有sources和part_name是必选,其他都是可选的;
## third_party/libdrm/tests/modetest/BUILD.gn
# 使用模板,用import,类似C语言的#include
import("//build/ohos.gni")
# 生成可执行文件,叫作modetest
ohos_executable("modetest"){
# 依赖编译相关文件
sources = [
"buffers.c",
"cursor.c",
"modetest.c",
]
cflags = [
"-Wno-pointer-arith",
]
include_dirs = [
"../",
".",
]
# 配置
configs = [ "//third_party/libdrm:libdrm_config" ]
public_configs = [ "//third_party/libdrm:libdrm_public_config" ]
# 部件内模块依赖
deps = [
"//third_party/libdrm:libdrm",
"//third_party/libdrm/tests/util/:util",
]
public_deps = []
# 可选,缺省在/system/lib64或/system/lib下, 模块安装路径,模块安装路径,从system/,vendor/后开始指定
# module_install_dir = ""
# 可选,模块安装相对路径,相对于/system/lib64或/system/lib;如果有module_install_dir配置时,该配置不生效
# relative_install_dir = ""
# 可选,缺省值system,指定模块安装到那个分区镜像中,可以指定多个
install_images = [
"system",
]
# 必选,所属部件名称
part_name = "graphic_standard"
# 可选,所属子系统名称
subsystem_name = "graphic"
}
1.2 为 modetest构建依赖util
在前面的BUILD.gn中我们看到了modeset依赖了util,所以我们这里必须为util构建编译脚本!
因为libdrm会自动构建,所以我们不需要自己构建编译啦😄
## third_party\libdrm\tests\util\BUILD.gn
import("//build/ohos.gni")
ohos_static_library("util"){
sources = [
"format.c",
"kms.c",
"pattern.c",
]
include_dirs = [
"../",
"."
]
configs = [ "//third_party/libdrm:libdrm_config" ]
public_configs = [ "//third_party/libdrm:libdrm_public_config" ]
cflags = [
"-Wno-unused-variable",
"-Wno-sign-compare",
]
deps =[
"//third_party/libdrm:libdrm",
]
public_deps = []
}
1.3 将modetest加入到OHOS编译系统
这里比较简单,可以搜一搜哪个编译的依赖libdrm,然后选择一个加入到BUILD.gn即可
- 搜索//third_party/libdrm:libdrm
deps = [
"//third_party/libdrm:libdrm",
"//third_party/libdrm/tests/util/:util",
"//third_party/libdrm/tests/modetest/:modetest",
]
1.4 编译
- --build-target=ohos_executable的名字
编译命令:./build.sh --product-name d2000 --target-cpu arm64 --build-target=modeset --jobs=100
- 报错:
../../third_party/libdrm/tests/util/pattern.c:988:8: error: unused variable 'mem_base' [-Werror,-Wunused-variable]
- 解决:
第一个方法:屏蔽这行
第二个方法:修改cflags参数
注意这里的cflags需要添加到libdrm下面的BUILD.gn才可以,不然也会编译不过!
修改libdrm文件下面的BUILD.gn加入
"-Wno-unused-variable",
"-Wno-sign-compare",
"-Wno-pointer-sign",
"-Wno-deprecated-declarations",
2. 添加自己应用
2.1 在libdrm下建立对应的fovt_app文件夹
- 增加对应的gn文件和.c文件,然后按照上面的方法增加到一个编译里面去;
- gn
#fovt test application
import("//build/ohos.gni")
#单独编译它
#./build.sh --product-name d2000 --target-cpu arm64 --build-target=atomic_test
ohos_executable("atomic_test"){
sources = [
"atomic_test.c",
]
output_name = "atomic_test"
cflags = [
"-Wno-pointer-arith",
]
include_dirs = [
"../",
".",
]
configs = [ "//third_party/libdrm:libdrm_config" ]
public_configs = [ "//third_party/libdrm:libdrm_public_config" ]
deps = [
"//third_party/libdrm:libdrm",
]
public_deps = []
install_images = [
"system",
]
part_name = "graphic_standard"
subsystem_name = "graphic"
}