Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run custom rule in Android.mk before compilation?

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.

  1. What is LOCAL_INTERMEDIATE_TARGETS used for?
  2. Can I possibly achieve what I want to do here without writing an external script or makefile?
like image 444
m-ric Avatar asked Sep 11 '12 14:09

m-ric


1 Answers

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:

  1. $(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.

  2. 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.

  3. 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.

like image 136
Alex Cohn Avatar answered Sep 28 '22 09:09

Alex Cohn