summaryrefslogtreecommitdiff
path: root/configure
blob: ff11ca6dafb3b0aa5d599a5faf709daa94dfa480 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
#!/bin/sh

cd `dirname "$0"`
. package/info

# Helper functions

usage () {
  cat <<EOF
Usage: $0 [OPTION]... [TARGET]

Defaults for the options are specified in brackets.

System types:
  --target=TARGET               configure to run on target TARGET [detected]
  --host=TARGET                 same as --target

Installation directories:
  --prefix=PREFIX               main installation prefix [/]

Fine tuning of the installation directories:
  --dynlibdir=DIR               shared library files [PREFIX/lib]
  --libdir=DIR                  static library files [PREFIX/lib/$package]
  --includedir=DIR              include files for the C compiler [PREFIX/include]
  --datadir=DIR                 global configuration files [PREFIX/etc]
  --sysdepdir=DIR               sysdeps directory [PREFIX/lib/$package/sysdeps]

 If no --prefix option is given, by default libdir will be /usr/lib/$package,
 includedir will be /usr/include and sysdepdir will be /usr/lib/$package/sysdeps.

Dependencies:
  --with-include=DIR            add DIR to the list of searched directories for headers
  --with-lib=DIR                add DIR to the list of searched directories for static libraries
  --with-dynlib=DIR             add DIR to the list of searched directories for shared libraries

Optional features:
  --disable-shared              do not build shared libraries [enabled]
  --disable-static              do not build static libraries [enabled]
  --disable-all-pic             do not build static libraries as PIC [enabled]
  --enable-slashpackage[=ROOT]  assume /package installation at ROOT [disabled]

$package options:
  --disable-ipv6                do not build IPv6 support [enabled]
  --enable-iopause-select       prefer select() over poll() for iopause implementation [disabled]
  --enable-tai-clock            assume the system clock is TAI-10 instead of UTC [disabled]
  --with-default-path=PATH      default executable search path [/usr/bin:/bin]

Sysdeps autodetection override:
  --with-sysdep-K=V             assume sysdep K has the value V [autodetected]

List of mandatory sysdeps for cross-compiling:
  devurandom (yes|no): =yes if you have a working /dev/urandom
  posixspawnearlyreturn (yes|no): =yes if you have an old glibc or otherwise bad posix_spawn
EOF
  exit 0
}

# If your system does not have printf, you can comment this, but it is
# generally not a good idea to use echo.
# See http://etalabs.net/sh_tricks.html
echo () {
  IFS=' '
  printf %s\\n "$*"
}

echon () {
  IFS=' '
  printf %s "$*"
}

quote () {
  tr '\n' ' ' <<EOF | grep '^[-[:alnum:]_=,./:]* $' >/dev/null 2>&1 && { echo "$1" ; return 0 ; }
$1
EOF
  echo "$1" | sed -e "s/'/'\\\\''/g" -e "1s/^/'/" -e "\$s/\$/'/" -e "s#^'\([-[:alnum:]_,./:]*\)=\(.*\)\$#\1='\2#" -e "s|\*/|* /|g"
}

fail () {
  echo "$*" 1>&2
  exit 1
}

fnmatch () {
  eval "case \"\$2\" in $1) return 0 ;; *) return 1 ;; esac"
}

cmdexists () {
  type "$1" >/dev/null 2>&1
}

trycc () {
  test -z "$CC_AUTO" && cmdexists "$1" && CC_AUTO="$*"
}

stripdir () {
  while eval "fnmatch '*/' \"\${$1}\"" ; do
    eval "$1=\${$1%/}"
  done
}

tryflag () {
  echo "Checking whether compiler accepts $2 ..."
  echo "typedef int x;" > "$tmpc"
  if $CC_AUTO $CPPFLAGS_POST $CFLAGS_POST "$2" -c -o "$tmpo" "$tmpc" >/dev/null 2>&1 ; then
    echo "  ... yes"
    eval "$1=\"\${$1} \$2\""
    eval "$1=\${$1# }"
    return 0
  else
    echo "  ... no"
    return 1
  fi
}

