一般我们在写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变量,但是这个变量不是自己定义的,说明是环境别人传递进来的

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

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

TopDIR ?= $(src)

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

   1 # ==========================================================================  
   2 # Signing modules                                                             
   3 # ==========================================================================  
   5 PHONY := __modsign                                                            
   6 __modsign:                                                                    
   8 include scripts/Kbuild.include                                                
  10 __modules := $(sort $(shell grep -h '\.ko$$' /dev/null $(wildcard $(MODVERDIR)/*.mod)))
  11 modules := $(patsubst %.o,%.ko,$(wildcard $(__modules:.ko=.o)))               
  13 PHONY += $(modules)                                                           
  14 __modsign: $(modules)                                                         
  15         @:                                                                    
  17 quiet_cmd_sign_ko = SIGN [M] $(2)/$(notdir $@)                                
  18         cmd_sign_ko = $(mod_sign_cmd) $(2)/$(notdir $@)                       
  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))
  24 modinst_dir = $(if $(KBUILD_EXTMOD),$(ext-mod-dir),kernel/$(@D))              
  26 $(modules):                                                                   
  27         $(call cmd,sign_ko,$(MODLIB)/$(modinst_dir))                          
  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.       
  32 .PHONY: $(PHONY)