Update appimagetool.c
[appimagekit/gsi.git] / build.sh
blob04e25a042d39b4efcd19c04765550710e6cd9d4a
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 # Convert runtime into a data object that can be embedded into appimagetool
221 ld -r -b binary -o data.o runtime
223 # Test if we can read it back
224 readelf -x .upd_info runtime # hexdump
225 readelf -p .upd_info runtime || true # string
227 # The raw updateinformation data can be read out manually like this:
228 HEXOFFSET=$(objdump -h runtime | grep .upd_info | awk '{print $6}')
229 HEXLENGTH=$(objdump -h runtime | grep .upd_info | awk '{print $3}')
230 dd bs=1 if=runtime skip=$(($(echo 0x$HEXOFFSET)+0)) count=$(($(echo 0x$HEXLENGTH)+0)) | xxd
232 # Convert runtime into a data object that can be embedded into appimagetool
234 ld -r -b binary -o data.o runtime
236 # Compile appimagetool but do not link - glib version
238 $CC -DVERSION_NUMBER=\"$(git describe --tags --always --abbrev=7)\" -D_FILE_OFFSET_BITS=64 -I../squashfuse/ \
239 $(pkg-config --cflags glib-2.0) -g -Os ../getsection.c -c ../appimagetool.c
241 # Now statically link against libsquashfuse - glib version
242 if [ $STATIC_BUILD -eq 1 ]; then
243 # statically link against liblzma
244 $CC -o appimagetool data.o appimagetool.o ../elf.c ../getsection.c -DENABLE_BINRELOC ../binreloc.c \
245 ../squashfuse/.libs/libsquashfuse.a ../squashfuse/.libs/libfuseprivate.a \
246 -L../xz-5.2.3/build/lib \
247 -Wl,-Bdynamic -ldl -lpthread \
248 -Wl,--as-needed $(pkg-config --cflags --libs glib-2.0) -lz -Wl,-Bstatic -llzma -Wl,-Bdynamic
249 else
250 # dinamically link against distro provided liblzma
251 $CC -o appimagetool data.o appimagetool.o ../elf.c ../getsection.c -DENABLE_BINRELOC ../binreloc.c \
252 ../squashfuse/.libs/libsquashfuse.a ../squashfuse/.libs/libfuseprivate.a \
253 -Wl,-Bdynamic -ldl -lpthread \
254 -Wl,--as-needed $(pkg-config --cflags --libs glib-2.0) -lz -llzma
257 # Version without glib
258 # cc -D_FILE_OFFSET_BITS=64 -I ../squashfuse -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -g -Os -c ../appimagetoolnoglib.c
259 # 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
261 # Compile and link digest tool
263 if [ $STATIC_BUILD -eq 1 ]; then
264 $CC -o digest ../getsection.c ../digest.c -I../openssl-1.1.0c/build/include -L../openssl-1.1.0c/build/lib \
265 -Wl,-Bstatic -lssl -lcrypto -Wl,-Bdynamic -lz -ldl
266 else
267 $CC -o digest ../getsection.c ../digest.c -Wl,-Bdynamic -lssl -lcrypto -lz -ldl
270 $STRIP digest
272 # Compile and link validate tool
274 if [ $STATIC_BUILD -eq 1 ]; then
275 $CC -o validate ../getsection.c ../validate.c -I../openssl-1.1.0c/build/include -L../openssl-1.1.0c/build/lib \
276 -Wl,-Bstatic -lssl -lcrypto -Wl,-Bdynamic -Wl,--as-needed $(pkg-config --cflags --libs glib-2.0) -lz -ldl
277 else
278 $CC -o validate ../getsection.c ../validate.c -Wl,-Bdynamic -lssl -lcrypto \
279 -Wl,--as-needed $(pkg-config --cflags --libs glib-2.0) -lz -ldl
282 $STRIP validate
284 # AppRun
285 $CC $small_FLAGS $small_LDFLAGS ../AppRun.c -o AppRun
287 # check for libarchive name
288 have_libarchive3=0
289 archive_n=
290 if printf "#include <archive3.h>\nint main(){return 0;}" | cc -w -O0 -xc - -Wl,--no-as-needed -larchive3 2>/dev/null ; then
291 have_libarchive3=1
292 archive_n=3
294 rm -f a.out
296 # appimaged, an optional component
297 if [ $STATIC_BUILD -eq 1 ]; then
298 $CC -std=gnu99 -o appimaged -I../squashfuse/ ../getsection.c ../notify.c ../elf.c ../appimaged.c \
299 -D_FILE_OFFSET_BITS=64 -DHAVE_LIBARCHIVE3=0 -DVERSION_NUMBER=\"$(git describe --tags --always --abbrev=7)\" \
300 ../squashfuse/.libs/libsquashfuse.a ../squashfuse/.libs/libfuseprivate.a \
301 -I../openssl-1.1.0c/build/include -L../openssl-1.1.0c/build/lib -Wl,-Bstatic -lssl -lcrypto \
302 -I../libarchive-3.3.1/libarchive ../libarchive-3.3.1/.libs/libarchive.a \
303 -L../xz-5.2.3/build/lib -I../inotify-tools-3.14/build/include -L../inotify-tools-3.14/build/lib \
304 -Wl,-Bstatic -linotifytools -Wl,-Bdynamic \
305 -Wl,--as-needed \
306 $(pkg-config --cflags --libs glib-2.0) \
307 $(pkg-config --cflags --libs gio-2.0) \
308 $(pkg-config --cflags --libs cairo) \
309 -ldl -lpthread -lz -Wl,-Bstatic -llzma -Wl,-Bdynamic
310 else
311 $CC -std=gnu99 -o appimaged -I../squashfuse/ ../getsection.c ../notify.c ../elf.c ../appimaged.c \
312 -D_FILE_OFFSET_BITS=64 -DHAVE_LIBARCHIVE3=$have_libarchive3 -DVERSION_NUMBER=\"$(git describe --tags --always --abbrev=7)\" \
313 ../squashfuse/.libs/libsquashfuse.a ../squashfuse/.libs/libfuseprivate.a \
314 -Wl,-Bdynamic -linotifytools -larchive${archive_n} \
315 -Wl,--as-needed \
316 $(pkg-config --cflags --libs glib-2.0) \
317 $(pkg-config --cflags --libs gio-2.0) \
318 $(pkg-config --cflags --libs cairo) \
319 -ldl -lpthread -lz -llzma
322 cd ..
324 # Strip and check size and dependencies
326 rm build/*.o build/1024_blank_bytes
327 $STRIP build/* 2>/dev/null
328 chmod a+x build/*
329 ls -lh build/*
330 for FILE in $(ls build/*) ; do
331 echo "$FILE"
332 ldd "$FILE" || true
333 done
335 bash -ex "$HERE/build-appdirs.sh"
337 ls -lh
339 mkdir -p out
340 cp -r build/* ./*.AppDir out/