如何在Android.mk中添加自动生成的代码

如何在模块中生成代码,并让生成的代码加入到当前模块编译?

在bootable/recovery/updater/Android.mk 中有这么一段

inc := $(call local-generated-sources-dir)/register.inc
$(inc) : libs := $(TARGET_RECOVERY_UPDATER_LIBS)
$(inc) :
	$(call generate-register-inc,$@,$(libs))

LOCAL_GENERATED_SOURCES := $(inc)

inc :=

$(call local-generated-sources-dir) 这是系统自动生成路径的一个宏定义,位于out/target/product/generic_arm64/obj下面,比如本例子是编译一个EXECUTABLES,名字为updater,那么inc := out/target/product/generic_arm64/gen/EXECUTABLES/updater_intermediates/register.inc。

再来看看生成上述文件的宏定义:

define generate-register-inc
    $(hide) mkdir -p $(dir $(1))
    $(hide) echo "" > $(1)
    $(hide) $(foreach lib,$(2),echo "extern void Register_$(lib)(void);" >> $(1);)
    $(hide) echo "void RegisterDeviceExtensions() {" >> $(1)
    $(hide) $(foreach lib,$(2),echo "  Register_$(lib)();" >> $(1);)
    $(hide) echo "}" >> $(1)
endef

明显这里是生成一个头文件。将生成的文件加入到模块编译就是使用:LOCAL_GENERATED_SOURCES := $(inc) 这一句。

注意:在使用时,这些代码需要放在LOCAL_PATH与LOCAL_MODULE之后,因为里面的local-generated-sources-dir会用到这两个变量。

Android官方推荐的方法

Using Custom Tools

If you have a tool that generates source files for you, it's possible to have the build system get the dependencies correct for it. Here are a couple of examples. $@ is the make built-in variable for "the current target." The red parts are the parts you'll need to change.

You need to put this after you have declared LOCAL_PATH and LOCAL_MODULE, because the $(local-generated-sources-dir) and $(local-host-generated-sources-dir) macros use these variables to determine where to put the files.

Example 1

Here, there is one generated file, called chartables.c, which doesn't depend on anything. And is built by the tool built to $(HOST_OUT_EXECUTABLES)/dftables. Note on the second to last line that a dependency is created on the tool.

intermediates:= $(local-generated-sources-dir)
GEN := $(intermediates)/chartables.c
$(GEN): PRIVATE_CUSTOM_TOOL = $(HOST_OUT_EXECUTABLES)/dftables $@
$(GEN): $(HOST_OUT_EXECUTABLES)/dftables
	$(transform-generated-source)
LOCAL_GENERATED_SOURCES += $(GEN)

Example 2

Here as a hypothetical example, we use use cat as if it were to transform a file. Pretend that it does something useful. Note how we use a target-specific variable called PRIVATE_INPUT_FILE to store the name of the input file.

intermediates:= $(local-generated-sources-dir)
GEN := $(intermediates)/file.c
$(GEN): PRIVATE_INPUT_FILE := $(LOCAL_PATH)/input.file
$(GEN): PRIVATE_CUSTOM_TOOL = cat $(PRIVATE_INPUT_FILE) > $@
$(GEN): $(LOCAL_PATH)/input.file
	$(transform-generated-source)
LOCAL_GENERATED_SOURCES += $(GEN)

Example 3

If you have several files that are all similar in name, and use the same tool, you can combine them. (here the *.lut.h files are the generated ones, and the *.cpp files are the input files)

intermediates:= $(local-generated-sources-dir)
GEN := $(addprefix $(intermediates)/kjs/, \
            array_object.lut.h \
            bool_object.lut.h \
        )
$(GEN): PRIVATE_CUSTOM_TOOL = perl libs/WebKitLib/WebKit/JavaScriptCore/kjs/create_hash_table $< -i > $@
$(GEN): $(intermediates)/%.lut.h : $(LOCAL_PATH)/%.cpp
	$(transform-generated-source)
LOCAL_GENERATED_SOURCES += $(GEN)
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当使用 Android NDK(Native Development Kit)开发安卓应用时,Android.mk 文件是一个重要的构建配置文件,用于描述和控制 C/C++ 源代码的编译和构建过程。它是使用 GNU Make 构建系统的一部分,用于构建 Native 库或可执行文件。 Android.mk 文件的作用主要包括以下几个方面: 1. 定义模块:Android.mk 文件用于定义一个或多个模块,每个模块对应一个 Native 库或可执行文件的构建过程。你可以在文件指定模块的名称、类型(例如静态库、共享库、可执行文件等)以及所需的源文件和依赖库。 2. 指定源文件:通过 Android.mk 文件,你可以列出构建所需的源文件,包括 C/C++ 源代码文件、头文件等。你可以使用通配符来指定源文件的匹配模式,也可以手动列出每个源文件。 3. 配置编译选项:Android.mk 文件允许你配置编译选项,如编译器标志、链接选项等。你可以指定编译器的标准、优化级别、预定义宏等,并可以添加额外的编译选项以满足特定需求。 4. 管理依赖关系:Android.mk 文件允许你指定当前模块所依赖的其他模块或库。通过声明这些依赖关系,构建系统可以自动解析和处理模块之间的依赖关系,并确保正确的构建顺序。 5. 控制构建过程:Android.mk 文件的规则和命令可以用于控制构建过程。你可以定义编译、链接以及其他自定义的构建规则,并指定执行这些规则的命令。这样,构建系统会根据 Android.mk 文件的规则来执行相应的操作,从而生成目标文件。 总而言之,Android.mk 文件是用于描述和控制 C/C++ 源代码的编译和构建过程的配置文件。通过 Android.mk 文件,你可以定义模块、指定源文件、配置编译选项、管理依赖关系,并控制构建过程的规则和命令。这使得使用 NDK 构建 Native 库或可执行文件变得更加灵活和可配置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值