Clarify: Oldest still-supported Ubuntu LTS release
[appimagekit/gsi.git] / build.sh
blobd76ee14bad81ae9be123ec97345f7b41855b1c16
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
233 mv runtime runtime_with_magic
235 # Compile appimagetool but do not link - glib version
237 $CC -DVERSION_NUMBER=\"$(git describe --tags --always --abbrev=7)\" -D_FILE_OFFSET_BITS=64 -I../squashfuse/ \
238 $(pkg-config --cflags glib-2.0) -g -Os ../getsection.c -c ../appimagetool.c
240 # Now statically link against libsquashfuse - glib version
241 if [ $STATIC_BUILD -eq 1 ]; then
242 # statically link against liblzma
243 $CC -o appimagetool data.o appimagetool.o ../elf.c ../getsection.c -DENABLE_BINRELOC ../binreloc.c \
244 ../squashfuse/.libs/libsquashfuse.a ../squashfuse/.libs/libfuseprivate.a \
245 -L../xz-5.2.3/build/lib \
246 -Wl,-Bdynamic -ldl -lpthread \
247 -Wl,--as-needed $(pkg-config --cflags --libs glib-2.0) -lz -Wl,-Bstatic -llzma -Wl,-Bdynamic
248 else
249 # dinamically link against distro provided liblzma
250 $CC -o appimagetool data.o appimagetool.o ../elf.c ../getsection.c -DENABLE_BINRELOC ../binreloc.c \
251 ../squashfuse/.libs/libsquashfuse.a ../squashfuse/.libs/libfuseprivate.a \
252 -Wl,-Bdynamic -ldl -lpthread \
253 -Wl,--as-needed $(pkg-config --cflags --libs glib-2.0) -lz -llzma
256 # Version without glib
257 # cc -D_FILE_OFFSET_BITS=64 -I ../squashfuse -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -g -Os -c ../appimagetoolnoglib.c
258 # 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
260 # Compile and link digest tool
262 if [ $STATIC_BUILD -eq 1 ]; then
263 $CC -o digest ../getsection.c ../digest.c -I../openssl-1.1.0c/build/include -L../openssl-1.1.0c/build/lib \
264 -Wl,-Bstatic -lssl -lcrypto -Wl,-Bdynamic -lz -ldl
265 else
266 $CC -o digest ../getsection.c ../digest.c -Wl,-Bdynamic -lssl -lcrypto -lz -ldl
269 $STRIP digest
271 # Compile and link validate tool
273 if [ $STATIC_BUILD -eq 1 ]; then
274 $CC -o validate ../getsection.c ../validate.c -I../openssl-1.1.0c/build/include -L../openssl-1.1.0c/build/lib \
275 -Wl,-Bstatic -lssl -lcrypto -Wl,-Bdynamic -Wl,--as-needed $(pkg-config --cflags --libs glib-2.0) -lz -ldl
276 else
277 $CC -o validate ../getsection.c ../validate.c -Wl,-Bdynamic -lssl -lcrypto \
278 -Wl,--as-needed $(pkg-config --cflags --libs glib-2.0) -lz -ldl
281 $STRIP validate
283 # AppRun
284 $CC $small_FLAGS $small_LDFLAGS ../AppRun.c -o AppRun
286 # check for libarchive name
287 have_libarchive3=0
288 archive_n=
289 if printf "#include <archive3.h>\nint main(){return 0;}" | cc -w -O0 -xc - -Wl,--no-as-needed -larchive3 2>/dev/null ; then
290 have_libarchive3=1
291 archive_n=3
293 rm -f a.out
295 # appimaged, an optional component
296 if [ $STATIC_BUILD -eq 1 ]; then
297 $CC -std=gnu99 -o appimaged -I../squashfuse/ ../getsection.c ../notify.c ../elf.c ../appimaged.c \
298 -D_FILE_OFFSET_BITS=64 -DHAVE_LIBARCHIVE3=0 -DVERSION_NUMBER=\"$(git describe --tags --always --abbrev=7)\" \
299 ../squashfuse/.libs/libsquashfuse.a ../squashfuse/.libs/libfuseprivate.a \
300 -I../openssl-1.1.0c/build/include -L../openssl-1.1.0c/build/lib -Wl,-Bstatic -lssl -lcrypto \
301 -I../libarchive-3.3.1/libarchive ../libarchive-3.3.1/.libs/libarchive.a \
302 -L../xz-5.2.3/build/lib -I../inotify-tools-3.14/build/include -L../inotify-tools-3.14/build/lib \
303 -Wl,-Bstatic -linotifytools -Wl,-Bdynamic \
304 -Wl,--as-needed \
305 $(pkg-config --cflags --libs glib-2.0) \
306 $(pkg-config --cflags --libs gio-2.0) \
307 $(pkg-config --cflags --libs cairo) \
308 -ldl -lpthread -lz -Wl,-Bstatic -llzma -Wl,-Bdynamic
309 else
310 $CC -std=gnu99 -o appimaged -I../squashfuse/ ../getsection.c ../notify.c ../elf.c ../appimaged.c \
311 -D_FILE_OFFSET_BITS=64 -DHAVE_LIBARCHIVE3=$have_libarchive3 -DVERSION_NUMBER=\"$(git describe --tags --always --abbrev=7)\" \
312 ../squashfuse/.libs/libsquashfuse.a ../squashfuse/.libs/libfuseprivate.a \
313 -Wl,-Bdynamic -linotifytools -larchive${archive_n} \
314 -Wl,--as-needed \
315 $(pkg-config --cflags --libs glib-2.0) \
316 $(pkg-config --cflags --libs gio-2.0) \
317 $(pkg-config --cflags --libs cairo) \
318 -ldl -lpthread -lz -llzma
321 cd ..
323 ## Lib AppImage optional
325 mkdir -p libappimage/build
326 pushd libappimage/build
327 cmake .. && make all && ctest
328 popd
330 # Strip and check size and dependencies
332 rm build/*.o build/1024_blank_bytes
333 $STRIP build/AppRun build/appimaged build/appimagetool build/digest build/mksquashfs build/validate 2>/dev/null # Do NOT strip build/runtime_with_magic
334 chmod a+x build/*
335 ls -lh build/*
336 for FILE in $(ls build/*) ; do
337 echo "$FILE"
338 ldd "$FILE" || true
339 done
341 bash -ex "$HERE/build-appdirs.sh"
343 ls -lh
345 mkdir -p out
346 cp -r build/* ./*.AppDir out/