The Mer Wiki now uses your Mer user account and password (create account on https://bugs.merproject.org/)


Platform SDK and SB2

From Mer Wiki
Revision as of 11:30, 23 May 2014 by Mike7b4 (Talk | contribs)

Jump to: navigation, search

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 in a tutorial-like manner.

Contents

Prerequisites

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: No target specified and none set as default, 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.

Setup a new target for sb2

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.

You can either make your own or (eventually) download a target.

Make your own target

NOTE: this needs mer-core >= 0.20130124.1, check that with sdk-version cmd.

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

Also make sure that /sbin is included in your PATH environment variable.

Now we need a kickstart file. We will generate this with mer-kickstarter

cd /tmp
sudo zypper install mer-kickstarter mer-kickstarter-configs
mer-kickstarter -c /usr/share/kickstarter-configs/mer-reference-images/00reference.yaml -e /usr/share/kickstarter-configs/mer-reference-images/

You now have a large number of kickstart files - we're primarily interested in the mer-target-<arch> ones.

The .ks files created contain what is known as a 'token'; in this case it is the text @MER_RELEASE@. When running mic the --tokenmap option is used to provide a value.

After the kickstart file is created the image build can be started

sudo mic create fs mer-target-armv7hl.ks -o /parentroot/srv/mer/targets --pkgmgr=zypp --arch=armv7hl --tokenmap=MER_RELEASE:latest

Next we need 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/mer-target-armv7hl/*

Finally some commands (notably rpmbuild) need to know about your username/userid mapping so we'll add that. Make absolutely sure you are at the top of the sdkroot and don't use a leading / :

cd /parentroot/srv/mer/targets/mer-target-armv7hl/
grep :$(id -u): /etc/passwd >> etc/passwd
grep :$(id -g): /etc/group >> etc/group

This target could be any suitable rootfs such as a Nemo or PA rootfs and there are some alternate targets presented lower down the page.

Initialize Target in SB2

To tell SB2 about the target we use sb2-init which gives the target a name - in this case "mer-target-armv7hl".

IMPORTANT : You must change directory to the target rootfs before running sb2-init.

cd /parentroot/srv/mer/targets/mer-target-armv7hl
sb2-init -d -L "--sysroot=/" -C "--sysroot=/" -c /usr/bin/qemu-arm-dynamic -m sdk-build -n -N -t / mer-target-armv7hl /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 (note lack of leading /):

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-target-armv7hl -m sdk-install -R rpm --rebuilddb
sb2 -t mer-target-armv7hl -m sdk-install -R zypper ref --force

The target is now ready to be used by sb2.

If zypper fails with "Cant open /var/run/zypp.pid in mode w" you can fix it with:

cd /srv/mer/targets/<target_name>/var
rm run && mkdir run

Compile the simple C program

Again run:

sb2 -t mer-target-armv7hl 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-target-armv7hl
sb2 -t mer-target-armv7hl -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-target-armv7hl : 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

osc build --chroot-only 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://releases.nemomobile.org/snapshots/ (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:

sudo 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 .

note: if you're doing this as the root user and not with sudo, remember to replace $USER with your regular username.

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

Note: If you installed the mer-target-armv7hl target earlier, that will be your default target. If you, however, like the nemo-n950 to be your default target, add the -d switch to the command line above.

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 (note lack of leading /):

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.

The compiler should now work but you are still missing libraries to compile against. Install what you need into the n950 rootfs. For example:

sb2 -t nemo-n950 -m sdk-install -R ssu ur
sb2 -t nemo-n950 -m sdk-install -R zypper ref -f
sb2 -t nemo-n950 -m sdk-install -R zypper dup
sb2 -t nemo-n950 -m sdk-install -R zypper install gcc-c++ 

Whenever you see message about dbus connection failed on sdk installs, just do 'i' for ignore. For example:

Installation of openssh-5.6p1-1.1.84 failed:
 (with --nodeps --force) Error: Subprocess failed. Error: RPM failed: Failed to get D-Bus connection: No connection to service manager.

If zypper fails with "Cant open /var/run/zypp.pid in mode w" you can fix it with:

cd /srv/mer/targets/n950rootfs/var
rm run && mkdir run

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 -t nemo-n950 ./hello 
Hello

More typically you'll use

 sb2 -t nemo-n950 make

Example of building one of Nemo's RPM packages

Here's an example of building RPM of QmlCalc:

git clone https://github.com/nemomobile/qmlcalc.git
cd qmlcalc/
mb2 build -t nemo-n950 rpm/qmlcalc.spec

Troubleshooting

If

.../armv7hl-meego-linux-gnueabi/bits/os_defines.h:40:22: fatal error: features.h: No such file or directory

then

sb2 -t mer-target-armv7hl -m sdk-install -R zypper in glibc-devel

If

/var/tmp/rpm-tmp. .... : line 44: /usr/lib/rpm/meego/find-docs.sh: No such file or directory

then

sb2 -t mer-target-armv7hl -m sdk-install -R zypper in meego-rpm-config

If magic file issues:

error: magic_load failed: File 5.4 supports only version 7 magic files. `/usr/share/misc/magic.mgc' is version 10

then

# Save the target magic.mgc file (just in case) and use the one from the MerSDK
mv /parentroot/srv/mer/targets/nemo-i486-vm-wayland_SB2/usr/share/misc/magic.mgc /parentroot/srv/mer/targets/nemo-i486-vm-wayland_SB2/usr/share/misc/magic.mgc.orig
cp /usr/share/misc/magic.mgc /parentroot/srv/mer/targets/nemo-i486-vm-wayland_SB2/usr/share/misc/magic.mgc

Plasma Active

Plasma Active can run on various devices, such as Nexus 7, the Flying Squirrel (in previous iteration - Vivaldi) and so on.

Note that Vivaldi tablet is already obsolete, and Plasma Active project is shifting to other reference platform.

In the future there might be smaller rootfs releases, targeted specifically to be used with SDK and containing the minimized set of development tools.

Setup target

Important Note: review potential issues in the mer-target-armv7hl target example. They apply to setting up other targets as well.

Extract the PlasmaActive rootfs as a target and set file permissions (in this example we are taking a Nexus 7 tablet, where PA uses armv7hl build. For other targets and potential armv7l versions, adjust the instructions below accordingly:

target_device='nexus7'
arch='armv7hl'
rootfs='/path/to/plasma-active-nexus7-testing-mer-latest-2012-12-20.tar.bz2'
sudo mkdir -p "/srv/mer/targets/plasmaactive-${target_device}-rootfs"
cd /srv/mer/targets/plasmaactive-${target_device}-rootfs
sudo tar xjvf $rootfs
sudo chown -R $USER .

Initialize scratchbox2

cd /srv/mer/targets/plasmaactive-${target_device}-rootfs
sb2-init -L "--sysroot=/" -C "--sysroot=/" -c /usr/bin/qemu-arm-dynamic -m sdk-build -n -N -t / plasmaactive-${target_device} /opt/cross/bin/${arch}-meego-linux-gnueabi-gcc

Install some tools

sb2 -t plasmaactive-${target_device} -m sdk-install -R zypper refresh
sb2 -t plasmaactive-${target_device} -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 -t plasmaactive-${target_device} g++ hello.cpp -o hello

Run:

 sb2 -t plasmaactive-${target_device} ./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

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 -l
To set up a default target:
sb2-config -d <target>

Removing targets

When targets are created the information about target is put to ~/.scratchbox2/ directory, so in order to remove target one can just remove directories from there:

rm -r ~/.scratchbox2/<target_name>
Personal tools