summaryrefslogtreecommitdiff
path: root/configure
blob: bbef09add3d802fc817e0592179945e04bcb15a2 (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
#!/bin/sh

myname="${0##*/}"

VERSION=$( cat .version )
DATE=$( date +%Y%m%d )

PREFIX_DEFAULT=/usr/local

BINDIR_set=
LIBDIR_set=
DOCDIR_set=
MANDIR_set=
LOCAL_set=
FORCE=

do_quit=

# Simply print the error message, and exit. Obvious, he?
do_error() {
    printf "${myname}: ${@}\n"
    exit 1
}

# Given an option string and the following argument,
# echoes the value of the option.
# If --var=val => echoes val and returns 0, meaning second arg was not consumed
# If --var val => echoes val and returns non null, meaning second arg was used
get_optval(){
    case "$1" in
        --*=?*)
            printf "${1#*=}"
            return 0
            ;;
        *)
            printf "${2}"
            return 1
            ;;
    esac
}

# The set_xxx functions will set the corresponding configuration variable
# They return 0 if second arg was not consumed, and non-zero if it was consumed.
set_prefix() {
    PREFIX="$( get_optval "$1" "$2" )"
}
set_bindir() {
    BINDIR_set=1
    BINDIR="$( get_optval "$1" "$2" )"
}
set_libdir() {
    LIBDIR_set=1
    LIBDIR="$( get_optval "$1" "$2" )"
}
set_docdir() {
    DOCDIR_set=1
    DOCDIR="$( get_optval "$1" "$2" )"
}
set_mandir() {
    MANDIR_set=1
    MANDIR="$( get_optval "$1" "$2" )"
}
set_tool() {
    local var_name="${1%%=*}"
    var_name="${var_name#--with-}"
    eval ${var_name}="\$( get_optval "$1" "$2" )"
}

# var_list is a list of variables, each one holding a path to a
# tool, either detected by ./configure, or specified by the user.
var_list=""
kconfig_list=""

# This function adds a variable name to the above list of variable names.
# $1: the name of the variable to add to the list
add_to_var_list() {
    local v
    for v in ${var_list}; do
        [ "${v}" = "${1}" ] && return 0
    done
    var_list="${var_list} ${1}"
}
add_to_kconfig_list() {
    local k
    for k in ${kconfig_list}; do
        [ "${k}" = "${1}" ] && return 0
    done
    kconfig_list="${kconfig_list} ${1}"
}