tryldflag () {
  echo "Checking whether linker accepts $2 ..."
  echo "typedef int x;" > "$tmpc"
  if $CC_AUTO $CPPFLAGS_POST $CFLAGS_POST $LDFLAGS_POST -nostdlib "$2" -o "$tmpe" "$tmpc" >/dev/null 2>&1 ; then
    echo "  ... yes"
    eval "$1=\"\${$1} \$2\""
    eval "$1=\${$1# }"
    return 0
  else
    echo "  ... no"
    return 1
  fi
}

# Sysdeps determination functions

iscached ()
{
  if test -r "$tmps" && grep -q "^${1}: " "$tmps" ; then
    grep "^${1}: " "$tmps" | tail -n 1 |
    {
      IFS=' '
      read -r k v ldlibs
      if test -n "$ldlibs" ; then
        echo "  ... user-provided: $v with linker args: $ldlibs"
        echo "$ldlibs" >&3
      else
        echo "  ... user-provided: $v"
      fi
      echo "${1}: $v" >> "$sysdeps/sysdeps"
      return 0 ;
    }
  else
    return 1 ;
  fi
}

choose () {
  what="$1"
  name="$2"
  echo "Checking whether system has $3..."
  if iscached "$name" ; then return ; fi
  shift 3
  libs="$*"
  r=true
  case "$what" in
    *c*) $CC_AUTO $CPPFLAGS_AUTO $CPPFLAGS $CPPFLAGS_POST $CFLAGS_AUTO $CFLAGS $CFLAGS_POST -o "$tmpo" -c src/sysdeps/try$name.c 2>/dev/null || r=false ;;
  esac
  if $r ; then
    case "$what" in
      *l*) $CC_AUTO $CFLAGS_AUTO $CFLAGS $CFLAGS_POST $LDFLAGS_AUTO $LDFLAGS $LDFLAGS_POST -o try$name "$tmpo" $libs 2>/dev/null || r=false ;;
    esac
  fi
  if $r ; then
    case "$what" in
     *r*) if test -n "$cross" ; then
            rm -f try$name
            fail "$0: sysdep $name cannot be autodetected when cross-compiling. Please manually provide a value with the --with-sysdep-${name}=yes|no|... option."
          fi
          ./try$name >/dev/null 2>&1 ; r=$?
          case "$r" in
           111) fail "$0: test crashed, aborting." ;;
           0) r=true ;;
           *) r=false ;;
          esac
    esac
  fi
  rm -f try$name
  if $r ; then
    echo "$name: yes" >> $sysdeps/sysdeps
    echo "  ... yes"
  else
    echo "$name: no" >> $sysdeps/sysdeps
    echo "  ... no"
  fi
}

trybasic () {
  $CC_AUTO $CPPFLAGS_AUTO $CPPFLAGS $CPPFLAGS_POST $CFLAGS_AUTO $CFLAGS $CFLAGS_POST -o "$tmpo" -c "$1" 2>/dev/null
}

tryendianness () {
  echo "Checking endianness..."
  if iscached endianness ; then return ; fi
  for i in endian.h sys/endian.h machine/endian.h arpa/nameser_compat.h ; do
    cat > "$tmpc" <<EOF
#undef _POSIX_C_SOURCE
#undef _XOPEN_SOURCE
#ifndef _BSD_SOURCE
#define _BSD_SOURCE
#endif
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#ifndef _DEFAULT_SOURCE
#define _DEFAULT_SOURCE
#endif
#include <$i>
int a = LITTLE_ENDIAN ;
int b = BIG_ENDIAN ;
int c = BYTE_ORDER ;
EOF
    trybasic "$tmpc" || continue
    for j in little big pdp ; do
      k=`echo $j | tr a-z A-Z`
      cat > "$tmpc" <<EOF
#undef _POSIX_C_SOURCE
#undef _XOPEN_SOURCE
#ifndef _BSD_SOURCE
#define _BSD_SOURCE
#endif
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#ifndef _DEFAULT_SOURCE
#define _DEFAULT_SOURCE
#endif
#include <$i>
int a[BYTE_ORDER == ${k}_ENDIAN ? 1 : -1] ;
EOF
      if trybasic "$tmpc" ; then
        echo "endianness: $j" >> "$sysdeps/sysdeps"
        echo "  ... $j"
        rm -f "$tmpc"
        return
      fi
    done
    rm -f "$tmpc"
    fail "$0: error: unable to determine endianness according to $i"
  done
  rm -f "$tmpc"
  fail "$0: error: unable to determine endianness: no suitable endian.h found"
}

