configure 12.6 KB
Newer Older
1 2 3 4
#!/usr/bin/env bash

CONFIG="config.mk"
PREFIX="/usr/local"
5 6 7 8 9 10

test -f lib/libimagequant.h || { git submodule init && git submodule update; } || {
    echo "error: libimagequant not found in ./lib/"
    echo "If you're using git, do clone with --recursive, or download from https://github.com/ImageOptim/libimagequant"
    exit 1;
}
11
VERSION=$(grep LIQ_VERSION_STRING lib/libimagequant.h | grep -Eo "2\.[0-9.]+")
12 13 14 15

DEBUG=
SSE=auto
OPENMP=
16
LIBPNG_DIR=.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
if [[ "$OSTYPE" =~ "darwin" ]]; then
    COCOA_READER=auto
    LCMS2=0
else
    COCOA_READER=0
    LCMS2=auto
fi
EXTRA_CFLAGS=
EXTRA_LDFLAGS=

# make gcc default compiler unless CC is already set
CC=${CC:-gcc}

help() {
    printf "%4s %s\n" "" "$1"
}

for i in "$@"; do
    case $i in
    --help)
        echo
38 39 40
        help "--prefix=<dir>                installation directory [$PREFIX]"
        help "--extra-cflags=<flags>        append to CFLAGS"
        help "--extra-ldflags=<flags>       append to LDFLAGS"
41 42 43 44
        echo
        help "--enable-debug"
        help "--enable-sse/--disable-sse    enable/disable SSE instructions"
        echo
45
        help "--with-openmp=static          compile with multicore support"
46 47 48 49
        help "--with-lcms2/--without-lcms2  compile with color profile support"
if [[ "$OSTYPE" =~ "darwin" ]]; then
        help "--with-cocoa/--without-cocoa  use Cocoa framework to read images"
fi
50
        help "--with-libpng=<dir>           search for libpng in directory"
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
        echo
        help "CC=<compiler>                 use given compiler command"
        help "CFLAGS=<flags>                pass options to the compiler"
        help "LDFLAGS=<flags>               pass options to the linker"
        echo
        exit 0
        ;;
    # Can be set before or after configure. Latter overrides former.
    CC=*)
        CC=${i#*=}
        ;;
    CFLAGS=*)
        CFLAGS=${i#*=}
        ;;
    LDFLAGS=*)
        LDFLAGS=${i#*=}
        ;;
    --enable-debug)
        DEBUG=1
        ;;
    --enable-sse)
        SSE=1
        ;;
    --disable-sse)
        SSE=0
        ;;
    --with-openmp)
        OPENMP=1
        ;;
80 81 82
    --with-openmp=static)
        OPENMP=static
        ;;
83 84 85 86 87 88 89 90 91 92 93 94 95 96
    --with-lcms2)
        LCMS2=1
        COCOA_READER=0
        ;;
    --without-lcms2)
        LCMS2=0
        ;;
    --with-cocoa)
        COCOA_READER=1
        LCMS2=0
        ;;
    --without-cocoa)
        COCOA_READER=0
        ;;
97 98 99
    --with-libpng=*)
        LIBPNG_DIR=${i#*=}
        ;;
100 101 102 103 104 105 106 107 108 109 110
    --prefix=*)
        PREFIX=${i#*=}
        ;;
    # can be used multiple times or in quotes to set multiple flags
    --extra-cflags=*)
        EXTRA_CFLAGS="$EXTRA_CFLAGS ${i#*=}"
        ;;
    --extra-ldflags=*)
        EXTRA_LDFLAGS="$EXTRA_LDFLAGS ${i#*=}"
        ;;
    *)
111
        echo "warning: unknown switch ${i%%=*} (see $0 --help for the list)"
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
        ;;
    esac
done

# If someone runs sudo make install as very first command, and configure later,
# $CONFIG cannot be overwritten, and must be deleted before continuing.
if [[ -f "$CONFIG" && ! -w "$CONFIG" ]]; then
    echo "Cannot overwrite file $CONFIG! Please delete it."
    exit 1
fi

cflags() {
    CFLAGS="$CFLAGS $1"
}

lflags() {
    LDFLAGS="$LDFLAGS $1"
}

status() {
    printf "%10s: %s\n" "$1" "$2"
}

