In Android NDK, I build JNI files generated automatically by SWIG. callmanager_wrap.cpp is part of a shared library:
LOCAL_SRC_FILES += callmanager_wrap.cpp
include $(BUILD_SHARED_LIBRARY)
But I would like to append/edit callmanager_wrap.cpp
before compiling. To be more explicit:
cat jnistuff.txt >> callmanager_wrap.cpp
Content I need to add is known in advance but callmanager_wrap.cpp is not. It is generated by SWIG. Ultimately, my custom rule will have to run following command to generate callmanager_wrap.cpp:
swig -c++ -java -package com.package.my -o callmanager_wrap.cpp callmanager.i
According to this post, it is not possible to add custom rules to Android.mk
. But in Android sources, I believe there are some Android.mk
handling steps after BUILT or INSTALLED. I tried the following:
MY_JNI_WRAP=callmanager_wrap.cpp
include $(CLEAR_VARS)
LOCAL_SRC_FILES += callmanager_wrap.cpp
LOCAL_INTERMEDIATE_TARGETS += myjni
myjni:
echo "in myjni target"
swig -c++ -java -package com.package.my -o $(MY_JNI_WRAP) callmanager.i
cat jnistuff.txt >> $(MY_JNI_WRAP)
include $(BUILD_SHARED_LIBRARY)
But myjni
target is never called.
I would suggest the following:
include $(CLEAR_VARS)
LOCAL_SRC_FILES += callmanager_wrap.cpp
MY_JNI_WRAP := $(LOCAL_PATH)/callmanager_wrap.cpp
$(MY_JNI_WRAP):
echo "in myjni target"
swig -c++ -java -package com.package.my -o $(MY_JNI_WRAP) callmanager.i
cat jnistuff.txt >> $(MY_JNI_WRAP)
.PHONY: $(MY_JNI_WRAP)
include $(BUILD_SHARED_LIBRARY)
That's it.
I probably owe you some explanations. So here we go:
$(LOCAL_SRC_FILES)
is a list of file names relative to $(LOCAL_PATH)
, so to address a file from outside the standard NDK actions, you need the full path for your file, which is $(LOCAL_PATH)/callmanager_wrap.cpp
.
We specify the file as .PHONY
to guarantee that the custom action is executed every time you run ndk-build. But if you know which are actual dependencies of callmanager_wrap.cpp
, you can specify them instead, like
$(MY_JNI_WRAP): callmanager.i jnistuff.txt $(LOCAL_PATH)/../src/com/package/my/Something.java
In this case, you will not need the .PHONY
line.
If you want your source directory to remain clean, you can declare the wrapper file as .INTERMEDIATE
like this:
.INTERMEDIATE: $(MY_JNI_WRAP)
Now make will delete the wrapper file after build, if it did not exist before the build.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With