contrib: add GPL license tag on files that miss it
[fw/openocd] / contrib / cross-build.sh
1 #!/bin/sh
2 # SPDX-License-Identifier: GPL-2.0-or-later
3
4 # This is an example of how to do a cross-build of OpenOCD using pkg-config.
5 # Cross-building with pkg-config is deceptively hard and most guides and
6 # tutorials are incomplete or give bad advice. Some of the traps that are easy
7 # to fall in but handled by this script are:
8 #
9 #  * Polluting search paths and flags with values from the build system.
10 #  * Faulty pkg-config wrappers shipped with distribution packaged cross-
11 #    toolchains.
12 #  * Build failing because pkg-config discards some paths even though they are
13 #    correctly listed in the .pc file.
14 #  * Getting successfully built binaries that cannot find runtime data because
15 #    paths refer to the build file system.
16 #
17 # This script is probably more useful as a reference than as a complete build
18 # tool but for some configurations it may be usable as-is. It only cross-builds
19 # libusb-1.0, hidapi, libftdi and capstone from source, but the script can be
20 # extended to build other prerequisites in a similar manner.
21 #
22 # Usage:
23 # export LIBUSB1_SRC=/path/to/libusb-1.0
24 # export HIDAPI_SRC=/path/to/hidapi
25 # export OPENOCD_CONFIG="--enable-..."
26 # cd /work/dir
27 # /path/to/openocd/contrib/cross-build.sh <host-triplet>
28 #
29 # For static linking, a workaround is to
30 # export LIBUSB1_CONFIG="--enable-static --disable-shared"
31 #
32 # All the paths must not contain any spaces.
33
34 set -e -x
35
36 WORK_DIR=$PWD
37
38 ## Source code paths, customize as necessary
39 : ${OPENOCD_SRC:="`dirname "$0"`/.."}
40 : ${LIBUSB1_SRC:=/path/to/libusb1}
41 : ${HIDAPI_SRC:=/path/to/hidapi}
42 : ${LIBFTDI_SRC:=/path/to/libftdi}
43 : ${CAPSTONE_SRC:=/path/to/capstone}
44
45 OPENOCD_SRC=`readlink -m $OPENOCD_SRC`
46 LIBUSB1_SRC=`readlink -m $LIBUSB1_SRC`
47 HIDAPI_SRC=`readlink -m $HIDAPI_SRC`
48 LIBFTDI_SRC=`readlink -m $LIBFTDI_SRC`
49 CAPSTONE_SRC=`readlink -m $CAPSTONE_SRC`
50
51 HOST_TRIPLET=$1
52 BUILD_DIR=$WORK_DIR/$HOST_TRIPLET-build
53 LIBUSB1_BUILD_DIR=$BUILD_DIR/libusb1
54 HIDAPI_BUILD_DIR=$BUILD_DIR/hidapi
55 LIBFTDI_BUILD_DIR=$BUILD_DIR/libftdi
56 CAPSTONE_BUILD_DIR=$BUILD_DIR/capstone
57 OPENOCD_BUILD_DIR=$BUILD_DIR/openocd
58
59 ## Root of host file tree
60 SYSROOT=$WORK_DIR/$HOST_TRIPLET-root
61
62 ## Install location within host file tree
63 : ${PREFIX=/usr}
64
65 ## Make parallel jobs
66 : ${MAKE_JOBS:=1}
67
68 ## OpenOCD-only install dir for packaging
69 : ${OPENOCD_TAG:=`git --git-dir=$OPENOCD_SRC/.git describe --tags`}
70 PACKAGE_DIR=$WORK_DIR/openocd_${OPENOCD_TAG}_${HOST_TRIPLET}
71
72 #######
73
74 # Create pkg-config wrapper and make sure it's used
75 export PKG_CONFIG=$WORK_DIR/$HOST_TRIPLET-pkg-config
76
77 cat > $PKG_CONFIG <<EOF
78 #!/bin/sh
79
80 SYSROOT=$SYSROOT
81
82 export PKG_CONFIG_DIR=
83 export PKG_CONFIG_LIBDIR=\${SYSROOT}$PREFIX/lib/pkgconfig:\${SYSROOT}$PREFIX/share/pkgconfig
84 export PKG_CONFIG_SYSROOT_DIR=\${SYSROOT}
85
86 # The following have to be set to avoid pkg-config to strip /usr/include and /usr/lib from paths
87 # before they are prepended with the sysroot path. Feels like a pkg-config bug.
88 export PKG_CONFIG_ALLOW_SYSTEM_CFLAGS=
89 export PKG_CONFIG_ALLOW_SYSTEM_LIBS=
90
91 exec pkg-config "\$@"
92 EOF
93 chmod +x $PKG_CONFIG
94
95 # Clear out work dir
96 rm -rf $SYSROOT $BUILD_DIR
97 mkdir -p $SYSROOT
98
99 # libusb-1.0 build & install into sysroot
100 if [ -d $LIBUSB1_SRC ] ; then
101   mkdir -p $LIBUSB1_BUILD_DIR
102   cd $LIBUSB1_BUILD_DIR
103   $LIBUSB1_SRC/configure --build=`$LIBUSB1_SRC/config.guess` --host=$HOST_TRIPLET \
104   --with-sysroot=$SYSROOT --prefix=$PREFIX \
105   $LIBUSB1_CONFIG
106   make -j $MAKE_JOBS
107   make install DESTDIR=$SYSROOT
108 fi
109
110 # hidapi build & install into sysroot
111 if [ -d $HIDAPI_SRC ] ; then
112   mkdir -p $HIDAPI_BUILD_DIR
113   cd $HIDAPI_BUILD_DIR
114   $HIDAPI_SRC/configure --build=`$HIDAPI_SRC/config.guess` --host=$HOST_TRIPLET \
115     --with-sysroot=$SYSROOT --prefix=$PREFIX \
116     $HIDAPI_CONFIG
117   make -j $MAKE_JOBS
118   make install DESTDIR=$SYSROOT
119 fi
120
121 # libftdi build & install into sysroot
122 if [ -d $LIBFTDI_SRC ] ; then
123   mkdir -p $LIBFTDI_BUILD_DIR
124   cd $LIBFTDI_BUILD_DIR
125   # note : libftdi versions < 1.5 requires libusb1 static
126   #   hint use : # export LIBUSB1_CONFIG="--enable-static ..."
127   #   not needed since libftdi-1.5 when LIBFTDI_CONFIG="-DSTATICLIBS=OFF ..."
128
129   # fix <toolchain>.cmake file
130   ESCAPED_SYSROOT=$(printf '%s\n' "$SYSROOT" | sed -e 's/[\/&]/\\&/g')
131   sed -i -E "s/(SET\(CMAKE_FIND_ROOT_PATH\s+).+\)/\1${ESCAPED_SYSROOT})/" \
132     ${LIBFTDI_SRC}/cmake/Toolchain-${HOST_TRIPLET}.cmake
133
134   cmake $LIBFTDI_CONFIG \
135     -DCMAKE_TOOLCHAIN_FILE=${LIBFTDI_SRC}/cmake/Toolchain-${HOST_TRIPLET}.cmake \
136     -DCMAKE_INSTALL_PREFIX=${PREFIX} \
137     -DPKG_CONFIG_EXECUTABLE=`which pkg-config` \
138     $LIBFTDI_SRC
139   make install DESTDIR=$SYSROOT
140 fi
141
142 # capstone build & install into sysroot
143 if [ -d $CAPSTONE_SRC ] ; then
144   mkdir -p $CAPSTONE_BUILD_DIR
145   cd $CAPSTONE_BUILD_DIR
146   cp -r $CAPSTONE_SRC/* .
147   make install DESTDIR=$SYSROOT PREFIX=$PREFIX \
148     CROSS="${HOST_TRIPLET}-" \
149     $CAPSTONE_CONFIG
150   # fix the generated capstone.pc
151   CAPSTONE_PC_FILE=${SYSROOT}${PREFIX}/lib/pkgconfig/capstone.pc
152   sed -i '/^libdir=/d' $CAPSTONE_PC_FILE
153   sed -i '/^includedir=/d' $CAPSTONE_PC_FILE
154   sed -i '/^archive=/d' $CAPSTONE_PC_FILE
155   sed -i '1s;^;prefix=/usr \
156 exec_prefix=${prefix} \
157 libdir=${exec_prefix}/lib \
158 includedir=${prefix}/include/capstone\n\n;' $CAPSTONE_PC_FILE
159 fi
160
161
162 # OpenOCD build & install into sysroot
163 mkdir -p $OPENOCD_BUILD_DIR
164 cd $OPENOCD_BUILD_DIR
165 $OPENOCD_SRC/configure --build=`$OPENOCD_SRC/config.guess` --host=$HOST_TRIPLET \
166 --with-sysroot=$SYSROOT --prefix=$PREFIX \
167 $OPENOCD_CONFIG
168 make -j $MAKE_JOBS
169 make install-strip DESTDIR=$SYSROOT
170
171 # Separate OpenOCD install w/o dependencies. OpenOCD will have to be linked
172 # statically or have dependencies packaged/installed separately.
173 make install-strip DESTDIR=$PACKAGE_DIR
174