Android-Copy in the compilation system

Source: Internet
Author: User

Android-Copy in the compilation system

When developing or porting the andriod system layer, you often need to add files to the compilation system, copy the files to out in the final compilation, and finally package them into an image, here we will summarize the Copy File method. Here we will take my android 4.2.1 as an example.

 

I. PRODUCT_COPY_FILES:

This variable is used to mark the Copy operation. The common form is as follows:

#jscese cp 3g script and PRODUCT_COPY_FILES +=     $(DEVICE_SOURCES)/3g-script/ip-up-datakey:system/etc/ppp/ip-up-datakey     $(DEVICE_SOURCES)/3g-script/ip-down-datakey:system/etc/ppp/ip-down-datakey     $(DEVICE_SOURCES)/3g-script/init.gprs-pppd:system/etc/ppp/init.gprs-pppd     device/sample/etc/apns-full-conf.xml:system/etc/apns-conf.xml     #external/usb-modeswitch/usb_modeswitch.d:system/etc/usb_modeswitch.d#PRODUCT_COPY_FILES +=       #$(DEVICE_SOURCES)/3g-script/ip-down-datakey:system/etc/ppp/ip-down-datakey     #end

 

You can see the format: Separated!

 


All those who have compiled the source code will see the following during the initial compilation:

 

PRODUCT_COPY_FILES frameworks/base/data/sounds/effects/ogg/Effect_Tick.ogg:system/media/audio/ui/Effect_Tick.ogg ignored.PRODUCT_COPY_FILES frameworks/base/data/sounds/effects/ogg/KeypressStandard.ogg:system/media/audio/ui/KeypressStandard.ogg ignored.PRODUCT_COPY_FILES frameworks/base/data/sounds/effects/ogg/KeypressSpacebar.ogg:system/media/audio/ui/KeypressSpacebar.ogg ignored.PRODUCT_COPY_FILES frameworks/base/data/sounds/effects/ogg/KeypressDelete.ogg:system/media/audio/ui/KeypressDelete.ogg ignored.PRODUCT_COPY_FILES frameworks/base/data/sounds/effects/ogg/KeypressReturn.ogg:system/media/audio/ui/KeypressReturn.ogg ignored....
This printing tells you where to print this item ~ /Build/core/MakefileThe most initial:

 

 

# -----------------------------------------------------------------# Define rules to copy PRODUCT_COPY_FILES defined by the product.# PRODUCT_COPY_FILES contains words like :
 
  [:
  
   ].# 
   
     is relative to $(PRODUCT_OUT), so it should look like,# e.g., system/etc/file.xml.# The filter part means only eval the copy-one-file rule if this# src:dest pair is the first one to match the same dest#$(1): the src:dest pairdefine check-product-copy-files$(if $(filter %.apk, $(1)),$(error     Prebuilt apk found in PRODUCT_COPY_FILES: $(1), use BUILD_PREBUILT instead!))endef# filter out the duplicate 
    :
    
      pairs.unique_product_copy_files_pairs :=$(foreach cf,$(PRODUCT_COPY_FILES),     $(if $(filter $(unique_product_copy_files_pairs),$(cf)),,        $(eval unique_product_copy_files_pairs += $(cf))))unique_product_copy_files_destinations :=$(foreach cf,$(unique_product_copy_files_pairs),     $(eval _src := $(call word-colon,1,$(cf)))     $(eval _dest := $(call word-colon,2,$(cf)))     $(call check-product-copy-files,$(cf))     $(if $(filter $(unique_product_copy_files_destinations),$(_dest)),         $(info PRODUCT_COPY_FILES $(cf) ignored.),         $(eval _fulldest := $(call append-path,$(PRODUCT_OUT),$(_dest)))         $(if $(filter %.xml,$(_dest)),            $(eval $(call copy-xml-file-checked,$(_src),$(_fulldest))),            $(eval $(call copy-one-file,$(_src),$(_fulldest))))         $(eval ALL_DEFAULT_INSTALLED_MODULES += $(_fulldest))         $(eval unique_product_copy_files_destinations += $(_dest))))unique_product_copy_files_pairs :=unique_product_copy_files_destinations :=
    
   
  
 

 

This isPRODUCT_COPY_FILESWhere it works! You can refer to the above annotations and describe the rule usage,Only file can be copied.!

That is, the source of the above compilation and printing, which indicates that the items are ignored. Detailed rules can be followed in detail, and they are nothing more than dependent on Copy.

 

 

Note that,PRODUCT_COPY_FILES cannot be in Android. mkTo add a new Copy item!

 

