Welcome to little lamb

Code » comain » master » tree

[master] / configure

   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
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
#!/bin/sh

. "$(dirname $0)"/comain/libcomain

usageoptions()
{
    local name=$2
    local desc
    local disabled
    local invisible
    local i
    eval desc="\$optdesc$name"
    eval disabled="\$optdisabled$name"
    eval invisible="\$optinvisible$name"
    if test $invisible -ne 0; then return; fi
    i=$((23 - ${#name}))
    printf "      --with-$name%${i}sEnable $desc"
    if test "$disabled" -eq 0; then
        echo " [default]"
    else
        echo ""
    fi
    i=$(($i - 3))
    printf "      --without-$name%${i}sDisable $desc"
    if test "$disabled" -eq 1; then
        echo " [default]"
    else
        echo ""
    fi
}

usage()
{
    local r="$1"
    s=
    if test $has_skalibs -eq 1; then
        s="
      --sysdeps=DIR                 Use skalibs' sysdeps from DIR [LIBDIR/skalibs/sysdeps]"
    fi
    cat <<EOF
usage: $0 [OPTION..]

      --target=TARGET               Set target to run as TARGET [detected]

      --prefix=PREFIX               Set main installation prefix to PREFIX [/usr]
      --bindir=DIR                  Set binary directory to DIR [PREFIX/bin]
      --libdir=DIR                  Set library directory to DIR [PREFIX/lib]
      --incdir=DIR                  Set include directory to DIR [PREFIX/include]
      --sharedir=DIR                Set share directory to DIR [PREFIX/share]
$s

      --add-deppath=DIR             Add DIR to the path to look for dependencies
      --add-incpath=DIR             Add DIR to the path to look for dependencies' headers

      --no-shared                   Don't build shared libraries
      --no-static                   Don't build static libraries
      --no-doc                      Don't build documentation

      --prefer-shared               Prefer to link shared libraires [default]
      --prefer-static               Prefer to link static libraires
      --set-shared=DEP              Use shared linking for dependency DEP
      --set-static=DEP              Use static linking for dependency DEP

EOF
    foreach optname $nb_options usageoptions
        cat <<EOF

  -h, --help                        Show this help screen and exit
EOF
    exit $r
}


##

target=

prefix=
bindir=
libdir=
incdir=
sharedir=

deppath=/lib:/usr/lib
incpath=/include:/usr/include

# this isn't quite right, since skalibs could be optional, but it'll do.
has_skalibs=0
if test -d meta/deps/skalibs; then has_skalibs=1; fi
sysdeps=
prefer=shared
noshared=0
nostatic=0
nodoc=0

CVARS=
CPPFLAGS=$CPPFLAGS
CFLAGS=$CFLAGS
LDFLAGS=$LDFLAGS
COMMON_CPPFLAGS=
COMMON_CFLAGS=
COMMON_LDFLAGS=
COMMON_CPPFLAGS_SHARED=
COMMON_CFLAGS_SHARED=
COMMON_LDFLAGS_SHARED=
VPATHS="# this will handle prefer or set shared/static"

loadoptions

setoption()
{
    local opt=$1
    local v=$2
    if test ! $(optexist $opt) = "1"; then error 1 "unknown option: $opt"; fi
    eval optdisabled$opt=$v
}

for arg ; do
    case "$arg" in
        --target=*) target=${arg#*=} ;;

        --prefix=*) prefix=${arg#*=} ;;
        --bindir=*) bindir=${arg#*=} ;;
        --libdir=*) libdir=${arg#*=} ;;
        --incdir=*) incdir=${arg#*=} ;;
        --sharedir=*) sharedir=${arg#*=} ;;

        --sysdeps=*) if test $has_skalibs -eq 1; then sysdeps=${arg#*=};
        else error 1 "invalid option '$arg'"; fi;;

        --no-shared) noshared=1 ;;
        --no-static) nostatic=1 ;;
        --no-doc|--no-docs|--no-documentation) nodoc=1 ;;

        --add-deppath=*) deppath="${arg#*=}:$deppath" ;;
        --add-incpath=*) incpath="${arg#*=}:$incpath" ;;

        --prefer-shared) prefer=shared ;;
        --prefer-static) prefer=static ;;
        --set-shared=*) eval "link${arg#*=}=1" ;;
        --set-static=*) eval "link${arg#*=}=2" ;;

        --with-*)    setoption $(expr substr "$arg"  8 ${#arg}) 0 ;;
        --without-*) setoption $(expr substr "$arg" 11 ${#arg}) 1 ;;
        --enable-*)  setoption $(expr substr "$arg" 10 ${#arg}) 0 ;;
        --disable-*) setoption $(expr substr "$arg" 11 ${#arg}) 1 ;;

        -h|--help) usage 0  ;;
        -*) error 1 "invalid option '$arg'" ;;
        *) target=$arg ;;
    esac
done

if test -n "$prefix"; then
    deppath="$prefix/lib:$deppath"
    incpath="$prefix/include:$incpath"
fi
if test -z "$prefix"; then prefix="/usr"; fi
if test -z "$bindir"; then bindir="$prefix/bin"; fi
if test -z "$libdir"; then libdir="$prefix/lib"; fi
if test -z "$incdir"; then incdir="$prefix/include"; fi
if test -z "$sharedir"; then sharedir="$prefix/share"; fi

deppath="$(echo $deppath | sed 's/\/\+/\//g')"
incpath="$(echo $incpath | sed 's/\/\+/\//g')"
bindir="$(echo $bindir | sed 's/\/\+/\//g')"
libdir="$(echo $libdir | sed 's/\/\+/\//g')"
incdir="$(echo $incdir | sed 's/\/\+/\//g')"
sharedir="$(echo $sharedir | sed 's/\/\+/\//g')"

ndeppath=$(echo $deppath | tr -dc : | wc -c)
nincpath=$(echo $incpath | tr -dc : | wc -c)
ndeppath=$(( ndeppath + 1 ))
nincpath=$(( nincpath + 1 ))

# pass 2, to be able to use state of other options as selection
setoptionsdefault

cprefix=$(sanitize "$name")

procdep_real()
{
    local depdir="$1"
    local intdir="$2"

    local depname="${depdir##*/}"

    # internal or not?
    local isint=0
    if test -d "$intdir/$depname"; then isint=1; fi

    # is it lila?
    local islila=0
    if ! test -e "$depdir/get_version"; then islila=1; fi

    if test $isint -eq 1; then
        procdep_search "$depname" "$depdir" "$intdir" $isint $islila "$intdir/$depname"
    else
        local i=1
        local s

        while test $i -le $ndeppath; do
            s="$(echo $deppath | cut -d: -f$i)"
            s="$(procdep_search "$depname" "$depdir" "$intdir" $isint $islila "$s")"
            if ! test "$(expr substr "$s" 3 8)" = "NOTFOUND"; then
                echo $s
                return
            fi
            i=$(( i + 1 ))
        done
        echo "$isint NOTFOUND"
    fi
}

procdep_search()
{
    local depname="$1"
    local depdir="$2"
    local intdir="$3"
    local isint="$4"
    local islila="$5"
    local libdir="$6"
    local d="$libdir"

    local incdir

    # look for it
    local vpath
    local vpatha
    local inc
    if ! test -e "$depdir/cpnt"; then
        if test $islila -eq 1; then
            vpath="$d"
            vpatha="$d"
            if test $isint -eq 1; then d="$d/build"; fi
            if ! test -d "$d/comain/$depname"; then
                echo $isint NOTFOUND
                return
            fi
        else
            r=0
            vpath="$d/$($depdir/is_found $isint shared "$d" 2>/dev/null)"
            if test $? -eq 0; then r=1; fi
            vpatha="$d/$($depdir/is_found $isint static "$d" 2>/dev/null)"
            if test $? -eq 0; then r=1; fi
            if test $r -eq 0; then
                echo $isint NOTFOUND
                return
            fi

            local p
            local n
            if test $isint -eq 1; then
                n=1
                p="$d"
            else
                n=$nincpath
                p="$incpath"
            fi

            local i=1
            local s

            r=0
            while test $i -le $n; do
                s="$(echo $p | cut -d: -f$i)"
                inc="$($depdir/is_found $isint include "$s" 2>/dev/null)"
                if test $? -eq 0; then
                    r=1
                    break
                fi
                i=$(( i + 1 ))
            done
            if test $r -eq 0; then
                echo $isint INCNOTFOUND
                return
            fi
            if test "$inc" = "-"; then
                incdir=
            else
                incdir="$s/$inc"
            fi
        fi
    fi

    # get version
    local version
    if test $islila -eq 1; then
        if test -e "$depdir/cpnt"; then
            d="$intdir/$depname/meta"
        else
            if test $isint -eq 1; then
                d="$intdir/$depname/build"
            else
                d="$libdir"
            fi
            d="$d/comain/$depname"
        fi
        version="$(cat "$d/version" 2>/dev/null)"
    else
        version="$("$depdir/get_version" $isint "$intdir/$depname" "$libdir" "$incdir" 2>/dev/null)"
    fi

    if test -z "$version"; then
        echo $isint NOVERSION
        return
    fi

    # needed version
    local needver="$(cat "$depdir/version" 2>/dev/null)"
    if test -z "$needver"; then
        echo $isint NONEEDVER
        return
    fi

    local verok=$(vercmp "$version" "$needver")
    if test $verok -lt 0; then
        echo $isint BADVERSION $version $needver $verok
        return
    fi

    # get data: libs, flags...
    local libs
    local cflags
    local cppflags
    local ldflags
    if test $islila -eq 1; then
        libs="$(cat "$d/libs" 2>/dev/null)"
        libsA="$(cat "$d/libs.static" 2>/dev/null)"
        if test $isint -eq 1; then
            inc="$intdir/$depname/src/lib$depname/include"
        else
            inc="$(cat "$d/incdir" 2>/dev/null)"
        fi
        ldflags="$(cat "$d/ldflags" 2>/dev/null)"
        ldflagsA="$(cat "$d/ldflags.static" 2>/dev/null)"
        cflags="$(cat "$d/cflags" 2>/dev/null)"
        cflagsA="$(cat "$d/cflags.static" 2>/dev/null)"
        cppflags="$(cat "$d/cppflags" 2>/dev/null)"
        cppflagsA="$(cat "$d/cppflags.static" 2>/dev/null)"
    else
        libs="$("$depdir/get_libs" shared $isint "$intdir/$depname" "$libdir" "$incdir" 2>/dev/null)"
        libsA="$("$depdir/get_libs" static $isint "$intdir/$depname" "$libdir" "$incdir" 2>/dev/null)"
        inc="$incdir"
        ldflags="$("$depdir/get_ldflags" shared $isint "$intdir/$depname" "$libdir" "$incdir" 2>/dev/null)"
        ldflagsA="$("$depdir/get_ldflags" static $isint "$intdir/$depname" "$libdir" "$incdir" 2>/dev/null)"
        cflags="$("$depdir/get_cflags" shared $isint "$intdir/$depname" "$libdir" "$incdir" 2>/dev/null)"
        cflagsA="$("$depdir/get_cflags" static $isint "$intdir/$depname" "$libdir" "$incdir" 2>/dev/null)"
        cppflags="$("$depdir/get_cppflags" shared $isint "$intdir/$depname" "$libdir" "$incdir" 2>/dev/null)"
        cppflagsA="$("$depdir/get_cppflags" static $isint "$intdir/$depname" "$libdir" "$incdir" 2>/dev/null)"
    fi
    test -n "$inc" && cppflags="-isystem $inc $cppflags"

    # lila: dependencies?
    if test $islila -eq 1; then
        if test $isint -eq 1; then
            d="$intdir/$depname/build"
        else
            d="$libdir"
        fi
        d="$d/comain/$depname/deps"

        if test -d "$d"; then
            for d in "$d/"*; do
                local tmp=configure-$PPID-$$
                while test -e $tmp; do
                    tmp=configure-$PPID-$$-"$(date +%s 2>/dev/null)"
                done
                local err
                local i=0
                mkfifo $tmp
                procdep_real "$d" "$intdir" > "$tmp" &
                while IFS= read -r arg; do
                    if test $i -ne 1; then
                        i=1
                        err="$(expr substr "$arg" 3 ${#arg})"
                    else
                        case "$arg" in
                            LIBS=*) libs="$libs $(expr substr "$arg" 6 ${#arg}) " ;;
                            LIBSA=*) libsA="$libsA $(expr substr "$arg" 7 ${#arg}) " ;;
                            LDFLAGS=*) ldflags="$ldflags $(expr substr "$arg" 9 ${#arg}) " ;;
                            LDFLAGSA=*) ldflagsA="$ldflagsA $(expr substr "$arg" 10 ${#arg}) " ;;
                            CFLAGS=*) cflags="$cflags $(expr substr "$arg" 8 ${#arg}) " ;;
                            CFLAGSA=*) cflagsA="$cflagsA $(expr substr "$arg" 9 ${#arg}) " ;;
                            CPPFLAGS=*) cppflags="$cppflags $(expr substr "$arg" 10 ${#arg}) " ;;
                            CPPFLAGSA=*) cppflagsA="$cppflagsA $(expr substr "$arg" 11 ${#arg}) " ;;
                            VPATH=*) vpath="$vpath $(expr substr "$arg" 7 ${#arg}) " ;;
                            VPATHA=*) vpathA="$vpathA $(expr substr "$arg" 8 ${#arg}) " ;;
                        esac
                    fi
                done < $tmp
                rm -r $tmp
                if ! test "$(expr substr "$err" 1 3)" = "OK "; then
                    echo $isint ERRDEP ${d##*/} $err
                    return
                fi
            done
        fi
    fi

    cat <<EOF
$isint OK $version $needver $verok
LIBS=$(echo $libs | tr '\n' ' ')
LIBSA=$(echo $libsA | tr '\n' ' ')
LDFLAGS=$(echo $ldflags | tr '\n' ' ')
LDFLAGSA=$(echo $ldflagsA | tr '\n' ' ')
CFLAGS=$(echo $cflags | tr '\n' ' ')
CFLAGSA=$(echo $cflagsA | tr '\n' ' ')
CPPFLAGS=$(echo $cppflags | tr '\n' ' ')
CPPFLAGSA=$(echo $cppflagsA | tr '\n' ' ')
VPATH=$(echo $vpath | tr '\n' ' ')
VPATHA=$(echo $vpathA | tr '\n' ' ')
EOF
}

