Upstream a whole host of RISC-V changes.
[openocd.git] / contrib / cross-build.sh
blob8b31a3f00987180e64bd600e05b24496f27e015a
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-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.
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/libusb1}
40 : ${HIDAPI_SRC:=/path/to/hidapi}
41 : ${LIBFTDI_SRC:=/path/to/libftdi}
42 : ${CAPSTONE_SRC:=/path/to/capstone}
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`
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
58 ## Root of host file tree
59 SYSROOT=$WORK_DIR/$HOST_TRIPLET-root
61 ## Install location within host file tree
62 : ${PREFIX=/usr}
64 ## Make parallel jobs
65 : ${MAKE_JOBS:=1}
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}
71 #######
73 # Create pkg-config wrapper and make sure it's used
74 export PKG_CONFIG=$WORK_DIR/$HOST_TRIPLET-pkg-config
76 cat > $PKG_CONFIG <<EOF
77 #!/bin/sh
79 SYSROOT=$SYSROOT
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}
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=
90 exec pkg-config "\$@"
91 EOF
92 chmod +x $PKG_CONFIG
94 # Clear out work dir
95 rm -rf $SYSROOT $BUILD_DIR
96 mkdir -p $SYSROOT
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
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
120 # libftdi build & install into sysroot
121 if [ -d $LIBFTDI_SRC ] ; then
122 mkdir -p $LIBFTDI_BUILD_DIR
123 cd $LIBFTDI_BUILD_DIR
124 # libftdi requires libusb1 static libraries, granted by:
125 # export LIBUSB1_CONFIG="--enable-static ..."
126 cmake $LIBFTDI_CONFIG \
127 -DLIBUSB_INCLUDE_DIR=${SYSROOT}${PREFIX}/include/libusb-1.0 \
128 -DLIBUSB_LIBRARIES=${SYSROOT}${PREFIX}/lib/libusb-1.0.a \
129 -DCMAKE_INSTALL_PREFIX=${PREFIX} \
130 -DPKG_CONFIG_EXECUTABLE=`which pkg-config` \
131 $LIBFTDI_SRC
132 make install DESTDIR=$SYSROOT
135 # capstone build & install into sysroot
136 if [ -d $CAPSTONE_SRC ] ; then
137 mkdir -p $CAPSTONE_BUILD_DIR
138 cd $CAPSTONE_BUILD_DIR
139 cp -r $CAPSTONE_SRC/* .
140 make install DESTDIR=$SYSROOT PREFIX=$PREFIX \
141 CROSS="${HOST_TRIPLET}-" \
142 $CAPSTONE_CONFIG
143 # fix the generated capstone.pc
144 CAPSTONE_PC_FILE=${SYSROOT}${PREFIX}/lib/pkgconfig/capstone.pc
145 sed -i '/^libdir=/d' $CAPSTONE_PC_FILE
146 sed -i '/^includedir=/d' $CAPSTONE_PC_FILE
147 sed -i '/^archive=/d' $CAPSTONE_PC_FILE
148 sed -i '1s;^;prefix=/usr \
149 exec_prefix=${prefix} \
150 libdir=${exec_prefix}/lib \
151 includedir=${prefix}/include\n\n;' $CAPSTONE_PC_FILE
155 # OpenOCD build & install into sysroot
156 mkdir -p $OPENOCD_BUILD_DIR
157 cd $OPENOCD_BUILD_DIR
158 $OPENOCD_SRC/configure --build=`$OPENOCD_SRC/config.guess` --host=$HOST_TRIPLET \
159 --with-sysroot=$SYSROOT --prefix=$PREFIX \
160 $OPENOCD_CONFIG
161 make -j $MAKE_JOBS
162 make install-strip DESTDIR=$SYSROOT
164 # Separate OpenOCD install w/o dependencies. OpenOCD will have to be linked
165 # statically or have dependencies packaged/installed separately.
166 make install-strip DESTDIR=$PACKAGE_DIR