ABS(Android Build System)中在编译时生成源代码文件

编译时经常有的需求是有些.c或者.h文件需要在编译时由某个接口定义文件生成。同时还可能有下面的需求:
* 同一个接口定义文件会用于产生多个源文件。
* 生成的源文件可能会被其它的Project使用。

举例来说,现在需要从接口定义文件xxx.xml通过可执行文件generator生成源文件xxx_A.h,xxx_B.h和xxx_C.c。命令为:
generator A < xxx.xml > xxx_A.h
generator B < xxx.xml > xxx_B.h
generator C < xxx.xml > xxx_C.c

一个方法是用ABS里定义的transform-generated-source函数:

GENERATOR := $(HOST_OUT_EXECUTABLES)/generator$(HOST_EXECUTABLE_SUFFIX)                                      
IDL_DIR := ./                                                                                                 
IDL_SRC := $(IDL_DIR)/xxx.xml                                                                                    
IDL_TGT_CODE := $(IDL_DIR)/xxx_C.c                                                                        
IDL_TGT_A := $(IDL_DIR)/xxx_A.h                                                        
IDL_TGT_B := $(IDL_DIR)/xxx_B.h                                                        
                                                                                                                         
GEN := $(LOCAL_PATH)/$(IDL_TGT_A)                                                                          
$(GEN): PRIVATE_PATH := $(LOCAL_PATH)                                                                                    
$(GEN): PRIVATE_CUSTOM_TOOL = $(GENERATOR) A < $< > $@                                                 
$(GEN): $(LOCAL_PATH)/$(IDL_SRC)                                                                                       
    $(transform-generated-source)                                                                                        
                                                                                                                         
LOCAL_GENERATED_SOURCES += $(GEN)                                                                                        
                                                                                                                         
GEN := $(LOCAL_PATH)/$(IDL_TGT_B)                                                                          
$(GEN): PRIVATE_PATH := $(LOCAL_PATH)                                                                                    
$(GEN): PRIVATE_CUSTOM_TOOL = $(GENERATOR) B < $< > $@                                                 
$(GEN): $(LOCAL_PATH)/$(IDL_SRC)                                                                                       
    $(transform-generated-source)                                                                                        
                                                                                                                         
LOCAL_GENERATED_SOURCES += $(GEN)                                                                                        
                                                                                                                         
GEN := $(LOCAL_PATH)/$(IDL_TGT_CODE)                                                                                   
$(GEN): PRIVATE_PATH := $(LOCAL_PATH)                                                                                    
$(GEN): PRIVATE_CUSTOM_TOOL = $(GENERATOR) C < $< > $@                                                          
$(GEN): $(LOCAL_PATH)/$(IDL_SRC) $(LOCAL_PATH)/$(IDL_TGT_A) $(LOCAL_PATH)/$(IDL_TGT_B)     
    $(transform-generated-source)                                                                                        
                                                                                                                         
LOCAL_GENERATED_SOURCES += $(GEN)      

...

LOCAL_SRC_FILES := \                
    $(IDL_TGT_CODE) \             
    ...

如果接口定义文件有多个,比如可能有a.xml, b.xml等等,那可能下面方法更方便一些。

GENERATOR := $(HOST_OUT_EXECUTABLES)/generator$(HOST_EXECUTABLE_SUFFIX) 
IDL_DIR := ./     
IDL_TGT_CODE := $(IDL_DIR)/xxx_C.c                                                                
                                                                                          
define transform-generated-A                                        
@echo "target Generated $@ <= $<"                                                         
$(GENERATOR) A < $< > $@                                                
endef                                                                                     
                                                                                          
define transform-generated-B                                        
@echo "target Generated $@ <= $<"                                                         
$(GENERATOR) B < $< > $@                                                
endef                                                                                     
                                                                                          
define transform-generated-C                                               
@echo "target Generated $@ <= $<"                                                         
$(GENERATOR) C < $< > $@                                                         
endef                                                                                     
                                                                                          
$(LOCAL_PATH)/$(IDL_DIR)/%_A.h : $(LOCAL_PATH)/$(IDL_DIR)/%.xml         
    $(transform-generated-A)                                        
                                                                                          
$(LOCAL_PATH)/$(IDL_DIR)/%_B.h : $(LOCAL_PATH)/$(IDL_DIR)/%.xml         
    $(transform-generated-B)                                        
                                                                                          
$(LOCAL_PATH)/$(IDL_DIR)/%-C.c : $(LOCAL_PATH)/$(IDL_DIR)/%.xml                
    $(transform-generated-C)                                               

...

LOCAL_SRC_FILES := \                
    $(IDL_TGT_CODE) \             
    ...

LOCAL_ADDITIONAL_DEPENDENCIES += $(LOCAL_PATH)/xxx_A.h  $(LOCAL_PATH)/xxx_B.h   

另外如果想编译出来的image中某几个特定binary带symbol,可以在Android.mk中加:

LOCAL_STRIP_MODULE := false

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。