procneeddep()
{
    local depname=$1
    local needed
    eval needed=\"\$need$depname\"
    if test -z "$needed"; then return 0; fi
    return 1
}

procdep_error()
{
    local s
    s="$@"
    case ${s%% *} in
        BADVERSION) echo "version too old" ;;
        NOTFOUND) echo "not found" ;;
        INCNOTFOUND) echo "include dir not found" ;;
        NOVERSION) echo "version unknown";;
        NONEEDVER) echo "needed version unknown" ;;
        ERRDEP) echo "dependency error" ;;
        *) echo "unexpected error" ;;
    esac
}

procdep()
{
    local idx=$1
    local dep="$2"
    local depname="${dep##*/}"
    local cpnt="$3"
    local link=0
    local needver="$(cat $dep/version 2>/dev/null)"
    # include dir: defaults to $incdir/NAME
    local path="$depname"
    # a different dirname (inside $incdir) might be specified
    if test -e "$dep/include"; then path="$(cat $dep/include)"; fi
    local path="$incdir/$path"
    local internal=0
    local found=0
    local depdir=

    if test "$cpnt" -ne 1; then
        type="Dependency"
        depdir="deps/$depname"
    else
        type="Component"
        internal=1
        depdir="./$depname"
    fi

    if procneeddep "$depname"; then
        echo "  -> $type $depname not needed; SKipping"
        eval idx$depname=$1
        eval after$depname=0
        return
    fi
    local tmp=configure-$PPID-$$
    while test -e $tmp; do
        tmp=configure-$PPID-$$-"$(date +%s 2>/dev/null)"
    done
    local s
    local i=0
    mkfifo $tmp
    procdep_real "$dep" "deps" > "$tmp" &
    while IFS= read -r arg; do
        if test $i -ne 1; then
            i=1
            s="$arg"
        else
            case "$arg" in
                LIBS=*) deplibs="$(expr substr "$arg" 6 ${#arg}) " ;;
                LIBSA=*) deplibsA="$(expr substr "$arg" 7 ${#arg}) " ;;
                LDFLAGS=*) depldflags="$(expr substr "$arg" 9 ${#arg}) " ;;
                LDFLAGSA=*) depldflagsA="$(expr substr "$arg" 10 ${#arg}) " ;;
                CFLAGS=*) depcflags="$(expr substr "$arg" 8 ${#arg}) " ;;
                CFLAGSA=*) depcflagsA="$(expr substr "$arg" 9 ${#arg}) " ;;
                CPPFLAGS=*) depcppflags="$(expr substr "$arg" 10 ${#arg}) " ;;
                CPPFLAGSA=*) depcppflagsA="$(expr substr "$arg" 11 ${#arg}) " ;;
                VPATH=*) vpath="$(expr substr "$arg" 7 ${#arg}) " ;;
                VPATHA=*) vpathA="$(expr substr "$arg" 8 ${#arg}) " ;;
            esac
        fi
    done < $tmp
    rm -r $tmp

    if test "$(expr substr "$s" 1 1)" = "1"; then internal=1; fi
    s="$(expr substr "$s" 3 ${#s})"

    echon "  -> $type $depname "
    if test $internal -ne 1; then
        echon "(system) "
    else
        echon "(internal) "
        # internal deps defaults to static linking
        link=2
    fi

    local err="$(procdep_error $s)"
    local code="${s%% *}"
    if test "$code" = "OK" || test "$code" = "BADVERSION"; then
        local i=0
        for s in $s; do
            case $i in
                1) depver=$s ;;
                2) needver=$s ;;
                3) cmp=$s ;;
            esac
            i=$(( $i + 1 ))
        done
        echon "version $depver ($needver needed)"
    fi

    if ! test "$code" = "OK"; then
        echo
        local m=
        if test "$code" = "ERRDEP"; then
            m=${s#* }
            m=" (${m%% *}: $(procdep_error ${m#* }))"
        fi
        test -z "$fail" && fail="$type $depname: $err$m"
        return
    fi

    # linking type
    local l="$(eval "echo \$link$depname")"
    if test -n "$l" && test "$l" -ge 0; then link="$l"; fi

    if test $cpnt -ne 1 && test $link -gt 0; then
        if test $link -eq 1; then
            echo " (shared linking)"
        else
            echo " (static linking)"
        fi
    else
        echo
    fi

    if test $cpnt -ne 1; then
        eval idx$depname=$1
        eval after$depname=0
        eval "cflags$depname=\"$depcflags\""
        eval "cflagsA$depname=\"$depcflagsA\""
        eval "cppflags$depname=\"$depcppflags\""
        eval "cppflagsA$depname=\"$depcppflagsA\""
        eval "ldflags$depname=\"$depldflags\""
        eval "ldflagsA$depname=\"$depldflagsA\""
        eval "libs$depname=\"$deplibs\""
        eval "libsA$depname=\"$deplibsA\""

        mkvpath()
        {
            local link=$1
            local vpath="$2"
            shift 2
            if test $link -eq 0; then link=3; fi
            local ext1=so
            local ext2=a
            if test $prefer = "static"; then
                ext1=a
                ext2=so
                if test $link -lt 3; then
                    link=$((1 + ($link & 1)))
                fi
            fi
            for lib in $@; do
                if test $(($link & 5)) -ge 1; then
                    echo vpath lib$(expr substr "$lib" 3 ${#lib}).$ext1 $vpath
                fi
                if test $(($link & 6)) -ge 2; then
                    echo vpath lib$(expr substr "$lib" 3 ${#lib}).$ext2 $vpath
                fi
                link=$(($link | 4))
            done
        }

        VPATHS="$VPATHS
$(mkvpath $link "$vpath" $deplibs)"

        if test $depname = "skalibs"; then
            if test $internal -eq 1; then
                sysdeps="$depdir/sysdeps.cfg"
            else
                if test -z "$sysdeps"; then sysdeps="$(echo $vpath)/skalibs/sysdeps"; fi
                sysdeps="$(echo $sysdeps | sed 's/\/\+/\//g')"
            fi
        fi
    fi
}

if test -d deps; then
    echo "Building internal dependencies..."
    comain/blddeps build
fi

fail=
cat <<EOF
Configuring $name $version :
 => Prefer $prefer linking for dependencies
 => Checking requirements...
EOF

loaddeps
foreach "cpnt" $nb_cpnt procdep 1
foreach "deps" $nb_deps procdep 0

setdepsafters()
{
    local dep
    for dep in meta/deps/*/after; do
        if test "$dep" = "meta/deps/*/after"; then break; fi
        local depname="$(expr substr "$dep" 11 $((${#dep} - 16)))"
        local depidx=$(($(eval echo \$idx$depname) + 0))
        local after=$(($(eval echo \$after$depname) + 0))
        local name
        for name in meta/deps/$depname/after/*; do
            name=${name##*/}
            local idx=$(eval echo \$idx$name)
            after=$(($after | (1 << ($idx - 1))))
        done
        eval "after$depname=$after"
    done
}
setdepsafters

if test -n "$fail"; then
    error 2 "Cannot continue: $fail"
fi


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

trycmd()
{
    local name="$1"
    shift
    test -z "$(eval echo \$$name)" && cmdexists "$1" && eval "$name=\"$*\""
}

tryflag()
{
    echon "  -> Checking whether compiler accepts $2.."
    echo "typedef int x;" > "tmp.c"
    if $CC $CPPFLAGS $CFLAGS "$2" -c -o "tmp.o" "tmp.c" > /dev/null 2>&1; then
        echo ".. yes"
        eval "$1=\"\${$1} \$2\""
        eval "$1=\${$1# }"
        return 0
    else
        echo ".. no"
        return 1
    fi
}

tryldflag()
{
    echon "  -> Checking whether linker accepts $2.."
    echo "typedef int x;" > "tmp.c"
    if $CC $CPPFLAGS $CFLAGS $LDFLAGS -nostdlib "$2" -o "tmp.o" "tmp.c" > /dev/null 2>&1; then
        echo ".. yes"
        eval "$1=\"\${$1} \$2\""
        eval "$1=\${$1# }"
        return 0
    else
        echo ".. no"
        return 1
    fi
}

trap 'rm -f tmp.c tmp.o config.mk.tmp config.h.tmp' EXIT ABRT INT QUIT TERM HUP

echo " => Checking compiler..."

echon "  -> Looking for a C compiler.."
trycmd CC "gcc"
trycmd CC "clang"
trycmd CC "cc"
if test -z "$CC"; then
    echo ".. not found"
    error 2 "Unable to find a C compiler"
else
    echo ".. $CC"
fi
trycmd AR "ar"
trycmd RANLIB "ranlib"

ENDIAN=0
if test $has_skalibs -eq 1; then
    echo " => Checking sysdeps... $sysdeps"
    echon "  -> Checking target.."
    if test -z "$target" ; then
        target=$($CC -dumpmachine 2>/dev/null) || target=unknown
    fi
    BITS=32
    case "$target" in
        *x86_64*) BITS=64 ;;
    esac
    echo ".. $target [${BITS}bit]"
    if test ! -d "$sysdeps" || test ! -f "$sysdeps/target"; then
        error 2 "$sysdeps is not a valid sysdeps directory"
    fi
    if test "x$target" != "x$(cat "$sysdeps/target")"; then
        error 2 "target $target does not match the contents of $sysdeps/target ($(cat "$sysdeps/target"))"
    fi

    echon "  -> Checking endianness.."
    case "$(grep ^endianness: "$sysdeps/sysdeps" | cut -d' ' -f2)" in
        little) ENDIAN=LITTLE; echo ".. little" ;;
        big)    ENDIAN=BIG; echo ".. big" ;;
        pdp)    ENDIAN=PDP; echo ".. pdp" ;;
        *)  echo ".. unsupported";
            error 2 "unsupported endianness ($(grep ^endianness: "$sysdeps/sysdeps"))"
            ;;
    esac
