397b7f41a9672da684a1d5c2c2b7e8c0a998e15e
[openocd.git] / contrib / cross-build.sh
1 #!/bin/sh
2
3 # This is an example of how to do a cross-build of OpenOCD using pkg-config.
4 # Cross-building with pkg-config is deceptively hard and most guides and
5 # tutorials are incomplete or give bad advice. Some of the traps that are easy
6 # to fall in but handled by this script are:
7 #
8 # * Polluting search paths and flags with values from the build system.
9 # * Faulty pkg-config wrappers shipped with distribution packaged cross-
10 # toolchains.
11 # * Build failing because pkg-config discards some paths even though they are
12 # correctly listed in the .pc file.
13 # * Getting successfully built binaries that cannot find runtime data because
14 # paths refer to the build file system.
15 #
16 # This script is probably more useful as a reference than as a complete build
17 # tool but for some configurations it may be usable as-is. It only cross-
18 # builds libusb-1.0 from source, but the script can be extended to build other
19 # prerequisities in a similar manner.
20 #
21 # Usage:
22 # export LIBUSB1_SRC=/path/to/libusb-1.0
23 # export OPENOCD_CONFIG="--enable-..."
24 # cd /work/dir
25 # /path/to/openocd/contrib/cross-build.sh <host-triplet>
26 #
27 # For static linking, a workaround is to
28 # export LIBUSB1_CONFIG="--enable-static --disable-shared"
29 #
30 # All the paths must not contain any spaces.
31
32 set -e -x
33
34 WORK_DIR=$PWD
35
36 ## Source code paths, customize as necessary
37 : ${OPENOCD_SRC:="`dirname "$0"`/.."}
38 : ${LIBUSB1_SRC:=/path/to/libusb}
39
40 OPENOCD_SRC=`readlink -m $OPENOCD_SRC`
41 LIBUSB1_SRC=`readlink -m $LIBUSB1_SRC`
42
43 HOST_TRIPLET=$1
44 BUILD_DIR=$WORK_DIR/$HOST_TRIPLET-build
45 LIBUSB1_BUILD_DIR=$BUILD_DIR/libusb1
46 OPENOCD_BUILD_DIR=$BUILD_DIR/openocd
47
48 ## Root of host file tree
49 SYSROOT=$WORK_DIR/$HOST_TRIPLET-root
50
51 ## Install location within host file tree
52 : ${PREFIX=/usr}
53
54 ## OpenOCD-only install dir for packaging
55 PACKAGE_DIR=$WORK_DIR/openocd_`git --git-dir=$OPENOCD_SRC/.git describe`_$HOST_TRIPLET
56
57 #######
58
59 # Create pkg-config wrapper and make sure it's used
60 export PKG_CONFIG=$WORK_DIR/$HOST_TRIPLET-pkg-config
61
62 cat > $PKG_CONFIG <<EOF
63 #!/bin/sh
64
65 SYSROOT=$SYSROOT
66
67 export PKG_CONFIG_DIR=
68 export PKG_CONFIG_LIBDIR=\${SYSROOT}$PREFIX/lib/pkgconfig:\${SYSROOT}$PREFIX/share/pkgconfig
69 export PKG_CONFIG_SYSROOT_DIR=\${SYSROOT}
70
71 # The following have to be set to avoid pkg-config to strip /usr/include and /usr/lib from paths
72 # before they are prepended with the sysroot path. Feels like a pkg-config bug.
73 export PKG_CONFIG_ALLOW_SYSTEM_CFLAGS=
74 export PKG_CONFIG_ALLOW_SYSTEM_LIBS=
75
76 exec pkg-config "\$@"
77 EOF
78 chmod +x $PKG_CONFIG
79
80 # Clear out work dir
81 rm -rf $SYSROOT $BUILD_DIR
82 mkdir -p $SYSROOT
83
84 # libusb-1.0 build & install into sysroot
85 mkdir -p $LIBUSB1_BUILD_DIR
86 cd $LIBUSB1_BUILD_DIR
87 $LIBUSB1_SRC/configure --build=`$LIBUSB1_SRC/config.guess` --host=$HOST_TRIPLET \
88 --with-sysroot=$SYSROOT --prefix=$PREFIX \
89 $LIBUSB1_CONFIG
90 make
91 make install DESTDIR=$SYSROOT
92
93 # OpenOCD build & install into sysroot
94 mkdir -p $OPENOCD_BUILD_DIR
95 cd $OPENOCD_BUILD_DIR
96 $OPENOCD_SRC/configure --build=`$OPENOCD_SRC/config.guess` --host=$HOST_TRIPLET \
97 --with-sysroot=$SYSROOT --prefix=$PREFIX \
98 $OPENOCD_CONFIG
99 make
100 make install DESTDIR=$SYSROOT
101
102 # Separate OpenOCD install w/o dependencies. OpenOCD will have to be linked
103 # statically or have dependencies packaged/installed separately.
104 make install DESTDIR=$PACKAGE_DIR