# Can't use first-makefiles-under here because# --mindepth=2 makes the prunes not work.subdir_makefiles := $(shell build/tools/findleaves.py --prune=out --prune=.repo --prune=.git $(subdirs) Android.mk)include $(subdir_makefiles)endif # ONE_SHOT_MAKEFILE# Now with all Android.mks loaded we can do post cleaning steps.include $(BUILD_SYSTEM)/post_clean.mkifeq ($(stash_product_vars),true)  $(call assert-product-vars, __STASHED)endif

 

Load all Android. mk here, focusing onAssert-product-varsFunction:

 

## Assert that the the variable stashed by stash-product-vars remains untouched.# $(1): The prefix as supplied to stash-product-vars#define assert-product-vars$(strip   $(eval changed_variables:=)  $(foreach v,$(_product_stash_var_list),     $(if $(call streq,$($(v)),$($(strip $(1))_$(call rot13,$(v)))),,         $(eval $(warning $(v) has been modified: $($(v))))         $(eval $(warning previous value: $($(strip $(1))_$(call rot13,$(v)))))         $(eval changed_variables := $(changed_variables) $(v)))    )   $(if $(changed_variables),    $(eval $(error The following variables have been changed: $(changed_variables))),))endef

If there is a change, an error will be reported. The compilation error is as follows:

 

 

Build/core/main. mk: 528: *** The following variables have been changed: PRODUCT_COPY_FILES. Stop.

 

 

UsePRODUCT_COPY_FILESIt should be the most commonly used Copy File method. It can be directly added to device. mk!

 

 

 

Ii. copy_to copy_from ALL_PREBUILT:

This method is used in Android. mk. For more information, see/System/core/rootdir/Android. mk:

 

copy_from += etc/init.goldfish.shcopy_to := $(addprefix $(TARGET_OUT)/,$(copy_from))copy_from := $(addprefix $(LOCAL_PATH)/,$(copy_from))$(copy_to) : PRIVATE_MODULE := system_etcdir$(copy_to) : $(TARGET_OUT)/% : $(LOCAL_PATH)/% | $(ACP)    $(transform-prebuilt-to-target)ALL_PREBUILT += $(copy_to)

We can see that copy_from requires copy, and copy_to is the destination!

 

You can see the rules defined in/build/core/definitions. mk:

 

# Copy a prebuilt file to a target location.define transform-prebuilt-to-target@echo $(if $(PRIVATE_IS_HOST_MODULE),host,target) Prebuilt: $(PRIVATE_MODULE) ($@)$(copy-file-to-target)endef

define copy-file-to-target@mkdir -p $(dir $@)$(hide) $(ACP) -fp $< $@endef

Note that if we add some files to copy_from by ourselves, the following will appear:

 

 

Build/core/main. mk: 533: *** Some files have been added to ALL_PREBUILT.build/core/main. mk: 534: * build/core/main. mk: 535: * ALL_PREBUILT is a deprecated mechanic thatbuild/core/main. mk: 536: * shocould not be used for new files. build/core/main. mk: 537: * As an alternative, use PRODUCT_COPY_FILES inbuild/core/main. mk: 538: * the appropriate product definition. build/core/main. mk: 539: * build/target/product /Core. mk is the productbuild/core/main. mk: 540: * definition used in all products. build/core/main. mk: 541: * build/core/main. mk: 542: * unexpected usb_modeswitch.h in ALL_PREBUILTbuild/core/main. mk: 542: * unexpected usb_modeswitch.sh in ALL_PREBUILTbuild/core/main. mk: 542: * unexpected usb_modeswitch.tcl in ALL_PREBUILTbuild/core/main. mk: 543: * build/core/main. mk: 544: *** ALL_PREBUILT contains unexpecte D files. Stop.

The error occurs in /Build/core/main. mk

 

 

include $(BUILD_SYSTEM)/legacy_prebuilts.mkifneq ($(filter-out $(GRANDFATHERED_ALL_PREBUILT),$(strip $(notdir $(ALL_PREBUILT)))),)  $(warning *** Some files have been added to ALL_PREBUILT.)  $(warning *)  $(warning * ALL_PREBUILT is a deprecated mechanism that)  $(warning * should not be used for new files.)  $(warning * As an alternative, use PRODUCT_COPY_FILES in)  $(warning * the appropriate product definition.)  $(warning * build/target/product/core.mk is the product)  $(warning * definition used in all products.)  $(warning *)  $(foreach bad_prebuilt,$(filter-out $(GRANDFATHERED_ALL_PREBUILT),$(strip $(notdir $(ALL_PREBUILT)))),$(warning * unexpected $(bad_prebuilt) in ALL_PREBUILT))  $(warning *)  $(error ALL_PREBUILT contains unexpected files)endif

 

 

