With GitHub Pull Request #5772 merged, proper support for the Android operating system is coming to the next version of Nim.
Yes, Android is very similar to linux, but in some cases it's doing its own thing. That's why we introduced an own OS symbol for Android. That means that you can use the --os:android option to cross-compile for android. For example to compile koch:
nim compile --os:android koch
This also means that you can use the following to create code paths specific for android:
when defined(android):
echo "Hello from Android"
you can see this being used in the standard library, for example in the implementation of getTempDir on GitHub.
However, cross-compiling for android will only work, if you have an android cross-compiler (for example from the Android NDK) and as with all cross-compilers you'll have to set that up with the correct settings in your own nim.cfg
Alternatively, you can also install an app called termux on your android phone (or emulator) which is a terminal for Android and provides apt-get like packages. In there you'll have to run the following command to install the Android-Termux prerequisites for compiling Nim from source:
apt update
apt install libandroid-glob-dev libandroid-glob git clang nodejs pcre pcre-dev libsqlite --yes
Araq says on IRC that he'll update the csources repository tonight, so you'll be able to compile Nim from source in the same way as you would do on any other OS. Please note though that Android is a little bit annoying in that it does not report itself as Android to build.sh. Because of that replace ./build.sh with sh build.sh --os android to build nim from the csources repository.
This will create the nim Android shell executable nim in the bin folder just as you'd expect. And running nim --version should print the Nim version number. It should also specify Android as the operating system and, depending on your phone CPU architecture, print either arm, arm64 or i386.
Special thanks to stisa for doing the initial work in identifying where we needed to modify the standard library to get the very first nim build on Android.
Thanks also to TiberiumN for helping me test run nim on 64-bit ARM. :)
I see that building csources on termux now works:
pkg install libandroid-glob-dev libandroid-glob git clang nodejs pcre pcre-dev libsqlite
git clone https://github.com/nim-lang/Nim
cd Nim
git clone --depth 1 https://github.com/nim-lang/csources
cd csources
sh build.sh --os android
cd ..
However, when I try to run
bin/nim compile --os:android koch
I get an error that /bin/sh isn't found. I edited lib/pure/osproc.nim as suggested here but I still get the same error.
Hi,
I had the same problem, this worked for me
pkg update
pkg install libandroid-glob-dev libandroid-glob git clang nodejs pcre pcre-dev libsqlite
git clone https://github.com/nim-lang/Nim
cd Nim
git clone --depth 1 https://github.com/nim-lang/csources
find ./ -type f -exec sed -i 's|/bin/sh|sh|g' {} \;
cd csources
sh build.sh --os android
cd ..
echo 'passL=" -landroid-glob "' >> config/nim.cfg
bin/nim compile --os:android koch
Thanks to @hawk_king I get get koch to compile. However, bootstrapping nim fails:
./koch boot -d:release
gives me an error:
CC: compiler_nim
Error: invocation of external compiler program failed. File exists
Yes true, please try this:
pkg update
pkg install libandroid-glob-dev libandroid-glob git clang nodejs pcre pcre-dev libsqlite
git clone https://github.com/nim-lang/Nim
cd Nim
git clone --depth 1 https://github.com/nim-lang/csources
cd csources
find ./ -type f -exec sed -i 's|/bin/sh|sh|g' {} \;
sh build.sh --os android
cd ..
bin/nim compile --os:android --define:termux koch
./koch boot -d:release --define:termux
Nim Compiler Version 0.17.3 [Android: arm64]
Copyright (c) 2006-2017 by Andreas Rumpf
git hash: 14468e2336b889e188b9485e47636d92b19697b6
active boot switches: -d:release
Thanks @hawk_king ; this worked with the addition of the extra line in your earlier post
echo 'passL=" -landroid-glob "' >> config/nim.cfg
I tried compiling. Everything worked until :
sh build.sh --os android crtbegin.c:(.text+0x3a): undefined reference to main clang-7: error: linker command failed with exit code 1 (use -v to see invocation)