From b6107c8777a6f98dd89218ede1020a27ee83be70 Mon Sep 17 00:00:00 2001 From: Giacomo Tesio Date: Wed, 20 Nov 2024 21:51:43 +0100 Subject: [PATCH 1/2] Makefile: enable static compilation with musl Assuming a Debian GNU/Linux system with wget, tar, git, make and musl-gcc ``` mkdir build cd build export BUILD_TARGET=$PWD export CC="musl-gcc" echo download and build zlib wget http://zlib.net/current/zlib.tar.gz tar xvf zlib.tar.gz cd zlib-1.3.1/ ./configure --prefix=$BUILD_TARGET --static make make install cd .. echo download and build openssl wget https://github.com/openssl/openssl/releases/download/openssl-3.4.0/openssl-3.4.0.tar.gz tar xvf openssl-3.4.0.tar.gz cd openssl-3.4.0 CC="musl-gcc -fPIE -pie -static -idirafter /usr/include/ -idirafter /usr/include/x86_64-linux-gnu/" ./Configure no-shared no-async --prefix=$BUILD_TARGET --openssldir=$BUILD_TARGET/ssl linux-x86_64 make depend make make install cd .. echo download and build curl wget https://curl.se/download/curl-7.88.1.tar.gz tar xvf curl-7.88.1.tar.gz cd curl-7.88.1 ./configure --disable-shared --enable-static --disable-silent-rules --disable-debug --disable-warnings --disable-werror --disable-curldebug --disable-symbol-hiding --disable-ares --disable-rt --disable-ech --disable-dependency-tracking --disable-libtool-lock --enable-http --disable-ftp --disable-file --disable-ldap --disable-ldaps --disable-rtsp --disable-proxy --disable-dict --disable-telnet --disable-tftp --disable-pop3 --disable-imap --disable-smb --disable-smtp --disable-gopher --disable-mqtt --disable-manual --disable-libcurl-option --disable-ipv6 --disable-openssl-auto-load-config --disable-versioned-symbols --disable-verbose --disable-sspi --disable-crypto-auth --disable-ntlm --disable-ntlm-wb --disable-tls-srp --enable-unix-sockets --disable-cookies --disable-socketpair --disable-http-auth --disable-doh --disable-mime --disable-dateparse --disable-netrc --disable-progress-meter --disable-dnsshuffle --disable-get-easy-options --disable-alt-svc --disable-websockets --without-brotli --without-zstd --without-libpsl --without-libgsasl --without-librtmp --without-winidn --with-openssl=$BUILD_TARGET/ --disable-threaded-resolver --with-zlib=$BUILD_TARGET/ --prefix=$BUILD_TARGET/ make make install cd .. echo download and build snac2 git clone https://codeberg.org/grunfink/snac2.git cd snac2 # assuming this commit in master make CFLAGS="-g -Wall -Wextra -pedantic -static -DWITHOUT_SHM" PREFIX=$BUILD_TARGET LDFLAGS="-L$BUILD_TARGET/lib64 -lcurl -lssl -lcrypto -lz" make install PREFIX=$BUILD_TARGET cd .. echo a statically linked snac is ready at $BUILD_TARGET/bin ``` --- Makefile | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 600419d..05e65f5 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -PREFIX=/usr/local +PREFIX?=/usr/local PREFIX_MAN=$(PREFIX)/man CFLAGS?=-g -Wall -Wextra -pedantic @@ -6,16 +6,16 @@ all: snac snac: snac.o main.o data.o http.o httpd.o webfinger.o \ activitypub.o html.o utils.o format.o upgrade.o mastoapi.o - $(CC) $(CFLAGS) -L/usr/local/lib *.o -lcurl -lcrypto $(LDFLAGS) -pthread -o $@ + $(CC) $(CFLAGS) -L$(PREFIX)/lib *.o -lcurl -lcrypto $(LDFLAGS) -pthread -o $@ .c.o: - $(CC) $(CFLAGS) $(CPPFLAGS) -I/usr/local/include -c $< + $(CC) $(CFLAGS) $(CPPFLAGS) -I$(PREFIX)/include -c $< clean: rm -rf *.o *.core snac makefile.depend dep: - $(CC) -I/usr/local/include -MM *.c > makefile.depend + $(CC) -I$(PREFIX)/include -MM *.c > makefile.depend install: mkdir -p -m 755 $(PREFIX)/bin From 41081dd0d5918c6a82e1e6213de88c285f1b8158 Mon Sep 17 00:00:00 2001 From: Giacomo Tesio Date: Thu, 12 Dec 2024 19:33:49 +0100 Subject: [PATCH 2/2] examples: add instructions to statically link snac with musl libc --- examples/static-linking-with-musl.md | 77 ++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 examples/static-linking-with-musl.md diff --git a/examples/static-linking-with-musl.md b/examples/static-linking-with-musl.md new file mode 100644 index 0000000..b14132c --- /dev/null +++ b/examples/static-linking-with-musl.md @@ -0,0 +1,77 @@ +# How to build a statically linked Snac with musl + +Prepare the environment +``` +mkdir build +cd build +export BUILD_TARGET=$PWD +export CC="musl-gcc" +``` + +Download and build latest zlib +``` +wget http://zlib.net/current/zlib.tar.gz +tar xvf zlib.tar.gz +cd zlib-1.3.1/ +./configure --prefix=$BUILD_TARGET --static +make +make install +cd .. +``` + +Download and build latest openssl +``` +wget https://github.com/openssl/openssl/releases/download/openssl-3.4.0/openssl-3.4.0.tar.gz +tar xvf openssl-3.4.0.tar.gz +cd openssl-3.4.0 +CC="musl-gcc -fPIE -pie -static -idirafter /usr/include/ -idirafter /usr/include/x86_64-linux-gnu/" \ + ./Configure no-shared no-async --prefix=$BUILD_TARGET --openssldir=$BUILD_TARGET/ssl linux-x86_64 +make depend +make +make install +cd .. +``` + +Download and build latest curl +``` +wget https://curl.se/download/curl-7.88.1.tar.gz +tar xvf curl-7.88.1.tar.gz +cd curl-7.88.1 +./configure --disable-shared --enable-static --disable-silent-rules \ + --disable-debug --disable-warnings --disable-werror \ + --disable-curldebug --disable-symbol-hiding --disable-ares \ + --disable-rt --disable-ech --disable-dependency-tracking \ + --disable-libtool-lock --enable-http --disable-ftp \ + --disable-file --disable-ldap --disable-ldaps \ + --disable-rtsp --disable-proxy --disable-dict \ + --disable-telnet --disable-tftp --disable-pop3 \ + --disable-imap --disable-smb --disable-smtp --disable-gopher \ + --disable-mqtt --disable-manual --disable-libcurl-option --disable-ipv6 \ + --disable-openssl-auto-load-config --disable-versioned-symbols + --disable-verbose --disable-sspi --disable-crypto-auth \ + --disable-ntlm --disable-ntlm-wb --disable-tls-srp \ + --disable-unix-sockets --disable-cookies --disable-socketpair \ + --disable-http-auth --disable-doh --disable-mime --disable-dateparse \ + --disable-netrc --disable-progress-meter --disable-dnsshuffle \ + --disable-get-easy-options --disable-alt-svc --disable-websockets \ + --without-brotli --without-zstd --without-libpsl --without-libgsasl \ + --without-librtmp --without-winidn --disable-threaded-resolver \ + --with-openssl=$BUILD_TARGET/ --with-zlib=$BUILD_TARGET/ \ + --prefix=$BUILD_TARGET/ +make +make install +cd .. +``` + +Download and build latest snac2 +``` +git clone https://codeberg.org/grunfink/snac2.git # or cd to your existing repo +cd snac2 +make CFLAGS="-g -Wall -Wextra -pedantic -static -DWITHOUT_SHM" \ + LDFLAGS="-L$BUILD_TARGET/lib64 -lcurl -lssl -lcrypto -lz" \ + PREFIX=$BUILD_TARGET +make install PREFIX=$BUILD_TARGET +cd .. +``` + +Finally a statically linked snac is ready at $BUILD_TARGET/bin