当前位置:网站首页>鸿蒙移植i.mx6ull(六) Kconfig_GCC_Mkefile
鸿蒙移植i.mx6ull(六) Kconfig_GCC_Mkefile
2022-06-09 14:03:00 【da..】
1.Kconfig介绍

参考文档:
任一个Linux内核的Documentation\kbuild\kconfig-language.rst
https://www.rt-thread.org/document/site/programming-manual/kconfig/kconfig/
对于各类内核,只要支持
menuconfig配置界面,都是使用Kconfig。
在配置界面中,可以选择、设置选项,这些设置会保存在.config文件里。Makefile会包含.config,根据里面的值决定编译哪些文件、怎么编译文件。
1.1 配置界面示例

问题:
- 这个界面里,各个配置项来自哪里
- 这个界面里,这些配置项是怎么组织的
- 这个界面里,我们的选择、设置,结果保存在哪里
1.2 配置结果的保存
1.2.1 示例
在配置界面中操作的结果保存在
.config文件中,示例如下:
# LOSCFG_COMPILER_HIMIX_32 is not set
LOSCFG_COMPILER_CLANG_LLVM=y
#
# Platform
#
LOSCFG_PLATFORM="stm32mp157"
# LOSCFG_PLATFORM_HI3516DV300 is not set
# LOSCFG_PLATFORM_HI3518EV300 is not set
LOSCFG_PLATFORM_STM32MP157=y
# LOSCFG_PLATFORM_IMX6ULL is not set
LOSCFG_PLATFORM_BSP_GIC_V2=y
LOSCFG_ARCH_ARM=y
LOSCFG_ARCH_ARM_AARCH32=y
LOSCFG_ARCH_ARM_V7A=y
LOSCFG_ARCH_ARM_VER="armv7-a"
LOSCFG_ARCH_FPU_VFP_V4=y
LOSCFG_ARCH_FPU_VFP_D32=y
LOSCFG_ARCH_FPU_VFP_NEON=y
LOSCFG_ARCH_FPU="neon-vfpv4"
LOSCFG_ARCH_CORTEX_A7=y
LOSCFG_ARCH_CPU="cortex-a7"
Makefile会包含.config文件,它会根据里面的变量比如LOSCFG_PLATFORM_STM32MP157选择单板相关的文件。
1.2.2 配置项的前缀
在
Kconfig文件中,假设配置项的名字是XXX,在.config文件中:
- 默认情况下,它对应的变量名为
CONFIG_XXX- 如果设置了环境变量
CONFIG_=ABC,则对应的变量名为ABC_XXX- 在Liteos-a中的Makefile中
export CONFIG_=LOSCFG_,所以对应的变量名为LOSCFG_XXX

1.3 描述单个配置项config
1.3.1 示例
在
make menuconfig界面,可以看到这个配置项:

在配置界面,使用方向箭头游走到
Enable FAT Cache Sync Thread后,可以:
输入Y,选择配置项,在.config中对应
LOSCFG_FS_FAT_CACHE_SYNC_THREAD=y输入N,不选择配置项,在.config中对应
# LOSCFG_FS_FAT_CACHE_SYNC_THREAD is not set
上图中的配置项怎么实现的? (下面是帮助信息。我们可以通过这个找到
kconfig文件)

grep "FS_FAT_CACHE_SYNC_THREAD" * -nr | grep Kconfig

在
Kconfig文件中,它对应下列代码:

1.3.2 语法

解释如下:
- config 表示
config option,这是Kconfig的基本entry;其他entry是用来管理config的。config表示一个配置选项的开始,紧跟着的FS_FAT_CACHE_SYNC_THREAD是配置选项的名称。
config下面几行定义了该配置选项的属性。
属性可以是该配置选项的:类型、输入提示、依赖关系、默认值、帮助信息。
bool表示配置选项的类型,每个config菜单项都要有类型定义,变量有5种类型
bool布尔类型tristate三态类型string字符串hex十六进制int整型"
Enable FAT Cache Sync Thread":提示信息
depends on:表示依赖关系,只有FS_FAT_CACHE被选中,才可以选择FS_FAT_CACHE_SYNC_THREAD
select XXX:表示反向依赖关系,即当前配置选项被选中后,XXX选项就会被选中。
default表示配置选项的默认值,bool类型的默认值可以是y/n。
help帮助信息,在make menuconfig界面输入H键时,就会提示帮助信息。
1.4 实现菜单menu/endmenu
1.4.1 示例



