编译linux内核出现的问题:fatal error: dt-bindings/interrupt-controller/arm-gic.h: No such file or directory

下载了核桃派使用的linux内核(5.16.17+):https://github.com/walnutpi/linux.git

按照其文档(编译linux | 核桃派)编译时出现错误:

root@niu:/mnt/f/learning/walnutPi/sw/linux-5.16.17# make -j12
  DTC     arch/arm64/boot/dts/allwinner/sun50i-h616-walnutpi-1b.dtb
In file included from arch/arm64/boot/dts/allwinner/sun50i-h616-walnutpi-1b.dts:8:
arch/arm64/boot/dts/allwinner/sun50i-h616.dtsi:6:10: fatal error: dt-bindings/interrupt-controller/arm-gic.h: No such file or directory
    6 | #include <dt-bindings/interrupt-controller/arm-gic.h>
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
make[2]: *** [scripts/Makefile.lib:353: arch/arm64/boot/dts/allwinner/sun50i-h616-walnutpi-1b.dtb] Error 1
make[2]: *** Waiting for unfinished jobs....
  DTC     arch/arm64/boot/dts/allwinner/overlay/sun50i-h616-tft35.dtbo
arch/arm64/boot/dts/allwinner/overlay/sun50i-h616-tft35.dts:3:10: fatal error: dt-bindings/gpio/gpio.h: No such file or directory
    3 | #include <dt-bindings/gpio/gpio.h>
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.

看提示应该是找不到所包含的路径或文件。

在 Linux 内核源码中的 scripts/dtc/include-prefixes 目录用于存放设备树编译器(Device Tree Compiler,DTC)的头文件包含路径的前缀。设备树编译器 (DTC) 用于将设备树源码编译成二进制设备树(.dtb)文件,以便在 Linux 内核启动时由引导加载程序传递给内核。通过指定这些前缀,DTC 可以找到所需的头文件并正确地处理设备树源码。

然后进入scripts/dtc/include-prefixes查看这些路径是否有效:

root@niu:/mnt/f/learning/walnutPi/sw/linux-5.16.17# cd scripts/dtc/include-prefixes
root@niu:/mnt/f/learning/walnutPi/sw/linux-5.16.17/scripts/dtc/include-prefixes# ls
arc  arm  arm64  dt-bindings  h8300  microblaze  mips  nios2  openrisc  powerpc  sh  xtensa
root@niu:/mnt/f/learning/walnutPi/sw/linux-5.16.17/scripts/dtc/include-prefixes# cd ./dt-bindings
-bash: cd: ./dt-bindings: Not a directory

看提示应该是dt-bindings这个软链接(符号链接)失效了,删除dt-bindings重新建立软连接(它类似于 Windows 系统中的快捷方式):

root@niu:/mnt/f/learning/walnutPi/sw/linux-5.16.17/scripts/dtc/include-prefixes# rm ./dt-bindings
root@niu:/mnt/f/learning/walnutPi/sw/linux-5.16.17/scripts/dtc/include-prefixes# ls
arc  arm  arm64  h8300  microblaze  mips  nios2  openrisc  powerpc  sh  xtensa
root@niu:/mnt/f/learning/walnutPi/sw/linux-5.16.17/scripts/dtc/include-prefixes# ln -s ../../../include/dt-bindings .
root@niu:/mnt/f/learning/walnutPi/sw/linux-5.16.17/scripts/dtc/include-prefixes# ls
arc  arm  arm64  dt-bindings  h8300  microblaze  mips  nios2  openrisc  powerpc  sh  xtensa
root@niu:/mnt/f/learning/walnutPi/sw/linux-5.16.17/scripts/dtc/include-prefixes# cd dt-bindings
root@niu:/mnt/f/learning/walnutPi/sw/linux-5.16.17/scripts/dtc/include-prefixes/dt-bindings# ls
arm  clock    firmware  i2c    interconnect          mailbox  mfd   net      pmu    regulator  sound    usb
bus  display  gce       iio    interrupt-controller  media    mips  phy      power  reset      spmi
clk  dma      gpio      input  leds                  memory   mux   pinctrl  pwm    soc        thermal

然后再重新编译:

root@niu:/mnt/f/learning/walnutPi/sw/linux-5.16.17# make -j12
  DTC     arch/arm64/boot/dts/allwinner/sun50i-h616-walnutpi-1b.dtb
  DTC     arch/arm64/boot/dts/allwinner/overlay/sun50i-h616-tft35.dtbo
  CALL    scripts/atomic/check-atomics.sh
  CALL    scripts/checksyscalls.sh
  LDS     arch/arm64/kernel/vdso/vdso.lds
  CC      arch/arm64/kernel/vdso/vgettimeofday.o
  AS      arch/arm64/kernel/vdso/note.o
  AS      arch/arm64/kernel/vdso/sigreturn.o
  LD      arch/arm64/kernel/vdso/vdso.so.dbg
  VDSOSYM include/generated/vdso-offsets.h
  OBJCOPY arch/arm64/kernel/vdso/vdso.so
  CC      init/main.o
  CHK     include/generated/compile.h
  CC      init/do_mounts.o
  CC      init/do_mounts_initrd.o
  CC      init/initramfs.o
  CC      init/calibrate.o
  CC      init/init_task.o
  HOSTCC  usr/gen_init_cpio
  UPD     include/generated/compile.h
  CC      arch/arm64/net/bpf_jit_comp.o
  CC      arch/arm64/mm/dma-mapping.o
  CC      certs/system_keyring.o
  GEN     usr/initramfs_data.cpio

...