trysigned () {
  cat > "$tmpc" <<EOF
#include <sys/types.h>
int a[($1)-1 < 0 ? 1 : -1] ;
EOF
  trybasic "$tmpc"
  r=$?
  rm -f "$tmpc"
  return $r
}

trysizes () {
  t="$1" ; shift
  for arg ; do
    cat > "$tmpc" <<EOF
#include <sys/types.h>
int a[sizeof($t) == $arg ? 1 : -1] ;
EOF
    if trybasic "$tmpc" ; then
      rm -f "$tmpc"
      echo "$arg"
      return
    fi
  done
  rm -f "$tmpc"
  fail "$0: error: unable to determine the size of $t on the target"
}

trystdtype () {
  t="$1" ; shift
  iscached "sizeofu$t" || { echon "sizeofu${t}: " ; trysizes "$t" "$@" ; } >> "$sysdeps/sysdeps"
}

trytypes () {
  echo "Checking size and signedness of standard types..."
  trystdtype short 2 4
  trystdtype int 4 8 2
  trystdtype long 8 4
  for t in size uid gid pid time dev ino ; do
    iscached "signed$t" || { echon "signed${t}: " ; if trysigned "${t}_t" ; then echo "yes" ; else echo "no" ; fi ; } >> "$sysdeps/sysdeps"
    iscached "sizeof$t" || { echon "sizeof${t}: " ; trysizes "${t}_t" 4 8 2 ; } >> "$sysdeps/sysdeps"
  done  
  echo "  ... done"
}