fi

echo " => Testing C compiler..."
tryflag COMMON_CFLAGS -std=c99
tryflag COMMON_CFLAGS -fomit-frame-pointer
tryflag COMMON_CFLAGS -fno-exceptions
tryflag COMMON_CFLAGS -fno-unwind-tables
tryflag COMMON_CFLAGS -fno-asynchronous-unwind-tables
tryflag COMMON_CPPFLAGS -Werror=implicit-function-declaration
tryflag COMMON_CPPFLAGS -Werror=implicit-int
tryflag COMMON_CPPFLAGS -Werror=pointer-sign
tryflag COMMON_CPPFLAGS -Werror=pointer-arith
tryflag COMMON_CPPFLAGS -Wno-unused-value
tryflag COMMON_CFLAGS -ffunction-sections
tryflag COMMON_CFLAGS -fdata-sections
tryldflag COMMON_LDFLAGS -Wl,--as-needed
tryldflag COMMON_LDFLAGS -Wl,--sort-section=alignment
tryldflag COMMON_LDFLAGS -Wl,--sort-common
tryflag COMMON_CPPFLAGS_SHARED -fPIC
tryldflag COMMON_LDFLAGS_SHARED -Wl,--hash-style=gnu
tryldflag COMMON_LDFLAGS_SHARED -nostartfiles


