内核驱动编译之Makefile shell pwd路径问题

一般我们在写Makefile的时候为了获取到当前Makefile所在的文件夹路径,会使用TopDIR ?= $(shell pwd)来定义,后续的文件路径都是基于此TopDIR基础上使用。

今天在移植一个rtl8822bu的wifi驱动到平台上编译的时候出现错误,

drivers/net/wireless/rtl8822bu/Makefile:809: /home/liuxueneng/workCode/allwinner_h2_2018-0907/lichee/linux-4.4/rtl8822b.mk: No such file or directory

找不到lichee/linux-4.4/rtl8822b.mk这个文件

打开Makefile看一下,


806 ########### HAL_RTL8822B 807 ################################# 808 ifeq ($(CONFIG_RTL8822B), y) 809 include $(TopDIR)/rtl8822b.mk 810 endif

出错的地方正式使用了TopDIR的路径,Makefile的本意是包含当前驱动文件夹下(rtl8822bu)的rtl8822b.mk文件,现在编译的时候却找到了linux-4.4的目录下,说明使用pwd获取到的就是linux-4.4的目录,换句话说这个Makefile在linux-4.4目录下面被调用执行。

现在要修正这个路径获取问题,

仔细看了下这个Makefile,后面还涉及到文件路径指定使用的参考路径 src变量,但是这个变量不是自己定义的,说明是环境别人传递进来的

                                                                              
 242                                                                               
 243 EXTRA_CFLAGS += -I$(src)/platform                                             
 244 _PLATFORM_FILES := platform/platform_ops.o                                    
 245                                                                               
 246 EXTRA_CFLAGS += -I$(src)/hal/btc 

于是把TopDIR也换成这个src,验证了一下编译通过。

TopDIR ?= $(src)

这个src是从linux-4.4/scripts/Makefile.modsign 传进来的。

   1 # ==========================================================================  
   2 # Signing modules                                                             
   3 # ==========================================================================  
   4                                                                               
   5 PHONY := __modsign                                                            
   6 __modsign:                                                                    
   7                                                                               
   8 include scripts/Kbuild.include                                                
   9                                                                               
  10 __modules := $(sort $(shell grep -h '.ko$$' /dev/null $(wildcard $(MODVERDIR)/*.mod)))
  11 modules := $(patsubst %.o,%.ko,$(wildcard $(__modules:.ko=.o)))               
  12                                                                               
  13 PHONY += $(modules)                                                           
  14 __modsign: $(modules)                                                         
  15         @:                                                                    
  16                                                                               
  17 quiet_cmd_sign_ko = SIGN [M] $(2)/$(notdir $@)                                
  18         cmd_sign_ko = $(mod_sign_cmd) $(2)/$(notdir $@)                       
  19                                                                               
  20 # Modules built outside the kernel source tree go into extra by default       
  21 INSTALL_MOD_DIR ?= extra                                                      
  22 ext-mod-dir = $(INSTALL_MOD_DIR)$(subst $(patsubst %/,%,$(KBUILD_EXTMOD)),,$(@D))
  23                                                                               
  24 modinst_dir = $(if $(KBUILD_EXTMOD),$(ext-mod-dir),kernel/$(@D))              
  25                                                                               
  26 $(modules):                                                                   
  27         $(call cmd,sign_ko,$(MODLIB)/$(modinst_dir))                          
  28                                                                               
  29 # Declare the contents of the .PHONY variable as phony.  We keep that         
  30 # information in a variable se we can use it in if_changed and friends.       
  31                                                                               
  32 .PHONY: $(PHONY) 
原文地址:https://www.cnblogs.com/tid-think/p/10904214.html