Search code examples
androidcmakefileandroid-ndkandroid.mk

How to dynamically get the current compiler target file name in Android.mk's LOCAL_CFLAGS?


I am currently trying to build a native module using Android's NDK. My project consists of several source files (e.g.: FILENAME.c) and for each of them I need to declare a define in their CFLAGS (-DOPERATION_FILENAME).

In order to do that I need to dynamically fetch the name of the current target file of the Android NDK's cross-compiler and use it to for the define value.

I could not find any information about how to do this and the Makefile way (CFLAGS += -DOPERATION_echo $* | sed 's/_$$//') does not apply/work here.

My current Android.mk looks like this:

LOCAL_PATH:=$(call my-dir)

include $(CLEAR_VARS)

LOCAL_ARM_MODE := arm

LOCAL_MODULE := libmpn

LOCAL_SRC_FILES := \
<cut>

LOCAL_CFLAGS := \
-std=gnu99 \
-DHAVE_CONFIG_H \
-D__GMP_WITHIN_GMP \
-O2 \
-pedantic \
-fomit-frame-pointer \
-mfloat-abi=softfp \
-DOPERATION_`echo $* | sed 's/_$$//'`

include $(BUILD_SHARED_LIBRARY)

Does anyone know of a working way to get the file name of the current cross-compiler target in Android.mk? Thanks!


Solution

  • In the beginning of your Android.mk, add the line that redefines get-src-file-target-cflags, like here:

    get-src-file-target-cflags = $(LOCAL_SRC_FILES_TARGET_CFLAGS.$1) -DOPERATION_$(basename $1)
    
    LOCAL_PATH := $(call my-dir)
    
    include $(CLEAR_VARS)
    LOCAL_SRC_FILES := t.cpp qq.c
    LOCAL_MODULE := tm
    LOCAL_LDLIBS := -latomic
    
    include $(BUILD_SHARED_LIBRARY)
    

    You can put this line in Application.mk, if you choose.