Compiling Perl for Android

Hey guys!
This tutorial will compile and install Perl on an Android device (or emulator, as per your choosing)
Credits to PerlTricks for the method! :)


Prerequisites
  • (Non emulator) Rooted Android device
  • Any Unix-Like OS that can run the Android NDK tools
  • Perl v5.20.0 or above: Link
  • Android SDK (command-line tools): Link
  • Android NDK: Link
First, download the SDK, NDK and the Perl Source from the given links and extract them to a convenient directory. Next, update the PATH variable to include the SDK tools by entering the following in the terminal (adjust as necessary):
export PATH=$PATH:<ANDROID_SDK_EXTRACTED_LOCATION>/tools
export PATH= \
    $PATH:<ANDROID_SDK_EXTRACTED_LOCATION>/platform-tools
Then, set up the build environment by issuing the following commands in the terminal (adjust as necessary):
export ANDROID_NDK=<ANDROID_NDK_EXTRACTED_LOCATION>
export TARGET_ARCH=arm-linux-androideabi
export ANDROID_TOOLCHAIN=\

    /tmp/toolchain-arm-linux-androideabi
export SYSROOT=$ANDROID_TOOLCHAIN/sysroot
export PATH= \

    $PATH:$ANDROID_NDK/toolchains/$TARGET_ARCH-4.8/prebuilt/linux-x86_64/bin
Connect your device (or launch your emulator) and find the device ID by typing:
adb devices
The sequence of numbers and letters (or the emulator-****) is the device ID. Keep a note of it.

Restart adbd on the device to make it run as root:
adb root
Create the toolchain for building Perl using:
$ANDROID_NDK/build/tools/make-standalone-toolchain.sh \
--platform=android-9 --install-dir=$ANDROID_TOOLCHAIN \
--system=`uname | tr '[A-Z]' '[a-z]'`-x86_64 \
--toolchain=arm-linux-androideabi-4.8
Now, create a folder on the device to keep the Perl build (If you want to install Perl to a different directory, replace /data/perl with something else):
adb -s <DEVICE_ID> shell mkdir /data/perl
Finally, compile Perl by issuing (If you chose to install to a different directory, change /data/perl to the directory that you have chosen):
cd <PERL_EXTRACTED_SOURCE>
./Configure -des \
-Dusedevel \
-Dusecrosscompile \
-Dtargetrun=adb \
-Dcc=arm-linux-androideabi-gcc \
-Dsysroot=$SYSROOT \
-Dtargetdir=/data/perl \
-Dtargethost=<DEVICE_ID>
make -j<NUMBER_OF_CPU_CORES>
Now this is where things get counter intuitive. Trying to run "make install" will result in failure. Instead, the built files are installed in the "make test" task. So, install the files by running:
make test -j<NUMBER_OF_CPU_CORES>
If you don't want to run the full test suite, press Ctrl+C after it finishes copying all the built files (You can identify when to stop by looking at the command that is currently running. If it ends with ./TEST, it has copied all files)

You can try the Perl installation by opening a terminal emulator (or adb shell) and typing (as root)(If you changed the install location, use that instead of /data/perl):
/data/perl/perl -v
If it prints out the Perl version, it has been installed properly. :)

Notes

  • You can create a folder in /data called bin to keep a symlink to perl (and any other executables) and add /data/bin to the PATH
  • You might have understood by now, but when running perl scripts, use "perl <script_name>" instead of "./<script_name>"
  • If you try to run any perl script that uses standard modules, it will fail because the standard modules are not loaded by default. to load them, run the script using (assuming perl has been installed in /data/perl and has been added to the PATH,): "perl -I/data/perl/lib <script_name>". You can make an alias for perl with the above flag to save some typing.
  • Scripts referring to modules not in the standard library will not work unless the module is cross compiled
Cheers!
-Technohacker

Comments

Popular Posts