Android編譯系統詳解(三)

來源:互聯網
上載者:User

++++++++++++++++++++++++++++++++++++++++++

本文系本站原創,歡迎轉載! 轉載請註明出處:

http://blog.csdn.net/mr_raptor/article/details/7540730

++++++++++++++++++++++++++++++++++++++++++

 

    Android編譯系統詳解(一):http://blog.csdn.net/mr_raptor/article/details/7539978

    Android編譯系統詳解(二):http://blog.csdn.net/mr_raptor/article/details/7540066

    Android編譯系統詳解(三):http://blog.csdn.net/mr_raptor/article/details/7540730

前面兩節講解了自訂Android編譯項和建立Product產品設定檔,除了編譯和定義產品相關環境變數外,還需要定義Board相關環境變數。

1. build/core/config.mk

[plain]
view plaincopy
  1. 109 # ---------------------------------------------------------------   
  2. 110 # Define most of the global variables.  These are the ones that   
  3. 111 # are specific to the user's build configuration.   
  4. 112 include $(BUILD_SYSTEM)/envsetup.mk   
  5. 113    
  6. 114 # Boards may be defined under $(SRC_TARGET_DIR)/board/$(TARGET_DEVICE)   
  7. 115 # or under vendor/*/$(TARGET_DEVICE).  Search in both places, but   
  8. 116 # make sure only one exists.   
  9. 117 # Real boards should always be associated with an OEM vendor.   
  10. 118 board_config_mk := \   
  11. 119     $(strip $(wildcard \   
  12. 120         $(SRC_TARGET_DIR)/board/$(TARGET_DEVICE)/BoardConfig.mk \   
  13. 121         vendor/*/$(TARGET_DEVICE)/BoardConfig.mk \   
  14. 122     ))   
  15. 123 ifeq ($(board_config_mk),)   
  16. 124   $(error No config file found for TARGET_DEVICE $(TARGET_DEVICE))   
  17. 125 endif   
  18. 126 ifneq ($(words $(board_config_mk)),1)   
  19. 127   $(error Multiple board config files for TARGET_DEVICE $(TARGET_DEVICE): $(board_config_mk))   
  20. 128 endif   
  21. 129 include $(board_config_mk)   
  22. 130 TARGET_DEVICE_DIR := $(patsubst %/,%,$(dir $(board_config_mk)))   
  23. 131 board_config_mk :=   

上述代碼在上一節已經見到過,只是分析了112行的envsetup.mk,根據上一節內容可知,envsetup.mk設定了很多OUT變數,最終在build/core/product_config.mk檔案裡,設定了TARGET_DEVICE = fs100。

我們從114行繼續分析。

  • 從114~117行解釋大意可知:

    Board相關設定檔會存在於$(SRC_TARGET_DIR)/board/$(TARGET_DEVICE)/或vendor/*/$(TARGET_DEVICE)/目錄中,一個Vendor廠商只能有一個對應的Board設定檔。

  • 118行,定義board_config_mk變數:

     $(wildcard xxx)函數就是找到與xxx的匹配項放到空格列表裡,前面定義TARGET_DEVICE變數 = fs100,所以$(SRC_TARGET_DIR)/board/fs100/BoardConfig.mk不存在,必須要存在vendor/*/fs1 00/BoardConfig.mk檔案來定義開發板配置資訊。

  • 129行,通過include將vendor/*/fs100/BoardConfig.mk包含進來,
  • 130行,TARGET_DEVICE_DIR為board_config_mk的路徑,即:vendor/*/fs100

  總結:

 一個vendor廠商必須要有一個對應的Board設定檔,即:vendor/*/fs100/BoardConfig.mk

    定義TARGET_DEVICE_DIR變數,為board_config_mk的路徑,即:vendor/*/fs100

指定board 相關特性,一定要包含:
      TARGET_CPU_ABI := armeabi/...
其他屬性參見其他board範例.(build/target/board/XXX


2.  build/core/main.mk

[plain]
view plaincopy
  1. 141 # Bring in standard build system definitions.  
  2. 142 include $(BUILD_SYSTEM)/definitions.mk  
  3. ...  
  4. 347 ifeq ($(SDK_ONLY),true)  
  5. 348  
  6. 349 # ----- SDK for Windows ------  
  7. 350 # These configure the build targets that are available for the SDK under Cygwin.  
  8. 351 # The first section defines all the C/C++ tools that can be compiled under Cygwin,  
  9. 352 # the second section defines all the Java ones (assuming javac is available.)  
  10. 353  
  11. 354 subdirs := \  
  12. 355     prebuilt \  
  13. 356     build/libs/host \  
  14. 357     build/tools/zipalign \  
  15. ...  
  16. 382 # The following can only be built if "javac" is available.  
  17. 383 # This check is used when building parts of the SDK under Cygwin.  
  18. 384 ifneq (,$(shell which javac 2>/dev/null))  
  19. 385 $(warning sdk-only: javac available.)  
  20. 386 subdirs += \  
  21. 387     build/tools/signapk \  
  22. 388     dalvik/dx \  
  23. 389     dalvik/libcore \  
  24. ...  
  25. 414 else    # !SDK_ONLY  
  26. 415 ifeq ($(BUILD_TINY_ANDROID), true)  
  27. 416  
  28. 417 # TINY_ANDROID is a super-minimal build configuration, handy for board  
  29. 418 # bringup and very low level debugging  
  30. 419  
  31. 420 subdirs := \  
  32. 421     bionic \  
  33. 422     system/core \  
  34. 423     build/libs \  
  35. 424     build/target \  
  36. ...  
  37. 433 else    # !BUILD_TINY_ANDROID  
  38. 434  
  39. 435 #  
  40. 436 # Typical build; include any Android.mk files we can find.  
  41. 437 #  
  42. 438 subdirs := $(TOP)  
  43. 439  
  44. 440 FULL_BUILD := true  
  45. 441  
  46. 442 endif   # !BUILD_TINY_ANDROID  
  47. 443  
  48. 444 endif   # !SDK_ONLY  
  49. ...  
  50. 464 #  
  51. 465 # Include all of the makefiles in the system  
  52. 466 #  
  53. 467  
  54. 468 # Can't use first-makefiles-under here because  
  55. 469 # --mindepth=2 makes the prunes not work.  
  56. 470 subdir_makefiles := \  
  57. 471     $(shell build/tools/findleaves.py --prune=out --prune=.repo --prune=.git $(subdirs) Android.mk)  
  58. 472  
  59. 473 include $(subdir_makefiles)  

上一節只是講了main.mk第49行中包含了config.mk,我們繼續分析。

142行包含了:build/core/definitions.mk,該檔案定義了很多全域變數與函數。

如下列常見函數:

    my-dir:返回當前路徑

    all-java-files-under:獲得指定目錄及子目錄一所有java檔案

    all-subdir-c-files:獲得目前的目錄下及子目錄下所有c檔案

354~444行,定義了subdirs變數,依據不同的使用者編譯條件,而包含Android源碼中不同的目錄。

470行,定義了subdir_makefile變數,其值為subdirs定義的目錄中的Android.mk檔案。

473行,將所有編譯目錄中的Android.mk檔案包含進來。


3. build/target/board/Android.mk

[plain]
view plaincopy
  1. 26 ifeq (,$(wildcard $(TARGET_DEVICE_DIR)/AndroidBoard.mk))  
  2.  27   ifeq (,$(wildcard $(TARGET_DEVICE_DIR)/Android.mk))  
  3.  28     $(error Missing "$(TARGET_DEVICE_DIR)/AndroidBoard.mk")  
  4.  29   else  
  5.  30     # TODO: Remove this check after people have had a chance to switch,  
  6.  31     # after April 2009.  
  7.  32     $(error Please rename "$(TARGET_DEVICE_DIR)/Android.mk" to "$(TARGET_DEVICE_DIR)/AndroidBoard.mk")  
  8.  33   endif  
  9.  34 endif  
  10.  35 include $(TARGET_DEVICE_DIR)/AndroidBoard.mk  

由於將所有目錄中Android.mk檔案include進來,build/target/board/Android.mk自然被包含進來,根據前面分析,TARGET_DEVICE_DIR = vendor/*/fs100,其中26~35行用來判斷對應的產品目錄下是否存在AndrodiBoard.mk,如果不存在,提示出錯退出,如果存在,將其包含到編譯指令碼中。

由此可見:我們必須要在產品目錄下建立AndrodiBoard.mk檔案,來描述開發板相關配置項,我們可以借鑒:build/target/board/generic/AndroidBoard.mk內容,同時根據前面所分析,還要建立BoardConfig.mk檔案。

[plain]
view plaincopy
  1. $ cp build/target/board/generic/AndroidBoard.mk build/target/board/generic/BoardConfig.mk  vendor/farsight/fs100/  

至此,自訂Android編譯選項基本步驟已經分部分析完,細節還需要針對不同開發板具體分析。

總結:

build/core/main.mk包含了config.mk,它主要定義了編譯全部代碼的依賴關係

      build/core/config.mk         定義了大量的編譯指令碼命令,編譯時間用到的環境變數,引入了envsetup.mk 檔案,載入board相關設定檔。
      build/core/envsetup.mk   定義了編譯時間用到的大量OUT輸出目錄,載入product_config.mk檔案
      build/core/product_config.mk 定義了Vendor目錄下Product相關設定檔解析指令碼,讀取AndrodProducts.mk產生TARGET_DEVICE變數
      build/target/product          product config
      build/target/board            board config
      build/core/combo             build flags config 

      這裡解釋下這裡的board和product。borad主要是設計到硬體晶片的配置,比如是否提供硬體的某些功能,比如說GPU等等,或者晶片支援浮 點運算等等。product是指標對當前的晶片配置定義你將要生產產品的個性配置,主要是指APK方面的配置,哪些APK會包含在哪個product中,哪些APK在當前product中是不提供的。
      config.mk是一個總括性的東西,它裡面定義了各種module編譯所需要使用的HOST工具以及如何來編譯各種模組,比如說 BUILT_PREBUILT就定義了如何來編譯先行編譯模組。envsetup.mk主要會讀取由envsetup.sh寫入環境變數中的一些變數來配置編譯過程中的輸出目錄,combo裡面主要定義了各種Host和Target結合的編譯器和編譯選項。

1. 在vendor目錄下建立自己公司目錄,然後在公司目錄下建立一個新的vendorsetup.sh,在裡面添加上自己的產品編譯項

[plain]
view plaincopy
  1. $ mkdir vendor/farsight/   
  2. $ touch vendor/farsight/vendorsetup.sh   
  3. $ echo "add_lunch_combo fs100-eng" > vendor/farsight/vendorsetup.sh   

2. 仿著Android範例程式碼,在公司目錄下建立products目錄

[plain]
view plaincopy
  1. $ mkdir -p vendor/farsight/products  

3. 仿著Android範例程式碼,在products目錄下建立兩個mk檔案

[plain]
view plaincopy
  1. $ touch vendor/farsight/products/AndroidProduct.mk vendor/farsight/products/fs100.mk  

    在AndroidProduct.mk裡添加如下內容:

[plain]
view plaincopy
  1. PRODUCT_MAKEFILES := $(LOCAL_DIR)/fs100.mk   

    在產品設定檔裡添加最基本資料

[plain]
view plaincopy
  1. $(call inherit-product, build/target/product/generic.mk)   
  2.    
  3. # Overrides   
  4. PRODUCT_MANUFACTURER := farsight   
  5. PRODUCT_NAME := fs100   
  6. PRODUCT_DEVICE := fs100   

4. 借鑒build/target/board/generic/AndroidBoard.mk和BoardConfig.mk,建立對應檔案。

[plain]
view plaincopy
  1. $ cp build/target/board/generic/AndroidBoard.mk build/target/board/generic/BoardConfig.mk  vendor/farsight/fs100/  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.