xhost + # Workaround for: QXcbConnection: Could not connect to display :0
[appimagekit.git] / build.sh
blobe567f27497f0c3eae4973e7dd7ecddf871861b41
1 #!/bin/bash
3 # This script installs the required build-time dependencies
4 # and builds AppImage
7 small_FLAGS="-Os -ffunction-sections -fdata-sections"
8 small_LDFLAGS="-s -Wl,--gc-sections"
9 CC="cc -O2 -Wall -Wno-deprecated-declarations -Wno-unused-result"
11 STRIP="strip"
12 INSTALL_DEPENDENCIES=1
13 STATIC_BUILD=1
14 JOBS=${JOBS:-1}
16 while [ $1 ]; do
17 case $1 in
18 '--debug' | '-d' )
19 STRIP="true"
21 '--no-dependencies' | '-n' )
22 INSTALL_DEPENDENCIES=0
24 '--use-shared-libs' | '-s' )
25 STATIC_BUILD=0
27 '--clean' | '-c' )
28 rm -rf build
29 git clean -df
30 rm -rf squashfuse/* squashfuse/.git
31 rm -rf squashfs-tools/* squashfs-tools/.git
32 exit
34 '--help' | '-h' )
35 echo 'Usage: ./build.sh [OPTIONS]'
36 echo
37 echo 'OPTIONS:'
38 echo ' -h, --help: Show this help screen'
39 echo ' -d, --debug: Build with debug info.'
40 echo ' -n, --no-dependencies: Do not try to install distro specific build dependencies.'
41 echo ' -s, --use-shared-libs: Use distro provided shared versions of inotify-tools and openssl.'
42 echo ' -c, --clean: Clean all artifacts generated by the build.'
43 exit
45 esac
47 shift
48 done
50 echo $KEY | md5sum
52 set -e
53 set -x
55 HERE="$(dirname "$(readlink -f "${0}")")"
56 cd "$HERE"
58 # Install dependencies if enabled
59 if [ $INSTALL_DEPENDENCIES -eq 1 ]; then
60 . ./install-build-deps.sh
63 # Fetch git submodules
64 git submodule init
65 git submodule update
67 # Clean up from previous run
68 rm -rf build/ || true
70 # Build static libraries
71 if [ $STATIC_BUILD -eq 1 ]; then
72 # Build inotify-tools
73 if [ ! -e "./inotify-tools-3.14/build/lib/libinotifytools.a" ] ; then
74 if [ ! -e "./inotify-tools-3.14/src" ] ; then
75 wget -c http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
76 tar xf inotify-tools-3.14.tar.gz
77 # Pull the latest `configure` scripts to handle newer platforms.
78 wget "http://git.savannah.gnu.org/gitweb/?p=config.git;a=blob_plain;f=config.guess;hb=HEAD" \
79 --output-document=inotify-tools-3.14/config.guess
80 wget "http://git.savannah.gnu.org/gitweb/?p=config.git;a=blob_plain;f=config.sub;hb=HEAD" \
81 --output-document=inotify-tools-3.14/config.sub
83 cd inotify-tools-3.14
84 mkdir -p build/lib
85 ./configure --prefix=$(pwd)/build --libdir=$(pwd)/build/lib --enable-shared --enable-static # disabling shared leads to linking errors?
86 make -j$JOBS
87 make install
88 cd -
91 # Build openssl
92 if [ ! -e "./openssl-1.1.0c/build/lib/libssl.a" ] ; then
93 wget -c https://www.openssl.org/source/openssl-1.1.0c.tar.gz
94 tar xf openssl-1.1.0c.tar.gz
95 cd openssl-1.1.0c
96 mkdir -p build/lib
97 ./config --prefix=$(pwd)/build no-shared
98 make -j$JOBS && make install PROCESS_PODS=''
99 cd -
103 # Build lzma always static because the runtime gets distributed with
104 # the generated .AppImage file.
105 if [ ! -e "./xz-5.2.3/build/lib/liblzma.a" ] ; then
106 wget -c http://tukaani.org/xz/xz-5.2.3.tar.gz
107 tar xf xz-5.2.3.tar.gz
108 cd xz-5.2.3
109 mkdir -p build/lib
110 CFLAGS="-Wall $small_FLAGS" ./configure --prefix=$(pwd)/build --libdir=$(pwd)/build/lib --enable-static --disable-shared
111 make -j$JOBS && make install
112 cd -
115 # Build libarchive
116 if [ $STATIC_BUILD -eq 1 ] && [ ! -e "./libarchive-3.3.1/.libs/libarchive.a" ] ; then
117 wget -c http://www.libarchive.org/downloads/libarchive-3.3.1.tar.gz
118 tar xf libarchive-3.3.1.tar.gz
119 cd libarchive-3.3.1
120 mkdir -p build/lib
121 ./configure --prefix=$PWD/build --libdir=$PWD/build/lib --disable-shared --enable-static \
122 --disable-bsdtar --disable-bsdcat --disable-bsdcpio --with-zlib \
123 --without-bz2lib --without-iconv --without-lz4 --without-lzma \
124 --without-lzo2 --without-nettle --without-openssl --without-xml2 --without-expat
125 make && make install
126 cd -
129 # Patch squashfuse_ll to be a library rather than an executable
131 cd squashfuse
132 if [ ! -e ./ll.c.orig ]; then
133 patch -p1 --backup < ../squashfuse.patch
134 patch -p1 --backup < ../squashfuse_dlopen.patch
136 if [ ! -e ./squashfuse_dlopen.c ]; then
137 cp ../squashfuse_dlopen.c .
139 if [ ! -e ./squashfuse_dlopen.h ]; then
140 cp ../squashfuse_dlopen.h .
143 # Build libsquashfuse_ll library
145 if [ ! -e ./Makefile ] ; then
146 export ACLOCAL_FLAGS="-I /usr/share/aclocal"
147 libtoolize --force
148 aclocal
149 autoheader
150 automake --force-missing --add-missing
151 autoreconf -fi || true # Errors out, but the following succeeds then?
152 autoconf
153 sed -i '/PKG_CHECK_MODULES.*/,/,:./d' configure # https://github.com/vasi/squashfuse/issues/12
154 CFLAGS="-Wall $small_FLAGS" ./configure --disable-demo --disable-high-level --without-lzo --without-lz4 --with-xz=$(pwd)/../xz-5.2.3/build
156 # Patch Makefile to use static lzma
157 sed -i "s|XZ_LIBS = -llzma -L$(pwd)/../xz-5.2.3/build/lib|XZ_LIBS = -Bstatic -llzma -L$(pwd)/../xz-5.2.3/build/lib|g" Makefile
160 bash --version
162 make -j$JOBS
164 cd ..
166 # Build mksquashfs with -offset option to skip n bytes
167 # https://github.com/plougher/squashfs-tools/pull/13
168 cd squashfs-tools/squashfs-tools
170 # Patch squashfuse-tools Makefile to link against static llzma
171 sed -i "s|CFLAGS += -DXZ_SUPPORT|CFLAGS += -DXZ_SUPPORT -I../../xz-5.2.3/build/include|g" Makefile
172 sed -i "s|LIBS += -llzma|LIBS += -Bstatic -llzma -L../../xz-5.2.3/build/lib|g" Makefile
174 make -j$JOBS XZ_SUPPORT=1 mksquashfs # LZ4_SUPPORT=1 did not build yet on CentOS 6
175 $STRIP mksquashfs
177 cd ../../
181 mkdir build
182 cd build
184 cp ../squashfs-tools/squashfs-tools/mksquashfs .
186 # Compile runtime but do not link
188 $CC -DVERSION_NUMBER=\"$(git describe --tags --always --abbrev=7)\" -I../squashfuse/ -D_FILE_OFFSET_BITS=64 -g $small_FLAGS -c ../runtime.c
190 # Prepare 1024 bytes of space for updateinformation
191 printf '\0%.0s' {0..1023} > 1024_blank_bytes
193 objcopy --add-section .upd_info=1024_blank_bytes \
194 --set-section-flags .upd_info=noload,readonly runtime.o runtime2.o
196 objcopy --add-section .sha256_sig=1024_blank_bytes \
197 --set-section-flags .sha256_sig=noload,readonly runtime2.o runtime3.o
199 # Now statically link against libsquashfuse_ll, libsquashfuse and liblzma
200 # and embed .upd_info and .sha256_sig sections
201 $CC $small_FLAGS $small_LDFLAGS -o runtime ../elf.c ../notify.c ../getsection.c runtime3.o \
202 ../squashfuse/.libs/libsquashfuse_ll.a ../squashfuse/.libs/libsquashfuse.a ../squashfuse/.libs/libfuseprivate.a \
203 -L../xz-5.2.3/build/lib -Wl,-Bdynamic -lpthread -lz -Wl,-Bstatic -llzma -Wl,-Bdynamic -ldl
204 $STRIP runtime
206 # Test if we can read it back
207 readelf -x .upd_info runtime # hexdump
208 readelf -p .upd_info runtime || true # string
210 # The raw updateinformation data can be read out manually like this:
211 HEXOFFSET=$(objdump -h runtime | grep .upd_info | awk '{print $6}')
212 HEXLENGTH=$(objdump -h runtime | grep .upd_info | awk '{print $3}')
213 dd bs=1 if=runtime skip=$(($(echo 0x$HEXOFFSET)+0)) count=$(($(echo 0x$HEXLENGTH)+0)) | xxd
215 # Insert AppImage magic bytes
217 printf '\x41\x49\x02' | dd of=runtime bs=1 seek=8 count=3 conv=notrunc
219 # Test if we can read it back
220 readelf -x .upd_info runtime # hexdump
221 readelf -p .upd_info runtime || true # string
223 # The raw updateinformation data can be read out manually like this:
224 HEXOFFSET=$(objdump -h runtime | grep .upd_info | awk '{print $6}')
225 HEXLENGTH=$(objdump -h runtime | grep .upd_info | awk '{print $3}')
226 dd bs=1 if=runtime skip=$(($(echo 0x$HEXOFFSET)+0)) count=$(($(echo 0x$HEXLENGTH)+0)) | xxd
228 # Convert runtime into a data object that can be embedded into appimagetool
230 ld -r -b binary -o data.o runtime
232 xxd runtime | head -n 1
234 # Compile appimagetool but do not link - glib version
236 $CC -DVERSION_NUMBER=\"$(git describe --tags --always --abbrev=7)\" -D_FILE_OFFSET_BITS=64 -I../squashfuse/ \
237 $(pkg-config --cflags glib-2.0) -g -Os ../getsection.c -c ../appimagetool.c
239 # Now statically link against libsquashfuse - glib version
240 if [ $STATIC_BUILD -eq 1 ]; then
241 # statically link against liblzma
242 $CC -o appimagetool data.o appimagetool.o ../elf.c ../getsection.c -DENABLE_BINRELOC ../binreloc.c \
243 ../squashfuse/.libs/libsquashfuse.a ../squashfuse/.libs/libfuseprivate.a \
244 -L../xz-5.2.3/build/lib \
245 -Wl,-Bdynamic -ldl -lpthread \
246 -Wl,--as-needed $(pkg-config --cflags --libs glib-2.0) -lz -Wl,-Bstatic -llzma -Wl,-Bdynamic
247 else
248 # dinamically link against distro provided liblzma
249 $CC -o appimagetool data.o appimagetool.o ../elf.c ../getsection.c -DENABLE_BINRELOC ../binreloc.c \
250 ../squashfuse/.libs/libsquashfuse.a ../squashfuse/.libs/libfuseprivate.a \
251 -Wl,-Bdynamic -ldl -lpthread \
252 -Wl,--as-needed $(pkg-config --cflags --libs glib-2.0) -lz -llzma
255 # Version without glib
256 # cc -D_FILE_OFFSET_BITS=64 -I ../squashfuse -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -g -Os -c ../appimagetoolnoglib.c
257 # cc data.o appimagetoolnoglib.o -DENABLE_BINRELOC ../binreloc.c ../squashfuse/.libs/libsquashfuse.a ../squashfuse/.libs/libfuseprivate.a -Wl,-Bdynamic -ldl -lpthread -lz -Wl,-Bstatic -llzma -Wl,-Bdynamic -o appimagetoolnoglib
259 # Compile and link digest tool
261 if [ $STATIC_BUILD -eq 1 ]; then
262 $CC -o digest ../getsection.c ../digest.c -I../openssl-1.1.0c/build/include -L../openssl-1.1.0c/build/lib \
263 -Wl,-Bstatic -lssl -lcrypto -Wl,-Bdynamic -lz -ldl
264 else
265 $CC -o digest ../getsection.c ../digest.c -Wl,-Bdynamic -lssl -lcrypto -lz -ldl
268 $STRIP digest
270 # Compile and link validate tool
272 if [ $STATIC_BUILD -eq 1 ]; then
273 $CC -o validate ../getsection.c ../validate.c -I../openssl-1.1.0c/build/include -L../openssl-1.1.0c/build/lib \
274 -Wl,-Bstatic -lssl -lcrypto -Wl,-Bdynamic -Wl,--as-needed $(pkg-config --cflags --libs glib-2.0) -lz -ldl
275 else
276 $CC -o validate ../getsection.c ../validate.c -Wl,-Bdynamic -lssl -lcrypto \
277 -Wl,--as-needed $(pkg-config --cflags --libs glib-2.0) -lz -ldl
280 $STRIP validate
282 # AppRun
283 $CC $small_FLAGS $small_LDFLAGS ../AppRun.c -o AppRun
285 # check for libarchive name
286 have_libarchive3=0
287 archive_n=
288 if printf "#include <archive3.h>\nint main(){return 0;}" | cc -w -O0 -xc - -Wl,--no-as-needed -larchive3 2>/dev/null ; then
289 have_libarchive3=1
290 archive_n=3
292 rm -f a.out
294 # appimaged, an optional component
295 if [ $STATIC_BUILD -eq 1 ]; then
296 $CC -std=gnu99 -o appimaged -I../squashfuse/ ../getsection.c ../notify.c ../elf.c ../appimaged.c \
297 -D_FILE_OFFSET_BITS=64 -DHAVE_LIBARCHIVE3=0 -DVERSION_NUMBER=\"$(git describe --tags --always --abbrev=7)\" \
298 ../squashfuse/.libs/libsquashfuse.a ../squashfuse/.libs/libfuseprivate.a \
299 -I../openssl-1.1.0c/build/include -L../openssl-1.1.0c/build/lib -Wl,-Bstatic -lssl -lcrypto \
300 -I../libarchive-3.3.1/libarchive ../libarchive-3.3.1/.libs/libarchive.a \
301 -L../xz-5.2.3/build/lib -I../inotify-tools-3.14/build/include -L../inotify-tools-3.14/build/lib \
302 -Wl,-Bstatic -linotifytools -Wl,-Bdynamic \
303 -Wl,--as-needed \
304 $(pkg-config --cflags --libs glib-2.0) \
305 $(pkg-config --cflags --libs gio-2.0) \
306 $(pkg-config --cflags --libs cairo) \
307 -ldl -lpthread -lz -Wl,-Bstatic -llzma -Wl,-Bdynamic
308 else
309 $CC -std=gnu99 -o appimaged -I../squashfuse/ ../getsection.c ../notify.c ../elf.c ../appimaged.c \
310 -D_FILE_OFFSET_BITS=64 -DHAVE_LIBARCHIVE3=$have_libarchive3 -DVERSION_NUMBER=\"$(git describe --tags --always --abbrev=7)\" \
311 ../squashfuse/.libs/libsquashfuse.a ../squashfuse/.libs/libfuseprivate.a \
312 -Wl,-Bdynamic -linotifytools -larchive${archive_n} \
313 -Wl,--as-needed \
314 $(pkg-config --cflags --libs glib-2.0) \
315 $(pkg-config --cflags --libs gio-2.0) \
316 $(pkg-config --cflags --libs cairo) \
317 -ldl -lpthread -lz -llzma
320 cd ..
322 # Strip and check size and dependencies
324 rm build/*.o build/1024_blank_bytes
325 $STRIP build/* 2>/dev/null
326 chmod a+x build/*
327 ls -lh build/*
328 for FILE in $(ls build/*) ; do
329 echo "$FILE"
330 ldd "$FILE" || true
331 done
333 bash -ex "$HERE/build-appdirs.sh"
335 ls -lh
337 mkdir -p out
338 cp -r build/* ./*.AppDir out/