(轉)autoconfig.mk文件的自動生成


autoconf.mk

uboot的頂層Makefile中有如下的一段代碼
[plain]  view plain copy 在CODE上查看代碼片 派生到我的代碼片
 
  1. #  
  2. # Auto-generate the autoconf.mk file (which is included by all makefiles)  
  3. #  
  4. # This target actually generates 2 files; autoconf.mk and autoconf.mk.dep.  
  5. # the dep file is only include in this top level makefile to determine when  
  6. # to regenerate the autoconf.mk file.  
  7. $(obj)include/autoconf.mk.dep: $(obj)include/config.h include/common.h  
  8.     @$(XECHO) Generating $@ ; \  
  9.     set -e ; \  
  10.     : Generate the dependancies ; \  
  11.     $(CC) -x c -DDO_DEPS_ONLY -M $(CFLAGS) $(CPPFLAGS) \  
  12.         -MQ $(obj)include/autoconf.mk include/common.h > $@  
  13.   
  14. $(obj)include/autoconf.mk: $(obj)include/config.h  
  15.     @$(XECHO) Generating $@ ; \  
  16.     set -e ; \  
  17.     : Extract the config macros ; \  
  18.     $(CPP) $(CFLAGS) -DDO_DEPS_ONLY -dM include/common.h | \  
  19.         sed -n -f tools/scripts/define2mk.sed > $@.tmp && \  
  20.     mv $@.tmp $@  
[plain]  view plain  copy
 
 在CODE上查看代碼片派生到我的代碼片
  1. #  
  2. # Auto-generate the autoconf.mk file (which is included by all makefiles)  
  3. #  
  4. # This target actually generates 2 files; autoconf.mk and autoconf.mk.dep.  
  5. # the dep file is only include in this top level makefile to determine when  
  6. # to regenerate the autoconf.mk file.  
  7. $(obj)include/autoconf.mk.dep: $(obj)include/config.h include/common.h  
  8.     @$(XECHO) Generating $@ ; \  
  9.     set -e ; \  
  10.     : Generate the dependancies ; \  
  11.     $(CC) -x c -DDO_DEPS_ONLY -M $(CFLAGS) $(CPPFLAGS) \  
  12.         -MQ $(obj)include/autoconf.mk include/common.h > $@  
  13.   
  14. $(obj)include/autoconf.mk: $(obj)include/config.h  
  15.     @$(XECHO) Generating $@ ; \  
  16.     set -e ; \  
  17.     : Extract the config macros ; \  
  18.     $(CPP) $(CFLAGS) -DDO_DEPS_ONLY -dM include/common.h | \  
  19.         sed -n -f tools/scripts/define2mk.sed > $@.tmp && \  
  20.     mv $@.tmp $@  

先看第一個: $(obj)include/autoconf.mk.dep: $(obj)include/config.h include/common.h, 它表示autoconf.mk.dep依賴於config.h和common.h這兩個文件.
  • @$(XECHO) Generating $@ ; \ 這句話會在編譯階段輸出編譯信息 Generating include/autoconf.mk.dep
  • set -e ; \ 這句話表示, 當下面命令返回值不會0時, 整個腳本立即停止退出
  • : Generate the dependancies ; \  沒有明白是啥意思~~
  • $(CC) -x c -DDO_DEPS_ONLY -M $(CFLAGS) $(CPPFLAGS) -MQ $(obj)include/autoconf.mk include/common.h > $@
    • -DDO_DEPS_ONLY: 設置flag, 具體來說, 我們可以在common.h里面看見#ifdef DO_DEPS_ONLY這樣的語句, 這個-D就表示DO_DEPS_ONLY被定義了
    • -M : 表示生成依賴關系. 我還專門做了個實驗, 如下
      • gcc -M main.c 輸出結果為 main.o: main.c
    • -MQ: 表示指定依賴關系中target的名稱, 看下面的實驗
      • gcc -M -MQ newname.mk main.c 輸出結果為 newname.mk: main.c
    • 這句話表示: 生成依賴關系 include/autoconf.mk: include/common.h, 結果最終輸出到include/autoconf.mk.dep
  • 打開編譯后生成的include/autoconf.mk.dep, 可以查閱里面的內容. 至於為什么需要生成這個文件, 我現在還不清楚.
 