echo " => Paths :"
cat <<EOF
  -> bindir   : $bindir
  -> libdir   : $libdir
  -> incdir   : $incdir
  -> sharedir : $sharedir
EOF

excl=
options=
optleft=0

showoption()
{
    local idx=$1
    local name=$2
    local disabled
    local invisible
    local cvar
    eval disabled=\$optdisabled$name
    eval invisible=\$optinvisible$name
    eval cvar=\$optcvar$name
    if test $disabled -eq 1; then
        CVARS="$CVARS
#undef ${cprefix}_$cvar"
        if test -e meta/options/$name/objs; then
            while IFS= read -r line; do
                line="$(printf $line | sed -e 's/\//\\\//g')"
                excl="-e s/build\\/$line//g $excl"
                if test "$(expr substr "$line" $((${#line}-1)) 2)" = ".o"; then
                    line="$(expr substr "$line" 1 $((${#line}-2))).lo"
                    excl="-e s/build\\/$line//g $excl"
                fi
            done < meta/options/$name/objs
        fi
    else
        CVARS="$CVARS
#define ${cprefix}_$cvar 1"
        optleft=$(($optleft | (1 << ($idx - 1))))
        local desc
        eval desc=\$optdesc$name
        if test -n "$desc"; then
            options="$options
  -> $name : $desc"
        fi
    fi
}

