9328c3ab649bbe0c1285b641c4609a6515c9adc6
[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-builds
18 # libusb-1.0, hidapi, libftdi and capstone 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 : ${CAPSTONE_SRC:=/path/to/capstone}
43
44 OPENOCD_SRC=`readlink -m $OPENOCD_SRC`
45 LIBUSB1_SRC=`readlink -m $LIBUSB1_SRC`
46 HIDAPI_SRC=`readlink -m $HIDAPI_SRC`
47 LIBFTDI_SRC=`readlink -m $LIBFTDI_SRC`
48 CAPSTONE_SRC=`readlink -m $CAPSTONE_SRC`
49
50 HOST_TRIPLET=$1
51 BUILD_DIR=$WORK_DIR/$HOST_TRIPLET-build
52 LIBUSB1_BUILD_DIR=$BUILD_DIR/libusb1
53 HIDAPI_BUILD_DIR=$BUILD_DIR/hidapi
54 LIBFTDI_BUILD_DIR=$BUILD_DIR/libftdi
55 CAPSTONE_BUILD_DIR=$BUILD_DIR/capstone
56 OPENOCD_BUILD_DIR=$BUILD_DIR/openocd
57
58 ## Root of host file tree
59 SYSROOT=$WORK_DIR/$HOST_TRIPLET-root
60
61 ## Install location within host file tree
62 : ${PREFIX=/usr}
63
64 ## Make parallel jobs
65 : ${MAKE_JOBS:=1}
66
67 ## OpenOCD-only install dir for packaging
68 : ${OPENOCD_TAG:=`git --git-dir=$OPENOCD_SRC/.git describe --tags`}
69 PACKAGE_DIR=$WORK_DIR/openocd_${OPENOCD_TAG}_${HOST_TRIPLET}
70
71 #######
72
73 # Create pkg-config wrapper and make sure it's used
74 export PKG_CONFIG=$WORK_DIR/$HOST_TRIPLET-pkg-config
75
76 cat > $PKG_CONFIG <<EOF
77 #!/bin/sh
78
79 SYSROOT=$SYSROOT
80
81 export PKG_CONFIG_DIR=
82 export PKG_CONFIG_LIBDIR=\${SYSROOT}$PREFIX/lib/pkgconfig:\${SYSROOT}$PREFIX/share/pkgconfig
83 export PKG_CONFIG_SYSROOT_DIR=\${SYSROOT}
84
85 # The following have to be set to avoid pkg-config to strip /usr/include and /usr/lib from paths
86 # before they are prepended with the sysroot path. Feels like a pkg-config bug.
87 export PKG_CONFIG_ALLOW_SYSTEM_CFLAGS=
88 export PKG_CONFIG_ALLOW_SYSTEM_LIBS=
89
90 exec pkg-config "\$@"
91 EOF
92 chmod +x $PKG_CONFIG
93
94 # Clear out work dir
95 rm -rf $SYSROOT $BUILD_DIR
96 mkdir -p $SYSROOT
97
98 # libusb-1.0 build & install into sysroot
99 if [ -d $LIBUSB1_SRC ] ; then
100   mkdir -p $LIBUSB1_BUILD_DIR
101   cd $LIBUSB1_BUILD_DIR
102   $LIBUSB1_SRC/configure --build=`$LIBUSB1_SRC/config.guess` --host=$HOST_TRIPLET \
103   --with-sysroot=$SYSROOT --prefix=$PREFIX \
104   $LIBUSB1_CONFIG
105   make -j $MAKE_JOBS
106   make install DESTDIR=$SYSROOT
107 fi
108
109 # hidapi build & install into sysroot
110 if [ -d $HIDAPI_SRC ] ; then
111   mkdir -p $HIDAPI_BUILD_DIR
112   cd $HIDAPI_BUILD_DIR
113   $HIDAPI_SRC/configure --build=`$HIDAPI_SRC/config.guess` --host=$HOST_TRIPLET \
114     --with-sysroot=$SYSROOT --prefix=$PREFIX \
115     $HIDAPI_CONFIG
116   make -j $MAKE_JOBS
117   make install DESTDIR=$SYSROOT
118 fi
119
120 # libftdi build & install into sysroot
121 if [ -d $LIBFTDI_SRC ] ; then
122   mkdir -p $LIBFTDI_BUILD_DIR
123   cd $LIBFTDI_BUILD_DIR
124   # note : libftdi versions < 1.5 requires libusb1 static
125   #   hint use : # export LIBUSB1_CONFIG="--enable-static ..."
126   #   not needed since libftdi-1.5 when LIBFTDI_CONFIG="-DSTATICLIBS=OFF ..."
127
128   # fix <toolchain>.cmake file
129   ESCAPED_SYSROOT=$(printf '%s\n' "$SYSROOT" | sed -e 's/[\/&]/\\&/g')
130   sed -i -E "s/(SET\(CMAKE_FIND_ROOT_PATH\s+).+\)/\1${ESCAPED_SYSROOT})/" \
131     ${LIBFTDI_SRC}/cmake/Toolchain-${HOST_TRIPLET}.cmake
132
133   cmake $LIBFTDI_CONFIG \
134     -DCMAKE_TOOLCHAIN_FILE=${LIBFTDI_SRC}/cmake/Toolchain-${HOST_TRIPLET}.cmake \
135     -DCMAKE_INSTALL_PREFIX=${PREFIX} \
136     -DPKG_CONFIG_EXECUTABLE=`which pkg-config` \
137     $LIBFTDI_SRC
138   make install DESTDIR=$SYSROOT
139 fi
140
141 # capstone build & install into sysroot
142 if [ -d $CAPSTONE_SRC ] ; then
143   mkdir -p $CAPSTONE_BUILD_DIR
144   cd $CAPSTONE_BUILD_DIR
145   cp -r $CAPSTONE_SRC/* .
146   make install DESTDIR=$SYSROOT PREFIX=$PREFIX \
147     CROSS="${HOST_TRIPLET}-" \
148     $CAPSTONE_CONFIG
149   # fix the generated capstone.pc
150   CAPSTONE_PC_FILE=${SYSROOT}${PREFIX}/lib/pkgconfig/capstone.pc
151   sed -i '/^libdir=/d' $CAPSTONE_PC_FILE
152   sed -i '/^includedir=/d' $CAPSTONE_PC_FILE
153   sed -i '/^archive=/d' $CAPSTONE_PC_FILE
154   sed -i '1s;^;prefix=/usr \
155 exec_prefix=${prefix} \
156 libdir=${exec_prefix}/lib \
157 includedir=${prefix}/include/capstone\n\n;' $CAPSTONE_PC_FILE
158 fi
159
160
161 # OpenOCD build & install into sysroot
162 mkdir -p $OPENOCD_BUILD_DIR
163 cd $OPENOCD_BUILD_DIR
164 $OPENOCD_SRC/configure --build=`$OPENOCD_SRC/config.guess` --host=$HOST_TRIPLET \
165 --with-sysroot=$SYSROOT --prefix=$PREFIX \
166 $OPENOCD_CONFIG
167 make -j $MAKE_JOBS
168 make install-strip DESTDIR=$SYSROOT
169
170 # Separate OpenOCD install w/o dependencies. OpenOCD will have to be linked
171 # statically or have dependencies packaged/installed separately.
172 make install-strip DESTDIR=$PACKAGE_DIR
173