再看第二個: $(obj)include/autoconf.mk: $(obj)include/config.h, 它表示autoconf.mk依賴於include/config.h這個文件
  • @$(XECHO) Generating $@ ; \ 這句話會在編譯階段輸出編譯信息Generating include/autoconf.mk
  • set -e; \ 同上
  • $(CPP) $(CFLAGS) -DDO_DEPS_ONLY -dM include/common.h | sed -n -f tools/scripts/define2mk.sed > $@.tmp && mv $@.tmp $@
    • -DDO_DEPS_ONLY: 同上
    • -dM : 作用是輸出include/common.h中定義的所有宏
    • |      : shell中的管道, 表示將前面的結果傳遞給后面的命令
    • define2mk.sed : 查找和處理以“CONFIG_”開頭的宏定義的功能, 將處理的結果輸出到include/autoconf.mk.tmp
    • mv $@.tmp $@ : 重命名為include/autoconf.mk. 
    • 最終就會生成include/autoconf.mk
  •  include/common.h文件包含了include/config.h文件,而include/config.h文件又包含了config_defaults.h等uboot下的通用頭文件, 還會包含<configs/${CONFIG_NAME}.h>, 這個.h是我們自己創建的, 可以在里面添加自己的"CONFIG_"宏定義. 已決定開啟哪些功能.
至此, 我們知道, 系統中所有的"CONFIG_"開頭的宏開關, 都被放到了include/autoconf.mk中. 
 

depend

在uboot的頂層Makefile中, 我們經常會看到類似的代碼片段
[plain]  view plain copy 在CODE上查看代碼片 派生到我的代碼片
 
  1. $(obj)spl/u-boot-spl.bin:   $(SUBDIR_TOOLS) <span style="color: rgb(255, 0, 0);">depend</span>  
  2.         $(MAKE) -C spl all  
[plain]  view plain  copy
 
 在CODE上查看代碼片派生到我的代碼片
  1. $(obj)spl/u-boot-spl.bin:   $(SUBDIR_TOOLS) <span style="color:#ff00;">depend</span>  
  2.         $(MAKE) -C spl all  

這個depend是什么意思呢, Makefile中有如下一段代碼
[plain]  view plain copy 在CODE上查看代碼片 派生到我的代碼片
 
  1. # Explicitly make _depend in subdirs containing multiple targets to prevent  
  2. # parallel sub-makes creating .depend files simultaneously.  
  3. depend dep: $(TIMESTAMP_FILE) $(VERSION_FILE) \  
  4.         $(obj)include/spl-autoconf.mk \  
  5.         $(obj)include/tpl-autoconf.mk \  
  6.         $(obj)include/autoconf.mk \  
  7.         $(obj)include/generated/generic-asm-offsets.h \  
  8.         $(obj)include/generated/asm-offsets.h  
  9.         for dir in $(SUBDIRS) $(CPUDIR) $(LDSCRIPT_MAKEFILE_DIR) ; do \  
  10.             $(MAKE) -C $$dir _depend ; done  
