码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2015-05-17 18:46:08      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:

编译时经常有的需求是有些.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

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

标签:

原文地址:http://blog.csdn.net/jinzhuojun/article/details/45787905

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!