cmsis-dap: add initial cmsis-dap support
[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 HIDAPI_SRC=/path/to/hidapi
24 # export OPENOCD_CONFIG="--enable-..."
25 # cd /work/dir
26 # /path/to/openocd/contrib/cross-build.sh <host-triplet>
27 #
28 # For static linking, a workaround is to
29 # export LIBUSB1_CONFIG="--enable-static --disable-shared"
30 #
31 # All the paths must not contain any spaces.
32
33 set -e -x
34
35 WORK_DIR=$PWD
36
37 ## Source code paths, customize as necessary
38 : ${OPENOCD_SRC:="`dirname "$0"`/.."}
39 : ${LIBUSB1_SRC:=/path/to/libusb}
40 : ${HIDAPI_SRC:=/path/to/hidapi}
41
42 OPENOCD_SRC=`readlink -m $OPENOCD_SRC`
43 LIBUSB1_SRC=`readlink -m $LIBUSB1_SRC`
44 HIDAPI_SRC=`readlink -m $HIDAPI_SRC`
45
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
51
52 ## Root of host file tree
53 SYSROOT=$WORK_DIR/$HOST_TRIPLET-root
54
55 ## Install location within host file tree
56 : ${PREFIX=/usr}
57
58 ## OpenOCD-only install dir for packaging
59 PACKAGE_DIR=$WORK_DIR/openocd_`git --git-dir=$OPENOCD_SRC/.git describe`_$HOST_TRIPLET
60
61 #######
62
63 # Create pkg-config wrapper and make sure it's used
64 export PKG_CONFIG=$WORK_DIR/$HOST_TRIPLET-pkg-config
65
66 cat > $PKG_CONFIG <<EOF
67 #!/bin/sh
68
69 SYSROOT=$SYSROOT
70
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}
74
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=
79
80 exec pkg-config "\$@"
81 EOF
82 chmod +x $PKG_CONFIG
83
84 # Clear out work dir
85 rm -rf $SYSROOT $BUILD_DIR
86 mkdir -p $SYSROOT
87
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
96
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
106 fi
107
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
116
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