Adding a compiler flag to only one file in Android.mk

4.1k views Asked by At

I have an Android.mk file that has a number of files for which LOCAL_CFLAGS get applied to them. I would like to apply a different flag to only one of the files out of the many. How can this be accomplished?

I searched the internet from the Android perspective, but didn't find a whole lot. Considering the following example I would like to apply flag TEST3 to file test3.c only. I looked at Per-file CPPFLAGS in Android.mk, but I couldn't find anything as far as how to use PRIVATE_CPPFLAGS to one file. Any ideas?

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE := test
LOCAL_SRC_FILES := test1.c test2.c test3.c
LOCAL_CFLAGS := -DTEST1_2_AND_3

include $(BUILD_SHARED_LIBRARY)
1

There are 1 answers

1
Alex Cohn On

The supported way to achieve your goal is to use a separate static library for C/CPP files that need different parameters. In this particular case, the fix would be as easy as

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)  

LOCAL_MODULE := test3
LOCAL_SRC_FILES := test3.c
LOCAL_CFLAGS := -DTEST1_2_AND_3 -DTEST3
include $(BUILD_STATIC_LIBRARY)

include $(CLEAR_VARS)  

LOCAL_MODULE := test
LOCAL_SRC_FILES := test1.c test2.c
LOCAL_CFLAGS := -DTEST1_2_AND_3
LOCAL_WHOLE_STATIC_LIBRARIES := test3

include $(BUILD_SHARED_LIBRARY)

There is another approach, similar to one I forged a while ago

LOCAL_PATH := $(call my-dir)

TARGET-process-src-files-tags += $(call add-src-files-target-cflags, $(LOCAL_TEST3_SRC_FILES), $(LOCAL_TEST3_CFLAGS))

include $(CLEAR_VARS)  

LOCAL_MODULE := test
LOCAL_SRC_FILES := test1.c test2.c test3.c
LOCAL_CFLAGS := -DTEST1_2_AND_3

LOCAL_TEST3_SRC_FILES := test3.c
LOCAL_TEST3_CFLAGS := -DTEST3

include $(BUILD_SHARED_LIBRARY)

If -Dtest3 would do, you can use another hack:

LOCAL_PATH := $(call my-dir)

get-src-file-target-cflags = $(LOCAL_SRC_FILES_TARGET_CFLAGS.$1) -D$(basename $1)_DEFINE

include $(CLEAR_VARS)  

LOCAL_MODULE := test
LOCAL_SRC_FILES := test1.c test2.c test3.c
LOCAL_CFLAGS := -DTEST1_2_AND_3

include $(BUILD_SHARED_LIBRARY)

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