Building and running embedded Linux .NET applications from first principles
Overview
This walk-through has the aim of taking you from a clean system through to including Mono in a build image using the meta-mono layer, then building and packaging an example .NET project for inclusion in that image.
You may already have Yocto installed and just be looking to work with Mono for the first time, in which case you can jump forward to the section you find most relevant,
such as building an example package on the host to test or adding the meta-mono layer to the Yocto build system.
The following assumptions are made. You are:
- familiar with basic Linux admin tasks
- aware of the Yocto Project Reference Manual here.
- using Ubuntu 14.04.4 LTS as your host build system
- working with Yocto 2.0 (Jethro) release
Obtain the required packages for your host system to support Yocto
First we will install the required host packages for Ubuntu as detailed in the quickstart, i.e.
$ sudo apt-get install gawk wget git-core diffstat unzip texinfo gcc-multilib build-essential chrpath socat libsdl1.2-dev xterm nano
Full details of system requirements and installation can be found in the Yocto Quickstart here
In addition, if you wish to build the example on the host, outside Yocto, you will need autoconf installed
$ sudo apt-get install autoconf
Install Mono
Also install Mono on your host system as we'll use it to build and run some examples for test later
$ sudo apt-get install mono-complete $ mono --version
With Ubuntu 14.04.4 LTS this will install Mono version 3.2.8 which is now quite old
If you wish to install a newer build of Mono to your host system you can follow the instructions here.
$ sudo add-apt-repository ppa:directhex/monoxide $ sudo apt-get update $ sudo apt-get install mono-complete $ mono --version
Currently this will also install Mono 3.2.8 but may in future provide newer builds.
If you wish to use the absolute latest Mono then there are instructions you can follow to build a release tarball here and from git here. Be aware this may not be straightforward and that there can be issues, such as with missing files, if you follow this process.
Download and extract the Yocto 2.0 release
At the time of writing, the current release of Yocto (2.0) can be found here
$ cd ~ $ mkdir yocto $ cd yocto $ wget http://downloads.yoctoproject.org/releases/yocto/yocto-2.0/poky-jethro-14.0.0.tar.bz2 $ tar xjvf poky-jethro-14.0.0.tar.bz2
This will get you the Yocto 2.0 base meta-data and the bitbake tool. You can also add in extra layers, usually of the form "meta-foo" to provide machine support and additional functionality.
Configure the build environment to build an emulator image
$ cd ~/yocto/poky-jethro-14.0.0 $ source oe-init-build-env build_qemux86
This will create a build tree in "build_qemux86" although you could use a different name if you so wish with no adverse effects.
It is entirely possible to have many build trees in parallel in different folders and to switch between them using oe-init-build-env
.
oe-init-build-env
will create a default configuration file in conf/local/conf
which will build an emulator image suitable for execution with qemu
Build a baseline image
After configuring the environment you will be left in the build_qemux86 folder.
You should then build a baseline image, which will take some time (numbers of hours)
$ bitbake core-image-minimal
Build an example project on the host for testing (optional)
Building with autotools
The most straightforward way to compile non-.NET projects for different targets within Yocto is to make use of autotools
Projects which support autotools
provide a set of template files which are then used by the autotools
to generate Makefiles
and associated configuration files which are appropriate to build for the target environment.
Similarly it is possible to compile Mono/.NET projects using autotools
A very basic example 'Hello World' style project called mono-helloworld
has been committed to GitHub here
If you take a look at the two source files helloworld.cs and helloworldform.cs you can see the first outputs a 'Hello World' message to the console, and the second creates a Windows Form titled 'Hello World'.
Discussion of autotools
template configuration for Mono is outside the scope of this guide, but the mono-helloworld
project is based on the mono-skel
example which can be found in the Autotools section of the Mono Application Deployment guidelines here
The project itself builds two .NET executables, helloworld
and helloworldform
respectively, the first of which is a console application and the second of which is a simple Windows Forms application.
To build the project on the host independently of Yocto first clone the example repository
$ mkdir ~/host $ cd ~/host $ git clone https://github.com/DynamicDevices/mono-helloworld
Then run the autotools, configure the build, and make the project
$ cd mono-helloworld $ ./autogen.sh $ ./configure --enable-winformdemo $ make
Following a successful compilation you will have a number of new files in the root of the build folder.
There are two new .NET executables src/helloworld.exe
and src/helloworldform.exe
.
You can run the first with
$ mono src/helloworld.exe
It will output
HelloWorld
You can run the second with
$ mono src/helloworldform.exe
Depending on your host environment (e.g. using SSH) you may need to explicitly set the DISPLAY
variable for this to work, with
$ export DISPLAY=:0 $ mono src/helloworldform.exe
This will give you a basic Windows Forms window title
So you have now shown that you can successfully fetch configure and build the project on the host.
Next we will look at how Yocto automates the this process of fetching, configuring and building, then also installs and packages the output files.
Building with xbuild
Many individuals develop with Visual Studio, Mono Develop, Xamarin Studio or other similar integrated development environments (IDEs).
Mono provides xbuild
which is the Mono implementation of Microsoft's msbuild
, discussed here.
In essence this enables a developer to create a solution of projects within their IDE of choice, then use xbuild to build within the Mono environment.
A useful workflow to follow may be to develop locally with an IDE of choice, commit to a git repository upon release, then use a Yocto recipe to build and package that release into an existing image, or for provision to a package feed for update to existing targets in the field.
The mono-helloworld
project discussed above also provides a solution and project files to support build with xbuild
, or indeed with an IDE such as Visual Studio.
If you have already built the examples using autotools
remove the folder and start again.
$ cd ~/host $ rm -Rf mono-helloworld
Check out the mono-helloworld project again
$ git clone https://github.com/DynamicDevices/mono-helloworld
Run xbuild. (As you might guess from the name of the .sln file you could clone this example project to a Windows host and open it up with Visual Studio, and in fact that is how it was created)
$ xbuild /p:Configuration=Debug mono-helloworld_vs2010.sln
This results in a number of new files, including two new Mono/.NET executables in bin/Debug
helloworld.exe
and helloworldform.exe
You can run the first with
$ mono bin/Debug/helloworld.exe
It will output
HelloWorld
You can run the second with
$ mono bin/Debug/helloworldform.exe
Depending on your host environment (e.g. using SSH) you may need to explicitly set the DISPLAY
variable for this to work, with
$ export DISPLAY=:0 $ mono bin/Debug/helloworldform.exe
This will give you a basic Windows Forms window title
So you have now shown that you can successfully fetch configure and build the project on the host.
Next we will look at how Yocto automates the this process of fetching, configuring and building, then also installs and packages the output files.
Adding the meta-mono
layer to the Yocto build system
A preferred method for adding recipes to the build environment, and the method shown with this guide, is to place them within a new layer.
Layers isolate particular sets of build meta-data based on machine, functionality or similar, and help to keep the environment clean.
The meta-mono
layer contains Mono specific recipes to support execution of .NET applications on target boards. The layer can be found here.
To use a new layer such as this you first clone the layer from its git repository and then add the layer to your bitbake
configuration by editing conf/bblayers.conf
$ cd ~/yocto/poky-daisy-11.0.0 $ git clone git://git.yoctoproject.org/meta-mono $ cd ~/yocto/poky-daisy-11.0.0/build_qemux86 $ nano conf/bblayers.conf
Your bblayers.conf
should look similar to this
# LAYER_CONF_VERSION is increased each time build/conf/bblayers.conf # changes incompatibly LCONF_VERSION = "6" BBPATH = "${TOPDIR}" BBFILES ?= "" BBLAYERS ?= " \ /home/user/yocto/poky-daisy-11.0.0/meta \ /home/user/yocto/poky-daisy-11.0.0/meta-yocto \ /home/user/yocto/poky-daisy-11.0.0/meta-yocto-bsp \ " BBLAYERS_NON_REMOVABLE ?= " \ /home/user/yocto/poky-daisy-11.0.0/meta \ /home/user/yocto/poky-daisy-11.0.0/meta-yocto \ "
Make the new layer visible to bitbake
by adding a line to BBLAYERS
BBLAYERS ?= " \ /home/user/yocto/poky-daisy-11.0.0/meta \ /home/user/yocto/poky-daisy-11.0.0/meta-yocto \ /home/user/yocto/poky-daisy-11.0.0/meta-yocto-bsp \ /home/user/yocto/poky-daisy-11.0.0/meta-mono \ "
Now bitbake
can see the recipes in the new layer.
You will also see when bitbake
runs and shows the Build Configuration that the repository branch and hash of your layer is shown which is useful to know, particularly when comparing notes with others as to why a build fails, e.g.
Build Configuration: BB_VERSION = "1.22.0" BUILD_SYS = "i686-linux" NATIVELSBSTRING = "Ubuntu-12.04" TARGET_SYS = "i586-poky-linux" MACHINE = "qemux86" DISTRO = "poky" DISTRO_VERSION = "1.6" TUNE_FEATURES = "m32 i586" TARGET_FPU = "" meta meta-yocto meta-yocto-bsp = "<unknown>:<unknown>" meta-mono = "master:88c6d5f1961d58b3ec203ff19594f954c3e49cd9"
Build an image including Mono/.NET support
The meta-mono
layer includes a recipe to build an image core-image-mono
based on the Yocto standard image core-image-sato
To build this image
$ bitbake core-image-mono
This may take a while, even if you have already built core-image-minimal
as additional GUI support packages need to be built.
The core-image-mono
recipe can be found here and pulls in an include file from here.
You can see in the include file that extra packages are added to the standard core-image-sato
image.
IMAGE_INSTALL += "mono mono-helloworld"
This is how you would add Mono support to your image within a recipe, or within a .bbappend file. In fact it should only be necessary to add the mono
package as it is not necessary to have the examples unless you wish to for testing purposes.
The mono-helloworld
recipe included here shows how to build the example project using autotools
. For details see the recipe itself here, and more importantly the include file it pulls in here.
You could choose to replace mono-helloworld
with mono-helloworld-xbuild
which as the name suggests shows how to build the eaxmple project with xbuild
.
Testing the .NET executable on an emulated target
Having built core-image-mono
you can then run it up under qemu
To run up the image, simply use
$ runqemu qemux86
This will boot the emulator, load up the image, you'll see a kernel loading and then a basic user interface.
If you find that your keymap is incorrect you might wish to set this explicitly, for example
$ runqemu qemux86 qemuparams='-k en-gb'
or
$ runqemu qemux86 qemuparams='-k en-us'
Open up a terminal window using the appropriate icon, Log into the emulator as 'root', no password and run the examples.
You can run the first with
$ mono helloworld.exe
Or alternatively the recipe installs a script to wrap use of Mono, so you can use the form
$ helloworld
This will output
HelloWorld
You can run the second with
$ mono /usr/lib/helloworldform.exe
or
$ helloworldform
Depending on your host environment (e.g. using SSH) you may need to explicitly set the DISPLAY
variable for this to work, with
$ export DISPLAY=:0 $ mono /usr/lib/helloworld/helloworldform.exe
This will show a test Windows Forms form titled 'Hello World'
Lastly you can run a test GTK# application with
You can run the second with
$ mono /usr/lib/helloworldgtk.exe
or
$ helloworldgtk
Breakdown of an autotools recipe
This is the contents of the mono-helloworld_1.1.bb recipe here
require mono-helloworld.inc SRC_URI[md5sum] = "79b0ba0044689789a54e3d55ec400fc0" SRC_URI[sha256sum] = "56388435f29ce94007155acc39593c900b6d3248a7f281e83ed2101a6da455f0"
It can be seen that we provide a couple of checksums which relate to the release tarball that will be downloaded
Similarly the is the included mono-helloworld.inc file can be found here
SUMMARY = "Mono Hello World" DESCRIPTION = "Test applications for Mono console and windows forms" AUTHOR = "Alex J Lennon <ajlennon@dynamicdevices.co.uk>" HOMEPAGE = "http://www.dynamicdevices.co.uk" SECTION = "mono/applications" PRIORITY = "optional" LICENSE = "GPLv3" LIC_FILES_CHKSUM = "file://LICENSE;md5=783b7e40cdfb4a1344d15b1f7081af66" DEPENDS = "mono" SRC_URI = "https://github.com/DynamicDevices/mono-helloworld/archive/v${PV}.tar.gz" inherit autotools FILES_${PN} = "${libdir}/helloworld/helloworld.exe \ ${bindir}/helloworld \ ${libdir}/helloworld/helloworldform.exe \ ${bindir}/helloworldform \ "
For more details on check-sums, licenses and so forth, see Building your own recipes from first principles and the Recipe & Style Patch Guide.
We have a dependency on the mono
package, and again we inherit the autotools
class to make use of the bitbake
autotools
functionality.
Lastly we override FILES_${PN}
which controls the installed files which are added to the main output package. ${libdir}
${bindir}
are standard GNU variable naming conventions for installation paths. For details see here and here.
In this case we have made sure that the helloworld
executable goes to /usr/lib/helloworld/helloworld.exe
as does the helloworldform.exe
.
It might seem quite strange to be installing the executable assemblies to the /usr/lib
location, but this is in line with Mono application deployment recommendations here.
We then install wrapper scripts to /usr/bin
which can be called directly to run the respective examples. These scripts take the form
#!/bin/sh exec @MONO@ @prefix@/lib/helloworld/@APP@.exe $MONO_EXTRA_ARGS "$@"
Breakdown of an xbuild recipe
The xbuild
recipe is similar to the autotools
recipe above, excepting that we override a couple of methods to ensure xbuild
runs as we wish it too
This recipe can be found here.
First we include the definitions in the include file, and set the version specific checksums for the archive to be retrieved
require mono-helloworld.inc SRC_URI[md5sum] = "79b0ba0044689789a54e3d55ec400fc0" SRC_URI[sha256sum] = "56388435f29ce94007155acc39593c900b6d3248a7f281e83ed2101a6da455f0"
Then we set our source directory which must be correct for bitbake
to find extracted files from the retrieved archive
REALPN = "mono-helloworld" S = "${WORKDIR}/${REALPN}-${PV}"
Now we override the compilation method to call xbuild
to build a particular .NET configuration against the a .SLN file in the archive
CONFIGURATION = "Debug" do_compile() { xbuild /p:Configuration=${CONFIGURATION} ${REALPN}_vs2010.sln }
Next we modify the installation method to make sure that the correct output files are installed and the executable scripts are modified to run the output assemblies
do_install() { install -d "${D}${bindir}" install -d "${D}${libdir}/helloworld/.debug" install -m 0755 ${S}/bin/${CONFIGURATION}/*.mdb ${D}${libdir}/helloworld/.debug install -m 0755 ${S}/bin/${CONFIGURATION}/*.exe ${D}${libdir}/helloworld install -m 0755 ${S}/script.in ${D}${bindir}/helloworld sed -i "s|@MONO@|mono|g" ${D}${bindir}/helloworld sed -i "s|@prefix@|/usr|g" ${D}${bindir}/helloworld sed -i "s|@APP@|helloworld|g" ${D}${bindir}/helloworld install -m 0755 ${S}/script.in ${D}${bindir}/helloworldform sed -i "s|@MONO@|mono|g" ${D}${bindir}/helloworldform sed -i "s|@prefix@|/usr|g" ${D}${bindir}/helloworldform sed -i "s|@APP@|helloworld|g" ${D}${bindir}/helloworldform }
Lastly we make sure that the .MDB debug files which are output are packaged correctly in the -dbg
package. The other assemblies in ${libdir}
and ${bindir}
will be packaged correctly in the main output package by default
FILES_${PN}-dbg += "${libdir}/helloworld/.debug/*"
For more details on check-sums, licenses and so forth, see Building your own recipes from first principles and the Recipe & Style Patch Guide.
Feedback
This is a living document. Please feel free to send comments, questions, corrections to Alex Lennon here