[plain]  view plain  copy
 
 在CODE上查看代碼片派生到我的代碼片
  1. # Explicitly make _depend in subdirs containing multiple targets to prevent  
  2. # parallel sub-makes creating .depend files simultaneously.  
  3. depend dep: $(TIMESTAMP_FILE) $(VERSION_FILE) \  
  4.         $(obj)include/spl-autoconf.mk \  
  5.         $(obj)include/tpl-autoconf.mk \  
  6.         $(obj)include/autoconf.mk \  
  7.         $(obj)include/generated/generic-asm-offsets.h \  
  8.         $(obj)include/generated/asm-offsets.h  
  9.         for dir in $(SUBDIRS) $(CPUDIR) $(LDSCRIPT_MAKEFILE_DIR) ; do \  
  10.             $(MAKE) -C $$dir _depend ; done  

  • depend dep : 它表示這個規則有兩個目標. 也就是你自己的目標依賴depend或者dep都會跑到這里來繼續.
  • $(obj)include/spl-autoconf.mk \ : 表示depend依賴spl-autoconf.mk. 這個mk的生成規則也在Makefile中有定義. 細節方面參考上面的autoconf.mk
  • $(obj)include/tpl-autoconf.mk \ : 同上
  • $(obj)include/autoconf.mk \     : 同上
  • $(obj)include/generated/generic-asm-offsets.h \ : 暫不分析
  • $(obj)include/generated/asm-offsets.h : 暫不分析
  • for dir in $(SUBDIRS) $(CPUDIR) $(LDSCRIPT_MAKEFILE_DIR) ; do $(MAKE) -C $$dir _depend ; done
    • 這句話表示去上面各個目錄下執行 make _depend命令.
那我們看看對應子目錄下的Makefile, 例如$(CPUDIR) , 會看到如下的代碼片段
[plain]  view plain copy 在CODE上查看代碼片 派生到我的代碼片
 
  1. # defines $(obj).depend target  
  2. include $(SRCTREE)/rules.mk  
  3.   
  4. sinclude $(obj).depend  
[plain]  view plain  copy
 
 在CODE上查看代碼片派生到我的代碼片
  1. # defines $(obj).depend target  
  2. include $(SRCTREE)/rules.mk  
  3.   
  4. sinclude $(obj).depend  

並沒有看到_depend這個目標, 那make _depend在這個目錄下如何執行呢? 來看看它include的rules.mk
[plain]  view plain copy 在CODE上查看代碼片 派生到我的代碼片
 
  1. _depend:    $(obj).depend  
  2.   
  3. # Split the source files into two camps: those in the current directory, and  
  4. # those somewhere else. For the first camp we want to support CPPFLAGS_<fname>  
  5. # and for the second we don't / can't.  
  6. PWD_SRCS := $(filter $(notdir $(SRCS)),$(SRCS))  
  7. OTHER_SRCS := $(filter-out $(notdir $(SRCS)),$(SRCS))  
  8.   
  9. # This is a list of dependency files to generate  
  10. DEPS := $(basename $(patsubst %,$(obj).depend.%,$(PWD_SRCS)))  
  11.   
  12. # Join all the dependencies into a single file, in three parts  
  13. #   1 .Concatenate all the generated depend files together  
  14. #   2. Add in the deps from OTHER_SRCS which we couldn't process  
  15. #   3. Add in the HOSTSRCS  
  16. $(obj).depend:  $(src)Makefile $(TOPDIR)/config.mk $(DEPS) $(OTHER_SRCS) \  
  17.         $(HOSTSRCS)  
  18.     cat /dev/null $(DEPS) >$@  
  19.     @for f in $(OTHER_SRCS); do \  
  20.         g=`basename $$f | sed -e 's/\(.*\)\.[[:alnum:]_]/\1.o/'`; \  
  21.         $(CC) -M $(CPPFLAGS) -MQ $(obj) f >> $@ ; \  
  22.     done  
  23.     @for f in $(HOSTSRCS); do \  
  24.         g=`basename $$f | sed -e 's/\(.*\)\.[[:alnum:]_]/\1.o/'`; \  
  25.         $(HOSTCC) -M $(HOSTCPPFLAGS) -MQ $(obj) f >> $@ ; \  
  26.     done  
  27.   
  28. MAKE_DEPEND = $(CC) -M $(CPPFLAGS) $(EXTRA_CPPFLAGS_DEP) \  
  29.         -MQ $(addsuffix .o,$(obj)$(basename $<)) $< >$@  
  30.   
  31.   
  32. $(obj).depend.%:    %.c  
  33.     $(MAKE_DEPEND)  
  34.   
  35. $(obj).depend.%:    %.S  
  36.     $(MAKE_DEPEND)  
  37.   
  38. $(HOSTOBJS): $(obj)%.o: %.c  
  39.     $(HOSTCC) $(HOSTCFLAGS) $(HOSTCFLAGS_$(@F)) $(HOSTCFLAGS_$(BCURDIR)) -o $@ $< -c  
  40. $(NOPEDOBJS): $(obj)%.o: %.c  
  41.     $(HOSTCC) $(HOSTCFLAGS_NOPED) $(HOSTCFLAGS_$(@F)) $(HOSTCFLAGS_$(BCURDIR)) -o $@ $< -c  
  42.   
  43. #########################################################################  
