You are on page 1of 43

Cross Compile Hacks

JNR322 Andy Huang


2013/04/24

Jan. 2012

Outline
Why we need cross compile How to install a package on linux distribution How to build your own package Some important flags Just do configure

Some tricks
autobuild Reference data

Why we need cross compile

Confidential Material for Internal Use Only

Why we need cross compile


Native compile <-> Cross compile
Native compiler
a compiler running and final binary execution on the same architecture

Cross compiler
A cross-compiler is one that compiles binaries for architectures other than its own

Confidential Material for Internal Use Only

How to install a package on linux distribution

Confidential Material for Internal Use Only

How to install a package

apt-get/yum install

Do not consider library dependency


Build from source You need to take care library dependency

Confidential Material for Internal Use Only

How to build your own package

Confidential Material for Internal Use Only

How to build your own package


X86

configure

make

make install

Confidential Material for Internal Use Only

How to build your own package


Embedded system
You need to take care the library dependency by yourself

Download source

Check dependency

configure

make

make install

Confidential Material for Internal Use Only

How to build your own package


Understand your config help file content
Configuration

Installation directories

System types

Optional Features

Optional Packages

Some influential environment variables

Confidential Material for Internal Use Only

10

How to build your own package


configuration

Configuration: -h, --help display this help and exit --help=short display options specific to this package --help=recursive display the short help of all the included packages -V, --version display version information and exit -q, --quiet, --silent do not print `checking ...' messages --cache-file=FILE cache test results in FILE [disabled] -C, --config-cache alias for `--cache-file=config.cache' -n, --no-create do not create output files --srcdir=DIR find the sources in DIR [configure dir or `..']

Confidential Material for Internal Use Only

11

How to build your own package


Installation directories

--prefix=PREFIX install architecture-independent files in PREFIX [/usr/local] --exec-prefix=EPREFIX install architecture-dependent files in EPREFIX [PREFIX] For better control, use the options below. Fine tuning of the installation directories: --bindir=DIR user executables [EPREFIX/bin] --sbindir=DIR system admin executables [EPREFIX/sbin] --libexecdir=DIR program executables [EPREFIX/libexec] --sysconfdir=DIR read-only single-machine data [PREFIX/etc]

Confidential Material for Internal Use Only

12

How to build your own package


System types System types: --build=BUILD configure for building on BUILD [guessed] --host=HOST cross-compile to build programs to run on HOST [BUILD] <--target=TARGET configure for building compilers for TARGET [HOST]>

--build
--host --target

The environment your build code Let configure to take care The system on which the tools will run is called the host system

The system for which the tools generate code is called the target system

--host=arm-elf-linux or arm-elf buildhosttarget

Confidential Material for Internal Use Only

13

How to build your own package


Optional Features

--disable-option-checking ignore unrecognized --enable/--with options --disable-FEATURE do not include FEATURE (same as --enableFEATURE=no) --enable-FEATURE[=ARG] include FEATURE [ARG=yes] --disable-largefile omit support for large files --disable-protochain disable \"protochain\" insn --enable-ipv6 build IPv6-capable version [default=yes, if getaddrinfo available] --enable-optimizer-dbg build optimizer debugging code Turn on/off some package internal features

Confidential Material for Internal Use Only

14

How to build your own package


Optional Packages

--with-PACKAGE[=ARG] use PACKAGE [ARG=yes] --without-PACKAGE do not use PACKAGE (same as --with-PACKAGE=no) --without-gcc don't use gcc --with-sita include SITA support --with-pcap=TYPE use packet capture TYPE --without-libnl disable libnl support [default=yes, on Linux, if present] --with-dag[=DIR] include Endace DAG support ["yes", "no" or DIR; default="yes" on BSD and Linux if present] --with-dag-includes=DIR Endace DAG include directory --with-dag-libraries=DIR

Check package library dependency here

Confidential Material for Internal Use Only

15

How to build your own package