detectlibs () {
  args=
  name=$1
  shift
  if $CC_AUTO $CPPFLAGS_AUTO $CPPFLAGS $CPPFLAGS_POST $CFLAGS_AUTO $CFLAGS $CFLAGS_POST -c -o try$name.o src/sysdeps/try$name.c 2>/dev/null ; then
    until $CC_AUTO $CFLAGS_AUTO $CFLAGS $CFLAGS_POST $LDFLAGS_AUTO $LDFLAGS $LDFLAGS_POST -o "$tmpe" try$name.o $args 2>/dev/null ; do
      if test -z "$*" ; then
        rm -f try$name.o
        return 1
      fi
      args="$args $1"
      shift
    done
    echo ${args# }
    rm -f try$name.o
    return 0
  else
    return 1
  fi
}

trylibs () {
  name="$1"
  libvar="$2"_lib
  libfile="$2".lib
  echo "Checking whether system has $3..."
  shift 3
  if { args=`iscached $name 3>&1 1>&4 4>&-` ; } 4>&1 ; then :
  elif args=`detectlibs "$name" "$@"` ; then
    echo "${name}: yes" >> "${sysdeps}/sysdeps"
    if test -z "$args" ; then
      echo "  ... yes"
    else
      echo "  ... yes, with linker args: $args"
    fi
  else
    echo "${name}: no" >> ${sysdeps}/sysdeps
    echo "  ... no"
  fi
  eval "${libvar}=\"$args\""
  echo "$args" > "${sysdeps}/$libfile"
}


# The script starts HERE.

# Initialize variables
CC_AUTO=
CPPFLAGS_AUTO="-D_POSIX_C_SOURCE=200809L -D_XOPEN_SOURCE=700 -Isrc/include"
CPPFLAGS_POST="$CPPFLAGS"
CPPFLAGS=
CFLAGS_AUTO="-pipe -Wall"
CFLAGS_POST="$CFLAGS"
CFLAGS="-O2"
LDFLAGS_AUTO=
LDFLAGS_POST="$LDFLAGS"
LDFLAGS=
LDFLAGS_NOSHARED=
LDFLAGS_SHARED=-shared
prefix=
dynlibdir='$prefix/lib'
libdir='$prefix/lib/$package'
includedir='$prefix/include'
datadir='$prefix/etc'
sysdepdir='$prefix/lib/$package/sysdeps'
sysdeplist=
shared=true
static=true
allpic=true
slashpackage=false
ipv6=true
select=false
taiclock=false
ddefaultpath=/usr/bin:/bin
defaultpath=$ddefaultpath
dpathorig=true
sproot=
home=
exthome=
addincpath=''
addlibspath=''
addlibdpath=''
vpaths=''
vpathd=''
build=

# Parse command line
for arg ; do
  case "$arg" in
    --help) usage ;;
    --prefix=*) prefix=${arg#*=} ;;
    --dynlibdir=*) dynlibdir=${arg#*=} ;;
    --libdir=*) libdir=${arg#*=} ;;
    --includedir=*) includedir=${arg#*=} ;;
    --datadir=*) datadir=${arg#*=} ;;
    --sysdepdir=*) sysdepdir=${arg#*=} ;;
    --with-include=*) var=${arg#*=} ; stripdir var ; addincpath="$addincpath -I$var" ;;
    --with-lib=*) var=${arg#*=} ; stripdir var ; addlibspath="$addlibspath -L$var" ; vpaths="$vpaths $var" ;;
    --with-dynlib=*) var=${arg#*=} ; stripdir var ; addlibdpath="$addlibdpath -L$var" ; vpathd="$vpathd $var" ;;
    --enable-shared|--enable-shared=yes) shared=true ;;
    --disable-shared|--enable-shared=no) shared=false ;;
    --enable-static|--enable-static=yes) static=true ;;
    --disable-static|--enable-static=no) static=false ;;
    --enable-all-pic|--enable-all-pic=yes) allpic=true ;;
    --disable-all-pic|--enable-all-pic=no) allpic=false ;;
    --enable-slashpackage=*) sproot=${arg#*=} ; slashpackage=true ; ;;
    --enable-slashpackage) sproot= ; slashpackage=true ;;
    --disable-slashpackage) sproot= ; slashpackage=false ;;
    --enable-ipv6|--enable-ipv6=yes) ipv6=true ;;
    --disable-ipv6|--enable-ipv6=no) ipv6=false ;;
    --enable-iopause-select|--enable-iopause-select=yes) select=true ;;
    --disable-iopause-select|--enable-iopause-select=no) select=false ;;
    --enable-tai-clock|--enable-tai-clock=yes) taiclock=true ;;
    --disable-tai-clock|--enable-tai-clock=no) taiclock=false ;;
    --enable-monotonic|--enable-monotonic=yes) echo "$0: warning: --enable-monotonic is now obsolete" 1>&2 ;;
    --disable-monotonic|--enable-monotonic=no) ;;
    --with-default-path=*) defaultpath=${arg#*=} ; dpathorig=false ;;
    --without-default-path) defaultpath=$ddefaultpath ; dpathorig=true ;;
    --with-sysdep-*=*) sysdeplist="$sysdeplist ${arg#--with-sysdep-}" ;;
    --without-sysdep-*) sysdeplist="$sysdeplist ${arg#--without-sysdep-}=no" ;; 
    --enable-*|--disable-*|--with-*|--without-*|--*dir=*) ;;
    --host=*|--target=*) target=${arg#*=} ;;
    --build=*) build=${arg#*=} ;;
    -* ) echo "$0: unknown option $arg" ;;
    *=*) eval "${arg%%=*}=\${arg#*=}" ;;
    *) target=$arg ;;
  esac
done

# Add /usr in the default default case
if test -z "$prefix" ; then
  if test "$libdir" = '$prefix/lib/$package' ; then
    libdir=/usr/lib/$package
  fi
  if test "$includedir" = '$prefix/include' ; then
    includedir=/usr/include
  fi
  if test "$sysdepdir" = '$prefix/lib/$package/sysdeps' ; then
    sysdepdir=/usr/lib/$package/sysdeps
  fi
fi

# Expand installation directories
stripdir prefix
for i in exec_prefix dynlibdir libdir includedir datadir sysdepdir sysdepspre sproot ; do
  eval tmp=\${$i}
  eval $i=$tmp
  stripdir $i
