• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env bash
2# configure script for zlib.
3#
4# Normally configure builds both a static and a shared library.
5# If you want to build just a static library, use: ./configure --static
6#
7# To impose specific compiler or flags or install directory, use for example:
8#    prefix=$HOME CC=cc CFLAGS="-O4" ./configure
9# or for csh/tcsh users:
10#    (setenv prefix $HOME; setenv CC cc; setenv CFLAGS "-O4"; ./configure)
11
12# Incorrect settings of CC or CFLAGS may prevent creating a shared library.
13# If you have problems, try without defining CC and CFLAGS before reporting
14# an error.
15
16# start off configure.log
17echo -------------------- >> configure.log
18echo $0 $* >> configure.log
19date >> configure.log
20
21SRCDIR=$(cd $(dirname $0); pwd)
22BUILDDIR=$(pwd)
23
24# set command prefix for cross-compilation
25if [ -n "${CHOST}" ]; then
26    # normalize the chost before parsing it
27    NORM_CHOST=$(sh "$SRCDIR"/tools/config.sub $CHOST)
28    uname="$(echo "${NORM_CHOST}" | sed -e 's/^[^-]*-\([^-]*\)$/\1/' -e 's/^[^-]*-[^-]*-\([^-]*\)$/\1/' -e 's/^[^-]*-[^-]*-\([^-]*\)-.*$/\1/')"
29    CROSS_PREFIX="${CHOST}-"
30    ARCH="$(echo "${NORM_CHOST}" | sed -e 's/-.*//')"
31else
32    ARCH="`uname -m`"
33fi
34
35case "${ARCH}" in
36    x86_64)
37        case "${CFLAGS}" in
38            *-m32*)
39                ARCH=i686
40            ;;
41        esac
42    ;;
43    i386 | i486 | i586 | i686)
44        case "${CFLAGS}" in
45            *-m64*)
46                ARCH=x86_64
47            ;;
48        esac
49    ;;
50esac
51
52# destination name for windows import library
53IMPORTLIB=
54
55# establish commands for library building
56if "${CROSS_PREFIX}ar" --version >/dev/null 2>/dev/null || test $? -lt 126; then
57    AR=${AR-"${CROSS_PREFIX}ar"}
58    test -n "${CROSS_PREFIX}" && echo Using ${AR} | tee -a configure.log
59else
60    AR=${AR-"ar"}
61    test -n "${CROSS_PREFIX}" && echo Using ${AR} | tee -a configure.log
62fi
63ARFLAGS=${ARFLAGS-"rc"}
64if "${CROSS_PREFIX}ranlib" --version >/dev/null 2>/dev/null || test $? -lt 126; then
65    RANLIB=${RANLIB-"${CROSS_PREFIX}ranlib"}
66    test -n "${CROSS_PREFIX}" && echo Using ${RANLIB} | tee -a configure.log
67else
68    RANLIB=${RANLIB-"ranlib"}
69fi
70
71# set defaults before processing command line options
72LDCONFIG=${LDCONFIG-"ldconfig"}
73LDFLAGS=${LDFLAGS}
74LDSHAREDLIBC="${LDSHAREDLIBC}"
75DEFFILE=
76RC=
77RCFLAGS=
78RCOBJS=
79STRIP=
80ARCHS=
81prefix=${prefix-/usr/local}
82exec_prefix=${exec_prefix-'${prefix}'}
83bindir=${bindir-'${exec_prefix}/bin'}
84libdir=${libdir-'${exec_prefix}/lib'}
85sharedlibdir=${sharedlibdir-'${libdir}'}
86includedir=${includedir-'${prefix}/include'}
87mandir=${mandir-'${prefix}/share/man'}
88shared_ext='.so'
89shared=1
90gzfileops=1
91compat=0
92cover=0
93build32=0
94build64=0
95buildacle=1
96buildneon=1
97builddfltccdeflate=0
98builddfltccinflate=0
99with_sanitizer=""
100with_fuzzers=0
101floatabi=
102native=0
103forcesse2=0
104avx2flag="-mavx2"
105sse2flag="-msse2"
106ssse3flag="-mssse3"
107sse4flag="-msse4"
108sse42flag="-msse4.2"
109pclmulflag="-mpclmul"
110acleflag=
111neonflag=
112noltoflag="-fno-lto"
113without_optimizations=0
114without_new_strategies=0
115gcc=0
116warn=0
117debug=0
118old_cc="$CC"
119old_cflags="$CFLAGS"
120OBJC='$(OBJZ)'
121PIC_OBJC='$(PIC_OBJZ)'
122INSTALLTARGETS="install-shared install-static"
123UNINSTALLTARGETS="uninstall-shared uninstall-static"
124
125TEST="teststatic"
126
127# leave this script, optionally in a bad way
128leave()
129{
130  if test "$*" != "0"; then
131    echo "** $0 aborting." | tee -a configure.log
132  fi
133  rm -f $test.[co] $test $test$shared_ext $test.gcno ./--version
134  echo -------------------- >> configure.log
135  echo >> configure.log
136  echo >> configure.log
137  exit $1
138}
139
140# process command line options
141while test $# -ge 1
142do
143case "$1" in
144    -h* | --help)
145      echo 'usage:' | tee -a configure.log
146      echo '  configure [--prefix=PREFIX]  [--eprefix=EXPREFIX]' | tee -a configure.log
147      echo '    [--static] [--32] [--64] [--libdir=LIBDIR] [--sharedlibdir=LIBDIR]' | tee -a configure.log
148      echo '    [--includedir=INCLUDEDIR] [--archs="-arch i386 -arch x86_64"]' | tee -a configure.log
149      echo '    [--warn]                    Enables extra compiler warnings' | tee -a configure.log
150      echo '    [--debug]                   Enables extra debug prints during operation' | tee -a configure.log
151      echo '    [--zlib-compat]             Compiles for zlib-compatible API instead of zlib-ng API' | tee -a configure.log
152      echo '    [--without-gzfileops]       Compiles with the gzfile parts of the API enabled' | tee -a configure.log
153      echo '    [--without-optimizations]   Compiles without support for optional instruction sets' | tee -a configure.log
154      echo '    [--without-new-strategies]  Compiles without using new additional deflate strategies' | tee -a configure.log
155      echo '    [--without-acle]            Compiles without ARM C Language Extensions' | tee -a configure.log
156      echo '    [--without-neon]            Compiles without ARM Neon SIMD instruction set' | tee -a configure.log
157      echo '    [--with-dfltcc-deflate]     Use DEFLATE CONVERSION CALL instruction for compression on IBM Z' | tee -a configure.log
158      echo '    [--with-dfltcc-inflate]     Use DEFLATE CONVERSION CALL instruction for decompression on IBM Z' | tee -a configure.log
159      echo '    [--force-sse2]              Assume SSE2 instructions are always available (disabled by default on x86, enabled on x86_64)' | tee -a configure.log
160      echo '    [--with-sanitizer]          Build with sanitizer (memory, address, undefined)' | tee -a configure.log
161      echo '    [--with-fuzzers]            Build test/fuzz (disabled by default)' | tee -a configure.log
162      echo '    [--native]                  Compiles with full instruction set supported on this host' | tee -a configure.log
163        exit 0 ;;
164    -p*=* | --prefix=*) prefix=`echo $1 | sed 's/.*=//'`; shift ;;
165    -e*=* | --eprefix=*) exec_prefix=`echo $1 | sed 's/.*=//'`; shift ;;
166    -l*=* | --libdir=*) libdir=`echo $1 | sed 's/.*=//'`; shift ;;
167    --sharedlibdir=*) sharedlibdir=`echo $1 | sed 's/.*=//'`; shift ;;
168    -i*=* | --includedir=*) includedir=`echo $1 | sed 's/.*=//'`;shift ;;
169    -u*=* | --uname=*) uname=`echo $1 | sed 's/.*=//'`;shift ;;
170    -p* | --prefix) prefix="$2"; shift; shift ;;
171    -e* | --eprefix) exec_prefix="$2"; shift; shift ;;
172    -l* | --libdir) libdir="$2"; shift; shift ;;
173    -i* | --includedir) includedir="$2"; shift; shift ;;
174    -s* | --shared | --enable-shared) shared=1; shift ;;
175    -t | --static) shared=0; shift ;;
176    --zlib-compat) compat=1; shift ;;
177    --without-gzfileops) gzfileops=0; shift ;;
178    --cover) cover=1; shift ;;
179    -3* | --32) build32=1; shift ;;
180    -6* | --64) build64=1; shift ;;
181    --without-acle) buildacle=0; shift ;;
182    --without-neon) buildneon=0; shift ;;
183    --with-dfltcc-deflate) builddfltccdeflate=1; shift ;;
184    --with-dfltcc-inflate) builddfltccinflate=1; shift ;;
185    --force-sse2) forcesse2=1; shift ;;
186    -n | --native) native=1; shift ;;
187    -a*=* | --archs=*) ARCHS=`echo $1 | sed 's/.*=//'`; shift ;;
188    --sysconfdir=*) echo "ignored option: --sysconfdir" | tee -a configure.log; shift ;;
189    --localstatedir=*) echo "ignored option: --localstatedir" | tee -a configure.log; shift ;;
190    -noopt | --without-optimizations) without_optimizations=1; shift;;
191    -oldstrat | --without-new-strategies) without_new_strategies=1; shift;;
192    -w* | --warn) warn=1; shift ;;
193    -d* | --debug) debug=1; shift ;;
194    --with-sanitizer=*) with_sanitizer=`echo $1 | sed 's/.*=//'`; shift ;;
195    --with-fuzzers) with_fuzzers=1; shift ;;
196
197    *)
198      echo "unknown option: $1" | tee -a configure.log
199      echo "$0 --help for help" | tee -a configure.log
200      leave 1;;
201    esac
202done
203
204# temporary file name
205test=ztest$$
206
207# put arguments in log, also put test file in log if used in arguments
208show()
209{
210  case "$*" in
211    *$test.c*)
212      echo === $test.c === >> configure.log
213      cat $test.c >> configure.log
214      echo === >> configure.log;;
215  esac
216  echo $* >> configure.log
217}
218
219# check for gcc vs. cc and set compile and link flags based on the system identified by uname
220cat > $test.c <<EOF
221extern int getchar();
222int main() {return getchar();}
223EOF
224
225cc=${CC-${CROSS_PREFIX}gcc}
226echo -n "Checking for compiler... " | tee -a configure.log
227case "$cc" in
228  *gcc*) gcc=1 ;;
229  *clang*) gcc=1 ;;
230esac
231case `$cc -v 2>&1` in
232  *gcc*) gcc=1 ;;
233  *clang*) gcc=1 ;;
234esac
235
236if test $native -eq 1; then
237  avx2flag=""
238  sse2flag=""
239  ssse3flag=""
240  sse4flag=""
241  sse42flag=""
242  pclmulflag=""
243  noltoflag=""
244fi
245
246if test $build32 -eq 1; then
247  CFLAGS="${CFLAGS} -m32"
248  SFLAGS="${SFLAGS} -m32"
249  LDFLAGS="${LDFLAGS} -m32"
250fi
251if test $build64 -eq 1; then
252  CFLAGS="${CFLAGS} -m64"
253  SFLAGS="${SFLAGS} -m64"
254  LDFLAGS="${LDFLAGS} -m64"
255fi
256
257# Set library name depending on zlib-compat option
258if test $compat -eq 0; then
259  LIBNAME=libz-ng
260  LIBNAME2=zlib-ng
261  SUFFIX=-ng
262else
263  LIBNAME=libz
264  LIBNAME2=zlib
265  SUFFIX=""
266fi
267
268STATICLIB=${LIBNAME}.a
269MAPNAME=${LIBNAME2}.map
270
271# extract zlib version numbers from zlib.h
272if test $compat -eq 0; then
273  VER=`sed -n -e '/ZLIBNG_VERSION "/s/.*"\(.*\)".*/\1/p' < ${SRCDIR}/zlib-ng.h`
274  VER3=`sed -n -e '/ZLIBNG_VERSION "/s/.*"\([0-9]*\\.[0-9]*\\.[0-9]*\).*/\1/p' < ${SRCDIR}/zlib-ng.h`
275  VER2=`sed -n -e '/ZLIBNG_VERSION "/s/.*"\([0-9]*\\.[0-9]*\)\\..*/\1/p' < ${SRCDIR}/zlib-ng.h`
276  VER1=`sed -n -e '/ZLIBNG_VERSION "/s/.*"\([0-9]*\)\\..*/\1/p' < ${SRCDIR}/zlib-ng.h`
277else
278  VER=`sed -n -e '/ZLIB_VERSION "/s/.*"\(.*\)".*/\1/p' < ${SRCDIR}/zlib.h`
279  VER3=`sed -n -e '/ZLIB_VERSION "/s/.*"\([0-9]*\\.[0-9]*\\.[0-9]*\).*/\1/p' < ${SRCDIR}/zlib.h`
280  VER2=`sed -n -e '/ZLIB_VERSION "/s/.*"\([0-9]*\\.[0-9]*\)\\..*/\1/p' < ${SRCDIR}/zlib.h`
281  VER1=`sed -n -e '/ZLIB_VERSION "/s/.*"\([0-9]*\)\\..*/\1/p' < ${SRCDIR}/zlib.h`
282fi
283
284show $cc -c $test.c
285if test "$gcc" -eq 1 && ($cc $CFLAGS -c $test.c) >> configure.log 2>&1; then
286  echo "$cc" | tee -a configure.log
287  CC="$cc"
288  CFLAGS="${CFLAGS} -std=c99"
289
290  # Re-check ARCH if the compiler is a cross-compiler.
291  if $CC -print-multiarch 1> /dev/null 2>&1 && test -n "$($CC -print-multiarch)" 1> /dev/null 2>&1; then
292      CC_ARCH=`$CC $CFLAGS -print-multiarch | sed 's/-.*//g'`
293  else
294      CC_ARCH=`$CC $CFLAGS -dumpmachine | sed 's/-.*//g'`
295  fi
296  case $CC_ARCH in
297    i386 | i486 | i586 | i686)
298      # Honor user choice if gcc is multilib and 64-bit is requested
299      if test $build64 -eq 1; then
300        ARCH=x86_64
301      else
302        ARCH=$CC_ARCH
303      fi ;;
304    x86_64)
305      # Honor user choice if gcc is multilib and 32-bit is requested
306      if test $build32 -ne 1; then
307        ARCH=$CC_ARCH
308      fi ;;
309    arm | armeb)
310      if test $native -eq 0; then
311        ARCH=arm
312      else
313        ARCH=native
314      fi
315      if test "${uname}" = "eabi"; then
316        # No ACLE support
317        uname=arm
318        if test $buildacle -eq 1; then
319          echo ACLE support not available
320          buildacle=0
321        fi
322      fi
323      if test $buildacle -eq 1; then
324        if test $native -eq 0; then
325          ARCH=armv8-a+crc
326        fi
327      fi ;;
328    armv8l)
329      if test $native -eq 0; then
330        ARCH=armv8-a
331      else
332        ARCH=native
333      fi ;;
334    aarch64 | aarch64_be)
335      if test "${uname}" = "elf"; then
336        uname=aarch64
337      fi
338      if test $native -eq 0; then
339        ARCH=aarch64
340      else
341        ARCH=native
342      fi ;;
343    powerpc | ppc)
344      ARCH=powerpc ;;
345    powerpc64 | ppc64)
346      ARCH=powerpc64 ;;
347    powerpc64le | ppc64le)
348      ARCH=powerpc64le ;;
349  esac
350  CFLAGS="-O2 ${CFLAGS}"
351  if test -n "${ARCHS}"; then
352    CFLAGS="${CFLAGS} ${ARCHS}"
353    LDFLAGS="${LDFLAGS} ${ARCHS}"
354  fi
355  CFLAGS="${CFLAGS} -Wall"
356  SFLAGS="${CFLAGS} -fPIC"
357  if test $native -eq 1; then
358    case $ARCH in
359      powerpc*)
360        NATIVE_FLAG="-mcpu=native" ;;
361      *)
362        NATIVE_FLAG="-march=native" ;;
363    esac
364    CFLAGS="${CFLAGS} ${NATIVE_FLAG}"
365    SFLAGS="${SFLAGS} ${NATIVE_FLAG}"
366  fi
367  if test "$warn" -eq 1; then
368    CFLAGS="${CFLAGS} -Wextra -Wpedantic -Wno-implicit-fallthrough"
369  fi
370  if test $debug -eq 1; then
371    CFLAGS="${CFLAGS} -DZLIB_DEBUG"
372    SFLAGS="${SFLAGS} -DZLIB_DEBUG"
373  fi
374  if test -z "$uname"; then
375    uname=`(uname -s || echo unknown) 2>/dev/null`
376  fi
377  case "$uname" in
378  Linux* | linux* | GNU | GNU/* | solaris*)
379        LDSHARED=${LDSHARED-"$cc"}
380        LDSHAREDFLAGS="-shared -Wl,-soname,${LIBNAME}.so.${VER1},--version-script,${SRCDIR}/${MAPNAME}" ;;
381  *BSD | *bsd* | DragonFly)
382        LDSHARED=${LDSHARED-"$cc"}
383        LDSHAREDFLAGS="-shared -Wl,-soname,${LIBNAME}.so.${VER1},--version-script,${SRCDIR}/${MAPNAME}"
384        LDCONFIG="ldconfig -m" ;;
385  CYGWIN* | Cygwin* | cygwin*)
386        ARFLAGS="rcs"
387        SFLAGS="${CFLAGS}"
388        shared_ext='.dll'
389        sharedlibdir='${bindir}'
390        if test $compat -eq 0; then
391          SHAREDLIB=cygz-ng$shared_ext
392        else
393          SHAREDLIB=cygz$shared_ext
394        fi
395        SHAREDLIBM=''
396        SHAREDLIBV=''
397        SHAREDTARGET=$SHAREDLIB
398        IMPORTLIB="${LIBNAME}.dll.a"
399        LDSHARED=${LDSHARED-"$cc"}
400        LDSHAREDFLAGS="-shared -Wl,--out-implib,${IMPORTLIB},--version-script,${SRCDIR}/${MAPNAME}"
401        LDSHAREDLIBC=""
402        DEFFILE='win32/${LIBNAME2}.def'
403        RC="${CROSS_PREFIX}windres"
404        RCFLAGS='--define GCC_WINDRES'
405        RCOBJS='zlibrc.o'
406        STRIP="${CROSS_PREFIX}strip"
407        EXE='.exe' ;;
408  MSYS* | msys*)
409        ARFLAGS="rcs"
410        SFLAGS="${CFLAGS}"
411        shared_ext='.dll'
412        sharedlibdir='${bindir}'
413        if test $compat -eq 0; then
414          SHAREDLIB=msys-z-ng$shared_ext
415        else
416          SHAREDLIB=msys-z$shared_ext
417        fi
418        SHAREDLIBM=''
419        SHAREDLIBV=''
420        SHAREDTARGET=$SHAREDLIB
421        IMPORTLIB="${LIBNAME}.dll.a"
422        LDSHARED=${LDSHARED-"$cc"}
423        LDSHAREDFLAGS="-shared -Wl,--out-implib,${IMPORTLIB}"
424        LDSHAREDLIBC=""
425        DEFFILE='win32/${LIBNAME2}.def'
426        RC="${CROSS_PREFIX}windres"
427        RCFLAGS='--define GCC_WINDRES'
428        RCOBJS='zlibrc.o'
429        STRIP="${CROSS_PREFIX}strip"
430        EXE='.exe' ;;
431  MINGW* | mingw*)
432        ARFLAGS="rcs"
433        CFLAGS="${CFLAGS} -D_POSIX_C_SOURCE=200809L -D_GNU_SOURCE=1"
434        SFLAGS="${CFLAGS}"
435        shared_ext='.dll'
436        sharedlibdir='${bindir}'
437        SHAREDLIB=${LIBNAME}-$VER1$shared_ext
438        SHAREDLIBM=''
439        SHAREDLIBV=''
440        SHAREDTARGET=$SHAREDLIB
441        IMPORTLIB="${LIBNAME}.dll.a"
442        LDSHARED=${LDSHARED-"$cc"}
443        LDSHAREDFLAGS="-shared -Wl,--out-implib=${IMPORTLIB} -Wl,--version-script=${SRCDIR}/${MAPNAME}"
444        LDSHAREDLIBC=""
445        DEFFILE='win32/${LIBNAME2}.def'
446        RC="${CROSS_PREFIX}windres"
447        RCFLAGS='--define GCC_WINDRES'
448        if [ "$CC" == "mingw32-gcc" ]; then
449          case $ARCH in
450          i386 | i486 | i586 | i686) RCFLAGS="${RCFLAGS} -F pe-i386";;
451          esac;
452        fi
453        RCOBJS='zlibrc.o'
454        STRIP="${CROSS_PREFIX}strip"
455        EXE='.exe' ;;
456  QNX*)  # This is for QNX6. I suppose that the QNX rule below is for QNX2,QNX4
457         # (alain.bonnefoy@icbt.com)
458                 LDSHARED=${LDSHARED-"$cc"}
459                 LDSHAREDFLAGS="-shared -Wl,-h${LIBNAME}.so.${VER1}" ;;
460  HP-UX*)
461         LDSHARED=${LDSHARED-"$cc"}
462         LDSHAREDFLAGS="-shared"
463         case `(uname -m || echo unknown) 2>/dev/null` in
464         ia64)
465                 shared_ext='.so'
466                 SHAREDLIB='${LIBNAME}.so' ;;
467         *)
468                 shared_ext='.sl'
469                 SHAREDLIB='${LIBNAME}.sl' ;;
470         esac ;;
471  Darwin* | darwin*)
472             shared_ext='.dylib'
473             SHAREDLIB=${LIBNAME}$shared_ext
474             SHAREDLIBV=${LIBNAME}.$VER$shared_ext
475             SHAREDLIBM=${LIBNAME}.$VER1$shared_ext
476             SHAREDTARGET=$SHAREDLIBV
477             LDSHARED=${LDSHARED-"$cc"}
478             LDSHAREDFLAGS="-dynamiclib -install_name $libdir/$SHAREDLIBM -compatibility_version $VER1 -current_version $VER3"
479             if libtool -V 2>&1 | grep Apple > /dev/null; then
480                 AR="libtool"
481             else
482                 AR="/usr/bin/libtool"
483             fi
484             ARFLAGS="-o" ;;
485  aarch64)
486             LDSHARED=${LDSHARED-"$cc"}
487             LDSHAREDFLAGS="-shared -Wl,-soname,${LIBNAME}.so.${VER1} -Wl,--version-script,${SRCDIR}/${MAPNAME}"
488             LDSHAREDLIBC="-Wl,--start-group -lc -lrdimon -Wl,--end-group" ;;
489  *)
490             LDSHARED=${LDSHARED-"$cc"}
491             LDSHAREDFLAGS="-shared" ;;
492  esac
493else
494  # find system name and corresponding cc options
495  CC=${CC-cc}
496  gcc=0
497  echo "$CC" | tee -a configure.log
498  if test -z "$uname"; then
499    uname=`(uname -sr || echo unknown) 2>/dev/null`
500  fi
501  case "$uname" in
502  HP-UX*)    SFLAGS=${CFLAGS-"-O +z"}
503             CFLAGS=${CFLAGS-"-O"}
504             LDSHARED=${LDSHARED-"ld"}
505             LDSHAREDFLAGS="-b"
506         case `(uname -m || echo unknown) 2>/dev/null` in
507         ia64)
508             shared_ext='.so'
509             SHAREDLIB='${LIBNAME}.so' ;;
510         *)
511             shared_ext='.sl'
512             SHAREDLIB='${LIBNAME}.sl' ;;
513         esac ;;
514  AIX*)  # Courtesy of dbakker@arrayasolutions.com
515             SFLAGS=${CFLAGS-"-O -qmaxmem=8192"}
516             CFLAGS=${CFLAGS-"-O -qmaxmem=8192"}
517             LDSHARED=${LDSHARED-"xlc"}
518             LDSHAREDFLAGS="-G" ;;
519  # send working options for other systems to zlib@gzip.org
520  *)         SFLAGS=${CFLAGS-"-O"}
521             CFLAGS=${CFLAGS-"-O"}
522             LDSHARED=${LDSHARED-"cc"}
523             LDSHAREDFLAGS="-shared" ;;
524  esac
525fi
526
527# Simplify some later conditionals
528case "$uname" in
529Linux* | linux*)
530  LINUX=1 ;;
531*)
532  LINUX=0 ;;
533esac
534
535# destination names for shared library if not defined above
536SHAREDLIB=${SHAREDLIB-"${LIBNAME}$shared_ext"}
537SHAREDLIBV=${SHAREDLIBV-"${LIBNAME}$shared_ext.$VER"}
538SHAREDLIBM=${SHAREDLIBM-"${LIBNAME}$shared_ext.$VER1"}
539SHAREDTARGET=${SHAREDTARGET-"${LIBNAME}$shared_ext.$VER"}
540
541echo >> configure.log
542
543# define functions for testing compiler and library characteristics and logging the results
544
545cat > $test.c <<EOF
546#error error
547EOF
548if ($CC -c $CFLAGS $test.c) 2>/dev/null; then
549  try()
550  {
551    show $*
552    test "`( $* ) 2>&1 | tee -a configure.log`" = ""
553  }
554  echo - using any output from compiler to indicate an error >> configure.log
555else
556try()
557{
558  show $*
559  ( $* ) >> configure.log 2>&1
560  ret=$?
561  if test $ret -ne 0; then
562    echo "(exit code "$ret")" >> configure.log
563  fi
564  return $ret
565}
566fi
567
568tryboth()
569{
570  show $*
571  got=`( $* ) 2>&1`
572  ret=$?
573  printf %s "$got" >> configure.log
574  if test $ret -ne 0; then
575    return $ret
576  fi
577  test "$got" = ""
578}
579
580cat > $test.c << EOF
581int foo() { return 0; }
582EOF
583echo "Checking for obsessive-compulsive compiler options..." >> configure.log
584if try $CC -c $CFLAGS $test.c; then
585  :
586else
587  echo "Compiler error reporting is too harsh for $0 (perhaps remove -Werror)." | tee -a configure.log
588  leave 1
589fi
590
591echo >> configure.log
592
593if test "$with_sanitizer" = "address"; then
594    echo -n "Checking for address sanitizer... " | tee -a configure.log
595    sanitizers=""
596    for san in address pointer-compare pointer-subtract; do
597        if try $CC -c $CFLAGS $test.c -fsanitize=$san ; then
598            if test -n "$sanitizers"; then
599                sanitizers="$sanitizers,$san"
600            else
601                sanitizers="$san"
602            fi
603        fi
604    done
605
606    if test -n "$sanitizers"; then
607        echo "-fsanitize=$sanitizers" | tee -a configure.log
608        CFLAGS="$CFLAGS -fsanitize=$sanitizers"
609        SFLAGS="$SFLAGS -fsanitize=$sanitizers"
610        LDFLAGS="$LDFLAGS -fsanitize=$sanitizers"
611    else
612        echo No | tee -a configure.log
613    fi
614
615    echo -n "Checking for leak sanitizer... " | tee -a configure.log
616    if try $CC -c $CFLAGS $test.c -fsanitize=leak; then
617        echo "-fsanitize=leak" | tee -a configure.log
618        CFLAGS="$CFLAGS -fsanitize=leak"
619        SFLAGS="$SFLAGS -fsanitize=leak"
620        LDFLAGS="$LDFLAGS -fsanitize=leak"
621    else
622        echo No | tee -a configure.log
623    fi
624
625    echo >> configure.log
626fi
627
628if test "$with_sanitizer" = "memory"; then
629    echo -n "Checking for memory sanitizer... " | tee -a configure.log
630    if try $CC -c $CFLAGS $test.c -fsanitize=memory ; then
631        echo "-fsanitize=memory" | tee -a configure.log
632        CFLAGS="$CFLAGS -fsanitize=memory"
633        SFLAGS="$SFLAGS -fsanitize=memory"
634        LDFLAGS="$LDFLAGS -fsanitize=memory"
635    else
636        echo No | tee -a configure.log
637    fi
638
639    echo >> configure.log
640fi
641
642if test "$with_sanitizer" = "undefined"; then
643    echo -n "Checking for undefined behavior sanitizer... " | tee -a configure.log
644    sanitizers=""
645    for san in array-bounds bool bounds builtin enum float-cast-overflow float-divide-by-zero function integer-divide-by-zero local-bounds null nonnull-attribute object-size pointer-overflow return returns-nonnull-attribute shift shift-base shift-exponent signed-integer-overflow undefined unsigned-integer-overflow unsigned-shift-base vla-bound vptr; do
646        if try $CC -c $CFLAGS $test.c -fsanitize=$san; then
647            if test -n "$sanitizers"; then
648                sanitizers="$sanitizers,$san"
649            else
650                sanitizers="$san"
651            fi
652        fi
653    done
654
655    if test -n "$sanitizers"; then
656        echo "-fsanitize=$sanitizers" | tee -a configure.log
657        CFLAGS="$CFLAGS -fsanitize=$sanitizers"
658        SFLAGS="$SFLAGS -fsanitize=$sanitizers"
659        LDFLAGS="$LDFLAGS -fsanitize=$sanitizers"
660    else
661        echo No | tee -a configure.log
662    fi
663
664    echo >> configure.log
665fi
666
667# see if shared library build supported
668cat > $test.c <<EOF
669extern int getchar();
670int hello() {return getchar();}
671EOF
672if test $shared -eq 1; then
673  echo -n "Checking for shared library support... " | tee -a configure.log
674  # we must test in two steps (cc then ld), required at least on SunOS 4.x
675  if try $CC -w -c $SFLAGS $test.c &&
676     try $LDSHARED $LDSHAREDFLAGS $LDFLAGS -o $test$shared_ext $test.o $LDSHAREDLIBC; then
677    echo "Building shared library $SHAREDTARGET with $CC." | tee -a configure.log
678  elif test -z "$old_cc" -a -z "$old_cflags"; then
679    echo "No shared library support." | tee -a configure.log
680    shared=0;
681  else
682    echo 'No shared library support; try without defining CC and CFLAGS' | tee -a configure.log
683    shared=0;
684  fi
685fi
686if test $shared -eq 0; then
687  LDSHARED="$CC"
688  LDSHAREDFLAGS=""
689  ALL="static"
690  SHAREDLIB=""
691  SHAREDLIBV=""
692  SHAREDLIBM=""
693  SHAREDTARGET=""
694  INSTALLTARGETS=install-static
695  UNINSTALLTARGETS=uninstall-static
696  echo Building static library $STATICLIB version $VER with $CC. | tee -a configure.log
697else
698  ALL="static shared"
699  TEST="${TEST} testshared"
700fi
701
702echo >> configure.log
703
704# check for large file support, and if none, check for fseeko()
705cat > $test.c <<EOF
706#include <sys/types.h>
707off64_t dummy = 0;
708EOF
709if try $CC -c $CFLAGS -D_LARGEFILE64_SOURCE=1 $test.c; then
710  CFLAGS="${CFLAGS} -D_LARGEFILE64_SOURCE=1"
711  SFLAGS="${SFLAGS} -D_LARGEFILE64_SOURCE=1"
712  echo "Checking for off64_t... Yes." | tee -a configure.log
713  echo "Checking for fseeko... Yes." | tee -a configure.log
714else
715  echo "Checking for off64_t... No." | tee -a configure.log
716  echo >> configure.log
717  cat > $test.c <<EOF
718#include <sys/types.h>
719int main() {
720  _off64_t dummy = 0;
721  return 0;
722}
723EOF
724  if try $CC $CFLAGS -o $test $test.c $LDSHAREDLIBC; then
725    echo "Checking for _off64_t... Yes." | tee -a configure.log
726  else
727    echo "Checking for _off64_t... No." | tee -a configure.log
728  fi
729  echo >> configure.log
730  cat > $test.c <<EOF
731#include <stdio.h>
732int main(void) {
733  fseeko(NULL, 0, 0);
734  return 0;
735}
736EOF
737  if try $CC $CFLAGS -o $test $test.c $LDSHAREDLIBC; then
738    echo "Checking for fseeko... Yes." | tee -a configure.log
739  else
740    CFLAGS="${CFLAGS} -DNO_FSEEKO"
741    SFLAGS="${SFLAGS} -DNO_FSEEKO"
742    echo "Checking for fseeko... No." | tee -a configure.log
743  fi
744fi
745
746echo >> configure.log
747
748# check for strerror() for use by gz* functions
749cat > $test.c <<EOF
750#include <string.h>
751#include <errno.h>
752int main() { return strlen(strerror(errno)); }
753EOF
754if try $CC $CFLAGS -o $test $test.c $LDSHAREDLIBC; then
755  echo "Checking for strerror... Yes." | tee -a configure.log
756else
757  CFLAGS="${CFLAGS} -DNO_STRERROR"
758  SFLAGS="${SFLAGS} -DNO_STRERROR"
759  echo "Checking for strerror... No." | tee -a configure.log
760fi
761
762# We need to remove zconf.h from source directory if building outside of it
763if [ "$SRCDIR" != "$BUILDDIR" ]; then
764    rm -f $SRCDIR/zconf${SUFFIX}.h
765fi
766
767# copy clean zconf.h for subsequent edits
768cp -p $SRCDIR/zconf${SUFFIX}.h.in zconf${SUFFIX}.h
769
770echo >> configure.log
771
772# check for unistd.h and save result in zconf.h
773cat > $test.c <<EOF
774#include <unistd.h>
775int main() { return 0; }
776EOF
777if try $CC -c $CFLAGS $test.c; then
778  sed < zconf${SUFFIX}.h "/^#ifdef HAVE_UNISTD_H.* may be/s/def HAVE_UNISTD_H\(.*\) may be/ 1\1 was/" > zconf${SUFFIX}.temp.h
779  mv zconf${SUFFIX}.temp.h zconf${SUFFIX}.h
780  echo "Checking for unistd.h... Yes." | tee -a configure.log
781else
782  echo "Checking for unistd.h... No." | tee -a configure.log
783fi
784
785echo >> configure.log
786
787# check for ptrdiff_t and save result in zconf.h
788echo -n "Checking for ptrdiff_t... " | tee -a configure.log
789cat > $test.c <<EOF
790#include <stddef.h>
791int fun(ptrdiff_t *a) { (void)a; return 0; }
792EOF
793if try $CC -c $CFLAGS $test.c; then
794  echo "Yes." | tee -a configure.log
795else
796    echo "No." | tee -a configure.log
797    sed < zconf${SUFFIX}.h "/^#ifdef NEED_PTRDIFF_T.* may be/s/def NEED_PTRDIFF_T\(.*\) may be/ 1\1 was/" > zconf${SUFFIX}.temp.h
798    mv zconf${SUFFIX}.temp.h zconf${SUFFIX}.h
799
800    echo -n "Checking for sizeof(void *)... " | tee -a configure.log
801    cat > $test.c <<EOF
802#include <stdint.h>
803#define COMPILE_TIME_ASSERT(pred) struct s { int x: (pred) ? 1 : -1; }
804COMPILE_TIME_ASSERT(sizeof(int32_t) == sizeof(void *));
805EOF
806    if try $CC -c $CFLAGS $test.c; then
807        echo "sizeof(int32_t)." | tee -a configure.log
808        sed < zconf${SUFFIX}.h "s/^typedef PTRDIFF_TYPE ptrdiff_t;/typedef int32_t ptrdiff_t;/g" > zconf${SUFFIX}.temp.h
809        mv zconf${SUFFIX}.temp.h zconf${SUFFIX}.h
810    else
811        cat > $test.c <<EOF
812#include <stdint.h>
813#define COMPILE_TIME_ASSERT(pred) struct s { int x: (pred) ? 1 : -1; }
814COMPILE_TIME_ASSERT(sizeof(int64_t) == sizeof(void *));
815EOF
816        if try $CC -c $CFLAGS $test.c; then
817            echo "sizeof(int64_t)." | tee -a configure.log
818            sed < zconf${SUFFIX}.h "s/^typedef PTRDIFF_TYPE ptrdiff_t;/typedef int64_t ptrdiff_t;/g" > zconf${SUFFIX}.temp.h
819            mv zconf${SUFFIX}.temp.h zconf${SUFFIX}.h
820        else
821            echo "unknown." | tee -a configure.log
822            exit 1
823        fi
824    fi
825fi
826
827# if --zlib-compat was requested
828if test $compat -eq 1; then
829  gzfileops=1
830  CFLAGS="${CFLAGS} -DZLIB_COMPAT"
831  SFLAGS="${SFLAGS} -DZLIB_COMPAT"
832  case "$uname" in
833  CYGWIN* | Cygwin* | cygwin* | MSYS* | msys* | MINGW* | mingw*)
834    DEFFILE="win32/zlibcompat.def" ;;
835  esac
836fi
837
838# if --gzfileops was requested
839if test $gzfileops -eq 1; then
840  CFLAGS="${CFLAGS} -DWITH_GZFILEOP"
841  SFLAGS="${SFLAGS} -DWITH_GZFILEOP"
842  OBJC="${OBJC} \$(OBJG)"
843  PIC_OBJC="${PIC_OBJC} \$(PIC_OBJG)"
844fi
845
846# if code coverage testing was requested, use older gcc if defined, e.g. "gcc-4.2" on Mac OS X
847if test $cover -eq 1; then
848  CFLAGS="${CFLAGS} -fprofile-arcs -ftest-coverage"
849  LDFLAGS="${LDFLAGS} -fprofile-arcs -ftest-coverage"
850  if test -n "$GCC_CLASSIC"; then
851    CC=$GCC_CLASSIC
852  fi
853fi
854
855echo >> configure.log
856
857# Check for ANSI C compliant compiler
858cat > $test.c <<EOF
859#include <stdio.h>
860#include <stdarg.h>
861#include "zconf${SUFFIX}.h"
862int main() {
863#ifdef STDC
864  return 0;
865#endif
866  return 1;
867}
868EOF
869if try $CC -c $CFLAGS $test.c; then
870  echo "Checking for ANSI C compliant compiler...  Yes." | tee -a configure.log
871  :
872else
873  echo "Checking for ANSI C compliant compiler...  No." | tee -a configure.log
874  echo "Error: ANSI C compatible compiler needed, cannot continue." | tee -a configure.log
875  leave 1
876fi
877
878# Check for -fno-semantic-interposition compiler support
879echo "" > test.c
880  cat > $test.c <<EOF
881int main() { return 0; }
882EOF
883if test "$gcc" -eq 1 && ($cc $CFLAGS -fno-semantic-interposition -c $test.c) >> configure.log 2>&1; then
884  echo "Checking for -fno-semantic-interposition... Yes." | tee -a configure.log
885  SFLAGS="$SFLAGS -fno-semantic-interposition"
886else
887  echo "Checking for -fno-semantic-interposition... No." | tee -a configure.log
888fi
889
890# Check for -fno-lto compiler support
891if test $gcc -eq 1 -a $without_optimizations -eq 0 -a $native -eq 0; then
892  cat > $test.c <<EOF
893int main() { return 0; }
894EOF
895  if $cc $CFLAGS -fno-lto -c $test.c >> configure.log 2>&1; then
896    echo "Checking for -fno-lto... Yes." | tee -a configure.log
897  else
898    echo "Checking for -fno-lto... No." | tee -a configure.log
899    noltoflag=""
900  fi
901fi
902
903# see if we can hide zlib internal symbols that are linked between separate source files using hidden
904if test "$gcc" -eq 1; then
905  echo >> configure.log
906  cat > $test.c <<EOF
907#define Z_INTERNAL __attribute__((visibility ("hidden")))
908int Z_INTERNAL foo;
909int main() { return 0; }
910EOF
911  if tryboth $CC -c $CFLAGS $test.c; then
912    CFLAGS="$CFLAGS -DHAVE_VISIBILITY_HIDDEN"
913    SFLAGS="$SFLAGS -DHAVE_VISIBILITY_HIDDEN"
914    echo >> configure.log
915    echo "Checking for attribute(visibility(hidden)) support... Yes." | tee -a configure.log
916  else
917    echo >> configure.log
918    echo "Checking for attribute(visibility(hidden)) support... No." | tee -a configure.log
919  fi
920fi
921
922# see if we can hide zlib internal symbols that are linked between separate source files using internal
923if test "$gcc" -eq 1; then
924  echo >> configure.log
925  cat > $test.c <<EOF
926#define Z_INTERNAL __attribute__((visibility ("internal")))
927int Z_INTERNAL foo;
928int main() { return 0; }
929EOF
930  if tryboth $CC -c $CFLAGS $test.c; then
931    CFLAGS="$CFLAGS -DHAVE_VISIBILITY_INTERNAL"
932    SFLAGS="$SFLAGS -DHAVE_VISIBILITY_INTERNAL"
933    echo >> configure.log
934    echo "Checking for attribute(visibility(internal)) support... Yes." | tee -a configure.log
935  else
936    echo >> configure.log
937    echo "Checking for attribute(visibility(internal)) support... No." | tee -a configure.log
938  fi
939fi
940
941# Check for __builtin_ctz() support in compiler
942cat > $test.c << EOF
943int main(void) {
944    unsigned int zero = 0;
945    long test = __builtin_ctz(zero);
946    (void)test;
947    return 0;
948}
949EOF
950if try ${CC} ${CFLAGS} $test.c $LDSHAREDLIBC; then
951    echo "Checking for __builtin_ctz ... Yes." | tee -a configure.log
952    CFLAGS="$CFLAGS -DHAVE_BUILTIN_CTZ"
953    SFLAGS="$SFLAGS -DHAVE_BUILTIN_CTZ"
954else
955    echo "Checking for __builtin_ctz ... No." | tee -a configure.log
956fi
957
958# Check for __builtin_ctzll() support in compiler
959cat > $test.c << EOF
960int main(void) {
961    unsigned long long zero = 0;
962    long test = __builtin_ctzll(zero);
963    (void)test;
964    return 0;
965}
966EOF
967if try ${CC} ${CFLAGS} $test.c $LDSHAREDLIBC; then
968    echo "Checking for __builtin_ctzll ... Yes." | tee -a configure.log
969    CFLAGS="$CFLAGS -DHAVE_BUILTIN_CTZLL"
970    SFLAGS="$SFLAGS -DHAVE_BUILTIN_CTZLL"
971else
972    echo "Checking for __builtin_ctzll ... No." | tee -a configure.log
973fi
974
975# Check for SSE2 intrinsics
976case "${ARCH}" in
977    i386 | i486 | i586 | i686 | x86_64)
978        cat > $test.c << EOF
979#include <immintrin.h>
980int main(void) {
981    __m128i zero = _mm_setzero_si128();
982    (void)zero;
983    return 0;
984}
985EOF
986        if try ${CC} ${CFLAGS} ${sse2flag} $test.c; then
987            echo "Checking for SSE2 intrinsics ... Yes." | tee -a configure.log
988            HAVE_SSE2_INTRIN=1
989        else
990            echo "Checking for SSE2 intrinsics ... No." | tee -a configure.log
991            HAVE_SSE2_INTRIN=0
992        fi
993        ;;
994esac
995
996# Check for SSSE3 intrinsics
997
998cat > $test.c << EOF
999#include <x86intrin.h>
1000int main(void)
1001{
1002    __m128i u, v, w;
1003    u = _mm_set1_epi32(1);
1004    v = _mm_set1_epi32(2);
1005    w = _mm_hadd_epi32(u, v);
1006    (void)w;
1007    return 0;
1008}
1009EOF
1010if try ${CC} ${CFLAGS} ${ssse3flag} $test.c; then
1011    echo "Checking for SSSE3 intrinsics ... Yes." | tee -a configure.log
1012    HAVE_SSSE3_INTRIN=1
1013else
1014    echo "Checking for SSSE3 intrinsics ... No." | tee -a configure.log
1015    HAVE_SSSE3_INTRIN=0
1016fi
1017
1018# Check for SSE4.2 CRC inline assembly
1019case "${ARCH}" in
1020    i386 | i486 | i586 | i686 | x86_64)
1021        cat > $test.c << EOF
1022int main(void) {
1023    unsigned val = 0, h = 0;
1024    __asm__ __volatile__ ( "crc32 %1,%0" : "+r" (h) : "r" (val) );
1025    return (int) h;
1026}
1027EOF
1028        if try ${CC} ${CFLAGS} ${sse42flag} $test.c; then
1029            echo "Checking for SSE4.2 CRC inline assembly ... Yes." | tee -a configure.log
1030            HAVE_SSE42CRC_INLINE_ASM=1
1031        else
1032            echo "Checking for SSE4.2 CRC inline assembly ... No." | tee -a configure.log
1033            HAVE_SSE42CRC_INLINE_ASM=0
1034        fi
1035        ;;
1036esac
1037
1038# Check for SSE4.2 CRC intrinsics
1039case "${ARCH}" in
1040    i386 | i486 | i586 | i686 | x86_64)
1041        cat > $test.c << EOF
1042int main(void) {
1043    unsigned crc = 0;
1044    char c = 'c';
1045    crc = __builtin_ia32_crc32qi(crc, c);
1046    (void)crc;
1047    return 0;
1048}
1049EOF
1050        if try ${CC} ${CFLAGS} ${sse42flag} $test.c; then
1051            echo "Checking for SSE4.2 CRC intrinsics ... Yes." | tee -a configure.log
1052            HAVE_SSE42CRC_INTRIN=1
1053        else
1054            echo "Checking for SSE4.2 CRC intrinsics ... No." | tee -a configure.log
1055            HAVE_SSE42CRC_INTRIN=0
1056        fi
1057        ;;
1058esac
1059
1060# Check for SSE4.2 compare string intrinsics
1061case "${ARCH}" in
1062    i386 | i486 | i586 | i686 | x86_64)
1063        cat > $test.c << EOF
1064#include <immintrin.h>
1065int main(void)
1066{
1067    unsigned char a[64] = { 0 };
1068    unsigned char b[64] = { 0 };
1069    __m128i xmm_src0, xmm_src1;
1070    xmm_src0 = _mm_loadu_si128((__m128i *)(char *)a);
1071    xmm_src1 = _mm_loadu_si128((__m128i *)(char *)b);
1072    return _mm_cmpestri(xmm_src0, 16, xmm_src1, 16, 0);
1073}
1074EOF
1075        if try ${CC} ${CFLAGS} ${sse42flag} $test.c; then
1076            echo "Checking for SSE4.2 compare string intrinsics ... Yes." | tee -a configure.log
1077            HAVE_SSE42CMPSTR_INTRIN=1
1078        else
1079            echo "Checking for SSE4.2 compare string intrinsics ... No." | tee -a configure.log
1080            HAVE_SSE42CMPSTR_INTRIN=0
1081        fi
1082        ;;
1083esac
1084
1085# Check for PCLMULQDQ intrinsics
1086case "${ARCH}" in
1087    i386 | i486 | i586 | i686 | x86_64)
1088        cat > $test.c << EOF
1089#include <immintrin.h>
1090#include <wmmintrin.h>
1091int main(void) {
1092    __m128i a = _mm_setzero_si128();
1093    __m128i b = _mm_setzero_si128();
1094    __m128i c = _mm_clmulepi64_si128(a, b, 0x10);
1095    (void)c;
1096    return 0;
1097}
1098EOF
1099        if try ${CC} ${CFLAGS} ${pclmulflag} $test.c; then
1100            echo "Checking for PCLMULQDQ intrinsics ... Yes." | tee -a configure.log
1101            HAVE_PCLMULQDQ_INTRIN=1
1102        else
1103            echo "Checking for PCLMULQDQ intrinsics ... No." | tee -a configure.log
1104            HAVE_PCLMULQDQ_INTRIN=0
1105        fi
1106
1107        # Enable deflate_medium at level 1
1108        if test $without_new_strategies -eq 1; then
1109            CFLAGS="${CFLAGS} -DNO_QUICK_STRATEGY"
1110            SFLAGS="${SFLAGS} -DNO_QUICK_STRATEGY"
1111        fi
1112        # Enable deflate_medium at level 4-6
1113        if test $without_new_strategies -eq 1; then
1114            CFLAGS="${CFLAGS} -DNO_MEDIUM_STRATEGY"
1115            SFLAGS="${SFLAGS} -DNO_MEDIUM_STRATEGY"
1116        fi
1117        ;;
1118esac
1119
1120# Check for AVX2 intrinsics
1121case "${ARCH}" in
1122    i386 | i486 | i586 | i686 | x86_64)
1123        cat > $test.c << EOF
1124#include <immintrin.h>
1125int main(void) {
1126    __m256i x = _mm256_set1_epi16(2);
1127    const __m256i y = _mm256_set1_epi16(1);
1128    x = _mm256_subs_epu16(x, y);
1129    (void)x;
1130    return 0;
1131}
1132EOF
1133        if try ${CC} ${CFLAGS} ${avx2flag} $test.c; then
1134            echo "Checking for AVX2 intrinsics ... Yes." | tee -a configure.log
1135            HAVE_AVX2_INTRIN=1
1136        else
1137            echo "Checking for AVX2 intrinsics ... No." | tee -a configure.log
1138            HAVE_AVX2_INTRIN=0
1139        fi
1140        ;;
1141esac
1142
1143
1144# Check whether -mfpu=neon is available on ARM processors.
1145case "${ARCH}" in
1146    arm*)
1147        cat > $test.c << EOF
1148int main() { return 0; }
1149EOF
1150        if try $CC -c $CFLAGS -mfpu=neon $test.c; then
1151            MFPU_NEON_AVAILABLE=1
1152            echo "Check whether -mfpu=neon is available ... Yes." | tee -a configure.log
1153        else
1154            MFPU_NEON_AVAILABLE=0
1155            echo "Check whether -mfpu=neon is available ... No." | tee -a configure.log
1156        fi
1157        ;;
1158esac
1159
1160# Check whether features needed by POWER optimisations are available
1161case "${ARCH}" in
1162    powerpc*)
1163        cat > $test.c << EOF
1164#include <sys/auxv.h>
1165int main() { return (getauxval(AT_HWCAP2) & PPC_FEATURE2_ARCH_2_07); }
1166EOF
1167        if try $CC -c $CFLAGS -mcpu=power8 $test.c; then
1168            HAVE_POWER8=1
1169            echo "Check whether POWER8 instructions are available ... Yes." | tee -a configure.log
1170        else
1171            HAVE_POWER8=0
1172            echo "Check whether POWER8 instructions are available ... No." | tee -a configure.log
1173        fi
1174esac
1175
1176# Check whether sys/sdt.h is available
1177cat > $test.c << EOF
1178#include <sys/sdt.h>
1179int main() { return 0; }
1180EOF
1181if try ${CC} ${CFLAGS} $test.c; then
1182    echo "Checking for sys/sdt.h ... Yes." | tee -a configure.log
1183    CFLAGS="$CFLAGS -DHAVE_SYS_SDT_H"
1184    SFLAGS="$SFLAGS -DHAVE_SYS_SDT_H"
1185else
1186    echo "Checking for sys/sdt.h ... No." | tee -a configure.log
1187fi
1188
1189ARCHDIR='arch/generic'
1190ARCH_STATIC_OBJS=''
1191ARCH_SHARED_OBJS=''
1192
1193# Set ARCH specific FLAGS
1194case "${ARCH}" in
1195    # x86/amd64 specific optimizations
1196    i386 | i486 | i586 | i686 |x86_64)
1197        ARCHDIR=arch/x86
1198
1199        CFLAGS="${CFLAGS} -DUNALIGNED_OK -DUNALIGNED64_OK"
1200        SFLAGS="${SFLAGS} -DUNALIGNED_OK -DUNALIGNED64_OK"
1201
1202        # Enable arch-specific optimizations
1203        if test $without_optimizations -eq 0; then
1204            CFLAGS="${CFLAGS} -DX86_FEATURES"
1205            SFLAGS="${SFLAGS} -DX86_FEATURES"
1206
1207            ARCH_STATIC_OBJS="${ARCH_STATIC_OBJS} x86.o"
1208            ARCH_SHARED_OBJS="${ARCH_SHARED_OBJS} x86.lo"
1209
1210            if test ${HAVE_AVX2_INTRIN} -eq 1; then
1211                CFLAGS="${CFLAGS} -DX86_AVX2 -DX86_AVX2_ADLER32 -DX86_AVX_CHUNKSET"
1212                SFLAGS="${SFLAGS} -DX86_AVX2 -DX86_AVX2_ADLER32 -DX86_AVX_CHUNKSET"
1213                ARCH_STATIC_OBJS="${ARCH_STATIC_OBJS} slide_avx.o chunkset_avx.o compare258_avx.o adler32_avx.o"
1214                ARCH_SHARED_OBJS="${ARCH_SHARED_OBJS} slide_avx.lo chunkset_avx.lo compare258_avx.lo adler32_avx.lo"
1215            fi
1216
1217            if test ${HAVE_SSE42CRC_INTRIN} -eq 1 || test ${HAVE_SSE42CRC_INLINE_ASM} -eq 1; then
1218                CFLAGS="${CFLAGS} -DX86_SSE42_CRC_HASH"
1219                SFLAGS="${SFLAGS} -DX86_SSE42_CRC_HASH"
1220
1221                if test ${HAVE_SSE42CRC_INTRIN} -eq 1; then
1222                  CFLAGS="${CFLAGS} -DX86_SSE42_CRC_INTRIN"
1223                  SFLAGS="${SFLAGS} -DX86_SSE42_CRC_INTRIN"
1224                fi
1225
1226                ARCH_STATIC_OBJS="${ARCH_STATIC_OBJS} insert_string_sse.o"
1227                ARCH_SHARED_OBJS="${ARCH_SHARED_OBJS} insert_string_sse.lo"
1228            fi
1229
1230            if test ${HAVE_SSE42CMPSTR_INTRIN} -eq 1; then
1231                CFLAGS="${CFLAGS} -DX86_SSE42_CMP_STR"
1232                SFLAGS="${SFLAGS} -DX86_SSE42_CMP_STR"
1233
1234                ARCH_STATIC_OBJS="${ARCH_STATIC_OBJS} compare258_sse.o"
1235                ARCH_SHARED_OBJS="${ARCH_SHARED_OBJS} compare258_sse.lo"
1236            fi
1237
1238            if test ${HAVE_SSE2_INTRIN} -eq 1; then
1239                CFLAGS="${CFLAGS} -DX86_SSE2 -DX86_SSE2_CHUNKSET"
1240                SFLAGS="${SFLAGS} -DX86_SSE2 -DX86_SSE2_CHUNKSET"
1241                ARCH_STATIC_OBJS="${ARCH_STATIC_OBJS} chunkset_sse.o slide_sse.o"
1242                ARCH_SHARED_OBJS="${ARCH_SHARED_OBJS} chunkset_sse.lo slide_sse.lo"
1243
1244                if test $forcesse2 -eq 1; then
1245                    CFLAGS="${CFLAGS} -DX86_NOCHECK_SSE2"
1246                    SFLAGS="${SFLAGS} -DX86_NOCHECK_SSE2"
1247                fi
1248            fi
1249
1250            if test ${HAVE_SSSE3_INTRIN} -eq 1; then
1251                CFLAGS="${CFLAGS} -DX86_SSSE3 -DX86_SSSE3_ADLER32"
1252                SFLAGS="${SFLAGS} -DX86_SSSE3 -DX86_SSSE3_ADLER32"
1253                ARCH_STATIC_OBJS="${ARCH_STATIC_OBJS} adler32_ssse3.o"
1254                ARCH_SHARED_OBJS="${ARCH_SHARED_OBJS} adler32_ssse3.lo"
1255            fi
1256
1257            if test ${HAVE_PCLMULQDQ_INTRIN} -eq 1; then
1258                CFLAGS="${CFLAGS} -DX86_PCLMULQDQ_CRC"
1259                SFLAGS="${SFLAGS} -DX86_PCLMULQDQ_CRC"
1260                ARCH_STATIC_OBJS="${ARCH_STATIC_OBJS} crc_folding.o"
1261                ARCH_SHARED_OBJS="${ARCH_SHARED_OBJS} crc_folding.lo"
1262            fi
1263        fi
1264    ;;
1265
1266    # ARM specific optimizations
1267    arm*)
1268        [ ! -z $CROSS_PREFIX ] && QEMU_ARCH=arm
1269        ARCHDIR=arch/arm
1270
1271        if test $without_optimizations -eq 0; then
1272            CFLAGS="${CFLAGS} -DARM_FEATURES"
1273            SFLAGS="${SFLAGS} -DARM_FEATURES"
1274            ARCH_STATIC_OBJS="${ARCH_STATIC_OBJS} armfeature.o"
1275            ARCH_SHARED_OBJS="${ARCH_SHARED_OBJS} armfeature.lo"
1276
1277            if test $LINUX -eq 1; then
1278                cat > $test.c <<EOF
1279#include <sys/auxv.h>
1280int main() {
1281    return (getauxval(AT_HWCAP2) & HWCAP2_CRC32);
1282}
1283EOF
1284                if try $CC -c $CFLAGS $test.c; then
1285                    CFLAGS="${CFLAGS} -DARM_AUXV_HAS_CRC32"
1286                    SFLAGS="${SFLAGS} -DARM_AUXV_HAS_CRC32"
1287                else
1288                    echo "HWCAP2_CRC32 not present in sys/auxv.h; cannot detect support at runtime." | tee -a configure.log
1289                fi
1290
1291                cat > $test.c <<EOF
1292#include <sys/auxv.h>
1293int main() {
1294    return (getauxval(AT_HWCAP) & HWCAP_NEON);
1295}
1296EOF
1297                if try $CC -c $CFLAGS $test.c; then
1298                    CFLAGS="${CFLAGS} -DARM_AUXV_HAS_NEON"
1299                    SFLAGS="${SFLAGS} -DARM_AUXV_HAS_NEON"
1300                else
1301                    echo "HWCAP_NEON not present in sys/auxv.h; cannot detect support at runtime." | tee -a configure.log
1302                fi
1303            fi
1304        fi
1305
1306        cat > $test.c << EOF
1307int main() { return 0; }
1308EOF
1309        if try $CC -w -c $SFLAGS $test.c -mfloat-abi=softfp &&
1310           try $LDSHARED $LDSHAREDFLAGS $LDFLAGS -o $test$shared_ext $test.o $LDSHAREDLIBC; then
1311            floatabi="-mfloat-abi=softfp"
1312        else
1313            if try $CC -w -c $SFLAGS $test.c -mfloat-abi=hard &&
1314               try $LDSHARED $LDSHAREDFLAGS $LDFLAGS -o $test$shared_ext $test.o $LDSHAREDLIBC; then
1315                floatabi="-mfloat-abi=hard"
1316            fi
1317        fi
1318
1319        if [ -z $floatabi ]; then
1320            echo "ARM floating point arch not auto-detected" | tee -a configure.log
1321        else
1322            echo "ARM floating point arch: ${floatabi}" | tee -a configure.log
1323
1324            CFLAGS="${CFLAGS} ${floatabi}"
1325            SFLAGS="${SFLAGS} ${floatabi}"
1326        fi
1327
1328        case "${ARCH}" in
1329            armv[345]*)
1330                if test $without_optimizations -eq 0; then
1331                    if test $buildacle -eq 1; then
1332                        echo ACLE support not available
1333                    fi
1334
1335                    if test $buildneon -eq 1; then
1336                        echo NEON support not available
1337                    fi
1338                fi
1339            ;;
1340            armv6l | armv6hl)
1341                CFLAGS="${CFLAGS} -DUNALIGNED_OK"
1342                SFLAGS="${SFLAGS} -DUNALIGNED_OK"
1343
1344                if test $without_optimizations -eq 0; then
1345                    if test $buildacle -eq 1; then
1346                        echo ACLE support not available
1347                    fi
1348
1349                    if test $buildneon -eq 1; then
1350                        echo NEON support not available
1351                    fi
1352                fi
1353            ;;
1354            arm | armv7*)
1355                CFLAGS="${CFLAGS} -DUNALIGNED_OK"
1356                SFLAGS="${SFLAGS} -DUNALIGNED_OK"
1357
1358                if test $without_optimizations -eq 0; then
1359                    if test $buildacle -eq 1; then
1360                        echo ACLE support not available
1361                    fi
1362
1363                    if test $buildneon -eq 1; then
1364                        if test $MFPU_NEON_AVAILABLE -eq 1;then
1365                            neonflag="-mfpu=neon"
1366                        fi
1367
1368                        CFLAGS="${CFLAGS} -DARM_NEON_ADLER32 -DARM_NEON_CHUNKSET -DARM_NEON_SLIDEHASH"
1369                        SFLAGS="${SFLAGS} -DARM_NEON_ADLER32 -DARM_NEON_CHUNKSET -DARM_NEON_SLIDEHASH"
1370
1371                        ARCH_STATIC_OBJS="${ARCH_STATIC_OBJS} adler32_neon.o chunkset_neon.o slide_neon.o"
1372                        ARCH_SHARED_OBJS="${ARCH_SHARED_OBJS} adler32_neon.lo chunkset_neon.lo slide_neon.lo"
1373                    fi
1374                fi
1375            ;;
1376            armv8-a | armv8-a+simd)
1377                CFLAGS="${CFLAGS} -DUNALIGNED_OK -DUNALIGNED64_OK"
1378                SFLAGS="${SFLAGS} -DUNALIGNED_OK -DUNALIGNED64_OK"
1379
1380                if test $without_optimizations -eq 0; then
1381                    if test $buildacle -eq 1; then
1382                        echo ACLE support not available
1383                    fi
1384
1385                    if test $buildneon -eq 1; then
1386                        if test $MFPU_NEON_AVAILABLE -eq 1;then
1387                            neonflag="-mfpu=neon"
1388                        fi
1389
1390                        CFLAGS="${CFLAGS} -DARM_NEON_ADLER32 -DARM_NEON_CHUNKSET -DARM_NEON_SLIDEHASH"
1391                        SFLAGS="${SFLAGS} -DARM_NEON_ADLER32 -DARM_NEON_CHUNKSET -DARM_NEON_SLIDEHASH"
1392
1393                        ARCH_STATIC_OBJS="${ARCH_STATIC_OBJS} adler32_neon.o chunkset_neon.o slide_neon.o"
1394                        ARCH_SHARED_OBJS="${ARCH_SHARED_OBJS} adler32_neon.lo chunkset_neon.lo slide_neon.lo"
1395                    fi
1396                fi
1397            ;;
1398            armv8-a+crc | armv8-a+crc+simd | armv8.[1234]-a | armv8.[1234]-a+simd)
1399                CFLAGS="${CFLAGS} -DUNALIGNED_OK -DUNALIGNED64_OK"
1400                SFLAGS="${SFLAGS} -DUNALIGNED_OK -DUNALIGNED64_OK"
1401
1402                acleflag="-march=${ARCH}"
1403
1404                if test $without_optimizations -eq 0; then
1405                    CFLAGS="${CFLAGS} -DARM_ACLE_CRC_HASH"
1406                    SFLAGS="${SFLAGS} -DARM_ACLE_CRC_HASH"
1407
1408                    ARCH_STATIC_OBJS="${ARCH_STATIC_OBJS} crc32_acle.o insert_string_acle.o"
1409                    ARCH_SHARED_OBJS="${ARCH_SHARED_OBJS} crc32_acle.lo insert_string_acle.lo"
1410
1411                    if test $buildneon -eq 1; then
1412                        if test $MFPU_NEON_AVAILABLE -eq 1;then
1413                            neonflag="-mfpu=neon"
1414                        fi
1415
1416                        CFLAGS="${CFLAGS} -DARM_NEON_ADLER32 -DARM_NEON_CHUNKSET -DARM_NEON_SLIDEHASH"
1417                        SFLAGS="${SFLAGS} -DARM_NEON_ADLER32 -DARM_NEON_CHUNKSET -DARM_NEON_SLIDEHASH"
1418
1419                        ARCH_STATIC_OBJS="${ARCH_STATIC_OBJS} adler32_neon.o chunkset_neon.o slide_neon.o"
1420                        ARCH_SHARED_OBJS="${ARCH_SHARED_OBJS} adler32_neon.lo chunkset_neon.lo slide_neon.lo"
1421                    fi
1422                fi
1423            ;;
1424        esac
1425
1426    ;;
1427    # 64-bit ARM specific optimizations
1428    aarch64)
1429        [ ! -z $CROSS_PREFIX ] && QEMU_ARCH=aarch64
1430        ARCHDIR=arch/arm
1431
1432        if test $native -eq 0; then
1433            ARCH="armv8-a"
1434        else
1435            ARCH="native"
1436        fi
1437
1438        if test $without_optimizations -eq 0; then
1439            CFLAGS="${CFLAGS} -DARM_FEATURES"
1440            SFLAGS="${SFLAGS} -DARM_FEATURES"
1441            ARCH_STATIC_OBJS="${ARCH_STATIC_OBJS} armfeature.o"
1442            ARCH_SHARED_OBJS="${ARCH_SHARED_OBJS} armfeature.lo"
1443
1444            if test $LINUX -eq 1; then
1445                cat > $test.c <<EOF
1446#include <sys/auxv.h>
1447int main() {
1448    return (getauxval(AT_HWCAP2) & HWCAP2_CRC32);
1449}
1450EOF
1451                if try $CC -c $CFLAGS $test.c; then
1452                    CFLAGS="${CFLAGS} -DARM_AUXV_HAS_CRC32"
1453                    SFLAGS="${SFLAGS} -DARM_AUXV_HAS_CRC32"
1454                else
1455                    echo "HWCAP2_CRC32 not present in sys/auxv.h; cannot detect support at runtime." | tee -a configure.log
1456                fi
1457            fi
1458
1459            if test $buildacle -eq 1; then
1460                if test $native -eq 0; then
1461                    ARCH="${ARCH}+crc"
1462                fi
1463                CFLAGS="${CFLAGS} -DARM_ACLE_CRC_HASH"
1464                SFLAGS="${SFLAGS} -DARM_ACLE_CRC_HASH"
1465                ARCH_STATIC_OBJS="${ARCH_STATIC_OBJS} crc32_acle.o insert_string_acle.o"
1466                ARCH_SHARED_OBJS="${ARCH_SHARED_OBJS} crc32_acle.lo insert_string_acle.lo"
1467            fi
1468
1469            if test $buildneon -eq 1; then
1470                if test $native -eq 0; then
1471                    ARCH="${ARCH}+simd"
1472                fi
1473                CFLAGS="${CFLAGS} -DARM_NEON_ADLER32 -DARM_NEON_CHUNKSET -DARM_NEON_SLIDEHASH"
1474                SFLAGS="${SFLAGS} -DARM_NEON_ADLER32 -DARM_NEON_CHUNKSET -DARM_NEON_SLIDEHASH"
1475                ARCH_STATIC_OBJS="${ARCH_STATIC_OBJS} adler32_neon.o chunkset_neon.o slide_neon.o"
1476                ARCH_SHARED_OBJS="${ARCH_SHARED_OBJS} adler32_neon.lo chunkset_neon.lo slide_neon.lo"
1477            fi
1478        fi
1479
1480        neonflag="-march=${ARCH}"
1481        acleflag="-march=${ARCH}"
1482
1483        CFLAGS="${CFLAGS} -DUNALIGNED_OK -DUNALIGNED64_OK"
1484        SFLAGS="${SFLAGS} -DUNALIGNED_OK -DUNALIGNED64_OK"
1485    ;;
1486    powerpc*)
1487        case "${ARCH}" in
1488            powerpc)
1489                [ ! -z $CROSS_PREFIX ] && QEMU_ARCH=ppc
1490            ;;
1491            powerpc64)
1492                [ ! -z $CROSS_PREFIX ] && QEMU_ARCH=ppc64
1493            ;;
1494            powerpc64le)
1495                [ ! -z $CROSS_PREFIX ] && QEMU_ARCH=ppc64le
1496                CFLAGS="${CFLAGS} -DUNALIGNED_OK -DUNALIGNED64_OK"
1497                SFLAGS="${SFLAGS} -DUNALIGNED_OK -DUNALIGNED64_OK"
1498            ;;
1499        esac
1500
1501        ARCHDIR=arch/power
1502
1503        if test $without_optimizations -eq 0; then
1504            if test $HAVE_POWER8 -eq 1; then
1505                CFLAGS="${CFLAGS} -DPOWER8 -DPOWER_FEATURES -DPOWER8_VSX_ADLER32 -DPOWER8_VSX_SLIDEHASH"
1506                SFLAGS="${SFLAGS} -DPOWER8 -DPOWER_FEATURES -DPOWER8_VSX_ADLER32 -DPOWER8_VSX_SLIDEHASH"
1507
1508                ARCH_STATIC_OBJS="${ARCH_STATIC_OBJS} power.o adler32_power8.o slide_hash_power8.o"
1509                ARCH_SHARED_OBJS="${ARCH_SHARED_OBJS} power.lo adler32_power8.lo slide_hash_power8.lo"
1510            fi
1511        fi
1512    ;;
1513    s390x)
1514        [ ! -z $CROSS_PREFIX ] && QEMU_ARCH=s390x
1515        ARCHDIR=arch/s390
1516
1517        if test $without_optimizations -eq 0; then
1518            if test $builddfltccdeflate -eq 1 -o $builddfltccinflate -eq 1; then
1519                ARCH_STATIC_OBJS="${ARCH_STATIC_OBJS} dfltcc_common.o"
1520                ARCH_SHARED_OBJS="${ARCH_SHARED_OBJS} dfltcc_common.lo"
1521            fi
1522
1523            if test $builddfltccdeflate -eq 1; then
1524                CFLAGS="${CFLAGS} -DS390_DFLTCC_DEFLATE"
1525                SFLAGS="${SFLAGS} -DS390_DFLTCC_DEFLATE"
1526                ARCH_STATIC_OBJS="${ARCH_STATIC_OBJS} dfltcc_deflate.o"
1527                ARCH_SHARED_OBJS="${ARCH_SHARED_OBJS} dfltcc_deflate.lo"
1528                ARCH="${ARCH}+dfltcc-deflate"
1529            fi
1530
1531            if test $builddfltccinflate -eq 1; then
1532                CFLAGS="${CFLAGS} -DS390_DFLTCC_INFLATE"
1533                SFLAGS="${SFLAGS} -DS390_DFLTCC_INFLATE"
1534                ARCH_STATIC_OBJS="${ARCH_STATIC_OBJS} dfltcc_inflate.o"
1535                ARCH_SHARED_OBJS="${ARCH_SHARED_OBJS} dfltcc_inflate.lo"
1536                ARCH="${ARCH}+dfltcc-inflate"
1537            fi
1538        fi
1539    ;;
1540    *)
1541        [ ! -z $CROSS_PREFIX ] && QEMU_ARCH=$ARCH
1542    ;;
1543esac
1544
1545echo "ARCH: ${ARCH}"
1546echo "Using arch directory: ${ARCHDIR}"
1547
1548# show the results in the log
1549echo >> configure.log
1550echo ALL = $ALL >> configure.log
1551echo AR = $AR >> configure.log
1552echo ARFLAGS = $ARFLAGS >> configure.log
1553echo CC = $CC >> configure.log
1554echo CFLAGS = $CFLAGS >> configure.log
1555echo EXE = $EXE >> configure.log
1556echo LDCONFIG = $LDCONFIG >> configure.log
1557echo LDFLAGS = $LDFLAGS >> configure.log
1558echo LDSHARED = $LDSHARED >> configure.log
1559echo LDSHAREDFLAGS = $LDSHAREDFLAGS >> configure.log
1560echo LDSHAREDLIBC = $LDSHAREDLIBC >> configure.log
1561echo DEFFILE = $DEFFILE >> configure.log
1562echo RC = $RC >> configure.log
1563echo RCFLAGS = $RCFLAGS >> configure.log
1564echo RCOBJS = $RCOBJS >> configure.log
1565echo STRIP = $STRIP >> configure.log
1566echo OBJC = $OBJC >> configure.log
1567echo PIC_OBJC = $PIC_OBJC >> configure.log
1568echo RANLIB = $RANLIB >> configure.log
1569echo SFLAGS = $SFLAGS >> configure.log
1570echo SHAREDLIB = $SHAREDLIB >> configure.log
1571echo SHAREDLIBM = $SHAREDLIBM >> configure.log
1572echo SHAREDLIBV = $SHAREDLIBV >> configure.log
1573echo SHAREDTARGET = $SHAREDTARGET >> configure.log
1574echo IMPORTLIB = $IMPORTLIB >> configure.log
1575echo INSTALLTARGETS = $INSTALLTARGETS >> configure.log
1576echo UNINSTALLTARGETS = $UNINSTALLTARGETS >> configure.log
1577echo SRCDIR = $SRCDIR >> configure.log
1578echo BUILDDIR = $BUILDDIR >> configure.log
1579echo STATICLIB = $STATICLIB >> configure.log
1580echo TEST = $TEST >> configure.log
1581echo VER = $VER >> configure.log
1582echo exec_prefix = $exec_prefix >> configure.log
1583echo includedir = $includedir >> configure.log
1584echo bindir = $bindir >> configure.log
1585echo libdir = $libdir >> configure.log
1586echo mandir = $mandir >> configure.log
1587echo prefix = $prefix >> configure.log
1588echo sharedlibdir = $sharedlibdir >> configure.log
1589echo uname = $uname >> configure.log
1590echo sse2flag = $sse2flag >> configure.log
1591echo ssse3flag = $ssse3flag >> configure.log
1592echo sse4flag = $sse4flag >> configure.log
1593echo pclmulflag = $pclmulflag >> configure.log
1594echo acleflag = $acleflag >> configure.log
1595echo neonflag = $neonflag >> configure.log
1596echo ARCHDIR = ${ARCHDIR} >> configure.log
1597echo ARCH_STATIC_OBJS = ${ARCH_STATIC_OBJS} >> configure.log
1598echo ARCH_SHARED_OBJS = ${ARCH_SHARED_OBJS} >> configure.log
1599
1600# Handle sed incompatibilities when using -i
1601replace_in_file() {
1602  if [ "$OS" = 'Darwin' ]; then
1603    sed -i '.tmp' -e "$1" "$2"
1604  else
1605    sed -i'.tmp' -e "$1" "$2"
1606  fi
1607}
1608
1609# update Makefile with the configure results
1610
1611INCLUDES="-I$SRCDIR"
1612if [ "$SRCDIR" != "$BUILDDIR" ]; then INCLUDES="-I$BUILDDIR ${INCLUDES}"; fi
1613
1614sed < $SRCDIR/Makefile.in "
1615/^CC *=/s#=.*#=$CC#
1616/^CFLAGS *=/s#=.*#=$CFLAGS#
1617/^WITH_FUZZERS *=/s#=.*#=$with_fuzzers#
1618/^SFLAGS *=/s#=.*#=$SFLAGS#
1619/^LDFLAGS *=/s#=.*#=$LDFLAGS#
1620/^LDSHARED *=/s#=.*#=$LDSHARED#
1621/^LDSHAREDFLAGS *=/s#=.*#=$LDSHAREDFLAGS#
1622/^LIBNAME1 *=/s#=.*#=$LIBNAME#
1623/^LIBNAME2 *=/s#=.*#=$LIBNAME2#
1624/^SUFFIX *=/s#=.*#=$SUFFIX#
1625/^STATICLIB *=/s#=.*#=$STATICLIB#
1626/^SHAREDLIB *=/s#=.*#=$SHAREDLIB#
1627/^SHAREDLIBV *=/s#=.*#=$SHAREDLIBV#
1628/^SHAREDLIBM *=/s#=.*#=$SHAREDLIBM#
1629/^SHAREDTARGET *=/s#=.*#=$SHAREDTARGET#
1630/^IMPORTLIB *=/s#=.*#=$IMPORTLIB#
1631/^VER *=/s#=.*#=$VER#
1632/^VER1 *=/s#=.*#=$VER1#
1633/^AR *=/s#=.*#=$AR#
1634/^ARFLAGS *=/s#=.*#=$ARFLAGS#
1635/^RANLIB *=/s#=.*#=$RANLIB#
1636/^LDCONFIG *=/s#=.*#=$LDCONFIG#
1637/^LDSHAREDLIBC *=/s#=.*#=$LDSHAREDLIBC#
1638/^DEFFILE *=/s#=.*#=$DEFFILE#
1639/^RC *=/s#=.*#=$RC#
1640/^RCFLAGS *=/s#=.*#=$RCFLAGS#
1641/^RCOBJS *=/s#=.*#=$RCOBJS#
1642/^STRIP *=/s#=.*#=$STRIP#
1643/^EXE *=/s#=.*#=$EXE#
1644/^prefix *=/s#=.*#= $prefix#
1645/^exec_prefix *=/s#=.*#= $exec_prefix#
1646/^bindir *=/s#=.*#= $bindir#
1647/^libdir *=/s#=.*#= $libdir#
1648/^sharedlibdir *=/s#=.*#= $sharedlibdir#
1649/^includedir *=/s#=.*#= $includedir#
1650/^mandir *=/s#=.*#= $mandir#
1651/^SRCDIR *=/s#=.*#=$SRCDIR#
1652/^INCLUDES *=/s#=.*#=$INCLUDES#
1653/^OBJC *=/s#=.*#= $OBJC#
1654/^PIC_OBJC *=/s#=.*#= $PIC_OBJC#
1655/^all: */s#:.*#: $ALL#
1656/^install-libs: */s#:.*#: $INSTALLTARGETS#
1657/^uninstall-libs: */s#:.*#: $UNINSTALLTARGETS#
1658/^ARCHDIR *=/s#=.*#=$ARCHDIR#
1659/^ARCH_STATIC_OBJS *=/s#=.*#=$ARCH_STATIC_OBJS#
1660/^ARCH_SHARED_OBJS *=/s#=.*#=$ARCH_SHARED_OBJS#
1661" > Makefile
1662
1663# Append header files dependences.
1664for file in $(ls -1 $SRCDIR/*.c $SRCDIR/test/*.c $SRCDIR/test/fuzz/*.c $SRCDIR/$ARCHDIR/*.c $SRCDIR/tools/*.c); do
1665    short_name=$(echo $file | sed -e "s#$SRCDIR/##g")
1666    incs=$(grep -h include $file | sed -n 's/# *\include *"\(.*\.h\)".*/\1/p' | sort | uniq)
1667    includes=$(for i in $incs; do
1668                   # Check that the include file exists in the current dir,
1669                   # otherwise it may be one of the system include header.
1670                   if test -e $SRCDIR/$i; then
1671                       echo -n " \$(SRCDIR)/$i"
1672                   fi
1673                   # We also need to check whether the include file is in the ARCHDIR.
1674                   if test -e $SRCDIR/$ARCHDIR/$i; then
1675                       echo -n " \$(SRCDIR)/$ARCHDIR/$i"
1676                   fi
1677               done)
1678    obj=$(basename $(echo $file | sed -e 's/\.c/\.o/g' -e 's#^\./##g'))
1679    lobj=$(basename $(echo $file | sed -e 's/\.c/\.lo/g' -e 's#^\./##g'))
1680
1681    if grep -q "^$obj:" Makefile; then
1682        # Replace the existing line with a line with all dependences.
1683        $(replace_in_file "s#$obj:.*#$obj: \$(SRCDIR)/$short_name $includes#g" Makefile)
1684    else
1685        # Append at the end of Makefile a new line with the header dependences.
1686        echo "$obj: \$(SRCDIR)/$short_name $includes" >> Makefile
1687
1688        # In case this is one of the ARCHDIR files, append a dependence line
1689        # that will force the `$(MAKE) -C $(ARCHDIR)` generic rule. Without this
1690        # we would only execute the copy rule from ARCHDIR to SRCDIR.
1691        if test -e $SRCDIR/$ARCHDIR/$(basename $file); then
1692            echo "$ARCHDIR/$obj: \$(SRCDIR)/$short_name $includes" >> Makefile
1693        fi
1694    fi
1695
1696    if grep -q "^$lobj:" Makefile; then
1697        # Replace the existing line with a line with all dependences.
1698        $(replace_in_file "s#$lobj:.*#$lobj: \$(SRCDIR)/$short_name $includes#g" Makefile)
1699    else
1700        # Append at the end of Makefile a new line with the header dependences.
1701        echo "$lobj: \$(SRCDIR)/$short_name $includes" >> Makefile
1702    fi
1703done
1704
1705# Generate Makefile in arch dir
1706mkdir -p $ARCHDIR
1707
1708ARCHINCLUDES="-I$SRCDIR/$ARCHDIR -I$SRCDIR"
1709if [ "$SRCDIR" != "$BUILDDIR" ]; then ARCHINCLUDES="-I$BUILDDIR ${ARCHINCLUDES}"; fi
1710
1711sed < $SRCDIR/$ARCHDIR/Makefile.in "
1712/^CC *=/s#=.*#=$CC#
1713/^CFLAGS *=/s#=.*#=$CFLAGS#
1714/^SFLAGS *=/s#=.*#=$SFLAGS#
1715/^LDFLAGS *=/s#=.*#=$LDFLAGS#
1716/^INCLUDES *=/s#=.*#=$ARCHINCLUDES#
1717/^SUFFIX *=/s#=.*#=$SUFFIX#
1718/^SRCDIR *=/s#=.*#=$SRCDIR/$ARCHDIR#
1719/^SRCTOP *=/s#=.*#=$SRCDIR#
1720/^TOPDIR *=/s#=.*#=$BUILDDIR#
1721/^AVX2FLAG *=/s#=.*#=$avx2flag#
1722/^SSE2FLAG *=/s#=.*#=$sse2flag#
1723/^SSSE3FLAG *=/s#=.*#=$ssse3flag#
1724/^SSE4FLAG *=/s#=.*#=$sse4flag#
1725/^PCLMULFLAG *=/s#=.*#=$pclmulflag#
1726/^ACLEFLAG *=/s#=.*#=$acleflag#
1727/^NEONFLAG *=/s#=.*#=$neonflag#
1728/^NOLTOFLAG *=/s#=.*#=$noltoflag#
1729" > $ARCHDIR/Makefile
1730
1731# Append header files dependences.
1732for file in $(ls -1 $SRCDIR/$ARCHDIR/*.c); do
1733    incs=$(grep -h include $file | sed -n 's/# *\include *"\(.*\.h\)".*/\1/p' | sort | uniq)
1734    includes=$(for i in $incs; do
1735                   # Check that the include file exists in the current dir,
1736                   # otherwise it may be one of the system include header.
1737                   if test -e $SRCDIR/$i; then
1738                       echo -n " \$(SRCTOP)/$i"
1739                   fi
1740                   # We also need to check whether the include file is in the ARCHDIR.
1741                   if test -e $SRCDIR/$ARCHDIR/$i; then
1742                       echo -n " \$(SRCDIR)/$i"
1743                   fi
1744               done)
1745    obj=$(basename $(echo $file | sed -e 's/\.c/\.o/g' -e 's#^\./##g'))
1746    lobj=$(basename $(echo $file | sed -e 's/\.c/\.lo/g' -e 's#^\./##g'))
1747    short_name=$(basename $file)
1748    if grep -q "^$obj:" $ARCHDIR/Makefile; then
1749        # Replace the existing line with a line with all dependences.
1750        $(replace_in_file "s#$obj:.*#$obj: \$(SRCDIR)/$short_name $includes#g" $ARCHDIR/Makefile)
1751    else
1752        # Append at the end of Makefile a new line with the header dependences.
1753        echo "$obj: \$(SRCDIR)/$short_name $includes" >> $ARCHDIR/Makefile
1754    fi
1755
1756    if grep -q "^$lobj:" $ARCHDIR/Makefile; then
1757        # Replace the existing line with a line with all dependences.
1758        $(replace_in_file "s#$lobj:.*#$lobj: \$(SRCDIR)/$short_name $includes#g" $ARCHDIR/Makefile)
1759    else
1760        # Append at the end of Makefile a new line with the header dependences.
1761        echo "$lobj: \$(SRCDIR)/$short_name $includes" >> $ARCHDIR/Makefile
1762    fi
1763done
1764
1765# Generate Makefile in test dir
1766mkdir -p test
1767if test $compat -eq 1; then COMPATTESTS="compattests"; fi
1768if test $QEMU_ARCH; then QEMU_RUN="qemu-$QEMU_ARCH -L /usr/${CHOST}/"; fi
1769sed < $SRCDIR/test/Makefile.in "
1770/^CC *=/s#=.*#=$CC#
1771/^CFLAGS *=/s#=.*#=$CFLAGS#
1772/^LDFLAGS *=/s#=.*#=$LDFLAGS#
1773/^EXE *=/s#=.*#=$EXE#
1774/^oldtests: */s#:.*#: $TEST#
1775/^SRCDIR *=/s#=.*#=$SRCDIR/test#
1776/^SRCTOP *=/s#=.*#=$SRCDIR#
1777/^COMPATTESTS *=/s#=.*#=$COMPATTESTS#
1778/^QEMU_RUN *=/s#=.*#=$QEMU_RUN#
1779/^WITH_FUZZERS *=/s#=.*#=$with_fuzzers#
1780/^LIBNAME *=/s#=.*#=$LIBNAME#
1781" > test/Makefile
1782
1783# create zlib.pc with the configure results
1784sed < $SRCDIR/zlib.pc.in "
1785/^CC *=/s#=.*#=$CC#
1786/^CFLAGS *=/s#=.*#=$CFLAGS#
1787/^LDFLAGS *=/s#=.*#=$LDFLAGS#
1788/^LDSHARED *=/s#=.*#=$LDSHARED#
1789/^LDSHAREDFLAGS *=/s#=.*#=$LDSHAREDFLAGS#
1790/^STATICLIB *=/s#=.*#=$STATICLIB#
1791/^SHAREDLIB *=/s#=.*#=$SHAREDLIB#
1792/^SHAREDLIBV *=/s#=.*#=$SHAREDLIBV#
1793/^SHAREDLIBM *=/s#=.*#=$SHAREDLIBM#
1794/^IMPORTLIB *=/s#=.*#=$IMPORTLIB#
1795/^AR *=/s#=.*#=$AR#
1796/^ARFLAGS *=/s#=.*#=$ARFLAGS#
1797/^RANLIB *=/s#=.*#=$RANLIB#
1798/^EXE *=/s#=.*#=$EXE#
1799/^prefix *=/s#=.*#=$prefix#
1800/^exec_prefix *=/s#=.*#=$exec_prefix#
1801/^bindir *=/s#=.*#=$bindir#
1802/^libdir *=/s#=.*#=$libdir#
1803/^sharedlibdir *=/s#=.*#=$sharedlibdir#
1804/^includedir *=/s#=.*#=$includedir#
1805/^mandir *=/s#=.*#=$mandir#
1806/^LDFLAGS *=/s#=.*#=$LDFLAGS#
1807" | sed -e "
1808s/\@VERSION\@/$VER/g;
1809s/\@SUFFIX\@/$SUFFIX/g;
1810" > ${LIBNAME2}.pc
1811
1812# done
1813leave 0
1814