if test $nb_options -gt 0; then
    foreach "optname" $nb_options showoption
    if test -n "$options"; then
        echo " => Options :$options"
    fi
fi

setoptionflags()
{
    local name=$1
    local cppflags
    local cflags
    local ldflags
    eval cppflags=\"\$optcppflags$name\"
    eval cflags=\"\$optcflags$name\"
    eval ldflags=\"\$optldflags$name\"
    if test -n "$cppflags"; then COMMON_CPPFLAGS="$COMMON_CPPFLAGS $cppflags"; fi
    if test -n "$cflags"; then COMMON_CFLAGS="$COMMON_CFLAGS $cflags"; fi
    if test -n "$ldflags"; then COMMON_LDFLAGS="$COMMON_LDFLAGS $ldflags"; fi
}

setoptionsflags()
{
    local i=1
    while test $optleft -gt 0; do
        local n=$((1 << ($i - 1)))
        if test $(($optleft & $n)) -gt 0; then
            local name="$(eval echo \$optname$i)"
            local after=$((($(eval echo \$optafter$name) + 0) & $optleft))
            eval "optafter$name=$after"
            if test $after -eq 0; then
                setoptionflags "$name"
                optleft=$(($optleft & ~$n))
            fi
        fi
        if test $i -eq $nb_options; then
            i=1
        else
            i=$(($i + 1))
        fi
    done
}