# Append to CFLAGS if compiler supports flag, with optional prerequisite.
# Fails on errors and warnings.
conditional_cflags() {
138
    if [ -z "$(echo | "$CC" -xc -S -o /dev/null $2 $1 - 2>&1)" ]; then
139 140 141 142 143 144
        cflags "$1"
    fi
}

# returns first matching file in directory
find_f() {
145
    echo $(find "$1" -not -path '*/.git/*' -not -type d -name "$2" -print -quit 2> /dev/null)
146 147 148 149
}

# returns first matching file in directory (no symlinks)
find_h() {
150
    echo $(find "$1" -not -path '*/.*/*' -type f -name "$2" -print -quit 2> /dev/null)
151 152 153
}

find_pkgconfig() {
154
    local LIBNAME=$1
155 156 157 158 159 160 161 162 163 164
    if pkg-config --exists "$LIBNAME" &> /dev/null; then
        cflags "$(pkg-config --cflags "$LIBNAME")"
        lflags "$(pkg-config --libs "$LIBNAME")"
        status "$LIBNAME" "shared ($(pkg-config --modversion "$LIBNAME"))"
        return 0
    fi
    return 1
}

find_static() {
165 166 167
    local LIBNAME=$1
    local HEADERPATTERN=$2
    local STATICPATTERN=$3
168

169
    local HPATH=$(find_h . "$HEADERPATTERN")
170
    if [ -n "$HPATH" ]; then
171
        local APATH=$(find_f . "$STATICPATTERN")
172 173 174 175 176 177 178 179 180 181
        if [ -n "$APATH" ]; then
            cflags "-I${HPATH%/*}"
            lflags "${APATH}"
            status "$LIBNAME" "static"
            return 0
        fi
    fi
    return 1
}

182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
find_dynamic() {
    local LIBNAME=$1
    local HEADERPATTERN=$2
    local DYNAMICPATTERN=$3
    local HEADERDIR=$4
    local LIBDIR=$5

    local HPATH=$(find_h "$HEADERDIR" "$HEADERPATTERN")
    if [ -n "$HPATH" ]; then
        local SOPATH=$(find_f "$LIBDIR" "$DYNAMICPATTERN")
        if [ -n "$SOPATH" ]; then
            cflags "-I${HPATH%/*}"
            lflags "-L${SOPATH%/*} -l$DYNAMICLIBNAME"
            status "$LIBNAME" "shared ... $SOPATH"
            return 0
        fi
    fi
    return 1
}

202
find_library() {
203 204 205 206 207
    local LIBNAME=$1
    local DYNAMICLIBNAME=$2
    local HEADERPATTERN=$3
    local STATICPATTERN=$4
    local DYNAMICPATTERN=$5
208 209 210 211 212 213 214 215 216 217 218 219 220

    # try static in current directory first
    if find_static "$LIBNAME" "$HEADERPATTERN" "$STATICPATTERN"; then
        return 0;
    fi

    # try shared
    if find_pkgconfig "$LIBNAME"; then
        return 0
    fi

    for i in "${DIRS[@]}"; do
        DIR=($i)
221 222
        if find_dynamic "$LIBNAME" "$HEADERPATTERN" "$DYNAMICPATTERN" "${DIR[0]}" "${DIR[1]}"; then
            return 0
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
        fi
    done
    return 1
}

