GitHub: add workflow to provide an openocd snapshot binaries for win32
[fw/openocd] / 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, hidapi and libftdi from source, but the script can be
19 # extended to build other prerequisites 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/libusb1}
40 : ${HIDAPI_SRC:=/path/to/hidapi}
41 : ${LIBFTDI_SRC:=/path/to/libftdi}
42
43 OPENOCD_SRC=`readlink -m $OPENOCD_SRC`
44 LIBUSB1_SRC=`readlink -m $LIBUSB1_SRC`
45 HIDAPI_SRC=`readlink -m $HIDAPI_SRC`
46 LIBFTDI_SRC=`readlink -m $LIBFTDI_SRC`
47
48 HOST_TRIPLET=$1
49 BUILD_DIR=$WORK_DIR/$HOST_TRIPLET-build
50 LIBUSB1_BUILD_DIR=$BUILD_DIR/libusb1
51 HIDAPI_BUILD_DIR=$BUILD_DIR/hidapi
52 LIBFTDI_BUILD_DIR=$BUILD_DIR/libftdi
53 OPENOCD_BUILD_DIR=$BUILD_DIR/openocd
54
55 ## Root of host file tree
56 SYSROOT=$WORK_DIR/$HOST_TRIPLET-root
57
58 ## Install location within host file tree
59 : ${PREFIX=/usr}
60
61 ## Make parallel jobs
62 : ${MAKE_JOBS:=1}
63
64 ## OpenOCD-only install dir for packaging
65 : ${OPENOCD_TAG:=`git --git-dir=$OPENOCD_SRC/.git describe --tags`}
66 PACKAGE_DIR=$WORK_DIR/openocd_${OPENOCD_TAG}_${HOST_TRIPLET}
67
68 #######
69
70 # Create pkg-config wrapper and make sure it's used
71 export PKG_CONFIG=$WORK_DIR/$HOST_TRIPLET-pkg-config
72
73 cat > $PKG_CONFIG <<EOF
74 #!/bin/sh
75
76 SYSROOT=$SYSROOT
77
78 export PKG_CONFIG_DIR=
79 export PKG_CONFIG_LIBDIR=\${SYSROOT}$PREFIX/lib/pkgconfig:\${SYSROOT}$PREFIX/share/pkgconfig
80 export PKG_CONFIG_SYSROOT_DIR=\${SYSROOT}
81
82 # The following have to be set to avoid pkg-config to strip /usr/include and /usr/lib from paths
83 # before they are prepended with the sysroot path. Feels like a pkg-config bug.
84 export PKG_CONFIG_ALLOW_SYSTEM_CFLAGS=
85 export PKG_CONFIG_ALLOW_SYSTEM_LIBS=
86
87 exec pkg-config "\$@"
88 EOF
89 chmod +x $PKG_CONFIG
90
91 # Clear out work dir
92 rm -rf $SYSROOT $BUILD_DIR
93 mkdir -p $SYSROOT
94
95 # libusb-1.0 build & install into sysroot
96 if [ -d $LIBUSB1_SRC ] ; then
97   mkdir -p $LIBUSB1_BUILD_DIR
98   cd $LIBUSB1_BUILD_DIR
99   $LIBUSB1_SRC/configure --build=`$LIBUSB1_SRC/config.guess` --host=$HOST_TRIPLET \
100   --with-sysroot=$SYSROOT --prefix=$PREFIX \
101   $LIBUSB1_CONFIG
102   make -j $MAKE_JOBS
103   make install DESTDIR=$SYSROOT
104 fi
105
106 # hidapi build & install into sysroot
107 if [ -d $HIDAPI_SRC ] ; then
108   mkdir -p $HIDAPI_BUILD_DIR
109   cd $HIDAPI_BUILD_DIR
110   $HIDAPI_SRC/configure --build=`$HIDAPI_SRC/config.guess` --host=$HOST_TRIPLET \
111     --with-sysroot=$SYSROOT --prefix=$PREFIX \
112     $HIDAPI_CONFIG
113   make -j $MAKE_JOBS
114   make install DESTDIR=$SYSROOT
115 fi
116
117 # libftdi build & install into sysroot
118 if [ -d $LIBFTDI_SRC ] ; then
119   mkdir -p $LIBFTDI_BUILD_DIR
120   cd $LIBFTDI_BUILD_DIR
121   # libftdi requires libusb1 static libraries, granted by:
122   # export LIBUSB1_CONFIG="--enable-static ..."
123   cmake $LIBFTDI_CONFIG \
124     -DLIBUSB_INCLUDE_DIR=${SYSROOT}${PREFIX}/include/libusb-1.0 \
125     -DLIBUSB_LIBRARIES=${SYSROOT}${PREFIX}/lib/libusb-1.0.a \
126     -DCMAKE_INSTALL_PREFIX=${PREFIX} \
127     -DPKG_CONFIG_EXECUTABLE=`which pkg-config` \
128     $LIBFTDI_SRC
129   make install DESTDIR=$SYSROOT
130 fi
131
132 # OpenOCD build & install into sysroot
133 mkdir -p $OPENOCD_BUILD_DIR
134 cd $OPENOCD_BUILD_DIR
135 $OPENOCD_SRC/configure --build=`$OPENOCD_SRC/config.guess` --host=$HOST_TRIPLET \
136 --with-sysroot=$SYSROOT --prefix=$PREFIX \
137 $OPENOCD_CONFIG
138 make -j $MAKE_JOBS
139 make install-strip DESTDIR=$SYSROOT
140
141 # Separate OpenOCD install w/o dependencies. OpenOCD will have to be linked
142 # statically or have dependencies packaged/installed separately.
143 make install-strip DESTDIR=$PACKAGE_DIR
144