setoptionsflags


if test $(($noshared + $nostatic)) -gt 0; then
    echo " => Libraries"
    if test $noshared -eq 1; then
        echo "  -> Don't build shared libraries"
    fi
    if test $nostatic -eq 1; then
        echo "  -> Don't build static libraries"
    fi
fi

if test $nodoc -eq 1; then
    cat <<EOF
 => Miscellaneous
  -> Don't build documentation
EOF
fi

echo " => Generating files..."

echo "  -> Generating config.mk..."
exec 5>&1 1>config.mk.tmp
cat <<EOF
# This file was generated by:
# $0 $@
# Any changes you make in here will be lost on the next ./configure

# paths
BINDIR = $bindir
LIBDIR = $libdir
INCDIR = $incdir
SHAREDIR = $sharedir

# general variables
PROJECT_NAME = $name
PROJECT_VERSION = $version

CC = $CC
AR = $AR
RANLIB = $RANLIB

NOSHARED := $noshared
NOSTATIC := $nostatic

EOF

if test -n "$BITS"; then
    echo "BITS := $BITS"
    echo ""
fi

# add our own libraries so we can have binaries depending on them
intlib=$nb_deps
intaft=0
for lib in src/lib*; do
    if test $lib = "src/lib*"; then break; fi
    lib="$(expr substr "$lib" 8 ${#lib})"
    intaft=$(( $intaft + (1 << $nb_deps) ))
    nb_deps=$((nb_deps + 1))
    eval "idx$lib=$nb_deps"
    eval "deps$nb_deps=$lib"
    if test -e meta/lib$lib.libs; then
        eval "libs$lib=\"$(cat meta/lib$lib.libs)\""
    else
        eval "libs$lib=-l$lib"
    fi
    if test -e meta/lib$lib.libs.static; then
        eval "libsA$lib=\"$(cat meta/lib$lib.libs.static)\""
    fi
    if test -e meta/lib$lib.cflags; then
        eval "cflags$lib=\"$(cat meta/lib$lib.cflags)\""
    fi
    if test -e meta/lib$lib.cflags.static; then
        eval "cflagsA$lib=\"$(cat meta/lib$lib.cflags.static)\""
    fi
    if test -e meta/lib$lib.ldflags; then
        eval "ldflags$lib=\"$(cat meta/lib$lib.ldflags)\""
    fi
    if test -e meta/lib$lib.ldflags.static; then
        eval "ldflagsA$lib=\"$(cat meta/lib$lib.ldflags.static)\""
    fi
    # in case there was a --set-shared/--set-static for it
    link=$(eval echo \$link$lib)
    # if not, we shall default to static (if built)
    if test -z "$link"; then
        if test $nostatic -eq 1; then
            link=1
        else
            link=2
        fi
    fi
    if test $link -eq 1; then
        VPATHS="$VPATHS