influential environment variables

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 GLIB_CFLAGS C compiler flags for GLIB, overriding pkg-config GLIB_LIBS linker flags for GLIB, overriding pkg-config

Confidential Material for Internal Use Only

16

Some important flags

Confidential Material for Internal Use Only

17

Some important flags


CPP - preprocessor
Delete all the #define , and extend all the macro define process all the condition command , #if , #ifdef , #elif , #else , #endif process #include command delete all the comment code , // and /* */ #define __JNR__ is the same as -D__JNR__

CPPFLAGS
-I<include dir> , to let compiler find the headers

LDFAGS
-L<lib dir> , to let compiler find the library

Confidential Material for Internal Use Only

18

Some important flags


LIBS
-l<library> , to pass the reference lib name to the compiler -lz --> compiler will find libz.xx to link

CFLAGS
pass some optional arguments to compiler -g -O -Wall

CXX - C++ compiler command


Do not confuse with CPP

Confidential Material for Internal Use Only

19

Some important flags


PKG_CONFIG
let compiler find the dependency library automatically pkg-config and xxx.pc (package config) cat /usr/lib/pkgconfig/openssl.pc prefix=/usr exec_prefix=${prefix} libdir=${exec_prefix}/lib includedir=${prefix}/include Name: OpenSSL Description: Secure Sockets Layer and cryptography libraries and tools Version: 0.9.8o Requires: Libs: -L${libdir} -lssl -lcrypto Libs.private: -ldl -Wl,-Bsymbolic-functions -lz Cflags: -I${includedir}
Confidential Material for Internal Use Only

20

Some important flags


PKG_CONFIG
pkg-config --version ./openssl.pc pkg-config --libs ./openssl.pc pkg-config --cflags ./openssl.pc

Before you compile


export PKG_CONFIG_PATH=/xxx/xxx/lib/pkgconfig avoid Makefile to eat your x86 xxx.pc

Confidential Material for Internal Use Only

21

Just do configure

Confidential Material for Internal Use Only

22

Just do configure
libpcap + arp-scan (ARP scanning and fingerprinting tool) libpcap configure arguments RANLIB="/opt/arago/bin/arm-arago-linux-gnueabi-ranlib" \ CXX="/opt/arago/bin/arm-arago-linux-gnueabi-g++" \ CC="/opt/arago/bin/arm-arago-linux-gnueabi-gcc" \ ./configure --host=mips-elf-linux \ --prefix=/home/andy/ti-sdk-am335x-evm05.04.01.00/fakeroot \ --disable-ipv6 \ --disable-universal \ --disable-can \ --disable-canusb \ --disable-bluetooth \ --with-pcap=linux configure , make , make install
Confidential Material for Internal Use Only

23

Just do configure
libpcap + arp-scan (ARP scanning and fingerprinting tool)
arp-scan configure arguments
RANLIB="/opt/arago/bin/arm-arago-linux-gnueabi-ranlib" \ CXX="/opt/arago/bin/arm-arago-linux-gnueabi-g++" \ CC="/opt/arago/bin/arm-arago-linux-gnueabi-gcc" \ ./configure --host=mips-elf-linux \ --prefix=/home/andy/ti-sdk-am335x-evm-05.04.01.00/fakeroot

Confidential Material for Internal Use Only

24

Just do configure
Can not find xxx library ( RUN.1st)
check your library path or dependency
CPPFLAGS="-I/home/andy/ti-sdk-am335x-evm-05.04.01.00/fakeroot/include I/home/andy/ti-sdk-am335x-evm-05.04.01.00/fakeroot/include/pcap" LDFLAGS="-L/home/andy/ti-sdk-am335x-evm-05.04.01.00/fakeroot/lib" LIBS="-lpcap"

PKG_CONFIG_PATH
libpcap did not provide libpcap.pc

Confidential Material for Internal Use Only

25

Just do configure
Can not run test ( cannot run test program while cross compiling ) - RUN.2nd
check config.log (line 448) and trace configure file (line 5750) pre-fill the answer which configure want to check get the answer from x86

