LuaJIT 2.0 with devkitARM (Nintendo DS)

tung's picture

I just compiled LuaJIT with devkitARM, a toolchain for making homebrew Nintendo DS ROMs. The steps are:

  1. Have devkitARM set up.
  2. Get the LuaJIT source code.
  3. Tweak src/Makefile.
  4. Build LuaJIT.
  5. Copy over the LuaJIT headers and library.
  6. Use LuaJIT.

The following notes are really hacky; follow them at your own risk.

Have devkitARM

Install the devkitARM toolchain from the devkitPro website.

Get the LuaJIT source code

Grab it from LuaJIT's downloads page. I cloned the git repo, but the tarballs should work too.

Tweak src/Makefile

There are two changes to be made.

First, change BUILDMODE from mixed to static. This creates just a static library, since the DS doesn't have dynamic linking.

Then uncomment XCFLAGS+= -DLUAJIT_USE_SYSMALLOC. If you don't change this, the build will fail at lj_alloc.c when trying to include sys/mman.h, which devkitARM doesn't have for whatever reason.

Build LuaJIT

If you set up devkitARM correctly, you'll have DEVKITPRO and DEVKITARM defined, so the following command will build the static library:

make HOST_CC="gcc" \ CC="arm-eabi-gcc" \ AR="arm-eabi-ar" \ AS="arm-eabi-as" \ LD="arm-eabi-gcc" \ PATH="$DEVKITARM/bin:$DEVKITPRO/portlibs/arm/bin:$PATH" \ TARGET_CFLAGS="-g -Wall -O2 -march=armv5te -mtune=arm946e-s \ -fomit-frame-pointer -ffast-math -mthumb -mthumb-interwork \ -I$DEVKITPRO/libnds/include -DARM9" \ TARGET=arm

This will end with an error about not finding libdl, but it creates src/libluajit.a which is what we want.

Copy over the LuaJIT headers and library

cp src/lua.h src/lauxlib.h src/lualib.h src/luaconf.h \ $DEVKITPRO/libnds/include/ cp src/libluajit.a $DEVKITPRO/libnds/lib/

Use LuaJIT

The LuaJIT API is binary-compatible with stock Lua, so Part IV of Programming in Lua covers that.

To compile with LuaJIT, include -lluajit and -lm in the LIBS line of the stock libnds makefile.