Cross-compiling in Linux

Cross compiling for arm or aarch64 on Debian or Ubuntu

To find out for which architecture (32 bit or 64 bit ARM) you need to compile,

$ uname -m
  • x86_64 (standard PC)
  • x86_64
  • 32 bit ARMM
  • armv7l
  • 64 bit ARM (or aarch64)
  • aarch64
Install the necessary packages and tools for cross compiling for ARM,
  • For 32 bit ARM (arm)
  • 
    $ sudo apt install gcc make gcc-arm-linux-gnueabi binutils-arm-linux-gnueabi
    
  • For 64 bit ARM (aarch64)
  • 
    $ sudo apt install gcc make gcc-aarch64-linux-gnu binutils-aarch64-linux-gnu
    

Cross compiling with configure and make

To run configure, we need to specify the build and host platform so that we want to end up with a binary (statically linked) for ARM:

  --build=BUILD     configure for building on BUILD [guessed]
  --host=HOST       cross-compile to build programs to run on HOST [BUILD] speeds up one-time build
Use these variables to override the choices made by `configure' or to help it to find libraries and programs with nonstandard names/locations:

  CC          C compiler command
  CFLAGS      C compiler flags
  LDFLAGS     linker flags, e.g. -L<lib dir> if you have libraries in a
              nonstandard directory <lib dir>
  LIBS        libraries to pass to the linker, e.g. -l<library>
  CPPFLAGS    (Objective) C/C++ preprocessor flags, e.g. -I<include dir> if
              you have headers in a nonstandard directory <include dir>
  CPP         C preprocessor
  PKG_CONFIG  path to pkg-config utility
  PKG_CONFIG_PATH
              directories to add to pkg-config's search path
  PKG_CONFIG_LIBDIR
              path overriding pkg-config's built-in search path
  NCURSESW_CFLAGS
              C compiler flags for NCURSESW, overriding pkg-config
  NCURSESW_LIBS
              linker flags for NCURSESW, overriding pkg-config
  NCURSES_CFLAGS
              C compiler flags for NCURSES, overriding pkg-config
  NCURSES_LIBS
              linker flags for NCURSES, overriding pkg-config

pkg-config and ./configure

In the open source development, almost all the source codes are to be compiled by running the auto tools.


The final step is to execure ./configure to generate the Makefiles.

In the cross-platform building process, the problem happens sometimes while the ./configure checking the necessary libraries which are missed. The root cause is that ./configure uses pkg-config to get the compiling options and the library path about the installed libraries in the local host. For each library, a small .pc text file is installed in a standard location that contains the compilation flags needed for that library along with version number information.

To ask the pkg-config to get the information from the pre-built *.pc for your target platform, you need to set the variable PKG_CONFIG for the ./configure command.

For ex., if you want to cross- compile a library which requires the directFB library, it will complain the directFB library is missed while you running ./configure:

checking for DirectFB... configure: error:

To solve it, put the directfb.pc under a directory and set the environment variable PKG_CONFIG_PATH so that it points to that directory.

Besides, some variables are pre-defined values in the configure script, that causes errors sometimes because those values are always considered for the Intelx86 platforms. To re-define those values for your platform, you can use the configure option "--cache-file=host-linux.cache" and define the variable's value in the host-linux.cache file before executing the configure script. For ex., echo "var=val" > host-linux.cache


===================================================================
The man page of pkg-config:

PKG_CONFIG_PATH

A colon-separated (on Windows, semicolon-separated) list of directories to search for .pc files. The default directory will always be searched after searching the path; the default is libdir/pkg-config:datadir/pkgconfig where libdir is the libdir where pkg-config and datadir is the datadir where pkg-config was installed.

AUTOCONF MACROS

PKG_CHECK_MODULES(VARIABLE-PREFIX,MODULES[,ACTION-IF-FOUND,[ACTION-IF-NOT-FOUND]])
The macro PKG_CHECK_MODULES can be used in configure.ac to check whether modules exist. A typical usage would be:

PKG_CHECK_MODULES([MYSTUFF], [gtk+-2.0 >= 1.3.5 libxml = 1.8.4])

This would result in MYSTUFF_LIBS and MYSTUFF_CFLAGS substitution variables, set to the libs and cflags for the given module list. If a module is missing or has the wrong version, by default configure will abort with a message.

Metadata file(*.pc) syntax

To add a library to the set of packages pkg-config knows about, simply install a .pc file. You should
install this file to libdir/pkgconfig.

Here is an example file:

# This is a comment
prefix=/home/hp/unst   # this defines a variable
exec_prefix=${prefix}  # defining another variable in terms of the first
libdir=${exec_prefix}/lib
includedir=${prefix}/include

Name: GObject                            # human-readable name
Description: Object/type system for GLib # human-readable description
Version: 1.3.1
URL: http://www.gtk.org
Requires: glib-2.0 = 1.3.1
Conflicts: foobar <= 4.5
Libs: -L${libdir} -lgobject-1.3
Libs.private: -lm
Cflags: -I${includedir}/glib-2.0 -I${libdir}/glib/include

CrossCompiling

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

Arm64 Port

The port of Debian to the 64-bit Arm platform (arm64), known in some other places as AArch64.

To downloade ARM64 packages from Ubuntu archives, append the following to /etc/apt/sources.list on your ARM64 system:

  
# See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
# newer versions of the distribution.
deb http://ports.ubuntu.com/ubuntu-ports/ focal main restricted
# deb-src http://ports.ubuntu.com/ubuntu-ports/ focal main restricted

## Major bug fix updates produced after the final release of the
## distribution.
deb http://ports.ubuntu.com/ubuntu-ports/ focal-updates main restricted
# deb-src http://ports.ubuntu.com/ubuntu-ports/ focal-updates main restricted

## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team. Also, please note that software in universe WILL NOT receive any
## review or updates from the Ubuntu security team.
deb http://ports.ubuntu.com/ubuntu-ports/ focal universe
# deb-src http://ports.ubuntu.com/ubuntu-ports/ focal universe
deb http://ports.ubuntu.com/ubuntu-ports/ focal-updates universe
# deb-src http://ports.ubuntu.com/ubuntu-ports/ focal-updates universe

## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team, and may not be under a free licence. Please satisfy yourself as to
## your rights to use the software. Also, please note that software in
## multiverse WILL NOT receive any review or updates from the Ubuntu
## security team.
deb http://ports.ubuntu.com/ubuntu-ports/ focal multiverse
# deb-src http://ports.ubuntu.com/ubuntu-ports/ focal multiverse
deb http://ports.ubuntu.com/ubuntu-ports/ focal-updates multiverse
# deb-src http://ports.ubuntu.com/ubuntu-ports/ focal-updates multiverse

## N.B. software from this repository may not have been tested as
## extensively as that contained in the main release, although it includes
## newer versions of some applications which may provide useful features.
## Also, please note that software in backports WILL NOT receive any review
## or updates from the Ubuntu security team.
deb http://ports.ubuntu.com/ubuntu-ports/ focal-backports main restricted universe multiverse
# deb-src http://ports.ubuntu.com/ubuntu-ports/ focal-backports main restricted universe multiverse

## Uncomment the following two lines to add software from Canonical's
## 'partner' repository.
## This software is not part of Ubuntu, but is offered by Canonical and the
## respective vendors as a service to Ubuntu users.
# deb http://archive.canonical.com/ubuntu focal partner
# deb-src http://archive.canonical.com/ubuntu focal partner

deb http://ports.ubuntu.com/ubuntu-ports/ focal-security main restricted
# deb-src http://ports.ubuntu.com/ubuntu-ports/ focal-security main restricted
deb http://ports.ubuntu.com/ubuntu-ports/ focal-security universe
# deb-src http://ports.ubuntu.com/ubuntu-ports/ focal-security universe
deb http://ports.ubuntu.com/ubuntu-ports/ focal-security multiverse
# deb-src http://ports.ubuntu.com/ubuntu-ports/ focal-security multiverse
"apt update: should shows:

Get:1 http://ports.ubuntu.com/ubuntu-ports focal InRelease [265 kB]
Get:2 http://ports.ubuntu.com/ubuntu-ports focal-updates InRelease [114 kB]
Get:3 http://ports.ubuntu.com/ubuntu-ports focal-backports InRelease [101 kB]
Get:4 http://ports.ubuntu.com/ubuntu-ports focal-security InRelease [114 kB]
Get:5 http://ports.ubuntu.com/ubuntu-ports focal/main arm64 Packages [940 kB]
Get:6 http://ports.ubuntu.com/ubuntu-ports focal/main Translation-en [506 kB]
Get:7 http://ports.ubuntu.com/ubuntu-ports focal/restricted arm64 Packages [1300 B]
Get:8 http://ports.ubuntu.com/ubuntu-ports focal/restricted Translation-en [6212 B]
Get:9 http://ports.ubuntu.com/ubuntu-ports focal/universe arm64 Packages [8458 kB]
Get:10 http://ports.ubuntu.com/ubuntu-ports focal/universe Translation-en [5124 kB]
Get:11 http://ports.ubuntu.com/ubuntu-ports focal/multiverse arm64 Packages [114 kB]
Get:12 http://ports.ubuntu.com/ubuntu-ports focal/multiverse Translation-en [104 kB]
Get:13 http://ports.ubuntu.com/ubuntu-ports focal-updates/main arm64 Packages [847 kB]
Get:14 http://ports.ubuntu.com/ubuntu-ports focal-updates/main Translation-en [246 kB]
Get:15 http://ports.ubuntu.com/ubuntu-ports focal-updates/restricted arm64 Packages [2824 B]
Get:16 http://ports.ubuntu.com/ubuntu-ports focal-updates/restricted Translation-en [52.9 kB]
Get:17 http://ports.ubuntu.com/ubuntu-ports focal-updates/universe arm64 Packages [793 kB]
Get:18 http://ports.ubuntu.com/ubuntu-ports focal-updates/universe Translation-en [177 kB]
Get:19 http://ports.ubuntu.com/ubuntu-ports focal-updates/multiverse arm64 Packages [7720 B]
Get:20 http://ports.ubuntu.com/ubuntu-ports focal-updates/multiverse Translation-en [6876 B]
Get:21 http://ports.ubuntu.com/ubuntu-ports focal-backports/main arm64 Packages [2572 B]
Get:22 http://ports.ubuntu.com/ubuntu-ports focal-backports/main Translation-en [1120 B]
Get:23 http://ports.ubuntu.com/ubuntu-ports focal-backports/universe arm64 Packages [5816 B]
Get:24 http://ports.ubuntu.com/ubuntu-ports focal-backports/universe Translation-en [2068 B]
Get:25 http://ports.ubuntu.com/ubuntu-ports focal-security/main arm64 Packages [534 kB]
Get:26 http://ports.ubuntu.com/ubuntu-ports focal-security/main Translation-en [156 kB]
Get:27 http://ports.ubuntu.com/ubuntu-ports focal-security/restricted arm64 Packages [2632 B]
Get:28 http://ports.ubuntu.com/ubuntu-ports focal-security/restricted Translation-en [47.6 kB]
Get:29 http://ports.ubuntu.com/ubuntu-ports focal-security/universe arm64 Packages [585 kB]
Get:30 http://ports.ubuntu.com/ubuntu-ports focal-security/universe Translation-en [98.4 kB]
Get:31 http://ports.ubuntu.com/ubuntu-ports focal-security/multiverse arm64 Packages [3052 B]
Get:32 http://ports.ubuntu.com/ubuntu-ports focal-security/multiverse Translation-en [5072 B]

Debootstrap

debootstrap is a tool which just access to a Debian repository and will install a Debian base system into a subdirectory of another, already installed system.
It can also be used to create a rootfs for a machine of a different architecture, which is known as "cross-debootstrapping".

Schroot

Schroot allows users to execute commands or interactive shells in different chroots.
  • Installation
  • 
    $ sudo apt-get install schroot
    $ sudo apt-get install debootstrap
    $ sudo apt-get install qemu-user-static
      	
  • Setup
  • Configuration file:
    • /etc/schroot/schroot.conf
    • A file for all.
    • /etc/schroot/chroot.d/*
    • Separate configuration files.
    schroot.conf is a plain UTF-8 text file, describing the chroots available for use with schroot.
    The configuration format is an INI-style format, split into groups of key-value pairs separated by section names in square brackets.
    A chroot definition is started by the name of the chroot in square brackets. For example,
    
    [focal-arm64-sbuild] .
      	
    This is then followed by several key-value pairs, one per line:
    • type=type
    • Chroots of type '' or '' are directories accessible in the filesystem.
      • plain
      • The default.
        Filesystems such as /proc are not mounted in plain chroots; it is the responsibility of the system administrator to configure such chroots by hand.
        The directory for 'plain' chroots is mounted with the --rbind option to mount.
      • directory
      • directory chroots run setup scripts, filesystems are automatically configured.
    • directory=directory
    • This is where the root will be changed to when executing a login shell or a command.
    • users=user1,user2,...
    • A comma-separated list of users which are allowed access to the chroot. If empty or omitted, no users will be allowed access (unless a group they belong to is also specified in groups).
    • groups=group1,group2,...
    • A comma-separated list of groups which are allowed access to the chroot. If empty or omitted, no groups of users will be allowed access.
    • root-users=user1,user2,...
    • A comma-separated list of users which are allowed password-less root access to the chroot. If empty or omitted, no users will be allowed root access without a password (but if a user or a group they belong to is in users or groups, respectively, they may gain access with a password).
    • root-groups=group1,group2,...
    • A comma-separated list of groups which are allowed password-less root access to the chroot. If empty or omitted, no users will be allowed root access without a password (but if a user or a group they belong to is in users or groups, respectively, they may gain access with a password).
    • aliases=alias1,alias2,...
    • A comma-separated list of aliases (alternate names) for this chroot.
    • personality=persona
    • Set the personality (process execution domain) to use.
      This option is useful when using a 32-bit chroot on 64-bit system, for example.
      The default value is 'linux'.
      For a 32-bit chroot on a 64-bit system, 'linux32' is the option required.
  • Example
    • Create root file system
    • On debian based systems, you can manually initialize an installation using debootstrap.
      
      $ sudo mkdir -p /home/jerry/work/chroot/focal-arm64
      $ sudo debootstrap --arch=arm64 --foreign /home/jerry/work/chroot/focal-arm64
      		
      The above will create an ARM64 chroot system.
      The first stage downloads the needed .deb files and unpacks them into the directory you specify.
      Note the --foreign option that prevents debootstrap from running the second stage as this needs to be run in a chroot .
      Test the base system:
      • From a different build x86 architecture
      • qemu-aarch64-static is the magic cure here, which make possible chrooting into an Arm64 filesystem under x86_64 host system.
        Copy qemu to the arm64 target filesystem then chroot to target:
        
        $ sudo cp /usr/bin/qemu-aarch64-static /home/jerry/work/chroot/focal-arm64/usr/bin/
        $ sudo chroot /home/jerry/work/chroot/focal-arm64 /usr/bin/qemu-aarch64-static /bin/sh -i  
        # uname -ar
        Linux jerry-NUC10i7FNH 5.8.0-64-generic #72-Ubuntu SMP Wed Jul 21 16:25:30 UTC 2021 aarch64 aarch64 aarch64 GNU/Linux
        				
      • On the same amd64 build architecture
      • Chroot to target directly:
        
        $ sudo chroot /home/jerry/work/chroot/focal-amd64 /bin/sh -i
        				
      Using a x86 machine to create a Debian/Ubuntu installation for an embedded ARM, need debootstrap second stage.
      The second stage runs all of the package configuration scripts, which must be done using the target architecture (or by using qemu-user-static to emulate the target architecture).
      
      # /debootstrap/debootstrap --arch=arm64 --second-stage     
      ...
      I: Base system installed successfully.
              
    • Create a schroot configuration file
    • Create a file /etc/schroot/chroot.d/focal-arm64-test:
      
      # schroot chroot definitions.
      # See schroot.conf(5) for complete documentation of the file format.
      #
      # The following lines are examples only.  Uncomment and alter them to
      # customise schroot for your needs, or create a new entry from scratch.
      #
      [focal-arm64-sbuild]
      description=focal-arm64-sbuild
      aliases=arm64
      type=directory
      directory=/home/jerry/work/chroot/focal-arm64
      users=jerry
      groups=sbuild
      root-users=jerry
      root-groups=root
      profile=desktop
      personality=linux
      preserve-environment=true
              	
    • Check the schroot configurations:
    • 
      $ schroot -l -v --debug=notice
      D(1): Loading config file: /etc/schroot/schroot.conf
      D(1): Loading data file: /etc/schroot/schroot.conf
      ...
      chroot:focal-arm64-sbuild
      chroot:arm64
      			
    • test
    • 
      $ schroot -c arm64
      (focal-arm64-sbuild)jerry@jerry-NUC10i7FNH:~$ 	
      			
  • How do I manually cleaning up a broken session?
  • NOTE: Do not remove any directories without checking if there are any filesystems mounted below them.
    For example, to remove a session named my-session by hand:
    • Check for mounted filesystems
    •       
      $ /usr/lib/x86_64-linux-gnu/schroot/schroot-listmounts -m \
                      /var/run/schroot/mount/my-session
      		
    • Unmount any mounted filesystems
    •   
      $ sudo umount /run/schroot/mount/my-session/*
      		
    • Remove /var/run/schroot/mount/my-session
    •   
      $ sudo rmdir /var/run/schroot/mount/my-session      
      		
    • Remove the session configuration file
    • 
      $ sudo rm /var/lib/schroot/session/my-session
      		
    • Repeat for the other directories
    • such as /var/lib/schroot/union/underlay, /var/lib/schroot/union/overlay and /var/lib/schroot/unpack

Building with sbuild

sbuild rebuilds Debian binary packages from the corresponding Debian source, installing any missing source dependencies.
The build takes place in a dedicated clean build environment, rather than on the host system.

sbuild is a convenience wrapper script of schroot to build binary package easily under specified chroot.
sbuild is used on the official buildd network to build binary and source packages for all supported architectures.
The sbuild suite of programs (buildd and sbuild) are used to build binary packages from source packages.
sbuild does the actual package building.

This requires an schroot chroot (to do builds in).

  • Setup
    • Automatic setup
    • sbuild-debian-developer-setup is a convenience script to set up an sbuild environment for Debian Developers.
    • Manual setup
    • This ~/.sbuildrc sets up sbuild.
      
      ##
      ## SBUILD BEHAVIOUR
      ##
      
      # Default distribution.  By default, no distribution is defined, and
      # the user must specify it with the -d option.  However, a default may
      # be configured here if desired.  Users must take care not to upload
      # to the wrong distribution when this option is set, for example
      # experimental packages will be built for upload to unstable when this
      # is not what is required.
      $distribution = 'focal';
      
      # Default chroot (defaults to distribution[-arch][-sbuild])
      $chroot = 'focal-arm64-test';
      
              
  • Build
  • With properly configured ~/.sbuildrc as above, you can imply run the following to build a package from the source directory of a debianized package.
    Cross-compiling happens when host != build. This can be done via one of the following way:
    • download the package source, enter the source folder then run sbuild
    • 
      $ apt-get source wpa
      $ cd wpa/wpa-2.9
      $ sbuild --build=arm64 --host=arm64
      			
    • download the package source, give an explicit package_version for sbuild to build
    • 
      $ apt-get source wpa
      $ sbuild  --build=arm64 --host=arm64 wpa-2.9
      			
    Everything needed to build the latest sbuild version will be downloaded from the repositories and will be saved in your current directory after building of the packages is finished.
    • --arch=architecture, --host=architecture
    • Build using the architecture specified.
      A chroot named $distribution-$arch-sbuild or $distribution-arch is searched for, in that order of preference.
      This command line option sets the HOST_ARCH configuration variables. The chroot must be installed and configured appropriately to build as that architecture, For e.g., using personality=linux32 to build i386 packages on an amd64 system.
    • --build=architecture
    • This option is only useful for cross-building when used together with --host.
      If --build is not specified, the default system architecture is assumed.

      This command line option sets the BUILD_ARCH configuration variable in sbuild.conf.

  • Cross-compiling wpa failures
    • apt install libssl-dev
    • 
      /usr/include/openssl/e_os2.h:13:11: fatal error: openssl/opensslconf.h: No such file or directory
         13 | # include <openssl/opensslconf.h>
            |           ^~~~~~~~~~~~~~~~~~~~~~~
      compilation terminated.
      make[2]: *** [Makefile:1991: ../src/crypto/crypto_openssl.o] Error 1
      make[2]: *** Waiting for unfinished jobs....
      make[2]: Leaving directory '/<<PKGBUILDDIR>>/wpa_supplicant'
      dh_auto_build: error: cd wpa_supplicant && make -j12 "INSTALL=install --strip-program=true" PKG_CONFIG=arm-linux-gnueabihf-pkg-config CXX=arm-linux-gnueabihf-g\+\+ CC=cc returned exit code 2
      make[1]: *** [debian/rules:39: override_dh_auto_build] Error 25
      make[1]: Leaving directory '/<<PKGBUILDDIR>>'
      make: *** [debian/rules:97: build-arch] Error 2
      dpkg-buildpackage: error: debian/rules build-arch subprocess returned exit status 2    
          			
    • apt-get install libnl-3-dev libnl-genl-3-dev
    • 
      /usr/bin/ld: cannot find -lnl-3
      /usr/bin/ld: cannot find -lnl-genl-3
      collect2: error: ld returned 1 exit status
      make[2]: *** [Makefile:1915: wpa_passphrase] Error 1
      make[2]: *** Waiting for unfinished jobs....
      make[2]: Leaving directory '/<<PKGBUILDDIR>>/wpa_supplicant'
      dh_auto_build: error: cd wpa_supplicant && make -j12 "INSTALL=install --strip-program=true" PKG_CONFIG=arm-linux-gnueabihf-pkg-config CXX=arm-linux-gnueabihf-g\+\+ CC=cc returned exit code 2
      make[1]: *** [debian/rules:39: override_dh_auto_build] Error 25
      make[1]: Leaving directory '/<<PKGBUILDDIR>>'
      make: *** [debian/rules:97: build-arch] Error 2
      dpkg-buildpackage: error: debian/rules build-arch subprocess returned exit status 2  
          			
    • apt-get install libnl-route-3-dev libpcsclite-dev libdbus-1-dev
    • 
      /usr/bin/ld: cannot find -lnl-route-3
      /usr/bin/ld: cannot find -lpcsclite
      /usr/bin/ld: cannot find -ldbus-1
          			
    • 
      /usr/bin/ld: skipping incompatible /usr/lib/arm-linux-gnueabihf/librt.so when searching for -lrt
      /usr/bin/ld: skipping incompatible /usr/lib/arm-linux-gnueabihf/librt.a when searching for -lrt
      ...    
          			
    • 
      dpkg-buildpackage: info: host architecture arm64
       fakeroot debian/rules clean
      semop(1): encountered an error: Function not implemented
          			
      According to "[Focal] qemu can't support semtimedop syscall Edit", QEMU doesn't implement the semtimedop syscall that is used by glibc 2.31.

Building with pbuilder

pbuilder(personal package builder) allows users to setup a chroot environment for building Ubuntu packages.
A clean chroot environment makes it possible to check what dependencies are really required or missing.
It's one of the "best practices" for many maintainers of Ubuntu or Debian packages, and also for anyone interested in building packages using Ubuntu.

You can build packages without pbuilder but you must already have all the build dependencies of the package that you are building installed on your running system.

  • Install the required packages
  • 
    $ sudo apt-get install pbuilder debootstrap devscripts  
    	
  • Setup
  • .pbuilderrc is the configuration file for personal package builder Adding the following line to your ~/.pbuilderrc:
    
    ARCHITECTURE=arm64
    COMPONENTS="main universe multiverse restricted"
    PBUILDERSATISFYDEPENDSCMD="/usr/lib/pbuilder/pbuilder-satisfydepends-apt"
    BINDMOUNTS=/home/jerry/work/pbuilder
    BASETGZ=/home/jerry/work/pbuilder/focal-base.tgz
    DISTRIBUTION="focal"
    BUILDRESULT="/home/jerry/work/pbuilder/focal/result/"
    APTCACHE="/home/jerry/work/pbuilder/focal/aptcache/"
        
    • ARCHITECTURE=`dpkg --print-architecture`
    • Specifies the build architecture.
      It'll be the architecture used when creating the chroot, defaults to the system one. The supported values are the ones debootstrap supports.
    • COMPONENTS=main
    • Specify the default distribution components to use.
      This option is space-delimited, use quotes when listing multiple components.
    • PBUILDERSATISFYDEPENDSCMD=
    • This option is used by various parts of pbuilder to satisfy (i.e. install) the build-dependencies of a package.
      • pbuilder-satisfydepends-apt
      • To satisfy build dependencies and build conflicts from a .dsc; useful since it's pretty fast and doesn't require the installation of any non-required package in the chroot, nor of dummy packages.
      • pbuilder-satisfydepends-experimental
      • pbuilder-satisfydepends-aptitude
      • pbuilder-satisfydepends-gdebi
      • pbuilder-satisfydepnds-classic
    • BINDMOUNTS=
    • When this value is set, pbuilder will mount these directories using bind-mount.
    • BASETGZ=
    • Specifies the default location for the archived chroot image to be created and used.
      This is overridden with the option --basetgz.
    • DIST
    • DISTRIBUTION=
    • Specify the default distribution to use.
      This option only affects when doing pbuilder create or when --override-config is specified for pbuilder update option.
    • APTCACHE
  • Create a base tarball that will contain your chroot environment to build packages with
  • Because you are using ~/.pbuilderrc , then you need to use "sudo -E" pbuilder:
    
    $ sudo -E pbuilder create
    	
    You can use the '--variant=buildd' option when creating the base tarball.
    
    $ sudo -E pbuilder create --debootstrapopts --variant=buildd
    	
    The buildd variant will install the build-essential packages in the chroot environment, which is probably what you want, since you will be compiling packages.

Cross-building Ubuntu packages

Ubuntu packages have always been built natively, that is, built on the architecture they're being built for.
With a few adjustments this has allowed us to cross-build many packages with the equivalent of :

$ debuild -aarmhf -B
In many cases (e.g. reasonably simple packages using the GNU autotools and debhelper 9) packages require no modifications at all to cross-build cleanly.

Setup

Install


$ sudo apt install sbuild ubuntu-dev-tools

local setup

  • To create a chroot suitable for arm64 cross-building
  • mk-sbuild - creates chroots via schroot and sbuild
    
    $ mk-sbuild --target=arm64 focal
    ...
    Done building focal-amd64-arm64.
    
     To CHANGE the golden image: sudo schroot -c source:focal-amd64-arm64 -u root
     To ENTER an image snapshot: schroot -c focal-amd64-arm64
     To BUILD within a snapshot: sbuild -A -d focal-amd64-arm64 PACKAGE*.dsc
     To BUILD for arm64: sbuild -A -d focal-amd64-arm64 --host arm64 PACKAGE*.dsc
    	

Multiarch

Multiarch lets you install library packages from multiple architectures on the same machine.
In general you can have libraries of more than one architecture installed together and applications from one architecture or another installed as alternatives.

Concepts

There is a current machine architecture, as printed by

dpkg --print-architecture
It is built-in to the currently installed dpkg package. Packages can now be specified as 'package:architecture'.
For ex., we have libc:i386 and libc:amd64 .
The bare name 'package' refers to the current machine architecture in apt.

Available architectures are shown by


$ dpkg --print-foreign-architectures
i386
dpkg will manage packages for these architectures as well.

There is a 'Multi-Arch' header in the package metadata of any multiarch-aware package.

Usage

  • Configuring architectures
  • 
    $ dpkg --print-foreign-architectures
    $ sudo dpkg --add-architecture arm64
    $ dpkg --print-foreign-architectures
    arm64
            
    dpkg architectures are stored in /var/lib/dpkg/arch.
  • Remove an architecture
  • You need to remove all packages of that architecture before removing it:
    
    $ sudo apt-get purge ".*:arm64"
    $ sudo dpkg --remove-architecture arm64
            

QEMU arm64 cloud server emulation

Instal Cloud Image

  • Download the latest Ubuntu Focal cloud image.
  • 
    $ wget http://cloud-images.ubuntu.com/releases/focal/release/ubuntu-20.04-server-cloudimg-arm64.img    
        
  • Download the UEFI firmware image QEMU_EFI.fd
  • 
    $ wget https://releases.linaro.org/components/kernel/uefi-linaro/16.02/release/qemu64/QEMU_EFI.fd   
        
  • prepare your username and password
  • 
    $ whoami
    jerry
    $ cat ~/.ssh/id_rsa.pub
    ssh-rsa ...
    	
  • Create the cloud init file cloud.txt
  • Replace your username and password below:
      	
    $ cat > cloud.txt << EOF
    #cloud-config
    #cloud-config
    hostname: vm01
    manage_etc_hosts: true
    users:
      - name: vmadm
        sudo: ALL=(ALL) NOPASSWD:ALL
        groups: users, admin
        home: /home/vmadm
        shell: /bin/bash
        lock_passwd: false
    ssh_pwauth: true
    disable_root: false
    chpasswd:
      list: |
        vmadm:vmadm
      expire: false
    
    EOF
    	
  • Cread a cloud-config disk image
  •   	
    $ cloud-localds --disk-format qcow2 cloud.img cloud.txt  
    	
  • Backup your original image
  •   	  
    $ cp ubuntu-20.04-server-cloudimg-arm64.img ubuntu-20.04-server-cloudimg-arm64.img.orig
    	
  • Start QEMU with the cloud image
  • 
    $ sudo qemu-system-aarch64 \
        -smp 2 \
        -m 1024 \
        -M virt \
        -cpu cortex-a57 \
        -bios QEMU_EFI.fd \
        -nographic \
        -device virtio-blk-device,drive=image \
        -drive if=none,id=image,file=/home/jerry/work/imgs/ubuntu-20.04-server-cloudimg-arm64.img \
        -device virtio-blk-device,drive=cloud \
        -drive if=none,id=cloud,file=cloud.img \
        -device virtio-net-device,netdev=user0 \
        -netdev user,id=user0   
      

Install Necessary Packages


$ sudo apt install -y dpkg-dev devscripts 
# packages for wpa
$ sudo apt install -y libssl-dev libnl-3-dev libnl-genl-3-dev libnl-route-3-dev libpcsclite-dev libdbus-1-dev qtbase5-dev libncurses5-dev libreadline-dev docbook-utils

Resize the raw image


$ qemu-img info ubuntu-20.04-server-cloudimg-arm64.img
image: ubuntu-20.04-server-cloudimg-arm64.img
file format: qcow2
virtual size: 2.2 GiB (2361393152 bytes)
disk size: 1.36 GiB
cluster_size: 65536
Format specific information:
    compat: 0.10
    refcount bits: 16

$ qemu-img resize ubuntu-20.04-server-cloudimg-arm64.img 20G
Image resized.

Build the package


$ apt-get source wpa
$ cd wpa-2.9
$ sudo apt-get build-dep wpa
$ debuild -b -uc -us   

留言

熱門文章