[plain]  view plain  copy
 
 在CODE上查看代碼片派生到我的代碼片
  1. _depend:    $(obj).depend  
  2.   
  3. # Split the source files into two camps: those in the current directory, and  
  4. # those somewhere else. For the first camp we want to support CPPFLAGS_<fname>  
  5. # and for the second we don't / can't.  
  6. PWD_SRCS := $(filter $(notdir $(SRCS)),$(SRCS))  
  7. OTHER_SRCS := $(filter-out $(notdir $(SRCS)),$(SRCS))  
  8.   
  9. # This is a list of dependency files to generate  
  10. DEPS := $(basename $(patsubst %,$(obj).depend.%,$(PWD_SRCS)))  
  11.   
  12. # Join all the dependencies into a single file, in three parts  
  13. #   1 .Concatenate all the generated depend files together  
  14. #   2. Add in the deps from OTHER_SRCS which we couldn't process  
  15. #   3. Add in the HOSTSRCS  
  16. $(obj).depend:  $(src)Makefile $(TOPDIR)/config.mk $(DEPS) $(OTHER_SRCS) \  
  17.         $(HOSTSRCS)  
  18.     cat /dev/null $(DEPS) >$@  
  19.     @for f in $(OTHER_SRCS); do \  
  20.         g=`basename $$f | sed -e 's/\(.*\)\.[[:alnum:]_]/\1.o/'`; \  
  21.         $(CC) -M $(CPPFLAGS) -MQ $(obj)
     
    f >> $@ ; \  
  22.     done  
  23.     @for f in $(HOSTSRCS); do \  
  24.         g=`basename $$f | sed -e 's/\(.*\)\.[[:alnum:]_]/\1.o/'`; \  
  25.         $(HOSTCC) -M $(HOSTCPPFLAGS) -MQ $(obj)
     
    f >> $@ ; \  
  26.     done  
  27.   
  28. MAKE_DEPEND = $(CC) -M $(CPPFLAGS) $(EXTRA_CPPFLAGS_DEP) \  
  29.         -MQ $(addsuffix .o,$(obj)$(basename $<)) $< >$@  
  30.   
  31.   
  32. $(obj).depend.%:    %.c  
  33.     $(MAKE_DEPEND)  
  34.   
  35. $(obj).depend.%:    %.S  
  36.     $(MAKE_DEPEND)  
  37.   
  38. $(HOSTOBJS): $(obj)%.o: %.c  
  39.     $(HOSTCC) $(HOSTCFLAGS) $(HOSTCFLAGS_$(@F)) $(HOSTCFLAGS_$(BCURDIR)) -o $@ $< -c  
  40. $(NOPEDOBJS): $(obj)%.o: %.c  
  41.     $(HOSTCC) $(HOSTCFLAGS_NOPED) $(HOSTCFLAGS_$(@F)) $(HOSTCFLAGS_$(BCURDIR)) -o $@ $< -c  
  42.   
  43. #########################################################################  

這里有_depend目標, make _depend的時候, 執行的其實就是這里的_depend. 這個里面的細節不分析了, 大體來說有幾點
  • $(obj).depend: obj一般為空, 所以這里會在當前目錄下生成一個.depend文件
  • .depend里面的內容就是類似於 : main.o : main.c main.h . 
    • 想深究的話可以參考這里 : http://blog.csdn.net/panfengsoftware/article/details/7877864
最后在Makefile里面引用了當前目錄下的這個.depend


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM