An overlay build system to easily mass cross compile autoconf based projects for Android using the NDK for any target arch and API level.
You supply it with path to an Android NDK. Any NDK version can be used (or atleast as far back as r10e atleast). You place projects you want to build in the projects directory. You modify/add any archs and API levels you want to build for by modifying/adding arch source files in the archs directory. You create a custom build file in the root of each project to be built that contains the relevant configure, make and make install commands. Additional custom scripts can placed in the post_build_scripts to extract the required binaries/libraries from the install directory after all builds are complete. Then simply run the android_ndk_cross_compile_build_automator.sh script and it will automatically build the toolchains for each arch source file and API level set and then call the build file for each project after exporting the relevant compiler, bintools, target arch, API level and some other variables. The build file can update any additional project specific flags like CFLAGS, CXXFLAGS, LDFLAGS or any other flags or parameters and can call the relevant configure, make and make install commands to build the project. After builds of all projects of all arch source files are complete, then post build scripts can be run to process the built files.
-
Install dependencies: The following instructions are for Ubuntu. Will differ on other platforms. This project requires bash.
sudo apt updateIf you want to build autoconf based projects:
sudo apt install autoconf automake libtool -
Download Latest
Android NDKfrom here or an older version from here. Extract it to a directory. Default is$HOME/Android/ndk/. -
Download a
Android NDK Cross Compile Build Automatorrelease from here. Extract it in any directory. -
The directory structure by default should be something like this:
- android_ndk_cross_compile_build_automator
- config/
- archs/
- ARCH_SRC files...
- install/
- out/
- post_build_scripts/
- projects/
- tools/
- templates/
- android_ndk_cross_compile_build.sh
- android_ndk_cross_compile_build_automator.sh
-
Open the
android_ndk_cross_compile_build_automator.shfile and read theUSER MODIFIABLE VARIABLESsection. It defines important variables and what they all do. They are too long to write here. -
Open the config/archs directory and look over the arch source files. This project refers to the archs directory as
ARCHS_DIRand any files in it asARCH_SRC. EachARCH_SRCis sourced to define variables that contain information on which toolchain to build and to set any general compiler/linker flags needed to build for thatARCH_SRC. This project already has the followingARCH_SRCfiles that can be used as templates:armeabi-android4.0.4- armeabi armeabi-v7a arm64-v8a x86 x86-64. Thearmeabi-android4.0.4-is a specialARCH_SRCadded which is the same as armeabi other thanAPI_LEVELand can be used at runtime by projects to build separate binaries/libraries with and withoutPIE/PICflags or to build for lower API levels than armeabi. Check theandroid_ndk_cross_compile_build.shtemplate files for more info. The following variables are defined by default in the existingARCH_SRCfiles:ARCHdefines the arch for which the toolchain should be built to build projects for that ARCH. The archs that are supported are:arm armv7a arm64 x86 x86_64 mips mips64 mips32r6.armv7ais specially added to fix some edge cases and does not exist as an arch in the Android NDK, but if it is specified, the toolchain built will be the same asarm.API_LEVELis the minimum API level you want to target for which toolchains should be built for. This is the same asminSdkVersionorANDROID_PLATFORM.CFLAGSdefines the target arch specific c compiler flags passed toclang/gccwhile building every project.CXXFLAGSdefines the target arch specific c++ compiler flags passed toclang++/g++while building every project.LDFLAGSdefines the target arch specific linker flags passed toldwhile building every project. Set the variable values according to your needs.
-
Place any projects you want to build in the projects directory. This project refers to the projects directory as
PROJECTS_DIRand any directories in it asPROJECT. -
Now copy the
templates/android_ndk_cross_compile_build.shto the root of each project directory. This project refers to this build file asBUILD_FILE. The directory structure should look like this:
- android_ndk_cross_compile_build_automator
- ...
- projects/
- project1
- android_ndk_cross_compile_build.sh
- source files of the project1...
- project2
- android_ndk_cross_compile_build.sh
- source files of the project2...
-
Now modify
android_ndk_cross_compile_build.shof each project according to your requirements. Mainly the CPPFLAGS, CFLAGS, CXXFLAGS, LDFLAGS and command options to configure would need to be changed depending on each project. Read theandroid_ndk_cross_compile_build.sh, there is enough info in it to make appropriate changes. -
Now optionally place any post build scripts you want to run after all the builds are complete in the post_build_scripts directory. Post build scripts can be used to extract the needed binaries and executables from the install directory. This project refers to the post_build_scripts directory as
POST_BUILD_SCRIPTS_DIRand any files in it asPOST_BUILD_SCRIPT. The directory structure should look like this:
- android_ndk_cross_compile_build_automator
- ...
- post_build_scripts/
- post_build_script1.sh
- post_build_script2.sh
-
Then open
android_ndk_cross_compile_build_automator.sh. -
Then modify the value of
ARCHS_SRC_TO_BUILDand specify theARCH_SRCfiles in theARCHS_DIRthat you want to build the projects for. -
Then modify the value of
PROJECTS_TO_BUILDand specify thePROJECTs in thePROJECTS_DIRthat you want to build. -
Then modify the value of
POST_BUILD_SCRIPTS_TO_RUNand specify thePOST_BUILD_SCRIPTs in thePOST_BUILD_SCRIPTS_DIRthat you want to run after builds are complete. -
Then modify
ANDROID_NDK_ROOT,TOOLCHAIN_DIR_PREFIXand other variables that need changing. -
Finally run the command
bash android_ndk_cross_compile_build_automator.shto start the build process. -
The
android_ndk_cross_compile_build_automator.shwill process allARCH_SRCs files set inARCHS_SRC_TO_BUILDone by one. It will fist create a toolchain for theARCH_SRCbeing processed if it does not exist or it may use a prebuilt one, then run allBUILD_FILEs of all projects set inPROJECTS_TO_BUILDone by one. Then move to the nextARCH_SRCand build toolchain and projects for it and so on and so forth. Then it will run allPOST_BUILD_SCRIPTs files set inPOST_BUILD_SCRIPTS_TO_RUN. -
Any parameters passed to
android_ndk_cross_compile_build_automator.shwill also be passed to allBUILD_FILEs of all projects and all post build scripts. Moreover any CPPFLAGS, CFLAGS, CXXFLAGS, LDFLAGS that were set before this command is run will also be exported toBUILD_FILEs of all projects along with the flags set by theARCH_SRCfile being processed. -
Depending on you make commands the install directory should contain the files built. This project refers to the install directory as
INSTALL_DIR. If you are using the same DESTDIR for the make install command as in the template then installed files will be at$INSTALL_DIR/$PROJECT/$ARCH_SRCfor each PROJECT and ARCH_SRC.
Currently tested by building fusermount for android on Ubuntu 16.04 x86_64 with NDK r15c and r20. You can check it out here.
Credits:
libiconv
Daniel Pocock
jorgenpt