It all started with the Demolito chess engine. A long time ago, I asked for an update for Demolito in a forum. When I didn't get a response, I decided to do it myself. That's how I came across Android NDK. Once I had successfully compiled Demolito, I realized that I could also compile other chess engines. And that's how the story began.
If I had to calculate an average across all chess engines, I would conclude that the most important optimization parameters when compiling are „-O3 -flto -march=native“ (ideally with a profile build under Termux). This works with almost all chess engines. If the source code offers further optimizations, the corresponding DFLAGS must also be added (e.g. "-DIS_64BIT -DUSE_NEON=8 -DUSE_POPCNT", as in the case of Stockfish). This means that you have done most of your homework in terms of optimizations. Additional optimization parameters should be added with caution and, above all, should be tested for effectiveness (usually with a benchmark test). In the worst case, things can also get worse.
There are different ways to compile C and C++ based chess engines for Android. Some of them are described below. Additionally you will find short descriptions how to cross compile chess engines with Go and Rust. These are all programs that you run on the command line.
Android NDK:
With Android NDK you have three main ways to compile chess engines for Android: The make-based ndk-build, CMake and standalone toolchains. I mainly use ndk-build for compiling with Android NDK. For the compilation with ndk-build you need two files Android.mk and Application.mk which should be located in the jni directory (to see an example, see one of the attachments above). To start the compilation, you have to change to the jni directory and call the ndk-build command in the command line. If you need a template for C++ programs to be compiled, use Fruit_2.1.zip (see attachment) and modify Android.mk accordingly. For C programs use OliThink_5.10.1.zip (see attachment) as template and modify Android.mk accordingly. In very simple cases you only have to change the name of the chess engine. You can learn more about the syntax of Android.mk at https://developer.android.com/ndk/guides/android_mk.
Android NDK works on different platforms like Linux, Mac and Windows. In terms of performance, it doesn't matter which operating system you use. When cross compilation comes into play, you need Android NDK.
Android NDK can be downloaded from https://developer.android.com/ndk/downloads.
The installation is quite easy. All you have to do is unpack the archive to any directory and add the root directory to the path environment variable (ndk-build should be callable from the command line).
Clang and GCC:
Another possibility is to use the Clang or GCC compiler on Termux. With using Termux you have the benefit, that you can use profile-guided optimization (PGO). With a few exceptions, best performance can be achieved with the GCC compiler.
Termux should be installed from https://github.com/termux/termux-app.
If you are overwhelmed with installing Clang and GCC on Termux, there is a script (cecsa.sh) which can do the job for you. Originally intended for CECSA (see below), the script can also be used to install Clang and GCC only. Download the script from https://sourceforge.net/projects/chess-tools-for-android/files/cecsa.sh/download and follow the instructions at the beginning of the script.
CECSA (Chess Engine Compiler Script for Android):
For some of the chess engines I wrote a script for Termux that enables automatic compilation. The script first downloads the source code (from GitHub), changes the source code (if necessary) and then compiles the chess engine for different devices (arm64-v8.2a-dotprod, arm64-v8a, armeabi-v7a, x86 and x86_64). The most important things can be controlled via parameters at the beginning of the start script (start.sh).
Example:
How to compile Stockfish with CECSA
Go:
Documentation for Go can be found at https://go.dev. For compiling with Go I use Linux as operating system, more precisely Debian GNU/Linux. The following example is also based on this.
Example:
How to cross compile Combusken
Rust:
Documentation for Rust can be found at https://www.rust-lang.org. For compiling with Rust I use Linux as operating system, more precisely Debian GNU/Linux. The following example is also based on this.