The Mer Wiki now uses your Mer user account and password (create account on https://bugs.merproject.org/)
Platform SDK and SB2
The most straightforward way to use Scratchbox2 is within the Mer Platform SDK. This page will go through the steps of using SB2 to compile a simple program.
Contents |
Prerequisites
- A properly setup SDK
- Scratchbox2 pre-installed (see SB2 Installation if not)
- Your home directory is in /home/<username> (otherwise see directory mappings)
- A working osc
Create a simple C program
We'll use ~/src/hello.c as the location of our code.
mkdir ~/src cat <<EOF >~/src/hello.c #include <stdio.h> int main(int argc, char **argv) { printf("Hello World\n"); return 0; } EOF
And now to compile it:
sb2 gcc ~/src/hello.c -o ~/src/hello
This gets:
sb2: Error: Invalid target specified, aborting.
When sb2 runs it needs to know where to get header files and libraries and what architecture and cross-compiler to use. This is called a 'target'. SB2 can be configured to run against multiple named targets.
SB2 Setup
Target Preparation
A target is a rootfs that contains development files like headers, libraries and possibly programs. Notably it does *not* need to contain compilers, linkers etc.
Still running in the sdk we'll create a target in the recommended /srv/mer/targets/ area.
sudo mkdir -p /parentroot/srv/mer/targets sudo chown -R $USER /parentroot/srv/mer/targets
Now we need a kickstart file and then we can use mic to create the target
cd /tmp curl -O http://releases.merproject.org/releases/0.20120315.1/kickstarts/mer-core-armv7hl-xorg-basic-qmlviewer.ks sudo mic create fs mer-core-armv7hl-xorg-basic-qmlviewer.ks -o /parentroot/srv/mer/targets --pkgmgr=yum --arch armv7hl
(Note: if you use a kickstart with @BUILD_ID@ and use the --release= option to mic then mic creates a slightly different structure and your rootfs will end up in /parentroot/srv/mer/targets/<release>/images/<name> and you'll have to move it)
The final step is to make sure the rootfs is all owned by the user. SB2 uses fakeroot when needed and won't work if the target has root-owned files.
sudo chown -R $USER /parentroot/srv/mer/targets/*
This target could be any suitable rootfs such as a Nemo or PA rootfs.
Target Setup
To tell SB2 about the target we use sb2-init which gives the target a name - in this case "mer-core".
IMPORTANT : You must change directory to the target rootfs before running sb2-init.
cd /parentroot/srv/mer/targets/mer-core-armv7hl-xorg-basic-qmlviewer sb2-init -d -L "--sysroot=/" -C "--sysroot=/" -c /usr/bin/qemu-arm-dynamic -m sdk-build -n -N -t / mer-core /opt/cross/bin/armv7hl-meego-linux-gnueabi-gcc
Notes
- some messages about "cannot find 'ld'" and "no gcc" are expected.
- running sb2-init again with the same name will just change the values for that name - make sure each target is named uniquely.
We now need to work around bug #238:
echo -n "armv7hl-meego-linux" > etc/rpm/platform
Note that this may not affect all targets and will need an appropriate value for the target architecture until the bug is resolved.
And prepare the target for installation (all these steps will eventually be handled by a target preparation script or similar):
sb2 -t mer-core -m sdk-install -R rpm --rebuilddb sb2 -t mer-core -m sdk-install -R zypper ref --force
The target is now ready to be used by sb2.
Compile the simple C program
Again run:
sb2 gcc ~/src/hello.c -o ~/src/hello
This time:
~/src/hello.c:1:19: fatal error: stdio.h: No such file or directory
This is because the target has no development header files in it.
We need to be at the target root (this is bug #239) and then we can install some basic header files: Note the important use of the "-m sdk-install" and the "-R". The -m tells sb2 to use a mapping mode that permit the updating of the target and -R tells sb2 to run as fake root.
cd /parentroot/srv/mer/targets/mer-core-armv7hl-xorg-basic-qmlviewer sb2 -m sdk-install -R zypper in glibc-devel
You'll need to do this for any build requirements of your code.
SB2 Detailed setup information
The sb2-init options mean:
-d : make this the default target -L : linker options -C : compiler options -c : which qemu to use -m : which mapping to use by default -n : don't build libtool -N : don't do localisation for the target -t : the location of the build tools mer-core : The name of the target /opt/cross/bin/armv7hl-meego-linux-gnueabi-gcc : the cross-compiler to use
Alternate targets
Creating an SB2 Target for any (non-x86) Mer OBS package (draft)
osc provides the ability to do a "local build" where it creates a chroot within the SDK suitable for building a package.
When SB2 is enabled on the OBS (as it is in the MeeGo public OBS) then it also creates a suitable SB2 'target'.
So checkout the package from OBS, start a local build, setup the SB2 target:
osc co PROJECT PACKAGE cd PROJECT/PACKAGE
determine which repository/architecture to build for
osc repos
Then run a build (you can interrupt this once it's started, there is a new option to osc called --chroot-only coming soon)
osc build REPO ARCH
Note the build root from the above command or use this to find it:
osc chroot
Check the directory of the osc chroot and use the ./target/ directory in there for your SB2 target:
cd CHROOT/target sb2-init -L "--sysroot=/" -C "--sysroot=/" -c /usr/bin/qemu-arm-dynamic -m sdk-build -n -N -t / TARGETNAME /opt/cross/bin/armv7hl-meego-linux-gnueabi-gcc
(If you like you could move this to /srv/mer/targets/ before running the cd/sb2-init commands but then osc build wouldn't upgrade it anymore)
At this point you can go to your ~/src/package.git/ directory and run
sb2 -t TARGETNAME make
The advantage of this approach over a full rootfs target are that:
- it's smaller
- it uses the osc package cache so can be updated with smaller downloads
- it works with packages that are changing on the OBS (so can be run against branched projects)
- it's probably more suitable for working with other people and collaborating via the OBS
- the rootfs can be upgraded and packages added using osc (especially the -x option)
The disadvantages are
- it is currently more manual
- it only installs the development packages that are listed by the spec file as build-dependencies
- it's not as re-useable across multiple packages
N950 Nemo Mobile
Nemo/N950 rootfs can be found at: http://repository.maemo.org/meego/Nemo/ (choose the armv7hl-n950 images).
To setup cross compiling for N950 (armv7hl) do the following inside the sdk chroot
Setup target
Extract the Nemo N950 rootfs as a target and set file permissions:
mkdir -p /srv/mer/targets/n950rootfs cd /srv/mer/targets/n950rootfs sudo tar xjvf /path/to/nemo-n950-image.tar.bz2 sudo chown -R $USER .
Initialize scratchbox2
cd /srv/mer/targets/n950rootfs sb2-init -L "--sysroot=/" -C "--sysroot=/" -c /usr/bin/qemu-arm-dynamic -m sdk-build -n -N -t / nemo-n950 /opt/cross/bin/armv7hl-meego-linux-gnueabi-gcc
The compiler should now work but you are still missing libraries to compile against. Install what you need into the n950 rootfs by
sb2 -m sdk-install -R zypper ref sb2 -m sdk-install -R zypper install ... (eg qt-devel)
If the installation fails due to permission errors give your user write permissions to the places the packages are being installed and retry. After this you can compile for example a c hello world by
cd cat << "EOF" > hello.c #include <stdio.h> int main(int argc, char **argv) { printf("Hello\n"); return 0; } EOF
To run a simple compile, tell sb2 what target to use (ie the name used in the sb2-init):
sb2 -t nemo-n950 gcc hello.c -o hello
Verify that the compilation worked
$ file hello hello: ELF 32-bit LSB executable, ARM, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.25, not stripped $ sb2 ./hello Hello
More typically you'll use
sb2 -t nemo-n950 make
Vivaldi
Place here any Vivaldi specific instructions for Plasma Active or Nemo development
Plasma Active
Plasma Active rootfs can be found here (built 25 March 2012) - https://img.merproject.org/images/web/vgrade/6-20120325-173308/plasmaactive-armv7l-c71-devel-rootfs.tar.bz2
Setup target
Extract the PlasmaActive Vivaldi rootfs as a target and set file permissions:
sudo mkdir -p /srv/mer/targets/plasmaactive-vivaldi-rootfs cd /srv/mer/targets/plasmaactive-vivaldi-rootfs sudo tar xjvf /path/to/plasmaactive-armv7l-c71-devel-rootfs.tar.bz2 sudo chown -R $USER .
Initialize scratchbox2
Note usage of armv7l here (that's what's currently used for Plasma Active Vivaldi C71) in contrast to armv7hl in other examples.
cd /srv/mer/targets/plasmaactive-vivaldi-rootfs sb2-init -L "--sysroot=/" -C "--sysroot=/" -c /usr/bin/qemu-arm-dynamic -m sdk-build -n -N -t / plasmaactive-vivaldi /opt/cross/bin/armv7l-meego-linux-gnueabi-gcc
Install some tools
sb2 -m sdk-install -R zypper refresh sb2 -m sdk-install -R zypper install gcc-c++
Cross compilation
Create some source: hello.cpp:
#include <iostream> int main(int argc, char* argv[]) { std::cout << "Hello World!\n"; return 0; }
Compile it:
sb2 g++ hello.cpp -o hello
Run:
sb2 ./hello Hello World!
Check architecture:
file hello hello: ELF 32-bit LSB executable, ARM, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.25, not stripped
Nemo (Vivaldi)
Additional Notes
Installation
If your SDK did not have the SB2 tools installed then you can do:
sudo zypper ar http://releases.merproject.org/releases/latest/builds/i486/cross mer-cross-tools sudo zypper ref sudo zypper in sdk-sb2-config qemu-usermode mpc cross-armv6l-gcc cross-armv6l-binutils cross-armv7l-gcc cross-armv7l-binutils cross-mipsel-gcc cross-mipsel-binutils cross-armv7hl-gcc cross-armv7hl-binutils
Directory Mappings
SB2 uses a sophisticated file mapping system to access the right tools and the right source code. The definitions are stored in /usr/share/scratchbox2/modes/sdk-build/fs_rules.lua
The current SDK definitions recognises a few filesystems:
/home /mer /maemo /everything
Additional entries can be made if your source lives elsewhere (eg /media or /data etc).
Using multiple targets
You can use multiple targets with the SDK. Set up each target as described above.
To perform some action for a specific target add the -t option:sb2 -t <target> ...To list all set targets:
sb2-config -lTo set up a default target:
sb2-config -d <target>