Differences between revisions 9 and 40 (spanning 31 versions)
Revision 9 as of 2016-10-25 14:53:40
Size: 7622
Editor: ?JochenSprickerhof
Comment:
Revision 40 as of 2022-03-21 07:52:30
Size: 9232
Editor: ?vimer
Comment:
Deletions are marked like this. Additions are marked like this.
Line 7: Line 7:

This page documents the both manual procedures and building using sbuild. (pbuilder could be used instead but sbuild is more capable - docs for pbuilder usage are welcome).
 * There are fewer conflicts due to packages lacking `Multi-Arch: same`.

This page documents the both manual procedures and building using sbuild or pbuilder.
Line 12: Line 13:
== Nomenclature ==

It needs to be noted that tools listed here use a naming of architecture roles inherited from the GNU autotools, which some people used to nomenclature from other tools might perhaps find confusing. The difference is in perspective, either system-centric (this nomenclature) or object-centric (the other variant):
 * '''build''' means the architecture of the chroot/dpkg/compiler's executable, i.e. the architecture of the '''build''' system (called ''host'' by cmake/kernel/etc)
 * '''host''' means the architecture of produced executable objects, i.e. the architecture of the '''host''' system where these guest objects will run on (called ''target'' or sometimes ''build'' elsewhere)
 * '''target''' is what the produced executable objects will generate when producing executable objects, i.e. the architecture of the systems the built programs '''target''' their results to run on (relevant only for compilers and similar)
Line 15: Line 23:
 * an schroot chroot (to do builds in). See CrossCompiling#Install_tools below to set this up.

  
This is very simple if it works for you, as sbuild does a load of setup for you and hides the complexity. However it won't work for all packages or all suites yet. Jessie in particular needs extra setup as some piece are missing but from stretch onwards it should just work. You do need to have a chroot already setup (detailed below).
 * an schroot chroot (to do builds in). A standard chroot for native building just works for cross building since stretch.

 
Line 25: Line 31:
sbuild --host=armhf -d jessie sbuild --host=armhf -d buster
Line 30: Line 36:
sbuild --host=armhf -d jessie acl_2.2.52
}}}

=== Jessie wrinkles ===

Sbuild assumes that a package called crossbuild-essential-$arch is available which is uses to install the cross-toolchain. This is not true in jessie. You can either install a crosstoolchain in the chroot if you only build for one arch, or you can use an external repo to add the missing bits.

=== Using the cross-toolchain repo ===

This repository has pre-built cross-toolchains. crossbuild-essential meta-packages and few other updated packages to improve the crossbuilding experience (dpkg-cross, pkg-config and a few multiarch-modified)

See https://wiki.debian.org/CrossToolchains#Installation for details.

=== Building your own cross-toolchain ===

Full details are at MultiarchCrossToolchainBuild

In summary, this should build you a cross-toolchain (for armhf)
{{{
sudo dpkg --add-architecture armhf
sudo apt-get update
sudo apt-get install cross-gcc-dev
#Make sure bash (or zsh) is your default shell or add SHELL:=/bin/bash to 2nd line of /usr/share/cross-gcc/template/rules.generic
TARGET_LIST="armhf" cross-gcc-gensource 4.9
cd cross-gcc-packages-amd64/cross-gcc-4.9-armhf
sudo apt-get install debhelper gcc-4.9-source libc6-dev:armhf linux-libc-dev:armhf libgcc1:armhf binutils-arm-linux-gnueabihf bison flex libtool gdb sharutils netbase libcloog-isl-dev libmpc-dev libmpfr-dev libgmp-dev systemtap-sdt-dev autogen expect chrpath zlib1g-dev zip
dpkg-buildpackage
sbuild --host=armhf -d buster acl_2.2.52
}}}

== Building with pbuilder ==

You need to be pbuilder from stretch (or newer) to support cross-building; the feature was introduced in version 0.227.

You must use the apt "satisfydepends" resolver with pbuilder. This can be done by adding the following line to your '''~/.pbuilderrc''':

{{{
PBUILDERSATISFYDEPENDSCMD="/usr/lib/pbuilder/pbuilder-satisfydepends-apt"
}}}

If you're missing the above you might see the following error:
''E: Cross building is possible only with the APT dependency resolver''

If you don't already have a pbuilder base chroot (for native building), create one now by simply running:

{{{
pbuilder create
cowbuilder --create
}}}

Now to cross-build a package simply use the --host-arch option, eg. to build "my-package" for armhf use:

{{{
pbuilder build --host-arch armhf my-package.dsc
pdebuild -- --host-arch armhf
}}}

== Building with git-buildpackage with pbuilder ==

Similar to pbuilder part, you must use the apt "satisfydepends" resolver with pbuilder. This can be done by adding the following line to your '''~/.pbuilderrc''':

{{{
PBUILDERSATISFYDEPENDSCMD="/usr/lib/pbuilder/pbuilder-satisfydepends-apt"
}}}

If you're missing the above you might see the following error:
''E: Cross building is possible only with the APT dependency resolver''

Now to cross-build a package simply use the --git-pbuilder-options="--host-arch <ARCH>" as option, eg. to build for armhf use:

{{{
gbp buildpackage --git-pbuilder --git-pbuilder-options="--host-arch armhf"
Line 74: Line 98:
CONFIG_SITE=/etc/dpkg-cross/cross-config.<host_arch> DEB_BUILD_OPTIONS=nocheck dpkg-buildpackage -aarmhf CONFIG_SITE=/etc/dpkg-cross/cross-config.<host_arch> DEB_BUILD_OPTIONS=nocheck dpkg-buildpackage -aarmhf -Pcross,nocheck
Line 84: Line 108:
== Building manually outside of packages ==

You can look at the code for [[https://codesearch.debian.net/search?q=package:debhelper+is_cross_compiling|how debhelper does cross-compiling]].

The dpkg_architecture_value() function returns values that can be found in the output of the `dpkg-architecture` command.
Line 97: Line 126:
sudo sbuild-createchroot --make-sbuild-tarball=/srv/chroots/jessie-sbuild.tgz jessie /srv/chroots/jessie http://httpredir.debian.org/debian/
}}}
will create a base chroot, pack it up into /srv/chroots/jessie-sbuild.tgz, and add the config for it to schroot (in /etc/schroot/chroot.d)
sudo sbuild-createchroot --make-sbuild-tarball=/srv/chroots/sid-sbuild.tgz sid /srv/chroots/sid http://deb.debian.org/debian/
}}}
will create a base chroot, pack it up into /srv/chroots/sid-sbuild.tgz, and add the config for it to schroot (in /etc/schroot/chroot.d)
Line 119: Line 148:
chroot:jessie-amd64-sbuild
source:jessie-amd64-sbuild
chroot:sid-amd64-sbuild
source:sid-amd64-sbuild
Line 125: Line 154:
 1. the clean chroot chroot:jessie-amd64-sbuild, where none of your changes are saved so it's the same every time.
 2. the actual chroot tarball source:jessie-amd64-sbuild, where changes are packed up again and saved for next time.

Normally you use the clean chroot, and that's the default if you just say "jessie-amd64-sbuild" (the sbuild-shell command is an exception to this, as it implicitly uses the source: version)
 1. the clean chroot chroot:sid-amd64-sbuild, where none of your changes are saved so it's the same every time.
 2. the actual chroot tarball source:sid-amd64-sbuild, where changes are packed up again and saved for next time.

Normally you use the clean chroot, and that's the default if you just say "sid-amd64-sbuild" (the sbuild-shell command is an exception to this, as it implicitly uses the source: version)
Line 136: Line 165:
sudo sbuild-apt jessie-amd64-sbuild apt-get install zile sudo sbuild-apt sid-amd64-sbuild apt-get install zile
Line 141: Line 170:
sudo sbuild-shell jessie-amd64-sbuild
}}}
sudo sbuild-shell sid-amd64-sbuild
}}}
Line 156: Line 186:
in {{{/etc/schroot/chroot.d/jessie-amd64-sbuild-<uniqueID>}}} in {{{/etc/schroot/chroot.d/sid-amd64-sbuild-<uniqueID>}}}
Line 163: Line 193:
schroot -c jessie-amd64-sbuild schroot -c sid-amd64-sbuild
Line 181: Line 211:

== See also ==

 * [[https://enricozini.org/blog/2017/debian/qt-cross-architecture-development-in-debian/|Qt cross-architecture development in Debian]] by Enrico Zini
 * [[http://crossqa.debian.net/|Debian cross build quality assurance]]
 * [[HelmutGrohne/rebootstrap|Debian automated cross-bootstrapping]]
 * [[CrossBuildPackagingGuidelines|Guidelines for cross-build packaging]]
 * [[umlaeute/Packaging4CrossCompiling|Making your package ready for cross-compilation]]
 * [[DebianBootstrap|Debian bootstrapping]]
 * [[DebianList:debian-cross|debian-cross mailing list]]

Debian now has good support for crossbuilding packages using multiarch. This page describes how to do it.

You can build on your main system but it is recommended to use a chroot, because:

  • The build environment is clean, especially if you use a tarball/lvm snapshot
  • You don't end up with lots of build-deps and foreign architecture packages in your main system
  • There is no risk of accidentally messing up your system if something important gets swapped to the wrong architecture
  • There are fewer conflicts due to packages lacking Multi-Arch: same.

This page documents the both manual procedures and building using sbuild or pbuilder.

Nomenclature

It needs to be noted that tools listed here use a naming of architecture roles inherited from the GNU autotools, which some people used to nomenclature from other tools might perhaps find confusing. The difference is in perspective, either system-centric (this nomenclature) or object-centric (the other variant):

  • build means the architecture of the chroot/dpkg/compiler's executable, i.e. the architecture of the build system (called host by cmake/kernel/etc)

  • host means the architecture of produced executable objects, i.e. the architecture of the host system where these guest objects will run on (called target or sometimes build elsewhere)

  • target is what the produced executable objects will generate when producing executable objects, i.e. the architecture of the systems the built programs target their results to run on (relevant only for compilers and similar)

Building with sbuild

Prerequisites:

  • an schroot chroot (to do builds in). A standard chroot for native building just works for cross building since stretch.

Either download the package source, enter it and run sbuild, or give an explicit package_version

apt-get source acl
cd acl-2.2.52/
sbuild  --host=armhf -d buster

or:

sbuild  --host=armhf -d buster acl_2.2.52

Building with pbuilder

You need to be pbuilder from stretch (or newer) to support cross-building; the feature was introduced in version 0.227.

You must use the apt "satisfydepends" resolver with pbuilder. This can be done by adding the following line to your ~/.pbuilderrc:

PBUILDERSATISFYDEPENDSCMD="/usr/lib/pbuilder/pbuilder-satisfydepends-apt"

If you're missing the above you might see the following error: E: Cross building is possible only with the APT dependency resolver

If you don't already have a pbuilder base chroot (for native building), create one now by simply running:

pbuilder create
cowbuilder --create

Now to cross-build a package simply use the --host-arch option, eg. to build "my-package" for armhf use:

pbuilder build --host-arch armhf my-package.dsc
pdebuild -- --host-arch armhf

Building with git-buildpackage with pbuilder

Similar to pbuilder part, you must use the apt "satisfydepends" resolver with pbuilder. This can be done by adding the following line to your ~/.pbuilderrc:

PBUILDERSATISFYDEPENDSCMD="/usr/lib/pbuilder/pbuilder-satisfydepends-apt"

If you're missing the above you might see the following error: E: Cross building is possible only with the APT dependency resolver

Now to cross-build a package simply use the --git-pbuilder-options="--host-arch <ARCH>" as option, eg. to build for armhf use:

gbp buildpackage --git-pbuilder --git-pbuilder-options="--host-arch armhf"

Building with dpkg-buildpackage

If you are building packages with no library dependencies (such as kernels or bootloaders), and using a standalone cross-toolchain, then you don't need to enable the foreign architecture. Nearly all packages will need the foreign architecture enabling.

Enable the foreign architecture

sudo dpkg --add-architecture armhf
sudo apt-get update
sudo apt-get install build-essential crossbuild-essential-armhf

sudo apt-get build-dep -aarmhf <package>
cd <package>-<version>
CONFIG_SITE=/etc/dpkg-cross/cross-config.<host_arch>  DEB_BUILD_OPTIONS=nocheck dpkg-buildpackage -aarmhf -Pcross,nocheck


more to come here


Building manually outside of packages

You can look at the code for how debhelper does cross-compiling.

The dpkg_architecture_value() function returns values that can be found in the output of the dpkg-architecture command.

Install tools

You need sbuild installed. This will also bring in schroot and build-essential.

sudo apt-get install sbuild

Set up a chroot

This can be done lots of ways (debootstrap, mksbuild, multistrap, pbuilder create). sbuild-createchroot is good because it does the schroot config for you too, and can make tarballs trivially, for clean builds.

sudo sbuild-createchroot --make-sbuild-tarball=/srv/chroots/sid-sbuild.tgz sid /srv/chroots/sid http://deb.debian.org/debian/

will create a base chroot, pack it up into /srv/chroots/sid-sbuild.tgz, and add the config for it to schroot (in /etc/schroot/chroot.d)

If you have not used sbuild before on this machine it needs setting up on your machine:

sudo sbuild-adduser <your-username>

to allow your username to run builds, and

sbuild-update --keygen

to enable installation of build-dependencies in the chroot. (This command can take a really long time on servers with insufficient entropy - running it locally and copying the keyfiles over to the build machine can be a good idea in that case)

Check the new chroot exists with

schroot -l

Which on an amd64 machine will display:

chroot:sid-amd64-sbuild
source:sid-amd64-sbuild

(sbuild-createchroot adds the arch name by default when naming chroots - this is quite useful if you have more than one)

That is one chroot, accessible in two different ways:

  1. the clean chroot chroot:sid-amd64-sbuild, where none of your changes are saved so it's the same every time.
  2. the actual chroot tarball source:sid-amd64-sbuild, where changes are packed up again and saved for next time.

Normally you use the clean chroot, and that's the default if you just say "sid-amd64-sbuild" (the sbuild-shell command is an exception to this, as it implicitly uses the source: version)

Changing the sources in the chroot

By default the apt sources for the build are the one given on the sbuild-createchroot command line. If that's not what you want you need to change them:

First you need an editor in the chroot - there is none by default (choose vim.tiny, zile, nano) as you see fit). A few tools in the chroot are useful, but for clean builds you don't want too much stuff in there.

sudo sbuild-apt sid-amd64-sbuild apt-get install zile

Now enter the chroot to edit /etc/apt/sources.list or /etc/apt/sources.list.d/customsource.list

sudo sbuild-shell sid-amd64-sbuild

Making your home dir available in the chroot

It's very handy to have your home directory automatically mounted in the chroot. The default sbuild-createchroot command does not set this up. If you only ever do sbuild-builds then it doesn't matter. But if you enter the chroot to run manual builds or tests having your home dir available is really useful.

Do this by changing

profile=sbuild

to

profile=default

in /etc/schroot/chroot.d/sid-amd64-sbuild-<uniqueID>

Using the chroot

Basic usage to enter the chroot:

schroot -c sid-amd64-sbuild

Normal users and networking are available inside (and your home dir if you enabled that) so (if you install sudo) you can use sudo, download stuff, use files. If you are less interested in clean builds than conveninence then you can install lots of stuff permanently in the chroot.

Read the sbuild man pages for lots of clever stuff you can do. sbuild supports sessions, so once started you can leave and re-enter a chroot session. They can even be recovered after rebooting. The same commands can be run in multiple chroots. They can be updated from outside. command-hooks can be used to run things before builds, after builds, on entry etc.

Seeing how many schroot sessions you have (old ones can collect, and need tidying up sometimes):

schroot -la

Configuring cross-toolchains in schroot

Manual chroot setup

If you did not use sbuild-createchroot

See also