Compile a static version of pngquant

613 views Asked by At

I'm trying to create a statically linked version of pngquant in Oracle Linux Server release 7.1. I've compiled the static version of zlib and the static version of libpng.

Then, when I configure pngquant, I always get the information that it will be linked with a shared version of zlib.

$ ./configure --with-libpng=../libpng-1.6.21 --extra-cflags="-I../zlib-1.2.8" --extra-ldflags="../zlib-1.2.8/libz.a"

  Compiler: gcc
     Debug: no
       SSE: yes
    OpenMP: no
    libpng: static (1.6.21)
      zlib: shared (1.2.7)
     lcms2: no

If I execute make, in the output it seems that the options are correctly passed to the compiler. However, the resulting binary requires libz.so to be executed. It seems that my directives are ignored or that the installed version always takes precedence.

Is there any way of forcing pngquant to be compiled with the static version of zlib?

2

There are 2 answers

2
Sascha On

I'm not sure, if I got it right, but here's a patch to pngquant's configure that worked for me. configure now accepts --with-zlib=<dir> as parameter. Store it to pngquant.patch and apply it with patch -uN -p1 -i pngquant.patch.

    diff -ur pngquant-2.9.0/configure pngquant-2.9.0.fixed/configure
--- pngquant-2.9.0/configure    2017-03-06 09:37:30.000000000 +0100
+++ pngquant-2.9.0.fixed/configure  2017-03-07 09:57:20.246012152 +0100
@@ -48,6 +48,7 @@
         help "--with-cocoa/--without-cocoa  use Cocoa framework to read images"
 fi
         help "--with-libpng=<dir>           search for libpng in directory"
+        help "--with-zlib=<dir>             search for zlib in directory"
         echo
         help "CC=<compiler>                 use given compiler command"
         help "CFLAGS=<flags>                pass options to the compiler"
@@ -97,6 +98,9 @@
     --with-libpng=*)
         LIBPNG_DIR=${i#*=}
         ;;
+    --with-zlib=*)
+        ZLIB_DIR=${i#*=}
+        ;;
     --prefix=*)
         PREFIX=${i#*=}
         ;;
@@ -238,6 +242,19 @@
     echo "${MAJ}${MIN}"
 }
 
+# returns full zlib.h version string
+zlibh_string() {
+    echo "$(grep -m1 "define ZLIB_VERSION" "$1" | \
+            grep -Eo '"[^"]+"' | grep -Eo '[^"]+')"
+}
+
+# returns major minor version numbers from png.h
+zlibh_majmin() {
+    local MAJ=$(grep -m1 "define ZLIB_VER_MAJOR" "$1" | grep -Eo "[0-9]+")
+    local MIN=$(grep -m1 "define ZLIB_VER_MINOR" "$1" | grep -Eo "[0-9]+")
+    echo "${MAJ}${MIN}"
+}
+
 error() {
     status "$1" "error ... $2"
     echo
@@ -420,11 +437,42 @@
     error "libpng" "not found (try: $LIBPNG_CMD)"
 fi
 
-# zlib
-if ! find_library "zlib" "z" "zlib.h" "libz.a" "libz.$SOLIBSUFFIX*"; then
-    error "zlib" "not found (please install zlib-devel package)"
+# try if given flags are enough for zlib
+HAS_ZLIB=0
+if echo "#include \"zlib.h\"
+    int main(){
+    uLong test = zlibCompileFlags();
+    return 0;
+}" | "$CC" -xc -std=c99 -o /dev/null $CFLAGS $LDFLAGS - &> /dev/null; then
+    status "zlib" "custom flags"
+    HAS_ZLIB=1
 fi
 
+if [ "$HAS_ZLIB" -eq 0 ]; then
+    # try static in the given directory
+    ZLIBH=$(find_h "$ZLIB_DIR" "zlib.h")
+    if [ -n "$ZLIBH" ]; then
+        ZLIBH_STRING=$(zlibh_string "$ZLIBH")
+        ZLIBH_MAJMIN=$(zlibh_majmin "$ZLIBH")
+        if [[ -n "$ZLIBH_STRING" && -n "$ZLIBH_MAJMIN" ]]; then
+            ZLIBA=$(find_f "$ZLIB_DIR" "libz${ZLIBH_MAJMIN}.a")
+            if [ -z "$ZLIBA" ]; then
+                ZLIBA=$(find_f "$ZLIB_DIR" "libz.a")
+            fi
+            if [ -n "$ZLIBA" ]; then
+                cflags "-I${ZLIBH%/*}"
+                lflags "${ZLIBA}"
+                status "zlib" "static (${ZLIBH_STRING})"
+                HAS_ZLIB=1
+            fi
+        fi
+    fi
+fi
+# zlib
+if ! find_library "zlib" "z" "zlib.h" "libz.a" "zlib.$SOLIBSUFFIX*"; then
+    error "zlib" "not found (please install zlib-devel package)"
+fi
+
 # lcms2
 if [ "$LCMS2" != 0 ]; then
     if find_library "lcms2" "lcms2" "lcms2.h" "liblcms2.a" "liblcms2.$SOLIBSUFFIX*"; then
0
Kornel On

Sorry, the configure script does not support it. It shouldn't be too hard to modify configure to pass appropriate flags to pkg-config or do the same workaround it does for libpng.