done

# Set slashpackage values
if $slashpackage ; then
  home=${sproot}/package/${category}/${package}-${version}
  exthome=${sproot}/package/${category}/${package}
  sysdepdir=${home}/sysdeps
  binprefix=${home}/command
  extbinprefix=${exthome}/command
  dynlibdir=${home}/library.so
  libdir=${home}/library
  includedir=${home}/include
  if $dpathorig ; then
    if echo $defaultpath | tr : '\n' | grep -q '^/command$' ; then : ; else
      defaultpath="/command:$defaultpath"
    fi
  fi
fi

# Get usable temp filenames
i=0
set -C
while test "$i" -lt 50 ; do
  i=$(($i+1))
  tmpc="./tmp-configure-$$-$PPID-$i.c"
  tmpo="./tmp-configure-$$-$PPID-$i.o"
  tmpe="./tmp-configure-$$-$PPID-$i.tmp"
  tmps="./tmp-configure-$$-$PPID-$i.sysdeps"
  2>|/dev/null > "$tmpc" && break
  2>|/dev/null > "$tmpo" && break
  2>|/dev/null > "$tmpe" && break
  2>|/dev/null > "$tmps" && break
done
if test "$i" -gt 50 ; then
  fail "$0: cannot create temporary files"
fi
set +C
trap 'rm -f "$tmpc" "$tmpo" "$tmpe" "$tmps"' EXIT ABRT INT QUIT TERM HUP

