cortex_m: Check return value from cortex_m_dcc_read.
[openocd.git] / contrib / cross-build.sh
blob74ab0f4f56b5b3b300933302644d98bb79c045d6
1 #!/bin/sh
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:
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.
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.
21 # Usage:
22 # export LIBUSB1_SRC=/path/to/libusb-1.0
23 # export HIDAPI_SRC=/path/to/hidapi
24 # export OPENOCD_CONFIG="--enable-..."
25 # cd /work/dir
26 # /path/to/openocd/contrib/cross-build.sh <host-triplet>
28 # For static linking, a workaround is to
29 # export LIBUSB1_CONFIG="--enable-static --disable-shared"
31 # All the paths must not contain any spaces.
33 set -e -x
35 WORK_DIR=$PWD
37 ## Source code paths, customize as necessary
38 : ${OPENOCD_SRC:="`dirname "$0"`/.."}
39 : ${LIBUSB1_SRC:=/path/to/libusb}
40 : ${HIDAPI_SRC:=/path/to/hidapi}
42 OPENOCD_SRC=`readlink -m $OPENOCD_SRC`
43 LIBUSB1_SRC=`readlink -m $LIBUSB1_SRC`
44 HIDAPI_SRC=`readlink -m $HIDAPI_SRC`
46 HOST_TRIPLET=$1
47 BUILD_DIR=$WORK_DIR/$HOST_TRIPLET-build
48 LIBUSB1_BUILD_DIR=$BUILD_DIR/libusb1
49 HIDAPI_BUILD_DIR=$BUILD_DIR/hidapi
50 OPENOCD_BUILD_DIR=$BUILD_DIR/openocd
52 ## Root of host file tree
53 SYSROOT=$WORK_DIR/$HOST_TRIPLET-root
55 ## Install location within host file tree
56 : ${PREFIX=/usr}
58 ## OpenOCD-only install dir for packaging
59 PACKAGE_DIR=$WORK_DIR/openocd_`git --git-dir=$OPENOCD_SRC/.git describe`_$HOST_TRIPLET
61 #######
63 # Create pkg-config wrapper and make sure it's used
64 export PKG_CONFIG=$WORK_DIR/$HOST_TRIPLET-pkg-config
66 cat > $PKG_CONFIG <<EOF
67 #!/bin/sh
69 SYSROOT=$SYSROOT
71 export PKG_CONFIG_DIR=
72 export PKG_CONFIG_LIBDIR=\${SYSROOT}$PREFIX/lib/pkgconfig:\${SYSROOT}$PREFIX/share/pkgconfig
73 export PKG_CONFIG_SYSROOT_DIR=\${SYSROOT}
75 # The following have to be set to avoid pkg-config to strip /usr/include and /usr/lib from paths
76 # before they are prepended with the sysroot path. Feels like a pkg-config bug.
77 export PKG_CONFIG_ALLOW_SYSTEM_CFLAGS=
78 export PKG_CONFIG_ALLOW_SYSTEM_LIBS=
80 exec pkg-config "\$@"
81 EOF
82 chmod +x $PKG_CONFIG
84 # Clear out work dir
85 rm -rf $SYSROOT $BUILD_DIR
86 mkdir -p $SYSROOT
88 # libusb-1.0 build & install into sysroot
89 mkdir -p $LIBUSB1_BUILD_DIR
90 cd $LIBUSB1_BUILD_DIR
91 $LIBUSB1_SRC/configure --build=`$LIBUSB1_SRC/config.guess` --host=$HOST_TRIPLET \
92 --with-sysroot=$SYSROOT --prefix=$PREFIX \
93 $LIBUSB1_CONFIG
94 make
95 make install DESTDIR=$SYSROOT
97 # hidapi build & install into sysroot
98 if [ -d $HIDAPI_SRC ] ; then
99 mkdir -p $HIDAPI_BUILD_DIR
100 cd $HIDAPI_BUILD_DIR
101 $HIDAPI_SRC/configure --build=`$HIDAPI_SRC/config.guess` --host=$HOST_TRIPLET \
102 --with-sysroot=$SYSROOT --prefix=$PREFIX \
103 $HIDAPI_CONFIG
104 make
105 make install DESTDIR=$SYSROOT
108 # OpenOCD build & install into sysroot
109 mkdir -p $OPENOCD_BUILD_DIR
110 cd $OPENOCD_BUILD_DIR
111 $OPENOCD_SRC/configure --build=`$OPENOCD_SRC/config.guess` --host=$HOST_TRIPLET \
112 --with-sysroot=$SYSROOT --prefix=$PREFIX \
113 $OPENOCD_CONFIG
114 make
115 make install DESTDIR=$SYSROOT
117 # Separate OpenOCD install w/o dependencies. OpenOCD will have to be linked
118 # statically or have dependencies packaged/installed separately.
119 make install DESTDIR=$PACKAGE_DIR