The print information above tells usALL_PREBUILTIt is an out-of-date mechanism and is no longer used to copy new files. We recommend that you usePRODUCT_COPY_FILES!

 

You can addALL_PREBUILTThe variable member is defined inLegacy_prebuilts.mkMedium:

 # This is the list of modules grandfathered to use ALL_PREBUILT# DO NOT ADD ANY NEW MODULE TO THIS FILE## ALL_PREBUILT modules are hard to control and audit and we don't want# to add any new such module in the systemGRANDFATHERED_ALL_PREBUILT :=     akmd2     am     ap_gain.bin ...

I understand the annotations!

So if we want to add a file to compile copy, we cannot useCopy_from copy_to ALL_prebuiltThis mechanism!

 

 

 

Iii. BUILD_PREBUILT:

In this way, the file is used as a compilation project, and a file is copied in Android. mk:

LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS) LOCAL_MODULE := usb_modeswitch.conf LOCAL_MODULE_CLASS := ETC  LOCAL_MODULE_PATH := $(TARGET_OUT)/etc LOCAL_SRC_FILES :=$(LOCAL_MODULE)  include $(BUILD_PREBUILT) 

The above is the copy usb_modeswitch.conf file to the etc directory under the OUT Directory, which is often used to store configuration-related files.

All of the above are about Copy File, but if you need to Copy a File directory, You Need To perform other operations!

 

 

 

Iv. Copy Directory

Copy from usb-modeswitch (2) on the road of porting Android-4.2-3G from my previous blogUsb-modewitch.dThe data directory is used as an example.

In Android. mk there, I used the shell command Copy:

 

$(shell cp -rf $(LOCAL_PATH)/usb_modeswitch.d $(TARGET_OUT)/etc/usb_modeswitch.d)  

In this case, the source code has been compiled, because the $ (TARGET_OUT)/etc directory already exists, but it will not be copied as a new compilation,

 

Therefore, in the android compilation system, operations must be performed according to the mechanism provided by android. It is convenient to use shell like this, but it is not the right path!

My current processing:

In the shell above, include processes the mk file of the Copy usb-modewitch.d with the following content:

# $(1): module name# $(2): source file# $(3): destination directorydefine include-prebuilt-with-destination-directoryinclude $$(CLEAR_VARS)LOCAL_MODULE := $(1)LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/usb_modeswitch_data.mkLOCAL_MODULE_STEM := $(notdir $(2))LOCAL_MODULE_TAGS := optionalLOCAL_MODULE_CLASS := ETCLOCAL_MODULE_PATH := $(3)LOCAL_SRC_FILES := $(2)include $$(BUILD_PREBUILT)endef#== rename 's/:/_/' * ==#usb_modeswitch_data := $(notdir $(wildcard $(LOCAL_PATH)/usb_modeswitch.d/*))$(warning jscese display -usb_modeswitch_data--$(usb_modeswitch_data))usb_modeswitch_data_target_directory := $(TARGET_OUT)/etc/usb_modeswitch.d$(foreach data, $(usb_modeswitch_data), $(eval $(call include-prebuilt-with-destination-directory,target-data-$(notdir $(data)),usb_modeswitch.d/$(data),$(usb_modeswitch_data_target_directory))))usb_modeswitch_data_target := $(addprefix $(usb_modeswitch_data_target_directory)/,$(foreach cacert,$(usb_modeswitch_data),$(notdir $(usb_modeswitch_data)))).PHONY: usb_modeswitch_data_targetusb_modeswitch_data: $(usb_modeswitch_data_target)# This is so that build/target/product/core.mk can use usb_modeswitch_data in PRODUCT_PACKAGESALL_MODULES.usb_modeswitch_data.INSTALLED := $(usb_modeswitch_data_target)

We can see that the same principle as copying a single File is the same as BUILD_PREBUILT, but it just traverses the folder and writes it into it as a module for processing. PRODUCT_PACKAGES

For more information about the role of the PRODUCT_PACKAGES variable, see my previous blog. Android-Control Factors for compiling and installing modules

 

In addition, because the file names are of the XXXX: XXXX type, I will directly use the reanme command to replace all: _

If this parameter is not replaced, makefile may use the following code as a dependency symbol during file input ~ An error is reported.

If I useSubstReplace: With an escape character. It seems that the file cannot be found. I wonder if this problem has been encountered .....

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.