# Preprocess user-provided sysdeps
rm -f "$tmps"
if test -n "$sysdeplist" ; then
  :> "$tmps"
  for i in $sysdeplist ; do
    k=${i%%=*}
    echo ${i#*=} | sed 's/,/ /g' |
    {
      read v extras
      if test -z "$k" || test -z "$v" ; then fail "$0: invalid user-provided sysdep: $i" ; fi
      if test "$v" = "true" ; then v=yes
      elif test "$v" = "false" ; then v=no
      fi
      if grep -q -e "^${k}: " "$tmps" ; then
        grep -v -e "^${k}: " "$tmps" > "$tmpe"
        mv -f "$tmpe" "$tmps"
      fi
      if test -n "$extras" ; then
        echo "${k}: $v $extras"
      else
        echo "${k}: $v"
      fi >> "$tmps"
    }
  done
fi

# Find a C compiler to use
if test -n "$target" && test x${build} != x${target} ; then
  cross=${target}-
else
  cross=
fi
echo "Checking for C compiler..."
trycc ${CC}
if test -n "$CC_AUTO" ; then
  b=`basename "$CC"`
  adjust_cross=false
  if test "$b" != "$CC" ; then
    adjust_cross=true
    echo "$0: warning: compiler $CC is declared with its own path. If it's not accessible via PATH, you will need to pass AR, RANLIB and STRIP make variables to the make invocation." 1>&2
  fi
  if test -n "$cross" ; then
    if test "$b" = "${b##$cross}" ; then
      echo "$0: warning: compiler $CC is declared as a cross-compiler for target $target but does not start with prefix ${cross}" 1>&2
    elif $adjust_cross ; then
      cross=`dirname "$CC"`/"$cross"
    fi
  fi
fi
trycc ${cross}gcc
trycc ${cross}clang
trycc ${cross}cc
test -n "$CC_AUTO" || fail "$0: cannot find a C compiler"
echo "  ... $CC_AUTO"
echo "Checking whether C compiler works... "
echo "typedef int x;" > "$tmpc"
if $CC_AUTO $CPPFLAGS_AUTO $CPPFLAGS $CPPFLAGS_POST $CFLAGS_AUTO $CFLAGS $CFLAGS_POST -c -o "$tmpo" "$tmpc" 2>"$tmpe" ; then
  echo "  ... yes"
else
  echo "  ... no. Compiler output follows:"
  cat < "$tmpe"
  exit 1
fi

echo "Checking target system type..."
if test -z "$target" ; then
  if test -n "$build" ; then
    target=$build ;
  else
    target=$($CC_AUTO -dumpmachine 2>/dev/null) || target=unknown
  fi
fi
echo "  ... $target"

# Produce automatic compilation flags
if $allpic ; then
  tryflag CPPFLAGS_AUTO -fPIC
fi
tryflag CFLAGS_AUTO -std=c99
tryflag CFLAGS -fomit-frame-pointer
tryflag CFLAGS_AUTO -fno-exceptions
tryflag CFLAGS_AUTO -fno-unwind-tables
tryflag CFLAGS_AUTO -fno-asynchronous-unwind-tables
tryflag CPPFLAGS_AUTO -Werror=implicit-function-declaration
tryflag CPPFLAGS_AUTO -Werror=implicit-int
tryflag CPPFLAGS_AUTO -Werror=pointer-sign
tryflag CPPFLAGS_AUTO -Werror=pointer-arith
tryflag CPPFLAGS_AUTO -Wno-unused-value
tryflag CPPFLAGS_AUTO -Wno-parentheses
tryflag CFLAGS_AUTO -ffunction-sections
tryflag CFLAGS_AUTO -fdata-sections
tryldflag LDFLAGS_SHARED -nostartfiles
tryldflag LDFLAGS_AUTO -Wl,--as-needed
tryldflag LDFLAGS_AUTO -Wl,--sort-section=alignment
tryldflag LDFLAGS_AUTO -Wl,--sort-common
if $shared ; then
  tryldflag LDFLAGS -Wl,--hash-style=both
fi

# And now, determine a big fat batch of sysdeps.

sysdeps=sysdeps.cfg
mkdir -p $sysdeps
echo "$target" > $sysdeps/target
:> "$sysdeps/sysdeps"

util_lib=
echo > $sysdeps/util.lib

echo 'Checking required linker flags for socket functions...'
socket_lib=`detectlibs lsock -lsocket -lnsl` || fail "$0: unable to determine required linker flags for socket functions"
if test -n "$socket_lib" ; then
  echo "  ... $socket_lib"
else
  echo "  ... none"
fi
echo "$socket_lib" > $sysdeps/socket.lib

trylibs clockrt sysclock 'clock_gettime()' -lrt
choose cl clockmon CLOCK_MONOTONIC $sysclock_lib
choose cl clockboot CLOCK_BOOTTIME $sysclock_lib
trylibs posixspawn spawn 'posix_spawn()' -lrt
trylibs timer timer 'timer_create()' -lrt
trylibs pthread pthread 'pthread support' -lpthread -lrt

tryendianness
trytypes
choose cl accept4 'accept4()'
choose c cmsgcloexec 'MSG_CMSG_CLOEXEC'
choose cl dirfd 'dirfd()'
choose cl eventfd 'eventfd()'
choose cl flock 'flock()'
choose cl getpeereid 'getpeereid()'
choose cl sopeercred 'SO_PEERCRED'
choose cl getpeerucred 'getpeerucred()'
choose cl ipv6 'IPv6 support' $socket_lib
choose c msgdontwait 'MSG_DONTWAIT'
choose c odirectory 'O_DIRECTORY'
choose cl openat 'openat()'
choose cl linkat 'linkat()'
choose cl memmem 'memmem()'
choose cl pipe2 'pipe2()'
choose cl ppoll 'ppoll()'
choose cl revoke 'revoke()'
choose cl sendfile 'sendfile()'
choose cl setgroups 'setgroups()'
choose cl settimeofday 'settimeofday()'
choose cl signalfd 'signalfd()'
choose cl splice 'splice()'
choose cl strcasestr 'strcasestr()'
choose c strnlen 'strnlen()'
choose c uint64t 'uint64_t'
choose cl futimens 'futimens()'
choose cl futimes 'futimes()'
choose cl arc4random 'arc4random()'
choose cl arc4random_addrandom 'arc4random_addrandom()'
choose cl itimer 'setitimer()'
choose cl namespaces 'namespaces'
choose cl nsgetparent 'NS_GET_PARENT'
choose cl explicit_bzero 'explicit_bzero()'
choose cl getrandom 'getrandom()'
choose cl grndinsecure 'GRND_INSECURE'
choose cl chroot 'chroot()'
choose cl posixspawnsetsid 'POSIX_SPAWN_SETSID' $spawn_lib
choose cl posixspawnchdir 'posix_spawn_file_actions_addchdir()' $spawn_lib
choose cl posixspawnchdirnp 'posix_spawn_file_actions_addchdir_np()' $spawn_lib

# Here are the evil irreducible run-time sysdeps.
choose clr devurandom '/dev/urandom'
choose clr posixspawnearlyreturn 'posix_spawn() incorrectly returning early' $spawn_lib


# Finally, produce config.mak and config.h

rm -f "$tmps"
echo "Creating config.mak..."
cmdline=$(quote "$0")
for i ; do cmdline="$cmdline $(quote "$i")" ; done
exec 3>&1 1>config.mak
cat << EOF
# This file was generated by:
# $cmdline
# Any changes made here will be lost if configure is re-run.

target := $target
package := $package
prefix := $prefix
datadir := $datadir
sysdepdir := $sysdepdir
dynlibdir := $dynlibdir
libdir := $libdir
includedir := $includedir
sysdeps := $sysdeps
version := $version
sproot := $sproot
home := $home
exthome := ${exthome}
ipv6 := ${ipv6}
SPAWN_LIB := ${spawn_lib}
SOCKET_LIB := ${socket_lib}
SYSCLOCK_LIB := ${sysclock_lib}
TIMER_LIB := ${timer_lib}
UTIL_LIB := ${util_lib}
CC := ${CC_AUTO}
CPPFLAGS_AUTO := $CPPFLAGS_AUTO
CPPFLAGS := $CPPFLAGS $CPPFLAGS_POST
CFLAGS_AUTO := $CFLAGS_AUTO
CFLAGS := $CFLAGS $CFLAGS_POST
LDFLAGS_AUTO := $LDFLAGS_AUTO
LDFLAGS := $LDFLAGS $LDFLAGS_POST
LDFLAGS_NOSHARED := $LDFLAGS_NOSHARED
LDFLAGS_SHARED := $LDFLAGS_SHARED
CROSS_COMPILE := ${cross}
EOF
if test -n "$vpaths" ; then
  echo "vpath lib%a$vpaths"
fi
if test -n "$vpathd" ; then
  echo "vpath lib%.so$vpathd"
fi

if $static ; then
  echo "STATIC_LIBS := libskarnet.a.xyzzy"
else
  echo "STATIC_LIBS :="
fi
if $shared ; then
  echo "SHARED_LIBS := libskarnet.so.xyzzy"
else
  echo "SHARED_LIBS :="
fi
if $allpic ; then
  echo "STATIC_LIBS_ARE_PIC := 1"
else
  echo "STATIC_LIBS_ARE_PIC :="
fi

exec 1>&3 3>&-
echo "  ... done."

echo "Creating src/include/${package}/config.h..."
mkdir -p -m 0755 src/include/${package}
exec 3>&1 1> src/include/${package}/config.h
cat <<EOF
/* ISC license. */

/* Generated by: $cmdline */

#ifndef ${package_macro_name}_CONFIG_H
#define ${package_macro_name}_CONFIG_H

#define ${package_macro_name}_VERSION "$version"
#define ${package_macro_name}_DEFAULTPATH "$defaultpath"
#define ${package_macro_name}_ETC "$datadir"
#define ${package_macro_name}_SPROOT "$sproot"
#define ${package_macro_name}_HOME "$home"
EOF
if $taiclock ; then
  echo "#define ${package_macro_name}_FLAG_CLOCKISTAI"
else
  echo "#undef ${package_macro_name}_FLAG_CLOCKISTAI"
fi
if $ipv6 ; then
  echo "#define ${package_macro_name}_FLAG_WANTIPV6"
else
  echo "#undef ${package_macro_name}_FLAG_WANTIPV6"
fi
if $select ; then
  echo "#define ${package_macro_name}_FLAG_PREFERSELECT"
else
  echo "#undef ${package_macro_name}_FLAG_PREFERSELECT"
fi

echo
echo "#endif"
exec 1>&3 3>&-
echo "  ... done."