Erlang Central

Difference between revisions of "Cross compiling"

From ErlangCentral Wiki

m
(One intermediate revision by one user not shown)
Line 11:Line 11:
  
 
In this howto, the term ''target'' means the system you want to run Erlang on when you're finished. The term ''build platform'' means the system you're using to compile the Erlang runtime system.
 
In this howto, the term ''target'' means the system you want to run Erlang on when you're finished. The term ''build platform'' means the system you're using to compile the Erlang runtime system.
 +
 +
== From R13B04, Cross Compiling is Easier ==
 +
 +
As of R13B04 (released February 2010), there's documentation and support for cross-compiling Erlang directly in the OTP source distribution. If you want to cross-compile R13B04 or later, stop reading now. This note is about earlier
 +
versions.
  
 
== Prerequisites ==
 
== Prerequisites ==
Line 38:Line 43:
  
 
<pre>
 
<pre>
--- configure.in.orig  2006-03-17 17:38:39.000000000 0100
+
--- configure.in.orig  2006-03-17 17:38:39.000000000 +0100
    configure.in        2006-03-17 17:29:23.000000000 0100
+
+++ configure.in        2006-03-17 17:29:23.000000000 +0100
@@ -1322,7 1322,7 @@
+
@@ -1322,7 +1322,7 @@
 
   exit(0);
 
   exit(0);
 
  #endif
 
  #endif
 
  }
 
  }
 
-], poll_works=true, poll_works=false, poll_works=false)
 
-], poll_works=true, poll_works=false, poll_works=false)
], poll_works=true, poll_works=false, poll_works=true)
+
+], poll_works=true, poll_works=false, poll_works=true)
 
  case $poll_works in
 
  case $poll_works in
 
  true)
 
  true)
 
         AC_MSG_RESULT(ok)
 
         AC_MSG_RESULT(ok)
@@ -1365,7 1365,12 @@
+
@@ -1365,7 +1365,12 @@
 
     DED_CFLAGS="$DED_CFLAGS -fPIC"
 
     DED_CFLAGS="$DED_CFLAGS -fPIC"
 
  fi
 
  fi
 
   
 
   
 
-DED_LD=ld
 
-DED_LD=ld
if test "x$LD" = x; then
+
+if test "x$LD" = x; then
  DED_LD=ld
+
+ DED_LD=ld
else
+
+else
  DED_LD=$LD
+
+ DED_LD=$LD
fi
+
+fi
+
+
 
  DED_LD_FLAG_RUNTIME_LIBRARY_PATH="-R"
 
  DED_LD_FLAG_RUNTIME_LIBRARY_PATH="-R"
 
  STATIC_CFLAGS=""
 
  STATIC_CFLAGS=""
Line 95:Line 100:
 
     export PATH=$PATH:/usr/local/eldk31/usr/bin/
 
     export PATH=$PATH:/usr/local/eldk31/usr/bin/
 
     export CC=mips-linux-gcc
 
     export CC=mips-linux-gcc
     export CXX=mips-linux-g
+
     export CXX=mips-linux-g++
 
     export SHLIB_LD=mips-linux-gcc
 
     export SHLIB_LD=mips-linux-gcc
 
     export LD=mips-linux-ld
 
     export LD=mips-linux-ld
Line 137:Line 142:
 
== Slim the system ==
 
== Slim the system ==
  
Disk space is often limited on embedded systems. By deleting parts of OTP you don't use, stripping binaries and recompiling all .beams without debug information and with the compressed flag, it's  
+
Disk space is often limited on embedded systems. By deleting parts of OTP you don't use, stripping binaries and recompiling all .beams without debug information and with the +compressed flag, it's  
 
possible to get the entire Erlang system down to less than 3MB.
 
possible to get the entire Erlang system down to less than 3MB.
  

Revision as of 07:26, 1 March 2010

Contents

Author

Matthias Lang

Why Cross Compile

Cross compiling means using one type of system to compile the Erlang runtime system for a different type of system. A typical example would be using an x86-based linux system to compile Erlang for an embedded MIPS CPU.

You usually cross compile when you want to run Erlang on a system which doesn't have its own development environment, i.e. no C compiler, libraries and make system.

In this howto, the term target means the system you want to run Erlang on when you're finished. The term build platform means the system you're using to compile the Erlang runtime system.

From R13B04, Cross Compiling is Easier

As of R13B04 (released February 2010), there's documentation and support for cross-compiling Erlang directly in the OTP source distribution. If you want to cross-compile R13B04 or later, stop reading now. This note is about earlier versions.

