Compiling Arasan for Android

Discussion of chess software programming and technical issues.

Moderator: Ras

Peperoni
Posts: 72
Joined: Sun Nov 01, 2020 5:27 pm
Full name: Richard Porti

Compiling Arasan for Android

Post by Peperoni »

Hello,

I am trying to compile Arasan for Android but I have not succeeded so far, it is not clear how to set the TARGET in the makefile provided by Jon.
I have seen a "NEON" option but it seems it is used only for NNUE.

Any idea anyone?
Archimedes
Posts: 167
Joined: Tue Mar 05, 2019 3:43 pm
Full name: Archimedes

Re: Compiling Arasan for Android

Post by Archimedes »

Under which host operating system do you want to compile Arasan? Linux or Windows? In this case you will definitely need Android NDK. Termux? In this case you can choose between Android NDK, Clang and GCC as compiler (whether GCC has advantages over Clang, I haven't tested for Arasan yet). So far I compile Arasan under Windows with Android NDK (which is also using the Clang compiler).
Peperoni
Posts: 72
Joined: Sun Nov 01, 2020 5:27 pm
Full name: Richard Porti

Re: Compiling Arasan for Android

Post by Peperoni »

Archimedes wrote: Sun Mar 26, 2023 11:41 am Under which host operating system do you want to compile Arasan? Linux or Windows? In this case you will definitely need Android NDK. Termux? In this case you can choose between Android NDK, Clang and GCC as compiler (whether GCC has advantages over Clang, I haven't tested for Arasan yet). So far I compile Arasan under Windows with Android NDK (which is also using the Clang compiler).
Hello Archimedes,
I am trying to crosscompile on Windows using CMake in Qt (with all paths NDK, SDK) set, it compiles fine but it doesn't run on Android.
Do you have a public MakeFile somewhere that I could have a look?
I am probably doing something wrong.
Archimedes
Posts: 167
Joined: Tue Mar 05, 2019 3:43 pm
Full name: Archimedes

Re: Compiling Arasan for Android

Post by Archimedes »

Arasan is not the easiest of all exercises.

I use the Makefile based ndk-build of Android NDK for this. For this you need two files: Android.mk and Application.mk.

Application.mk:

Code: Select all

APP_ABI := 
APP_PLATFORM := android-21
APP_STL := c++_static
Android.mk:

Code: Select all

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := Arasan
LOCAL_C_INCLUDES := ../src/nnue ../src/syzygy/src
LOCAL_SRC_FILES := $(wildcard ../src/*.cpp) ../src/syzygy/src/tbprobe.c
LOCAL_CFLAGS += -std=c99 -DNDEBUG -O3 -flto -fexperimental-new-pass-manager -Wfatal-errors -Wall -Wextra -DNNUE -DSMP -DSMP_STATS -DSYZYGY_TBS -DARASAN_VERSION="230326"
LOCAL_CPPFLAGS += -std=c++17
LOCAL_CPP_FEATURES := exceptions

ifeq ($(TARGET_ARCH_ABI), arm64-v8a)
  LOCAL_CFLAGS += -D_64BIT -DNEON -DUSE_POPCNT
else
  ifeq ($(TARGET_ARCH_ABI), armeabi-v7a)
    LOCAL_CFLAGS += -DNEON
  else
    ifeq ($(TARGET_ARCH_ABI), x86)
      LOCAL_CFLAGS += -mssse3 -mfpmath=sse -m32 -DSIMD -DSSE2 -DUSE_ASM -DUSE_INTRINSICS
    else
      ifeq ($(TARGET_ARCH_ABI), x86_64)
        LOCAL_CFLAGS += -msse4.2 -mpopcnt -m64 -D_64BIT -DSIMD -DSSE2 -DSSE41 -DUSE_ASM -DUSE_INTRINSICS -DUSE_POPCNT
      endif
    endif
  endif
endif

LOCAL_LDFLAGS += 
include $(BUILD_EXECUTABLE)
Since I like to work with so-called wildcards, the following unneeded files must be deleted before compilation:

Code: Select all

topo.cpp
tune.cpp
tuner.cpp
Futhermore you have to add "#include "globals.h" in bitbase.cpp and delete the line „#include "bitbase.cpp“ in globals.cpp.

After that Arasan can be compiled with Android NDK (change to the jni directory and execute the ndk-build command).

Example with all necessary files:
https://pixeldrain.com/u/RHrWugiw
Archimedes
Posts: 167
Joined: Tue Mar 05, 2019 3:43 pm
Full name: Archimedes

Re: Compiling Arasan for Android

Post by Archimedes »

If I want to compile chess engines like Arasan under Termux with Clang or GCC (with the possibility to optimize with PGO), I would always use a separate Makefile, like I use it for other chess engines (e.g. Stockfish) in my CECSA script.

A corresponding Makefile for Arasan would look like this (especially for arm64-v8a):

Code: Select all

MODULE = Arasan
C_INCLUDES = -Innue -Isyzygy/src
SRC_FILES = *.cpp syzygy/src/tbprobe.c

CFLAGS += -std=c++17 -O3 -flto -mcpu=native -fexceptions
DFLAGS += -DNDEBUG -D_64BIT -DNEON -DUSE_POPCNT -DNNUE -DSMP -DSMP_STATS -DSYZYGY_TBS -DARASAN_VERSION="230326"
WFLAGS += -Wfatal-errors -Wall -Wextra
LFLAGS += -static-libstdc++

BENCH = bench >/dev/null

ifeq ($(COMP),$(filter $(COMP),gcc g++))
	override COMP = g++
	CFLAGS += -flto-partition=one -fno-gcse
	DFLAGS += -D__BIONIC_VERSIONER
	WFLAGS += 
	LFLAGS += 
else
	override COMP = clang++
	CFLAGS += -fexperimental-new-pass-manager --target=aarch64-linux-android21
	DFLAGS += 
	WFLAGS += 
	LFLAGS += 
endif

build:
	$(COMP) $(CFLAGS) $(C_INCLUDES) $(DFLAGS) $(WFLAGS) $(SRC_FILES) $(LFLAGS) -o $(MODULE)
	strip $(MODULE)

profile-build:
ifeq ($(COMP),$(filter $(COMP),gcc g++))
	$(COMP) $(CFLAGS) $(C_INCLUDES) -fprofile-generate $(DFLAGS) $(WFLAGS) $(SRC_FILES) $(LFLAGS) -lgcov -o $(MODULE)
	./$(MODULE) $(BENCH)
	$(COMP) $(CFLAGS) $(C_INCLUDES) -fprofile-use -fno-peel-loops -fno-tracer $(DFLAGS) $(WFLAGS) $(SRC_FILES) $(LFLAGS) -lgcov -o $(MODULE)
else
	$(COMP) $(CFLAGS) $(C_INCLUDES) -fprofile-instr-generate $(DFLAGS) $(WFLAGS) $(SRC_FILES) $(LFLAGS) -o $(MODULE)
	./$(MODULE) $(BENCH)
	llvm-profdata merge -output=default.profdata *.profraw
	$(COMP) $(CFLAGS) $(C_INCLUDES) -fprofile-instr-use $(DFLAGS) $(WFLAGS) $(SRC_FILES) $(LFLAGS) -o $(MODULE)
endif
	strip $(MODULE)
However, the whole thing currently only works for Clang (make build COMP=clang). For GCC (make build) an error still appears during compilation.
Peperoni
Posts: 72
Joined: Sun Nov 01, 2020 5:27 pm
Full name: Richard Porti

Re: Compiling Arasan for Android

Post by Peperoni »

Wow thank you so much Archimedes, will try that this week :)
Were you also able to build it for iOS?
Archimedes
Posts: 167
Joined: Tue Mar 05, 2019 3:43 pm
Full name: Archimedes

Re: Compiling Arasan for Android

Post by Archimedes »

Peperoni wrote: Sun Mar 26, 2023 6:23 pm Were you also able to build it for iOS?
No. And I wouldn't know how to do that either. Are there actually appropriate compilers for this?

Since you can compile Arasan, but it does not run on Android, it may also be due to the missing library libstdc++. This library must be included in the executable (must be embedded, statically linked). Another possible error can also be that you are using a too large API level. For example, I pay attention to compatibility with API level 21 (Android 5.0 and higher).
Peperoni
Posts: 72
Joined: Sun Nov 01, 2020 5:27 pm
Full name: Richard Porti

Re: Compiling Arasan for Android

Post by Peperoni »

Archimedes wrote: Mon Mar 27, 2023 7:06 am
Peperoni wrote: Sun Mar 26, 2023 6:23 pm Were you also able to build it for iOS?
No. And I wouldn't know how to do that either. Are there actually appropriate compilers for this?

Since you can compile Arasan, but it does not run on Android, it may also be due to the missing library libstdc++. This library must be included in the executable (must be embedded, statically linked). Another possible error can also be that you are using a too large API level. For example, I pay attention to compatibility with API level 21 (Android 5.0 and higher).
Huum I think you are right about libstdc++ ! Thanks a lot once again. In terms of API, now Google Play forces to have a minimum of API 29 ...
I will try to compile on iOS, never done that before, I will probably try with a simpler project first :)
Archimedes
Posts: 167
Joined: Tue Mar 05, 2019 3:43 pm
Full name: Archimedes

Re: Compiling Arasan for Android

Post by Archimedes »

NEON had not worked until now. But now it does. You can download the latest version below. The zip archive also contains the latest files for Android NDK.

Arasan 230329:
https://sourceforge.net/projects/chess- ... p/download