在
Kconfig中,代码如下:
menu "Lib"
config LIB_LIBC
bool "Enable Libc"
default y
help
Answer Y to enable libc for full code.
config LIB_ZLIB
bool "Enable Zlib"
default y
depends on LIB_LIBC
help
Answer Y to enable LiteOS support compress file library.
endmenu
1.4.2 语法
解释如下:
menu "xxx"表示一个菜单,菜单名是"xxx"
menu和endmenu之间的entry都是"xxx"菜单的选项在上面的例子中子菜单有2个选项:“
Enable Libc”、“Enable Zlib”由于第二个菜单项依赖于第一个菜单项,所以第二个菜单项缩进一格
1.5 实现单选choice/endchoice
1.5.1 示例

在上述界面中,对于
LiteOS_Compiler_Type,有2个选择:arm-linux-ohoseabi、clang-llvm。
在Kconfig文件中怎么描述?如下:
menu "Compiler"
choice
prompt "LiteOS_Compiler_Type"
default COMPILER_CLANG_LLVM
help
Enable arm-himix100 or aarch64-himix100 or compiler.
config COMPILER_HIMIX_32
bool "arm-linux-ohoseabi"
depends on PLATFORM_HI3518EV300 || PLATFORM_HI3516DV300 || PLATFORM_IMX6ULL || PLATFORM_STM32MP157
config COMPILER_CLANG_LLVM
bool "clang-llvm"
depends on PLATFORM_HI3518EV300 || PLATFORM_HI3516DV300 || PLATFORM_IMX6ULL || PLATFORM_STM32MP157
endchoice
endmenu
1.5.2 语法
解释如下:
choice表示"选择"choice和endchoice之间的entry是可以选择的项目- 它们之间,只能有一个被设置为"y":表示编进内核
- 它们之间,可以设置多个为"
m":表示编译为模块- 比如一个硬件有多个驱动程序
- 同一时间只能有一个驱动能编进内核
- 但是多个驱动都可以单独编译为模块
1.6 menuconfig
menuconfig XXX和config XXX类似, 唯一不同的是该选项除了能设置y/m/n外,还可以实现菜单效果(能回车进入该项内部)。
1.6.1 示例

对于上述界面,
Kconfig文件中代码如下:
menuconfig OF
bool "Device Tree and Open Firmware support"
help
This option enables the device tree infrastructure.
It is automatically selected by platforms that need it or can
be enabled manually for unittests, overlays or
compile-coverage.
if OF
config OF_UNITTEST
bool "Device Tree runtime unit tests"
depends on OF_IRQ
select OF_EARLY_FLATTREE
select OF_RESOLVE
help
This option builds in test cases for the device tree infrastructure
that are executed once at boot time, and the results dumped to the
console.
If unsure, say N here, but this option is safe to enable.
config OF_OVERLAY
bool "Device Tree overlays"
select OF_DYNAMIC
select OF_RESOLVE
help
Overlays are a method to dynamically modify part of the kernel's
device tree with dynamically loaded data.
While this option is selected automatically when needed, you can
enable it manually to improve device tree unit test coverage.
endif # OF
1.6.2 语法
menuconfig常用格式有2种:
menuconfig M
if M
config C1
config C2
endif
或:
menuconfig M
config C1
depends on M
config C2
depends on M
第1项
menuconfig M跟config M语法是一样的, 不同之处在于menuocnfig M后面可以跟着好几个依赖于M的config C1、config C2等子配置项。
1.7 if/endif
1.7.1 语法
在上面的
menuconfig中就有if/endif的使用,它的语法如下:
"if" <expr>
<if block>
"endif"
1.7.2 示例
示例如下,只有定义的
OF项,OF_UNITTEST和OF_OVERLAY才会显示出来:
if OF
config OF_UNITTEST
bool "Device Tree runtime unit tests"
depends on OF_IRQ
select OF_EARLY_FLATTREE
select OF_RESOLVE
help
This option builds in test cases for the device tree infrastructure
that are executed once at boot time, and the results dumped to the
console.
If unsure, say N here, but this option is safe to enable.
config OF_OVERLAY
bool "Device Tree overlays"
select OF_DYNAMIC
select OF_RESOLVE
help
Overlays are a method to dynamically modify part of the kernel's
device tree with dynamically loaded data.
While this option is selected automatically when needed, you can
enable it manually to improve device tree unit test coverage.
endif # OF
1.8 source
source语句用于读取另一个文件中的Kconfig文件,如:
source "../../kernel/liteos_a/platform/Kconfig"
1.9 comment
comment语句出现在界面的第一行,用于定义一些提示信息,如:
config ARCH_ARM
bool
source "arch/arm/Kconfig"
comment "Extra Configurations"
config ARCH_FPU_DISABLE
bool "Disable Floating Pointer Unit"
default n
help
This option will bypass floating procedure in system.
界面如下:

2.准备工作
安装软件在
GIT仓库:
doc_and_source_for_openharmony\工具\
codeblocks-20.03mingw-setup.exe
make-3.81.exe
2.1. arm-linux-gcc和gcc是类似的
- arm-linux-gcc
- 给ARM芯片编译程序
- gcc
- 在
x86编译程序- 用法基本一样
- 为方便演示,我们使用gcc
- 为了方便在windows下演示,我们使用
Code::Blocks- 它的安装程序自带
gcc
2.2. Code::Blocks
它是一款基于GCC的
windows IDE,可以用来开发C/C++/Fortran。
官网地址:http://www.codeblocks.org/在我们提供的GIT仓库里也有:
git clone https://e.coding.net/weidongshan/openharmony/doc_and_source_for_openharmony.git
下载GIT后,在工具目录下。
2.2.1 安装
按提示安装。
2.2.2 设置windows环境变量
在Path环境变量中添加:
C:\Program Files\CodeBlocks\MinGW\bin
2.2.3 命令行示例
启动
Git Bash,编译程序hello.c:
#include <stdio.h>
int main(void)
{
printf("hello, world!\n");
return 0;
}
编译、运行命令如下:
gcc -o hello hello.c
./hello.exe
2.3. Make
安装
make-3.81.exe后,在Path环境变量中添加:C:\Program Files (x86)\GnuWin32\bin
3.gcc编译过程详解
配套源码在GIT仓库里各个板子的
source\02_录制视频时编写的源码\01_gcc_options里。
3.1. 程序编译4步骤