Prerequisites

  • A working C compiler and C libraries. Verify that it generates object files which run on the target, for instance by writing hello world. This howto assumes that your build platform is a unix or unix-like system.
  • The autoconf tools.
  • The Erlang/OTP source code, from erlang.org. The examples assume R10B-10.
  • Some cross-compiling experience. If you've never cross-compiled before, it's probably worth starting with something a bit simpler than Erlang, just to get a feeling for how it's meant to work. One suggestion: busybox.
  • Experience with native compiling Erlang. If you haven't built the Erlang runtime from source before, do that first, to get a feeling for how it's meant to work.
  • A generous serving of gumption.

Unpack the source

Make a directory for the cross compiling work, say /usr/local/src/mips_erlang/ and untar the source distribution there.

Patch the configure script

A couple of small changes to the configure script supplied with the Erlang source makes cross compiling much easier. Take this patch:

--- configure.in.orig   2006-03-17 17:38:39.000000000 +0100
+++ configure.in        2006-03-17 17:29:23.000000000 +0100
@@ -1322,7 +1322,7 @@
   exit(0);
 #endif
 }
-], poll_works=true, poll_works=false, poll_works=false)
+], poll_works=true, poll_works=false, poll_works=true)
 case $poll_works in
 true)
        AC_MSG_RESULT(ok)
@@ -1365,7 +1365,12 @@
     DED_CFLAGS="$DED_CFLAGS -fPIC"
 fi
 
-DED_LD=ld
+if test "x$LD" = x; then
+ DED_LD=ld
+else
+ DED_LD=$LD
+fi
+
 DED_LD_FLAG_RUNTIME_LIBRARY_PATH="-R"
 STATIC_CFLAGS=""

and apply it in the erts/ directory in the source tree you just unpacked.

After applying the patch, you need to re-run autoconf to update the configure script:

cd erts
autoconf configure.in > configure

Set environment variables for non-detectable features

In a native compile, the 'configure' script autodetects many settings by compiling and running small C programs. When cross compiling, this isn't possible, so you need to do some manual setup to override the autodetection:

export ac_cv_prog_javac_ver_1_2=no 
export ac_cv_c_bigendian=yes
export ac_cv_func_setvbuf_reversed=no
export ac_cv_func_mmap_fixed_mapped=yes
export ac_cv_sizeof_long_long=8 
export ac_cv_sizeof_off_t=8

The values of the variables above are correct for an AU1000 MIPS CPU running linux 2.4.x. They're probably not correct for your particular system. You need to manually figure out the right settings, i.e. is your system bigendian? How large is a long_long on your system? If you're not sure, write a short C program to find out, and run the C program on your target.

Set environment variables to set up the C compiler

Next, we need to set up the environment so that we use the right C compiler and related tools:

    export PATH=$PATH:/usr/local/eldk31/usr/bin/
    export CC=mips-linux-gcc
    export CXX=mips-linux-g++
    export SHLIB_LD=mips-linux-gcc
    export LD=mips-linux-ld
    export AR=mips-linux-ar
    export RANLIB=mips-linux-ranlib
    export CROSS_COMPILE=mips_4KC-
    export CFLAGS=-Os

The above settings are correct for an Au1000 MIPS on linux target. Your settings are likely to be different.

Manually disable parts of HiPE

Some HiPE-related code attempts to run during the build process in spite of the --disable-hipe flag. The code fails because it expects the build host and target system to be the same.

A quick and dirty way to disable that is to manually edit erts/emulator/Makefile.in and kill HIPE_GENERATE. In addition, edit lib/Makefile and remove hipe from the OTHER_SUB_DIRECTORIES list.

Run the configure script

./configure --prefix=/tmp/cross_compiled --without-ssl --without-java --disable-hipe --host=mips-linux --build=i686-pc-linux-gnu

Assuming configure ran all the way through without failing, we're now ready to compile. At this point, it's worth spending five minutes manually browsing through 'config.h' and checking that everything looks reasonable. For instance, you can check that the endianness settings are correct for your system.

Run make

TARGET=mips-linux make noboot | cat > buildlog

The above will take a while. After it's done, you probably want to install the Erlang runtime somewhere. Become root, and then:

TARGET=mips-linux make install

Correct the bin/erl path

There's one thing left to manually fix: the installed copy of the Erlang runtime will have some incorrect paths in it. Edit 'bin/erl' by hand to fix that.

Slim the system

Disk space is often limited on embedded systems. By deleting parts of OTP you don't use, stripping binaries and recompiling all .beams without debug information and with the +compressed flag, it's possible to get the entire Erlang system down to less than 3MB.

History

The build system in Open Source Erlang includes some relatively unappetising support for cross compiling to a VxWorks target.

Matthias Lang realised that for "similar build and target systems", it was possible to run 'configure' natively on the build system, hack the resulting makefiles to take into account differences between the build and target and then compile using a cross compiler.

Brian Zhou then pointed out how to use manual overrides through environment variables to avoid much of the hacking.

The methods described in this article are used to cross-compile Erlang for Motorola MPC 860 PPC systems, Au1000 MIPS systems, NSLU2, and most optware platforms.

Reference