vpath lib$lib.so ."
    else
        VPATHS="$VPATHS
vpath lib$lib.a ."
    fi
    eval "cppflags$lib=\"-isystem src/lib$lib/include\""
done

# not sure this really matters, so long as both are mentioned it will be the
# order of our vpath that will matter really - still, let's keep it that way
if test "$prefer" = "static"; then
    echo "# prefer static linking"
    echo ".LIBPATTERN := lib%.a lib%.so"
else
    echo "# prefer shared linking"
    echo ".LIBPATTERN := lib%.so lib%.a"
fi
echo $VPATHS

PER_FLAGS=
echo "# dependencies"
echo ".SECONDEXPANSION:"
BLD_STATIC_LIBS=
BLD_SHARED_LIBS=
BLD_BINS=
BLD_DOCS=
INST_HDR=
HEADERS=
setupobjs()
{
    local docs_objs
    local d
    # FIXME find's -printf isn't POSIX
    lf="
"
    for d in src/*; do
        local name="$(expr substr "$d" 5 ${#d})"
        if test "$name" = "*" || test "$name" = "include"; then continue; fi
        local objs
        if test "$name" = "extra"; then
            continue
        elif test "$name" = "doc"; then
            if test $nodoc -eq 1; then continue; fi
            objs="$(find "$d" -name '*.md' \! -path "$d"/include/'*')"
            for i in 9 8 7 6 5 4 3 2 1 0; do
                local s="$(echo "$objs" \
                    | sed -e 's/^src\/doc\/\(.\+\).'$i'.md$/build\/doc\/\1.'$i'.md/g' \
                        -e 's/src\/.\+$//g' \
                    | tr '\n' ' ' | sed -e 's/ \+/ /g')"
                BLD_DOCS="BLD_MD$i=$s$lf$BLD_DOCS"
            done
            BLD_DOCS="BLD_DOCS=$(echo "$objs" \
                | sed -e 's/^src\/doc.*\/\(.\+\.\(.\)\.md\)$/\2\/\1/g' \
                | tr '\n' ' ' | sed -e 's/ \+/ /g')$lf$BLD_DOCS"
            BLD_DOCS="$(echo "$objs" \
                | sed -e 's/^src\/doc\(.*\)\/\(.\+\.\(.\)\.md\)$/\$(DESTMD)\/\3\/\2: build\/doc\1\/\2\n	$(INSTMD)/g' \
                )$lf$BLD_DOCS"
            continue
        elif test "$(expr substr "$name" 1 3)" = "lib"; then
            objs="$(find "$d"/include -type f)"
            INST_HDR="$(echo "$objs" \
                | sed -e 's/^src\/lib\(.\+\)\/include\/\(.\+\)$/$(DESTDIR)$(INCDIR)\/\2: \0\n	$(INSTHDR)/g' \
                )$lf$INST_HDR"
            HEADERS="$(echo "$objs" \
                | sed -e 's/^src\/lib.\+\/include\/\(.\+\)$/\1/g' \
                | tr '\n' ' ') $HEADERS"
        fi
        objs="$(find "$d" -name '*.[cS]' | sed -e s/^src/build/g \
            -e s/\\.c$/.o/g -e s/\\.S/.o/g $excl | tr '\n' ' ')"
        objs="$objs $(find "$d" -type l -name '*.o' -printf "build/%l ")"

        local cppflags=
        local cflags=
        local cflagsA=
        local ldflags=
        local ldflagsA=
        local libs=
        local libsA=
        local deps=0
        local depname
        local tmp=configure-$PPID-$$
        while test -e $tmp; do
            tmp=configure-$PPID-$$-"$(date +%s 2>/dev/null)"
        done
        mkfifo $tmp
        find "$d" -type f -name '+*' > $tmp  &
        while IFS= read -r depname; do
            depname=${depname##*/+}
            local idx=$(($(eval echo \$idx$depname) + 0))
            if test $idx -ge 1; then
                deps=$(($deps | (1 << ($idx - 1))))
            fi
        done < $tmp
        rm -r $tmp
        local i=1
        while test $deps -gt 0; do
            local n=$((1 << ($i - 1)))
            if test $(($deps & $n)) -gt 0; then
                depname=$(eval echo \$deps$i)
                depname=${depname##*/}
                local after=0
                if test $i -le $intlib; then
                    # this is an "actual" deps, i.e. not an internal one
                    # so we get its actual value for "after"
                    after=$((($(eval echo \$after$depname) + 0) & $deps))
                    # and make sure that it is also after all internal deps
                    after=$(( ($intaft & $deps) + $after ))
                fi
                if test $after -eq 0; then
                    libs="$libs $(eval echo \$libs$depname)"
                    libsA="$libsA $(eval echo \$libsA$depname)"
                    cppflags="$cppflags $(eval echo \$cppflags$depname)"
                    cflags="$cflags $(eval echo \$cflags$depname)"
                    cflagsA="$cflagsA $(eval echo \$cflagsA$depname)"
                    ldflags="$ldflags $(eval echo \$ldflags$depname)"
                    ldflagsA="$ldflagsA $(eval echo \$ldflagsA$depname)"
                    deps=$(($deps & ~$n))
                fi
            fi
            if test $i -eq $nb_deps; then
                i=1
            else
                i=$(($i + 1))
            fi
        done

        if test "$(expr substr "$name" 1 3)" = "lib"; then
            local l="$(expr substr "$name" 4 ${#name})"
            COMMON_CPPFLAGS="-isystem src/$name/include $COMMON_CPPFLAGS"
            if test $nostatic -eq 0; then
                echo "$name.a: $objs"
                BLD_STATIC_LIBS="$name.a $BLD_STATIC_LIBS"
            fi
            if test $noshared -eq 0; then
                objs="$(find "$d" -type f -name '*.[cS]' | sed -e s/^src/build/g \
                    -e s/\\.c$/.lo/g -e s/\\.S/.o/g $excl | tr '\n' ' ')"
                objs="$objs $(find "$d" -type l -printf "build/%l " | sed -e s/\\.o/.lo/g)"
                echo "$name.so: $objs $libs"
                BLD_SHARED_LIBS="$name.so $BLD_SHARED_LIBS"
            fi
        else
            BLD_BINS="$name $BLD_BINS"
            echo "$name: $objs $libs"
        fi

        cppflags=$(trim "$cppflags")
        if test -n "$cppflags"; then
            append PER_FLAGS "CPPFLAGS_$name = $cppflags"
        fi
        cflags=$(trim "$cflags")
        if test -n "$cflags"; then
            append PER_FLAGS "CFLAGS_$name = $cflags"
        fi
        cflagsA=$(trim "$cflagsA")
        if test -n "$cflagsA"; then
            append PER_FLAGS "CFLAGS_STATIC_$name = $cflagsA"
        fi
        ldflags=$(trim "$ldflags")
        if test -n "$ldflags"; then
            append PER_FLAGS "LDFLAGS_$name = $ldflags"
        fi
        ldflagsA=$(trim "$ldflagsA")
        if test -n "$ldflagsA"; then
            append PER_FLAGS "LDFLAGS_STATIC_$name = $ldflagsA $libsA $libs"
        fi

    done
    echo "# docs"
    echo "$docs_objs"
}
setupobjs