# A function to test for required tools/headers/libraries
# Return 0 (true) if found, !0 (false) if not found
#
# $*: [prog|inc|lib]=<name[ name...]>
#     the name(s) of tool(s) to test for
#     mandatory
#       eg: prog=bash   prog="curl wget"
# $*: var=<var_name>
#     the name of the variable to test and set
#     optional
#       eg: var=bash    if ${bash} is set and non-null, use that,
#                       else check for bash and set bash=$(which bash)
# $*: ver=<regexp>
#     for each 'prog', test if $(prog --version) matches 'regexp'
#     optional
#       eg: ver='^GNU bash, version [34]\.'
# $*: lib_exts=<extension[ extension...]>
#     the list of allowed library extension
#     mandatory
#       eg: lib_exts="so dylib"     lib_exts="so dylib a"
# $*: err=<error_message>
#     the error message to print if tool is missing
#     optional, defaults to: '${prog}: none found'
#       eg: err="'bash' 3.x or above was not found"
#     Note: err may be printed by caller, not us
# $*: kconfig=<var_name>
#     the name of a variable to pass down to kconfig if
#     the prog/inc/lib was found
#     optional, defaults to none
#       eg: kconfig=has_libncurses
# $*: skip=[y|n|]
#     if set to 'y', skip the test, but still register the
#     kconfig and var variables; if 'n' or empty, do the
#     test.
#     optional, default to 'n'
#       eg: skip="${static_link_ko}"
check_for() {
    local lib_exts
    local skip
    local val
    local item
    local where
    local status
    local ext

    # Note: prog/inc/lib and var/kconfig/ver/err are set here,
    # but declared by the caller (because it needs it)
    for item in "${@}"; do
        case "${item}" in
            prog=*|inc=*|lib=*|var=*|ver=*|err=*|kconfig=*|lib_exts=*|skip=*)
                eval ${item%%=*}=\"${item#*=}\"
                ;;
            *)  do_error "check_for: incorrect parameters: '${item}'";;
        esac
    done

    case "${prog}:${inc}:${lib}" in
        ?*:?*:|?*::?*|:?*:?*|?*:?*:?*)
            if [ -n "${var}" ]; then
                do_error "check_for: the use of var is not compatible with passing several of [prog|inc|lib] at once"
            fi
            ;;
        ::) do_error "check_for: [prog|inc|lib] is mandatory";;
    esac

    if [ -n "${var}" ]; then
        add_to_var_list "${var}"
    fi
    if [ -n "${kconfig}" ]; then
        add_to_kconfig_list "${kconfig}"
    fi

    if [ "${skip}" = "y" ]; then
        return 0
    fi

    if [ -n "${prog}" ]; then
        for item in ${prog}; do
            printf "Checking for '${item}'... "
            if [ -n "${var}" ]; then
                eval val="\${${var}}"
                if [ -n "${val}" ]; then
                    status="${val} (cached)\n"
                    break
                fi
            fi
            where="$( which "${item}" 2>/dev/null )"
            if [ -z "${where}" ]; then
                printf "no\n"
                continue
            elif [ -n "${ver}" ]; then
                str=$( LC_ALL=C "${where}" --version 2>&1   \
                       |grep -E "${ver}"                    \
                       |head -n 1
                     )
                if [ -z "${str}" ]; then
                    printf "no\n"
                    unset where
                    continue
                fi
            fi
            status="${where}"
            break
        done
        if [ -z "${status}" ]; then
            return 1
        fi
        printf "${status}\n"
        unset status
    fi

    if [ -n "${inc}" ]; then
        for item in ${inc}; do
            printf "Checking for '${item}'... "
            if printf "#include \"${item}\"" |gcc -x c -c - -o /dev/null >/dev/null 2>&1; then
                where="${item}"
                status=yes
                break;
            fi
            printf "no\n"
        done
        if [ -z "${status}" ]; then
            return 1
        fi
        printf "${status}\n"
        unset status
    fi

    if [ -n "${lib}" ]; then
        if [ -z "${lib_exts}" ]; then
            do_error "check_for: no library extension specified for '${lib}'"
        fi
        for item in ${lib}; do
            for ext in ${lib_exts}; do
                printf "Checking for '${item}.${ext}'... "
                where="$( gcc -print-file-name="${item}.${ext}" )"
                if [ "${where}" != "${item}.${ext}" ]; then
                    where="$( readlink "${where}" )"
                    status=yes
                    break 2;
                fi
                printf "no\n"
            done
        done
        if [ -z "${status}" ]; then
            return 1
        fi
        printf "${status}\n"
        unset status
    fi

    if [ -n "${var}" ]; then
        eval ${var}='"'"${where}"'"'
    fi
    if [ -n "${kconfig}" ]; then
        eval ${kconfig}=y
    fi
}

# This function checks for a tool, and aborts if not found
# See check_for(), above, for how to call has_or_abort
has_or_abort() {
    # We declare these 6 variables here, although they are
    # set in check_for(), called below
    local prog inc lib
    local var ver err kconfig

    if ! check_for "$@"; then
        printf " * A mandatory dependency is missing, or version mis-match:\n"
        printf " * - ${err:-${prog}${inc}${lib}: none found}\n"
        if [ -n "${var}" ]; then
            printf " * --> You can give the path to this tool using: --with-${var}=PATH\n"
        fi
        printf "\n"
        # Bail out if --force is not specified
        [ -z "${FORCE}" ] && do_error "Bailing out..."
        printf "<*                                          *>\n"
        printf "<*            FORCE in action:              *>\n"
        printf "<* Continuing despite missing pre-requisite *>\n"
        printf "<*          Prepare for breakage            *>\n"
        printf "<*                                          *>\n"
        printf "\n"
    fi
}

# This function checks for a tool, and warns if not found
# See check_for(), above, for how to call has_or_abort
# Note: if err is not set, then no error message is printed
has_or_warn() {
    # We declare these 6 variables here, although they are
    # set in check_for(), called below
    local prog inc lib
    local var ver err kconfig

    if ! check_for "$@"; then
        printf " * An optional dependency is missing, some features will be disabled"
        printf "${err:+:\n * - ${err}}\n"
        if [ -n "${var}" ]; then
            printf " * --> You can give the path to this tool using: --with-${var}=PATH\n"
        fi
    fi
}

