Bundles in Mono

by Miguel de Icaza

Over the weekend I checked into the repository a tool to easily create Mono bundles. A mono bundle allows you to ship your Mono application as a single binary: no runtime necessary: everything is "statically linked", there are no external dependencies on a Mono installation of any kind.

To use, just type:

    mono$ mkbundle --deps sample.exe -o mcs
    Sources: 1 Auto-dependencies: True
       embedding: /home/cvs/mcs/mcs/mcs.exe
       embedding: /mono/lib/mono/1.0/mscorlib.dll
       embedding: /mono/lib/mono/1.0/System.dll
       embedding: /mono/lib/mono/1.0/System.Xml.dll
    as -o /tmp/tmp7aa740ad.o temp.s 
    cc -o demo -Wall temp.c `pkg-config --cflags --libs mono-static` /tmp/tmp7aa740ad.o

In the example above the resulting binary is a healty 7.5 megabyte file, but it contains the Mono C# compiler and three of the system libraries as well as the Mono VM embedded into it:

	mono$ ls -l demo 
	-rwxr-xr-x  1 miguel users 7575630 2004-12-01 02:21 demo*

I then tuned the compilation to use a few shared libraries from the system, in this particular sample, I only want to avoid taking a Mono dependency:

	mono$ mono$ ldd demo
	linux-gate.so.1 =>  (0xffffe000)
	libpthread.so.0 => /lib/tls/libpthread.so.0 (0x4002c000)
	libm.so.6 => /lib/tls/libm.so.6 (0x4003c000)
	libgmodule-2.0.so.0 => /opt/gnome/lib/libgmodule-2.0.so.0 (0x4005e000)
	libdl.so.2 => /lib/libdl.so.2 (0x40062000)
	libgthread-2.0.so.0 => /opt/gnome/lib/libgthread-2.0.so.0 (0x40066000)
	libglib-2.0.so.0 => /opt/gnome/lib/libglib-2.0.so.0 (0x4006b000)
	libc.so.6 => /lib/tls/libc.so.6 (0x409e6000)
	librt.so.1 => /lib/tls/librt.so.1 (0x40afa000)
	/lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x40000000)
	libgcc_s.so.1 => /lib/libgcc_s.so.1 (0x40c68000)

This is just a productification of a feature that Paolo had developed for Mono.

The next step is to write an assembly "linker", so that only the functions necessary to run mcs.exe are included as opposed to the whole three assemblies.

The main problem is that we need a good library for dealing with CIL images, the contenders:

  • Reflection is slightly incomplete, but can be brute-forced to provide the information we need.
  • There is a rumor of a counter part to PEAPI from the folks that wrote Component Pascal, but so far no updates on their web site.
  • Using the Mono unmanaged C API: I rather not.
  • Perl script over the ildasm output. Paolo would be very happy ;-)
  • Use RAIL
  • Write a new library from scratch to cope with these problems.

Posted on 01 Dec 2004