我们经常使用“编译”泛指上面的4个步骤之一,甚至有时候会囊括这四个步骤。
3.2. gcc的使用方法
gcc [选项] 文件名
3.2.1 gcc使用示例
gcc hello.c // 输出一个名为a.out的可执行程序,然后可以执行./a.out
gcc -o hello hello.c // 输出名为hello的可执行程序,然后可以执行./hello
gcc -o hello hello.c -static // 静态链接
gcc -c -o hello.o hello.c // 先编译(不链接)
gcc -o hello hello.o // 再链接
3.2.2 gcc常用选项
3.2.2.1 手工控制编译过程
| 选项 | 功能 |
|---|---|
| -v | 查看gcc编译器的版本,显示gcc执行时的详细过程 |
| -o | 指定输出文件名为file,这个名称不能跟源文件名同名 |
| -E | 只预处理,不会编译、汇编、链接t |
| -S | 只编译,不会汇编、链接 |
| -c | 编译和汇编,不会链接 |
一个c/c++文件要经过预处理、编译、汇编和链接才能变成可执行文件。
(1)预处理C/C++源文件中,以“#”开头的命令被称为预处理命令,如包含命令“#include”、宏定义命令“#define”、条件编译命令“#if”、“#ifdef”等。预处理就是将要包含(include)的文件插入原文件中、将宏定义展开、根据条件编译命令选择要使用的代码,最后将这些东西输出到一个“.i”文件中等待进一步处理。
(2)编译
编译就是把C/C++代码(比如上述的“.i”文件)“翻译”成汇编代码。
(3)汇编
汇编就是将第二步输出的汇编代码翻译成符合一定格式的机器代码,在Linux系统上一般表现为ELF目标文件(OBJ文件)。“反汇编”是指将机器代码转换为汇编代码,这在调试程序时常常用到。
(4)链接
链接就是将上步生成的OBJ文件和系统库的OBJ文件、库文件链接起来,最终生成了可以在特定平台运行的可执行文件。
hello.c(预处理)->hello.i(编译)->hello.s(汇编)->hello.o(链接)->hello详细的每一步命令如下:
gcc -E -o hello.i hello.c
gcc -S -o hello.s hello.i
gcc -c -o hello.o hello.s
gcc -o hello hello.o
上面一连串命令比较麻烦,
gcc会对.c文件默认进行预处理操作,使用-c再来指明了编译、汇编,从而得到.o文件,再将.o文件进行链接,得到可执行应用程序。简化如下:
gcc -c -o hello.o hello.c//前三步骤
gcc -o hello hello.o
3.2.2.2 使用后缀名决定编译过程
参考《嵌入式Linux应用开发完全手册》:
- 总结 * 输入文件的后缀名和选项共同决定gcc到底执行那些操作
- 在编译过程中,最后的步骤都是链接
- 除非使用了-E、-S、-c选项
- 或者编译出错阻止了完整的编译过程
3.2.2.3 指定头文件目录
头文件在哪里?
- 系统目录 * 系统目录在哪?工具链里的某个include目录
- 怎么确定?
```
echo 'main(){}'| gcc -E -v - // 它会列出头文件目录、库目录(LIBRARY_PATH)
```
- 可以不使用系统
include目录吗?可以,编译时指定参数-nostdinc- 可以自己指定头文件目录
-I <头文件目录>
3.2.2.4 指定库文件
库文件在哪里?
- 系统目录 * 系统目录在哪?工具链里的某个lib目录
- 怎么确定?
```
echo 'main(){}'| gcc -E -v - // 它会列出头文件目录、库目录(LIBRARY_PATH)
```
- 可以不使用系统
lib目录吗?可以,编译时指定参数-nostdlib- 可以自己指定库文件目录
-L <库文件目录>
- 指定库文件
-l <abc> // 链接 libabc.so 或 lib.a
3.3 开发板程序编译示例
最后链接时,使用
arm-linux-ld而不是使用arm-linux-gcc
- 前者可以完全自己指定所连接的文件
- 后者会链接一些默认的启动文件
3.4 参考书籍
《嵌入式Linux应用开发完全手册》中的《3.1 交叉编译工具选项说明》
4.makefile
边栏推荐
- Machine learning basics memorandum
- 验证回文串
- PhD Debate | 自监督学习在推荐系统中的应用
- Is it reliable and safe to open an account for external futures?
- Enter a line of characters (80 at most), start from the location, intercept number characters and output them.
- Vscade markdown add, paste and import pictures
- Uniswapv2 peripheral contract learning (VII) -- exampleslidingwindoworacle sol
- 数据科学中的 10 个重要概念和图表的含义
- 2022年6月中国数据库排行榜:TiDB卷土重来摘桂冠,达梦蛰伏五月夺探花
- 【专题介绍】圆桌论坛——AI与音视频技术的融合之路
猜你喜欢

MySQL数据库的存储引擎

From just entering the testing industry to doubling my salary: talk about my advanced testing experience, which is worth learning from

机器学习基础备忘录

#yyds干货盘点# 解决剑指offer:矩形覆盖

入职腾讯外包一个月,我离职了

C listbox usage

The panorama of yuancosmos industrial investment, fast step into the new era of yuancosmos!

七牛云备份网站

GreatSQL如何做中国广受欢迎的开源数据库

15 Uncaught TypeError: Cannot set properties of null (setting ‘onclick‘)
随机推荐
Google installs the impression note clipping plug-in
Import word document picture VM virtual machine network settings
Data analysis--
From just entering the testing industry to doubling my salary: talk about my advanced testing experience, which is worth learning from
Docker-Compose实现Mysql主从
小众专业如何解决“成长”的烦恼?
Will quic become a disruptor of Internet transmission?
MySQL数据库的日志管理
One month after joining Tencent for outsourcing, I left
Machine learning basics memorandum
【专题介绍】圆桌论坛——AI与音视频技术的融合之路
C# 计算两个时间间隔
How can minority majors solve the problem of "growth"?
In WinCC, how to use C script to set + reset + negate variables?
期货外盘开户,还是内盘开户安全??
I haven't published a thesis for 5 years, and I want to give up my doctoral degree? Ten thousand words self narration of the doctoral director of the University of science and technology of China: he
MySQL数据库的存储引擎
Pourquoi l'instruction SQL utilise - t - elle un index, mais la requête est - elle lente?
Meanings of 10 important concepts and charts in Data Science
ERC721非同质代币(NFT)介绍