# returns full png.h version string
pngh_string() {
    echo "$(grep -m1 "define PNG_LIBPNG_VER_STRING" "$1" | \
            grep -Eo '"[^"]+"' | grep -Eo '[^"]+')"
}

# returns major minor version numbers from png.h
pngh_majmin() {
    local MAJ=$(grep -m1 "define PNG_LIBPNG_VER_MAJOR" "$1" | grep -Eo "[0-9]+")
    local MIN=$(grep -m1 "define PNG_LIBPNG_VER_MINOR" "$1" | grep -Eo "[0-9]+")
    echo "${MAJ}${MIN}"
}

error() {
    status "$1" "error ... $2"
    echo
    exit 1
}

echo

# basic check
250
if ! echo "int main(){}" | "$CC" -xc -std=c99 -o /dev/null - > /dev/null; then
251
    error "Compiler" "$CC failed to compile anything (make sure it's installed and supports C99)"
252 253 254 255 256
fi

status "Compiler" "$CC"

# init flags
257
CFLAGS=${CFLAGS:--fno-math-errno -funroll-loops -fomit-frame-pointer -Wall}
258 259 260 261
cflags "-std=c99 -I."

# DEBUG
if [ -z "$DEBUG" ]; then
262
    cflags "-O3 -DNDEBUG"
263 264
    status "Debug" "no"
else
265
    cflags "-O1 -g"
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
    status "Debug" "yes"
fi

# SSE
if [ "$SSE" = 'auto' ]; then
    if [[ "$(uname -m)" =~ (amd|x86_)64 ||
          "$(grep -E -m1 "^flags" /proc/cpuinfo)" =~ "sse" ]]; then
        SSE=1
    fi
fi

if [ "$SSE" -eq 1 ]; then
    status "SSE" "yes"
    cflags "-DUSE_SSE=1"
    cflags "-msse"
    # Silence a later ICC warning due to -msse working slightly different.
    conditional_cflags "-wd10121"
    # Must be set explicitly for GCC on x86_32. Other compilers imply it.
    conditional_cflags "-mfpmath=sse" "-msse"
elif [ "$SSE" -eq 0 ]; then
    status "SSE" "no"
    cflags "-DUSE_SSE=0"
fi

# OpenMP
if [ -n "$OPENMP" ]; then
292 293 294 295 296 297
    if [ "static" = "$OPENMP" ]; then
        OPENMPFLAGS="-static-libgcc -Bstatic -fopenmp -Bdynamic"
    else
        OPENMPFLAGS="-fopenmp"
    fi
    if [[ "$("$CC" -xc -E $OPENMPFLAGS <(echo "#ifdef _OPENMP
298 299
           #include <omp.h>
           #endif") 2>&1)" =~ "omp_get_thread_num" ]]; then
300 301
        cflags "$OPENMPFLAGS"
        lflags "$OPENMPFLAGS"
302 303 304 305 306 307 308 309 310 311 312 313 314
        status "OpenMP" "yes"
    else
        error "OpenMP" "not supported by compiler (please install a compiler that supports OpenMP (e.g. gcc) and specify it with the CC= argument)"
    fi
else
    # silence warnings about omp pragmas
    cflags "-Wno-unknown-pragmas"
    conditional_cflags "-wd3180" # ICC
    status "OpenMP" "no"
fi

# Cocoa
if [[ "$OSTYPE" =~ "darwin" ]]; then
315 316
    cflags "-mmacosx-version-min=10.6"
    lflags "-mmacosx-version-min=10.6"
317

318
    if [ "$COCOA_READER" != 0 ] && "$CC" 2>/dev/null 1>/dev/null -xc -E <(echo "#import <Cocoa/Cocoa.h>"); then
319 320 321 322 323 324 325 326 327
        COCOA_READER=1
        cflags "-DUSE_COCOA=1"
        lflags "-framework Cocoa"
        status "Cocoa" "yes"
    else
        status "Cocoa" "no"
    fi
fi

328
# pairs of possible *.h and lib*.so locations
329 330 331 332 333 334 335
DIRS=()

if command -v >/dev/null libpng-config; then
    DIRS+=("$(libpng-config --prefix) $(libpng-config --libdir)")
fi

DIRS+=("/usr/local/include /usr/local/lib"
336 337 338 339
      "/usr/include /usr/lib"
      "/opt/local/include /opt/local/lib" # macports
      )

340 341
if [[ "$OSTYPE" =~ "darwin" ]]; then
    SOLIBSUFFIX=dylib
342 343 344 345 346 347

    # Search Developer SDK paths, since Apple seems to have dropped the standard Unixy ones
    XCODE_CMD="xcode-select"
    XCODE_PATH=$($XCODE_CMD -p)
    DIRS+=("$XCODE_PATH/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/usr/include $XCODE_PATH/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/usr/lib")
    DIRS+=("$XCODE_PATH/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/usr/include $XCODE_PATH/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/usr/lib")
348 349 350 351 352 353
else
    SOLIBSUFFIX=so
fi


# libpng
354
# try if given flags are enough
355
HAS_LIBPNG=0
356 357 358 359 360
if echo "#include \"png.h\"
    int main(){
    return !png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
}" | "$CC" -xc -std=c99 -o /dev/null $CFLAGS $LDFLAGS - &> /dev/null; then
    status "libpng" "custom flags"
361
    HAS_LIBPNG=1
362 363
fi

364
if [ "$HAS_LIBPNG" -eq 0 ]; then
365 366 367 368 369 370 371 372 373 374 375
    # try static in the given directory
    PNGH=$(find_h "$LIBPNG_DIR" "png.h")
    if [ -n "$PNGH" ]; then
        PNGH_STRING=$(pngh_string "$PNGH")
        PNGH_MAJMIN=$(pngh_majmin "$PNGH")
        if [[ -n "$PNGH_STRING" && -n "$PNGH_MAJMIN" ]]; then
            LIBPNGA=$(find_f "$LIBPNG_DIR" "libpng${PNGH_MAJMIN}.a")
            if [ -n "$LIBPNGA" ]; then
                cflags "-I${PNGH%/*}"
                lflags "${LIBPNGA}"
                status "libpng" "static (${PNGH_STRING})"
376
                HAS_LIBPNG=1
377
            fi
378 379 380 381
        fi
    fi
fi
# try shared
382
if [ "$HAS_LIBPNG" -eq 0 ]; then
383
    if find_pkgconfig libpng; then
384
        HAS_LIBPNG=1
385 386 387 388 389 390 391 392 393 394 395 396 397
    else
        for i in "${DIRS[@]}"; do
            DIR=($i)
            PNGH=$(find_h "${DIR[0]}" "png.h")
            if [ -n "$PNGH" ]; then
                PNGH_STRING=$(pngh_string "$PNGH")
                PNGH_MAJMIN=$(pngh_majmin "$PNGH")
                if [[ -n "$PNGH_STRING" && -n "$PNGH_MAJMIN" ]]; then
                    LIBPNGSO=$(find_f "${DIR[1]}" "libpng${PNGH_MAJMIN}.$SOLIBSUFFIX*")
                    if [ -n "$LIBPNGSO" ]; then
                        cflags "-I${PNGH%/*}"
                        lflags "-L${LIBPNGSO%/*} -lpng${PNGH_MAJMIN}"
                        status "libpng" "shared (${PNGH_STRING})"
398
                        HAS_LIBPNG=1
399 400 401 402 403 404 405
                        break
                    fi
                fi
            fi
        done
    fi
fi
406
if [ "$HAS_LIBPNG" -eq 0 ]; then
407
    if [[ "$OSTYPE" =~ "darwin" ]]; then
408
        LIBPNG_CMD='`brew install libpng`'
409
    else
410
        LIBPNG_CMD='`apt-get install libpng16-dev` or `apt-get install libpng-dev` or `yum install libpng-devel`'
411
    fi
412
    error "libpng" "not found (try: $LIBPNG_CMD)"
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
fi

# zlib
if ! find_library "zlib" "z" "zlib.h" "libz.a" "libz.$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
        cflags "-DUSE_LCMS=1"
    else
        if [ "$LCMS2" = 'auto' ]; then
            status "lcms2" "no"
        else
            error "lcms2" "not found (please install libcms2-devel package)"
        fi
    fi
else
    status "lcms2" "no"
fi

echo

# As of GCC 4.5, 387 fp math is significantly slower in C99 mode without this.
# Note: CPUs without SSE2 use 387 for doubles, even when SSE fp math is set.
conditional_cflags "-fexcess-precision=fast"

# Intel C++ Compiler

# ICC does usually only produce fast(er) code when it can optimize to the full
# capabilites of the (Intel) CPU. This is equivalent to -march=native for GCC.
conditional_cflags "-xHOST"

# Disable unsafe fp optimizations and enforce fp precision as set in the source.
conditional_cflags "-fp-model source"

# Silence a gold linker warning about string misalignment.
conditional_cflags "-falign-stack=maintain-16-byte"

lflags "-lm" # Ubuntu requires this library last, issue #38

if [ -n "$EXTRA_CFLAGS" ]; then
    cflags "$EXTRA_CFLAGS"
fi

if [ -n "$EXTRA_LDFLAGS" ]; then
    lflags "$EXTRA_LDFLAGS"
fi

# Overwrite previous configuration.
echo "
# auto-generated by configure
PREFIX = $PREFIX
VERSION = $VERSION
CC = $CC
CFLAGS = $CFLAGS
LDFLAGS = $LDFLAGS
COCOA_READER = $COCOA_READER
472
SOLIBSUFFIX = $SOLIBSUFFIX
473 474 475 476
" > "$CONFIG"

# Configure static library the same way
cp "$CONFIG" lib/