do_help() {
    cat <<__EOF__
\`configure' configures crosstool-NG-${VERSION} to adapt to many kind of systems.

USAGE: ./configure [OPTION]...

Defaults for the options are specified in brackets.

Configuration:
  -h, --help              display this help and exit
      --force             force configure to continue, even in case
                          some pre-requisites are missing

Installation directories:
  --prefix=PREFIX         install files in PREFIX [${PREFIX_DEFAULT}]
  --local                 don't install, and use current directory

By default, \`make install' will install all the files in
\`${PREFIX_DEFAULT}/bin', \`${PREFIX_DEFAULT}/lib' etc.  You can specify
an installation prefix other than \`${PREFIX_DEFAULT}' using \`--prefix',
for instance \`--prefix=\${HOME}'.

For better control, use the options below.

Fine tuning of the installation directories:
  --bindir=DIR            user executables [PREFIX/bin]
  --libdir=DIR            object code libraries [PREFIX/lib]
  --docdir=DIR            info documentation [PREFIX/share/doc]
  --mandir=DIR            man documentation [PREFIX/share/man]

Optional Features:
  --with-install=PATH     Specify the full PATH to GNU install
  --with-make=PATH        Specify the full PATH to GNU make >= 3.80
  --with-grep=PATH        Specify the full PATH to GNU grep
  --with-sed=PATH         Specify the full PATH to GNU sed
  --with-bash=PATH        Specify the full PATH to bash >= 3.0
__EOF__
}

#---------------------------------------------------------------------
# Set user's options

while [ $# -ne 0 ]; do
    case "$1" in
        --local)    LOCAL_set="y"; shift;;
        --prefix*)  set_prefix "$1" "$2" && shift || shift 2;;
        --bindir*)  set_bindir "$1" "$2" && shift || shift 2;;
        --libdir*)  set_libdir "$1" "$2" && shift || shift 2;;
        --docdir*)  set_docdir "$1" "$2" && shift || shift 2;;
        --mandir*)  set_mandir "$1" "$2" && shift || shift 2;;
        --with-*)   set_tool   "$1" "$2" && shift || shift 2;;
        --force)    FORCE=1; shift;;
        --help|-h)  do_help; exit 0;;
        # Skip, auto-stuff compatibility
        --build=*|--host=*|--infodir=*|--datadir=*|--sysconfdir=*|--localstatedir=*) shift;;
        --build|--host|--infodir|--datadir|--sysconfdir|--localstatedir)             shift 2;;
        *)          printf "Unrecognised option: '${1}'\n"; do_help; exit 1;;
    esac
done

# Use defaults
[ -z "${PREFIX}" ] && set_prefix "" "${PREFIX_DEFAULT}"

# Special case when installing locally
if [ "${LOCAL_set}" = "y" ]; then
    set_prefix "" "$( pwd )"
    set_bindir "" "$( pwd )"
    set_libdir "" "$( pwd )"
    set_docdir "" "$( pwd )/docs"
    set_mandir "" "$( pwd )/docs"
fi

#---------------------------------------------------------------------
# Some sanity checks, now

# We check for grep and sed manually, because they are used in check_for()
printf "Checking for 'grep'... "
if [ -n "${grep}" ]; then
    printf "${grep} (cached)\n"
else
    grep="$( which grep 2>/dev/null )"
    if [ -z "${grep}" ]; then
        printf "not found\n"
    else
        printf "${grep}\n"
        printf "Checking whether '${grep}' supports -E... "
        if echo 'foo' |"${grep}" -E 'foo' >/dev/null 2>&1; then
            printf "yes\n"
        else
            printf "no\n"
            grep=
        fi
    fi
fi
if [ -z "${grep}" ]; then
    printf "Either you are missing entirely the needed tool,\n"
    printf "or the version you have is too old.\n"
    printf "You can give the path to this tool using: --with-grep=PATH\n"
    do_error "Bailing out..."
fi
add_to_var_list grep

printf "Checking for 'sed'... "
if [ -n "${sed}" ]; then
    printf "${sed} (cached)\n"
else
    sed="$( which sed 2>/dev/null )"
    if [ -z "${sed}" ]; then
        printf "not found\n"
    else
        printf "${sed}\n"
        printf "Checking whether '${sed}' supports -i and -e... "
        touch .ct-ng.sed.test
        if "${sed}" -r -i -e 's/foo/bar/' .ct-ng.sed.test >/dev/null 2>&1; then
            printf "yes\n"
        else
            printf "no\n"
            sed=
        fi
        rm -f .ct-ng.sed.test
    fi
fi
if [ -z "${sed}" ]; then
    printf "Either you are missing entirely the needed tool,\n"
    printf "or the version you have is too old.\n"
    printf "You can give the path to this tool using: --with-sed=PATH\n"
    do_error "Bailing out..."
fi
add_to_var_list sed

# The regular list of tools we can now easily check for
has_or_abort prog=bash                              \
             var=bash                               \
             ver='^GNU bash, version (3\.[1-9]|4)'  \
             err="'bash' 3.1 or above was not found"
has_or_abort prog=cut
has_or_abort prog=install var=install
has_or_abort prog=make                                  \
             var=make                                   \
             ver='^GNU Make (3.[89][[:digit:]]|[4-9])'  \
             err="GNU 'make' 3.80 or above was not found"
has_or_abort prog=gcc
has_or_abort prog="awk gawk" ver='^GNU Awk' err="GNU 'awk' was not found"
has_or_abort prog=bison
has_or_abort prog=flex
has_or_abort prog=makeinfo
has_or_abort prog=automake                                                      \
             ver='\(GNU automake\) (1\.[[:digit:]]{2,}|[2-9][[:digit:]]*\.)'    \
             err="'automake' 1.10 or above was not found"
has_or_abort prog=libtool                                                                           \
             var=libtool                                                                            \
             ver='\(GNU libtool.*\) (2[[:digit:]]*\.|1\.6[[:digit:]]*\.|1\.5\.[2-9][[:digit:]]+)'   \
             err="'libtool' 1.5.26 or above was not found"
has_or_abort prog=stat
has_or_abort prog="curl wget"
has_or_abort prog=patch
has_or_abort prog=tar
has_or_abort prog=gzip
has_or_abort prog=bzip2
has_or_abort prog=lzma
has_or_abort prog=readlink
has_or_abort prog=objcopy var=objcopy
has_or_abort prog=objdump var=objdump
has_or_abort prog=readelf var=readelf
has_or_abort prog=patch var=patch
has_or_warn  prog=cvs                                                   \
             kconfig=has_cvs                                            \
             err="it will not be possible to use newlib cvs snapshots"
has_or_abort prog=svn                               \
             kconfig=has_svn                        \
             err="subversion is required to download eglibc"

# Host system checks

printf "Checking for host system... "
host="$( uname -s )"
printf "%s\n" "${host}"
case "${host}" in
    Linux)  ;;
    Cygwin) ;;
    *)
        printf " * Runing under %s is not fully tested\n" "${host}"
        printf " * You may encounter some weird behavior\n"
        ;;
esac

printf "Checking if static linking is possible... "
static_link_ok=""
case "${host}" in
    Darwin) ;;
    *)  tmp=.static.tmp
        if gcc -xc - -static -o "${tmp}" >/dev/null 2>&1 <<-_EOF_
				int main() { return 0; }
			_EOF_
        then
            static_link_ok="y"
        fi
        rm -f "${tmp}"
        ;;
esac
if [ "${static_link_ok}" = "y" ]; then
    static_link_ko=""
    printf "yes\n"
else
    static_link_ko="y"
    printf "no\n"
    printf " * An optional host feature is missing, some features will be disabled:\n"
    printf " * - It will not be possible to statically link toolchain's binaries\n"
fi
add_to_kconfig_list static_link_ok

# Library checks
libs_exts="so dylib"
if [ "${static_link_ok}" = "y" ]; then
    libs_exts="${libs_exts} a"
fi

ncurses_hdrs="ncurses/ncurses.h ncurses/curses.h ncurses.h curses.h"
ncurses_libs="libncursesw libncurses libcurses"
has_or_abort lib="${ncurses_libs}"                                          \
             lib_exts="${libs_exts}"                                        \
             inc="${ncurses_hdrs}"                                          \
             err="The 'ncurses' library is needed fo the menuconfig frontend"

has_or_abort lib="libstdc++"            \
             lib_exts="${libs_exts}"    \
             err="The 'libstdc++' library is needed to build gcc"

# Yes, we may be checking twice for libstdc++.a
# The first is because we need one instance of libstdc++ (shared or static)
# because it is needed for PPL; the second is because the static version is
# required for static-linking, and if missing, the option is removed.
has_or_warn  lib="libstdc++"                \
             lib_exts="a"                   \
             err="static 'libstdc++' is needed to statically link the toolchain's executables" \
             kconfig=has_static_libstdcxx   \
             skip="${static_link_ko}"

has_or_warn  inc="expat.h"              \
             lib="libexpat"             \
             lib_exts="${libs_exts}"    \
             err="The 'expat' header file and library are needed to link cross-gdb's executables" \
             kconfig=has_expat

# Yes, we may be checking twice for libexpat.a
# The first is because we need one instance of libexpat (shared or static)
# because it is needed for cross-gdb; the second is because the static version
# is required for static-linking, and if missing, the option is removed.
has_or_warn  lib="libexpat"             \
             lib_exts="a"               \
             err="static 'expat' is needed to statically link cross-gdb's executables" \
             kconfig=has_static_expat   \
             skip="${static_link_ko}"

for v in 7 6 5 4; do
    python_incs="${python_incs} python2.${v}/Python.h"
    python_libs="${python_libs} libpython2.${v}"
done
has_or_warn  inc="${python_incs}"       \
             lib="${python_libs}"       \
             lib_exts="${libs_exts}"    \
             err="The 'python' header file and library are needed for some features of cross-gdb"

#---------------------------------------------------------------------
# Compute the version string

# If this version is n hg clone, try to get the revision number
# If we can't get the revision number, use date
printf "\nComputing version string... "
case "${VERSION}" in
    *+hg|hg)
        REVISION="$( hg id -n 2>/dev/null || true )"
        case "${REVISION}" in
            "")
                VERSION="${VERSION}_unknown@$( date +%Y%m%d.%H%M%S )";;
            *)
                VERSION="${VERSION}_$( hg id -b )@${REVISION%%+}_$( hg id -i )"
                ;;
        esac
        # Arrange to have no / in the directory name, no need to create an
        # arbitrarily deep directory structure
        VERSION="$( printf "${VERSION}\n" |"${sed}" -r -e 's|/+|_|g;' )"
        ;;
esac
printf "${VERSION}\n"

#---------------------------------------------------------------------
# Compute and check install paths

# Now we have the version string, we can build up the paths
[ -z "${BINDIR_set}" ] && BINDIR="${PREFIX}/bin"
[ -z "${LIBDIR_set}" ] && LIBDIR="${PREFIX}/lib"
[ -z "${DOCDIR_set}" ] && DOCDIR="${PREFIX}/share/doc"
[ -z "${MANDIR_set}" ] && MANDIR="${PREFIX}/share/man"

# Install support files in our own sub-dir, so as not to mangle (system)
# files and dirs, but only if not --local
if [ -z "${LOCAL_set}" ]; then
    LIBDIR="${LIBDIR}/ct-ng-${VERSION}"
    DOCDIR="${DOCDIR}/ct-ng-${VERSION}"
fi

# Check that install PATHs are absolute
for p in BIN LIB DOC MAN; do
    var="${p}DIR"
    eval v='"${'"${var}"'}"'
    case "${v}" in
        /*) ;;
        *)  do_error "'${var}' is not an absolute path: '${v}'"
    esac
done

#---------------------------------------------------------------------
# That's all, folks!

printf "Building up Makefile... "
var_sed="$( for var_name in ${var_list}; do
                eval echo 's,@@${var_name}@@,${'"${var_name}"'},g'
            done
          )"
kconfig_sed="s/@@KCONFIG@@/$( for k_name in ${kconfig_list}; do
                                  eval printf \"${k_name}=\${${k_name}} \"
                              done
                            )/"
"${sed}" -r -e "s,@@BINDIR@@,${BINDIR},g"       \
            -e "s,@@LIBDIR@@,${LIBDIR},g"       \
            -e "s,@@DOCDIR@@,${DOCDIR},g"       \
            -e "s,@@MANDIR@@,${MANDIR},g"       \
            -e "s,@@VERSION@@,${VERSION},g"     \
            -e "s,@@DATE@@,${DATE},g"           \
            -e "s,@@LOCAL@@,${LOCAL_set},g"     \
            -e "${var_sed}"                     \
            -e "${kconfig_sed}"                 \
         Makefile.in                            \
         >Makefile
echo "done"

cat <<__EOF__

crosstool-NG configured as follows:
  PREFIX='${PREFIX}'
  BINDIR='${BINDIR}'
  LIBDIR='${LIBDIR}'
  DOCDIR='${DOCDIR}'
  MANDIR='${MANDIR}'

Now run:
  make
__EOF__
if [ "${LOCAL_set}" != "y" ]; then
    printf "  make install\n"
fi