[Linux笔记]内核裁剪

文章目录

1、Linux内核裁剪

目标:

  1. 不使用make menuconfig,增加/减少Linux内核编译输出的模块
  2. 搞清楚Kconfig.configxxx_defconfigMakefile之间的关系

1.1.1、加载开发板默认配置

将 arch/arm/configs/100ask_imx6ull_defconfig 复制到 Linux项目的根目录下,并重命名为为 .config

bash 复制代码
$ make 100ask_imx6ull_defconfig
  1. 如果要在100ask_imx6ull_defconfig的配置基础上添加自己的配置可以👇
  2. 在相应的目录下编写驱动程序,例如在drivers/input/misc/下编写100ask_adxl345-spi.c
  3. drivers/input/misc/下找到Kconfig文件并修改
bash 复制代码
...
config INPUT_ADXL34X
	tristate "Analog Devices ADXL34x Three-Axis Digital Accelerometer"
	default n
	help
	  Say Y here if you have a Accelerometer interface using the
	  ADXL345/6 controller, and your board-specific initialization
	  code includes that in its table of devices.

	  This driver can use either I2C or SPI communication to the
	  ADXL345/6 controller.  Select the appropriate method for
	  your system.

	  If unsure, say N (but it's safe to say "Y").

	  To compile this driver as a module, choose M here: the
	  module will be called adxl34x.
	  
...

config INPUT_ADXL34x_MOD
    tristate "100ask SPI bus connection"
    depends on INPUT_ADXL34X && SPI
    default m
    help
        Say Y here if you have ADXL345/6 hooked to a SPI bus.

        To compile this driver as a module, choose M here: the
        module will be called 100ask_adxl34x-spi.
...

make menuconfig的GUI界面勾选编译选项后,会修改.config的编译配置

bash 复制代码
$ make menuconfig

被make menuconfig修改后的.config文件如下

bash 复制代码
...
CONFIG_INPUT_ADXL34X=y
...
CONFIG_INPUT_ADXL34x_MOD=m
...

👆这些变量名都会在编译时被读取到Kconfig同级目录的Makefile中,例如drivers/input/misc/的Makefile如下所示

bash 复制代码
...
obj-$(CONFIG_INPUT_ADXL34X)		+= adxl34x.o
...
obj-$(CONFIG_INPUT_ADXL34x_MOD)         += 100ask_adxl345-spi.o
...

1.1.4、把修改后的.config保存成新的xxx_defconfig

这里的xxx_defconfig == 100ask_imx6ull_defconfig

bash 复制代码
$ make savedefconfig
$ mv defconfig arch/arm/configs/my_name_defconfig

1.1.5、编译

bash 复制代码
# 如果obj-m, 则👇即可
make modules
# 如果obj-y, 则👇即可
make zImage

1.1.3章节中,直接手工修改.config文件即可

2、总结

  1. xxx_defconfig:文件用于记录配置好的编译配置
  2. .config:内核当前的编译配置,存在于内核的顶层目录,在make xxx_defconfig后出现,从 arch/.../configs/xxx_defconfig 生成 .config
  3. make menuconfig:修改.config,手动编辑 .config 可替代 menuconfig
  4. Kconfig:定义make menuconfig的菜单项
  5. Kconfig同级目录的Makefile:读取.config中的CONFIG_XXX变量来决定编译行为,通过 obj- $ (CONFIG_XXX) 响应配置
相关推荐
Coder个人博客5 小时前
Linux6.19-ARM64 mm mmu子模块深入分析
大数据·linux·车载系统·系统架构·系统安全·鸿蒙系统
羊群智妍8 小时前
2026 AI搜索流量密码:免费GEO监测工具,优化效果看得见
笔记·百度·微信·facebook·新浪微博
Doro再努力8 小时前
Vim 快速上手实操手册:从入门到生产环境实战
linux·编辑器·vim
wypywyp8 小时前
8. ubuntu 虚拟机 linux 服务器 TCP/IP 概念辨析
linux·服务器·ubuntu
Doro再努力8 小时前
【Linux操作系统10】Makefile深度解析:从依赖推导到有效编译
android·linux·运维·服务器·编辑器·vim
senijusene9 小时前
Linux软件编程:IO编程,标准IO(1)
linux·运维·服务器
忧郁的橙子.9 小时前
02-本地部署Ollama、Python
linux·运维·服务器
醇氧9 小时前
【linux】查看发行版信息
linux·运维·服务器
No8g攻城狮9 小时前
【Linux】Windows11 安装 WSL2 并运行 Ubuntu 22.04 详细操作步骤
linux·运维·ubuntu
XiaoFan01210 小时前
免密批量抓取日志并集中输出
java·linux·服务器