cat <<EOF

# build targets
$BLD_DOCS
$INST_HDR
BLD_STATIC_LIBS = $BLD_STATIC_LIBS
BLD_SHARED_LIBS = $BLD_SHARED_LIBS
BLD_BINS = $BLD_BINS
HEADERS = $HEADERS

# some flags
COMMON_CPPFLAGS += $COMMON_CPPFLAGS
COMMON_CFLAGS   += $COMMON_CFLAGS
COMMON_LDFLAGS  += $COMMON_LDFLAGS
COMMON_CPPFLAGS_SHARED += $COMMON_CPPFLAGS_SHARED
COMMON_CFLAGS_SHARED   += $COMMON_CFLAGS_SHARED
COMMON_LDFLAGS_SHARED  += $COMMON_LDFLAGS_SHARED
CPPFLAGS += $CPPFLAGS
CFLAGS   += $CFLAGS
LDFLAGS  += $LDFLAGS

$PER_FLAGS
EOF

exec 1>&5 5>&-
mv -f config.mk.tmp config.mk


echo "  -> Generating config.h..."

url="$(head -1 meta/site 2>/dev/null)"
if test -z "$url"; then url="https://lila.oss"; fi

exec 5>&1 1>config.h.tmp
cat <<EOF
/* This file was generated by:
 * $0 $@
 * Any changes you make in here will be lost on the next ./configure
 */
#ifndef ${cprefix}_CONFIG_H
#define ${cprefix}_CONFIG_H

#define ${cprefix}_CURYEAR   "$(date +%Y)"
#define ${cprefix}_AUTHOR    "Olivier Brunel"
#define ${cprefix}_VERSION   "$version"
#define ${cprefix}_URL       "$url"
#define ${cprefix}_LIBDIR    "$libdir/$name"
#define ${cprefix}_SHAREDIR  "$sharedir/$name"
$CVARS

#endif /* ${cprefix}_CONFIG_H */
EOF

exec 1>&5 5>&-
if ! test -d src/include; then mkdir src/include; fi
mv -f config.h.tmp src/include/config.h

rm -rf config.tmp
mkdir config.tmp
echo $incdir > config.tmp/incdir
if test $has_skalibs -eq 1; then
    echo $ENDIAN > config.tmp/endian
fi

echo "done."