--cache-file=arm-linux.cache pgac_cv_snprintf_long_long_int_format=%lld

Run RUN.3rd again

Confidential Material for Internal Use Only

26

Just do configure
Check your final binary file
file arp-scan arp-scan: ELF 32-bit LSB executable, ARM, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.16, not stripped

Confidential Material for Internal Use Only

27

Some tricks

Confidential Material for Internal Use Only

28

Some tricks
RUN file Record your configure parameters prefix Collect the same platform libraries together cache-file

Fix the configure issue when cross-compile


config.log Help you to debug config.help Understand your package
Confidential Material for Internal Use Only

29

Some tricks
CONFIG_SITE
set default values for configure scripts to share Put your default environment values like
CC LD RANLIB

cache_file
prefix host

CONFIG_SITE=/your/file/path libpcap demo

Confidential Material for Internal Use Only

30

Some tricks
Prefix side effect
conf and binary file will be fixed to your prefix path

prefix = /home/andy/ti-sdk-am335x-evm-05.04.01.00/fakeroot program_transform_name = s,x,x, psdir = ${docdir} sbindir = ${exec_prefix}/sbin sharedstatedir = ${prefix}/com srcdir = . sysconfdir = ${prefix}/etc target_alias = top_builddir = . top_srcdir = .

Confidential Material for Internal Use Only

31

Some tricks
Prefix side effect solution
Fine tune configure argument --bindir=DIR user executables [EPREFIX/bin] --sbindir=DIR system admin executables [EPREFIX/sbin] --libexecdir=DIR program executables [EPREFIX/libexec] --sysconfdir=DIR read-only single-machine data [PREFIX/etc]

Confidential Material for Internal Use Only

32

autobuild

Confidential Material for Internal Use Only

33

autobuild
autoconf
Generate configuration scripts

automake
Generate Makefile.in for configure from Makefile.am

aclocal
Generate `aclocal.m4' by scanning `configure.ac' or `configure.in'

libtool
Provide some scripts to generate static/shared libraries

Confidential Material for Internal Use Only

34

autobuild step 0
Hello.c
#include <stdio.h> int main() { printf("hello\n"); return 0; }

Confidential Material for Internal Use Only

35

autobuild step 1
autoscan cp configure.scan configure.ac

edit configure.ac
AC_PREREQ([2.67]) AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS]) AC_CONFIG_SRCDIR([hello.c]) AC_CONFIG_HEADERS([config.h]) # Checks for programs. AC_PROG_CC # Checks for libraries. # Checks for header files. # Checks for typedefs, structures, and compiler characteristics. # Checks for library functions. AC_OUTPUT
Confidential Material for Internal Use Only

AC_PREREQ([2.67]) AC_INIT(hello, 0.1, andy.huang@wnc.om.tw) AM_INIT_AUTOMAKE AC_CONFIG_SRCDIR([hello.c]) #AC_CONFIG_HEADERS([config.h])


# Checks for programs. AC_PROG_CC AC_CONFIG_FILES(Makefile) # Checks for libraries. # Checks for header files. # Checks for typedefs, structures, and compiler characteristics. # Checks for library functions. AC_OUTPUT
36

autobuild step 2
Create Makefile.am
bin_PROGRAMS=hello hello_SOURCES=hello.c

Confidential Material for Internal Use Only

37

autobuild step 3
aclocal autoconf
Generate configure file

automake a
Required file : NEWS README AUTHORS ChangeLog

./configure
make

Confidential Material for Internal Use Only

38

autobuild step 4
make distcheck
Pack your source into hello-0.1.tar.gz

RELEASE !!!!

Confidential Material for Internal Use Only

39

Reference data

Confidential Material for Internal Use Only

40

Reference data
Books
- GCC GNU Make

Confidential Material for Internal Use Only

41

Homework
Zlib Curl

Confidential Material for Internal Use Only

42

You might also like