1#!/bin/sh
2##
3##  configure.sh
4##
5##  This script is sourced by the main configure script and contains
6##  utility functions and other common bits that aren't strictly libvpx
7##  related.
8##
9##  This build system is based in part on the FFmpeg configure script.
10##
11
12
13#
14# Logging / Output Functions
15#
16die_unknown(){
17    echo "Unknown option \"$1\"."
18    echo "See $0 --help for available options."
19    clean_temp_files
20    exit 1
21}
22
23
24die() {
25    echo "$@"
26    echo
27    echo "Configuration failed. This could reflect a misconfiguration of your"
28    echo "toolchains, improper options selected, or another problem. If you"
29    echo "don't see any useful error messages above, the next step is to look"
30    echo "at the configure error log file ($logfile) to determine what"
31    echo "configure was trying to do when it died."
32    clean_temp_files
33    exit 1
34}
35
36
37log(){
38    echo "$@" >>$logfile
39}
40
41
42log_file(){
43    log BEGIN $1
44    cat -n $1 >>$logfile
45    log END $1
46}
47
48
49log_echo() {
50    echo "$@"
51    log "$@"
52}
53
54
55fwrite () {
56    outfile=$1
57    shift
58    echo "$@" >> ${outfile}
59}
60
61
62show_help_pre(){
63    for opt in ${CMDLINE_SELECT}; do
64        opt2=`echo $opt | sed -e 's;_;-;g'`
65        if enabled $opt; then
66            eval "toggle_${opt}=\"--disable-${opt2}\""
67        else
68            eval "toggle_${opt}=\"--enable-${opt2} \""
69        fi
70    done
71
72    cat <<EOF
73Usage: configure [options]
74Options:
75
76Build options:
77  --help                      print this message
78  --log=yes|no|FILE           file configure log is written to [config.log]
79  --target=TARGET             target platform tuple [generic-gnu]
80  --cpu=CPU                   optimize for a specific cpu rather than a family
81  --extra-cflags=ECFLAGS      add ECFLAGS to CFLAGS [$CFLAGS]
82  ${toggle_extra_warnings}    emit harmless warnings (always non-fatal)
83  ${toggle_werror}            treat warnings as errors, if possible
84                              (not available with all compilers)
85  ${toggle_optimizations}     turn on/off compiler optimization flags
86  ${toggle_pic}               turn on/off Position Independent Code
87  ${toggle_ccache}            turn on/off compiler cache
88  ${toggle_debug}             enable/disable debug mode
89  ${toggle_gprof}             enable/disable gprof profiling instrumentation
90  ${toggle_gcov}              enable/disable gcov coverage instrumentation
91  ${toggle_thumb}             enable/disable building arm assembly in thumb mode
92
93Install options:
94  ${toggle_install_docs}      control whether docs are installed
95  ${toggle_install_bins}      control whether binaries are installed
96  ${toggle_install_libs}      control whether libraries are installed
97  ${toggle_install_srcs}      control whether sources are installed
98
99
100EOF
101}
102
103
104show_help_post(){
105    cat <<EOF
106
107
108NOTES:
109    Object files are built at the place where configure is launched.
110
111    All boolean options can be negated. The default value is the opposite
112    of that shown above. If the option --disable-foo is listed, then
113    the default value for foo is enabled.
114
115Supported targets:
116EOF
117  show_targets ${all_platforms}
118  echo
119  exit 1
120}
121
122
123show_targets() {
124    while [ -n "$*" ]; do
125        if [ "${1%%-*}" = "${2%%-*}" ]; then
126            if [ "${2%%-*}" = "${3%%-*}" ]; then
127                printf "    %-24s %-24s %-24s\n" "$1" "$2" "$3"
128                shift; shift; shift
129            else
130                printf "    %-24s %-24s\n" "$1" "$2"
131                shift; shift
132            fi
133        else
134            printf "    %-24s\n" "$1"
135            shift
136        fi
137    done
138}
139
140
141show_help() {
142    show_help_pre
143    show_help_post
144}
145
146#
147# List Processing Functions
148#
149set_all(){
150    value=$1
151    shift
152    for var in $*; do
153        eval $var=$value
154    done
155}
156
157
158is_in(){
159    value=$1
160    shift
161    for var in $*; do
162        [ $var = $value ] && return 0
163    done
164    return 1
165}
166
167
168add_cflags() {
169    CFLAGS="${CFLAGS} $@"
170    CXXFLAGS="${CXXFLAGS} $@"
171}
172
173
174add_cflags_only() {
175    CFLAGS="${CFLAGS} $@"
176}
177
178
179add_cxxflags_only() {
180    CXXFLAGS="${CXXFLAGS} $@"
181}
182
183
184add_ldflags() {
185    LDFLAGS="${LDFLAGS} $@"
186}
187
188
189add_asflags() {
190    ASFLAGS="${ASFLAGS} $@"
191}
192
193
194add_extralibs() {
195    extralibs="${extralibs} $@"
196}
197
198#
199# Boolean Manipulation Functions
200#
201enable_feature(){
202    set_all yes $*
203}
204
205disable_feature(){
206    set_all no $*
207}
208
209enabled(){
210    eval test "x\$$1" = "xyes"
211}
212
213disabled(){
214    eval test "x\$$1" = "xno"
215}
216
217
218soft_enable() {
219    for var in $*; do
220        if ! disabled $var; then
221            log_echo "  enabling $var"
222            enable_feature $var
223        fi
224    done
225}
226
227soft_disable() {
228    for var in $*; do
229        if ! enabled $var; then
230            log_echo "  disabling $var"
231            disable_feature $var
232        fi
233    done
234}
235
236
237#
238# Text Processing Functions
239#
240toupper(){
241    echo "$@" | tr abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ
242}
243
244
245tolower(){
246    echo "$@" | tr ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz
247}
248
249
250#
251# Temporary File Functions
252#
253source_path=${0%/*}
254enable_feature source_path_used
255if [ -z "$source_path" ] || [ "$source_path" = "." ]; then
256    source_path="`pwd`"
257    disable_feature source_path_used
258fi
259
260if test ! -z "$TMPDIR" ; then
261    TMPDIRx="${TMPDIR}"
262elif test ! -z "$TEMPDIR" ; then
263    TMPDIRx="${TEMPDIR}"
264else
265    TMPDIRx="/tmp"
266fi
267RAND=$(awk 'BEGIN { srand(); printf "%d\n",(rand() * 32768)}')
268TMP_H="${TMPDIRx}/vpx-conf-$$-${RAND}.h"
269TMP_C="${TMPDIRx}/vpx-conf-$$-${RAND}.c"
270TMP_CC="${TMPDIRx}/vpx-conf-$$-${RAND}.cc"
271TMP_O="${TMPDIRx}/vpx-conf-$$-${RAND}.o"
272TMP_X="${TMPDIRx}/vpx-conf-$$-${RAND}.x"
273TMP_ASM="${TMPDIRx}/vpx-conf-$$-${RAND}.asm"
274
275clean_temp_files() {
276    rm -f ${TMP_C} ${TMP_CC} ${TMP_H} ${TMP_O} ${TMP_X} ${TMP_ASM}
277    enabled gcov && rm -f ${TMP_C%.c}.gcno ${TMP_CC%.cc}.gcno
278}
279
280#
281# Toolchain Check Functions
282#
283check_cmd() {
284    enabled external_build && return
285    log "$@"
286    "$@" >>${logfile} 2>&1
287}
288
289check_cc() {
290    log check_cc "$@"
291    cat >${TMP_C}
292    log_file ${TMP_C}
293    check_cmd ${CC} ${CFLAGS} "$@" -c -o ${TMP_O} ${TMP_C}
294}
295
296check_cxx() {
297    log check_cxx "$@"
298    cat >${TMP_CC}
299    log_file ${TMP_CC}
300    check_cmd ${CXX} ${CXXFLAGS} "$@" -c -o ${TMP_O} ${TMP_CC}
301}
302
303check_cpp() {
304    log check_cpp "$@"
305    cat > ${TMP_C}
306    log_file ${TMP_C}
307    check_cmd ${CC} ${CFLAGS} "$@" -E -o ${TMP_O} ${TMP_C}
308}
309
310check_ld() {
311    log check_ld "$@"
312    check_cc $@ \
313        && check_cmd ${LD} ${LDFLAGS} "$@" -o ${TMP_X} ${TMP_O} ${extralibs}
314}
315
316check_header(){
317    log check_header "$@"
318    header=$1
319    shift
320    var=`echo $header | sed 's/[^A-Za-z0-9_]/_/g'`
321    disable_feature $var
322    check_cpp "$@" <<EOF && enable_feature $var
323#include "$header"
324int x;
325EOF
326}
327
328
329check_cflags() {
330    log check_cflags "$@"
331    check_cc -Werror "$@" <<EOF
332int x;
333EOF
334}
335
336check_cxxflags() {
337    log check_cxxflags "$@"
338
339    # Catch CFLAGS that trigger CXX warnings
340    case "$CXX" in
341      *c++-analyzer|*clang++|*g++*) check_cxx -Werror "$@" <<EOF
342int x;
343EOF
344      ;;
345      *) check_cxx -Werror "$@" <<EOF
346int x;
347EOF
348      ;;
349    esac
350}
351
352check_add_cflags() {
353    check_cxxflags "$@" && add_cxxflags_only "$@"
354    check_cflags "$@" && add_cflags_only "$@"
355}
356
357check_add_asflags() {
358    log add_asflags "$@"
359    add_asflags "$@"
360}
361
362check_add_ldflags() {
363    log add_ldflags "$@"
364    add_ldflags "$@"
365}
366
367check_asm_align() {
368    log check_asm_align "$@"
369    cat >${TMP_ASM} <<EOF
370section .rodata
371align 16
372EOF
373    log_file ${TMP_ASM}
374    check_cmd ${AS} ${ASFLAGS} -o ${TMP_O} ${TMP_ASM}
375    readelf -WS ${TMP_O} >${TMP_X}
376    log_file ${TMP_X}
377    if ! grep -q '\.rodata .* 16$' ${TMP_X}; then
378        die "${AS} ${ASFLAGS} does not support section alignment (nasm <=2.08?)"
379    fi
380}
381
382# tests for -m$1 toggling the feature given in $2. If $2 is empty $1 is used.
383check_gcc_machine_option() {
384    opt="$1"
385    feature="$2"
386    [ -n "$feature" ] || feature="$opt"
387
388    if enabled gcc && ! disabled "$feature" && ! check_cflags "-m$opt"; then
389        RTCD_OPTIONS="${RTCD_OPTIONS}--disable-$feature "
390    else
391        soft_enable "$feature"
392    fi
393}
394
395write_common_config_banner() {
396    print_webm_license config.mk "##" ""
397    echo '# This file automatically generated by configure. Do not edit!' >> config.mk
398    echo "TOOLCHAIN := ${toolchain}" >> config.mk
399
400    case ${toolchain} in
401        *-linux-rvct)
402            echo "ALT_LIBC := ${alt_libc}" >> config.mk
403            ;;
404    esac
405}
406
407write_common_config_targets() {
408    for t in ${all_targets}; do
409        if enabled ${t}; then
410            if enabled universal || enabled child; then
411                fwrite config.mk "ALL_TARGETS += ${t}-${toolchain}"
412            else
413                fwrite config.mk "ALL_TARGETS += ${t}"
414            fi
415        fi
416    true;
417    done
418true
419}
420
421write_common_target_config_mk() {
422    saved_CC="${CC}"
423    saved_CXX="${CXX}"
424    enabled ccache && CC="ccache ${CC}"
425    enabled ccache && CXX="ccache ${CXX}"
426    print_webm_license $1 "##" ""
427
428    cat >> $1 << EOF
429# This file automatically generated by configure. Do not edit!
430SRC_PATH="$source_path"
431SRC_PATH_BARE=$source_path
432BUILD_PFX=${BUILD_PFX}
433TOOLCHAIN=${toolchain}
434ASM_CONVERSION=${asm_conversion_cmd:-${source_path}/build/make/ads2gas.pl}
435GEN_VCPROJ=${gen_vcproj_cmd}
436MSVS_ARCH_DIR=${msvs_arch_dir}
437
438CC=${CC}
439CXX=${CXX}
440AR=${AR}
441LD=${LD}
442AS=${AS}
443STRIP=${STRIP}
444NM=${NM}
445
446CFLAGS  = ${CFLAGS}
447CXXFLAGS  = ${CXXFLAGS}
448ARFLAGS = -rus\$(if \$(quiet),c,v)
449LDFLAGS = ${LDFLAGS}
450ASFLAGS = ${ASFLAGS}
451extralibs = ${extralibs}
452AS_SFX    = ${AS_SFX:-.asm}
453EXE_SFX   = ${EXE_SFX}
454VCPROJ_SFX = ${VCPROJ_SFX}
455RTCD_OPTIONS = ${RTCD_OPTIONS}
456EOF
457
458    if enabled rvct; then cat >> $1 << EOF
459fmt_deps = sed -e 's;^__image.axf;\${@:.d=.o} \$@;' #hide
460EOF
461    else cat >> $1 << EOF
462fmt_deps = sed -e 's;^\([a-zA-Z0-9_]*\)\.o;\${@:.d=.o} \$@;'
463EOF
464    fi
465
466    print_config_mk ARCH   "${1}" ${ARCH_LIST}
467    print_config_mk HAVE   "${1}" ${HAVE_LIST}
468    print_config_mk CONFIG "${1}" ${CONFIG_LIST}
469    print_config_mk HAVE   "${1}" gnu_strip
470
471    enabled msvs && echo "CONFIG_VS_VERSION=${vs_version}" >> "${1}"
472
473    CC="${saved_CC}"
474    CXX="${saved_CXX}"
475}
476
477
478write_common_target_config_h() {
479    print_webm_license ${TMP_H} "/*" " */"
480    cat >> ${TMP_H} << EOF
481/* This file automatically generated by configure. Do not edit! */
482#ifndef VPX_CONFIG_H
483#define VPX_CONFIG_H
484#define RESTRICT    ${RESTRICT}
485#define INLINE      ${INLINE}
486EOF
487    print_config_h ARCH   "${TMP_H}" ${ARCH_LIST}
488    print_config_h HAVE   "${TMP_H}" ${HAVE_LIST}
489    print_config_h CONFIG "${TMP_H}" ${CONFIG_LIST}
490    print_config_vars_h   "${TMP_H}" ${VAR_LIST}
491    echo "#endif /* VPX_CONFIG_H */" >> ${TMP_H}
492    mkdir -p `dirname "$1"`
493    cmp "$1" ${TMP_H} >/dev/null 2>&1 || mv ${TMP_H} "$1"
494}
495
496process_common_cmdline() {
497    for opt in "$@"; do
498        optval="${opt#*=}"
499        case "$opt" in
500        --child) enable_feature child
501        ;;
502        --log*)
503        logging="$optval"
504        if ! disabled logging ; then
505            enabled logging || logfile="$logging"
506        else
507            logfile=/dev/null
508        fi
509        ;;
510        --target=*) toolchain="${toolchain:-${optval}}"
511        ;;
512        --force-target=*) toolchain="${toolchain:-${optval}}"; enable_feature force_toolchain
513        ;;
514        --cpu)
515        ;;
516        --cpu=*) tune_cpu="$optval"
517        ;;
518        --extra-cflags=*)
519        extra_cflags="${optval}"
520        ;;
521        --enable-?*|--disable-?*)
522        eval `echo "$opt" | sed 's/--/action=/;s/-/ option=/;s/-/_/g'`
523        if echo "${ARCH_EXT_LIST}" | grep "^ *$option\$" >/dev/null; then
524            [ $action = "disable" ] && RTCD_OPTIONS="${RTCD_OPTIONS}--disable-${option} "
525        elif [ $action = "disable" ] && ! disabled $option ; then
526          echo "${CMDLINE_SELECT}" | grep "^ *$option\$" >/dev/null ||
527            die_unknown $opt
528        elif [ $action = "enable" ] && ! enabled $option ; then
529          echo "${CMDLINE_SELECT}" | grep "^ *$option\$" >/dev/null ||
530            die_unknown $opt
531        fi
532        ${action}_feature $option
533        ;;
534        --require-?*)
535        eval `echo "$opt" | sed 's/--/action=/;s/-/ option=/;s/-/_/g'`
536        if echo "${ARCH_EXT_LIST}" none | grep "^ *$option\$" >/dev/null; then
537            RTCD_OPTIONS="${RTCD_OPTIONS}${opt} "
538        else
539            die_unknown $opt
540        fi
541        ;;
542        --force-enable-?*|--force-disable-?*)
543        eval `echo "$opt" | sed 's/--force-/action=/;s/-/ option=/;s/-/_/g'`
544        ${action}_feature $option
545        ;;
546        --libc=*)
547        [ -d "${optval}" ] || die "Not a directory: ${optval}"
548        disable_feature builtin_libc
549        alt_libc="${optval}"
550        ;;
551        --as=*)
552        [ "${optval}" = yasm ] || [ "${optval}" = nasm ] \
553            || [ "${optval}" = auto ] \
554            || die "Must be yasm, nasm or auto: ${optval}"
555        alt_as="${optval}"
556        ;;
557        --size-limit=*)
558        w="${optval%%x*}"
559        h="${optval##*x}"
560        VAR_LIST="DECODE_WIDTH_LIMIT ${w} DECODE_HEIGHT_LIMIT ${h}"
561        [ ${w} -gt 0 ] && [ ${h} -gt 0 ] || die "Invalid size-limit: too small."
562        [ ${w} -lt 65536 ] && [ ${h} -lt 65536 ] \
563            || die "Invalid size-limit: too big."
564        enable_feature size_limit
565        ;;
566        --prefix=*)
567        prefix="${optval}"
568        ;;
569        --libdir=*)
570        libdir="${optval}"
571        ;;
572        --sdk-path=*)
573        [ -d "${optval}" ] || die "Not a directory: ${optval}"
574        sdk_path="${optval}"
575        ;;
576        --libc|--as|--prefix|--libdir|--sdk-path)
577        die "Option ${opt} requires argument"
578        ;;
579        --help|-h) show_help
580        ;;
581        *) die_unknown $opt
582        ;;
583        esac
584    done
585}
586
587process_cmdline() {
588    for opt do
589        optval="${opt#*=}"
590        case "$opt" in
591        *) process_common_cmdline $opt
592        ;;
593        esac
594    done
595}
596
597
598post_process_common_cmdline() {
599    prefix="${prefix:-/usr/local}"
600    prefix="${prefix%/}"
601    libdir="${libdir:-${prefix}/lib}"
602    libdir="${libdir%/}"
603    if [ "${libdir#${prefix}}" = "${libdir}" ]; then
604        die "Libdir ${libdir} must be a subdirectory of ${prefix}"
605    fi
606}
607
608
609post_process_cmdline() {
610    true;
611}
612
613setup_gnu_toolchain() {
614        CC=${CC:-${CROSS}gcc}
615        CXX=${CXX:-${CROSS}g++}
616        AR=${AR:-${CROSS}ar}
617        LD=${LD:-${CROSS}${link_with_cc:-ld}}
618        AS=${AS:-${CROSS}as}
619    STRIP=${STRIP:-${CROSS}strip}
620    NM=${NM:-${CROSS}nm}
621        AS_SFX=.s
622        EXE_SFX=
623}
624
625process_common_toolchain() {
626    if [ -z "$toolchain" ]; then
627        gcctarget="${CHOST:-$(gcc -dumpmachine 2> /dev/null)}"
628
629        # detect tgt_isa
630        case "$gcctarget" in
631            armv6*)
632                tgt_isa=armv6
633                ;;
634            armv7*-hardfloat*)
635                tgt_isa=armv7
636                float_abi=hard
637                ;;
638            armv7*)
639                tgt_isa=armv7
640                float_abi=softfp
641                ;;
642            armv5te*)
643                tgt_isa=armv5te
644                ;;
645            *x86_64*|*amd64*)
646                tgt_isa=x86_64
647                ;;
648            *i[3456]86*)
649                tgt_isa=x86
650                ;;
651            *powerpc64*)
652                tgt_isa=ppc64
653                ;;
654            *powerpc*)
655                tgt_isa=ppc32
656                ;;
657            *sparc*)
658                tgt_isa=sparc
659                ;;
660        esac
661
662        # detect tgt_os
663        case "$gcctarget" in
664            *darwin8*)
665                tgt_isa=universal
666                tgt_os=darwin8
667                ;;
668            *darwin9*)
669                tgt_isa=universal
670                tgt_os=darwin9
671                ;;
672            *darwin10*)
673                tgt_isa=x86_64
674                tgt_os=darwin10
675                ;;
676            *darwin11*)
677                tgt_isa=x86_64
678                tgt_os=darwin11
679                ;;
680            *darwin12*)
681                tgt_isa=x86_64
682                tgt_os=darwin12
683                ;;
684            *darwin13*)
685                tgt_isa=x86_64
686                tgt_os=darwin13
687                ;;
688            x86_64*mingw32*)
689                tgt_os=win64
690                ;;
691            *mingw32*|*cygwin*)
692                [ -z "$tgt_isa" ] && tgt_isa=x86
693                tgt_os=win32
694                ;;
695            *linux*|*bsd*)
696                tgt_os=linux
697                ;;
698            *solaris2.10)
699                tgt_os=solaris
700                ;;
701            *os2*)
702                tgt_os=os2
703                ;;
704        esac
705
706        if [ -n "$tgt_isa" ] && [ -n "$tgt_os" ]; then
707            toolchain=${tgt_isa}-${tgt_os}-gcc
708        fi
709    fi
710
711    toolchain=${toolchain:-generic-gnu}
712
713    is_in ${toolchain} ${all_platforms} || enabled force_toolchain \
714        || die "Unrecognized toolchain '${toolchain}'"
715
716    enabled child || log_echo "Configuring for target '${toolchain}'"
717
718    #
719    # Set up toolchain variables
720    #
721    tgt_isa=$(echo ${toolchain} | awk 'BEGIN{FS="-"}{print $1}')
722    tgt_os=$(echo ${toolchain} | awk 'BEGIN{FS="-"}{print $2}')
723    tgt_cc=$(echo ${toolchain} | awk 'BEGIN{FS="-"}{print $3}')
724
725    # Mark the specific ISA requested as enabled
726    soft_enable ${tgt_isa}
727    enable_feature ${tgt_os}
728    enable_feature ${tgt_cc}
729
730    # Enable the architecture family
731    case ${tgt_isa} in
732        arm*) enable_feature arm;;
733        mips*) enable_feature mips;;
734    esac
735
736    # PIC is probably what we want when building shared libs
737    enabled shared && soft_enable pic
738
739    # Handle darwin variants. Newer SDKs allow targeting older
740    # platforms, so find the newest SDK available.
741    case ${toolchain} in
742        *-darwin*)
743            if [ -z "${DEVELOPER_DIR}" ]; then
744                DEVELOPER_DIR=`xcode-select -print-path 2> /dev/null`
745                [ $? -ne 0 ] && OSX_SKIP_DIR_CHECK=1
746            fi
747            if [ -z "${OSX_SKIP_DIR_CHECK}" ]; then
748                OSX_SDK_ROOTS="${DEVELOPER_DIR}/SDKs"
749                OSX_SDK_VERSIONS="MacOSX10.4u.sdk MacOSX10.5.sdk MacOSX10.6.sdk"
750                OSX_SDK_VERSIONS="${OSX_SDK_VERSIONS} MacOSX10.7.sdk"
751                for v in ${OSX_SDK_VERSIONS}; do
752                    if [ -d "${OSX_SDK_ROOTS}/${v}" ]; then
753                        osx_sdk_dir="${OSX_SDK_ROOTS}/${v}"
754                    fi
755                done
756            fi
757            ;;
758    esac
759
760    if [ -d "${osx_sdk_dir}" ]; then
761        add_cflags  "-isysroot ${osx_sdk_dir}"
762        add_ldflags "-isysroot ${osx_sdk_dir}"
763    fi
764
765    case ${toolchain} in
766        *-darwin8-*)
767            add_cflags  "-mmacosx-version-min=10.4"
768            add_ldflags "-mmacosx-version-min=10.4"
769            ;;
770        *-darwin9-*)
771            add_cflags  "-mmacosx-version-min=10.5"
772            add_ldflags "-mmacosx-version-min=10.5"
773            ;;
774        *-darwin10-*)
775            add_cflags  "-mmacosx-version-min=10.6"
776            add_ldflags "-mmacosx-version-min=10.6"
777            ;;
778        *-darwin11-*)
779            add_cflags  "-mmacosx-version-min=10.7"
780            add_ldflags "-mmacosx-version-min=10.7"
781            ;;
782        *-darwin12-*)
783            add_cflags  "-mmacosx-version-min=10.8"
784            add_ldflags "-mmacosx-version-min=10.8"
785            ;;
786        *-darwin13-*)
787            add_cflags  "-mmacosx-version-min=10.9"
788            add_ldflags "-mmacosx-version-min=10.9"
789            ;;
790        *-iphonesimulator-*)
791            add_cflags  "-miphoneos-version-min=5.0"
792            add_ldflags "-miphoneos-version-min=5.0"
793            osx_sdk_dir="$(xcrun --sdk iphonesimulator --show-sdk-path)"
794            add_cflags  "-isysroot ${osx_sdk_dir}"
795            add_ldflags "-isysroot ${osx_sdk_dir}"
796            ;;
797    esac
798
799    # Handle Solaris variants. Solaris 10 needs -lposix4
800    case ${toolchain} in
801        sparc-solaris-*)
802            add_extralibs -lposix4
803            disable_feature fast_unaligned
804            ;;
805        *-solaris-*)
806            add_extralibs -lposix4
807            ;;
808    esac
809
810    # Process ARM architecture variants
811    case ${toolchain} in
812    arm*)
813        # on arm, isa versions are supersets
814        case ${tgt_isa} in
815        arm64|armv8)
816            soft_enable neon
817            ;;
818        armv7|armv7s)
819            soft_enable neon
820            soft_enable neon_asm
821            soft_enable media
822            soft_enable edsp
823            soft_enable fast_unaligned
824            ;;
825        armv6)
826            soft_enable media
827            soft_enable edsp
828            soft_enable fast_unaligned
829            ;;
830        armv5te)
831            soft_enable edsp
832            disable_feature fast_unaligned
833            ;;
834        esac
835
836        asm_conversion_cmd="cat"
837
838        case ${tgt_cc} in
839        gcc)
840            CROSS=${CROSS:-arm-none-linux-gnueabi-}
841            link_with_cc=gcc
842            setup_gnu_toolchain
843            arch_int=${tgt_isa##armv}
844            arch_int=${arch_int%%te}
845            check_add_asflags --defsym ARCHITECTURE=${arch_int}
846            tune_cflags="-mtune="
847            if [ ${tgt_isa} = "armv7" ] || [ ${tgt_isa} = "armv7s" ]; then
848                if [ -z "${float_abi}" ]; then
849                    check_cpp <<EOF && float_abi=hard || float_abi=softfp
850#ifndef __ARM_PCS_VFP
851#error "not hardfp"
852#endif
853EOF
854                fi
855                check_add_cflags  -march=armv7-a -mfloat-abi=${float_abi}
856                check_add_asflags -march=armv7-a -mfloat-abi=${float_abi}
857
858                if enabled neon || enabled neon_asm
859                then
860                    check_add_cflags -mfpu=neon #-ftree-vectorize
861                    check_add_asflags -mfpu=neon
862                fi
863
864                if [ -z "${tune_cpu}" ]; then
865                    tune_cpu=cortex-a8
866                fi
867            else
868                check_add_cflags -march=${tgt_isa}
869                check_add_asflags -march=${tgt_isa}
870            fi
871
872            enabled debug && add_asflags -g
873            asm_conversion_cmd="${source_path}/build/make/ads2gas.pl"
874            if enabled thumb; then
875                asm_conversion_cmd="$asm_conversion_cmd -thumb"
876                check_add_cflags -mthumb
877                check_add_asflags -mthumb -mimplicit-it=always
878            fi
879            ;;
880        vs*)
881            asm_conversion_cmd="${source_path}/build/make/ads2armasm_ms.pl"
882            AS_SFX=.s
883            msvs_arch_dir=arm-msvs
884            disable_feature multithread
885            disable_feature unit_tests
886            vs_version=${tgt_cc##vs}
887            if [ $vs_version -ge 12 ]; then
888                # MSVC 2013 doesn't allow doing plain .exe projects for ARM,
889                # only "AppContainerApplication" which requires an AppxManifest.
890                # Therefore disable the examples, just build the library.
891                disable_feature examples
892            fi
893            ;;
894        rvct)
895            CC=armcc
896            AR=armar
897            AS=armasm
898            LD="${source_path}/build/make/armlink_adapter.sh"
899            STRIP=arm-none-linux-gnueabi-strip
900            NM=arm-none-linux-gnueabi-nm
901            tune_cflags="--cpu="
902            tune_asflags="--cpu="
903            if [ -z "${tune_cpu}" ]; then
904                if [ ${tgt_isa} = "armv7" ]; then
905                    if enabled neon || enabled neon_asm
906                    then
907                        check_add_cflags --fpu=softvfp+vfpv3
908                        check_add_asflags --fpu=softvfp+vfpv3
909                    fi
910                    check_add_cflags --cpu=Cortex-A8
911                    check_add_asflags --cpu=Cortex-A8
912                else
913                    check_add_cflags --cpu=${tgt_isa##armv}
914                    check_add_asflags --cpu=${tgt_isa##armv}
915                fi
916            fi
917            arch_int=${tgt_isa##armv}
918            arch_int=${arch_int%%te}
919            check_add_asflags --pd "\"ARCHITECTURE SETA ${arch_int}\""
920            enabled debug && add_asflags -g
921            add_cflags --gnu
922            add_cflags --enum_is_int
923            add_cflags --wchar32
924        ;;
925        esac
926
927        case ${tgt_os} in
928        none*)
929            disable_feature multithread
930            disable_feature os_support
931            ;;
932
933        android*)
934            SDK_PATH=${sdk_path}
935            COMPILER_LOCATION=`find "${SDK_PATH}" \
936                               -name "arm-linux-androideabi-gcc*" -print -quit`
937            TOOLCHAIN_PATH=${COMPILER_LOCATION%/*}/arm-linux-androideabi-
938            CC=${TOOLCHAIN_PATH}gcc
939            CXX=${TOOLCHAIN_PATH}g++
940            AR=${TOOLCHAIN_PATH}ar
941            LD=${TOOLCHAIN_PATH}gcc
942            AS=${TOOLCHAIN_PATH}as
943            STRIP=${TOOLCHAIN_PATH}strip
944            NM=${TOOLCHAIN_PATH}nm
945
946            if [ -z "${alt_libc}" ]; then
947                alt_libc=`find "${SDK_PATH}" -name arch-arm -print | \
948                          awk '{n = split($0,a,"/"); \
949                                split(a[n-1],b,"-"); \
950                                print $0 " " b[2]}' | \
951                          sort -g -k 2 | \
952                          awk '{ print $1 }' | tail -1`
953            fi
954
955            add_cflags "--sysroot=${alt_libc}"
956            add_ldflags "--sysroot=${alt_libc}"
957
958            # linker flag that routes around a CPU bug in some
959            # Cortex-A8 implementations (NDK Dev Guide)
960            add_ldflags "-Wl,--fix-cortex-a8"
961
962            enable_feature pic
963            soft_enable realtime_only
964            if [ ${tgt_isa} = "armv7" ]; then
965                soft_enable runtime_cpu_detect
966            fi
967            if enabled runtime_cpu_detect; then
968                add_cflags "-I${SDK_PATH}/sources/android/cpufeatures"
969            fi
970          ;;
971
972        darwin*)
973
974            XCRUN_FIND="xcrun --sdk iphoneos -find"
975            CXX="$(${XCRUN_FIND} clang++)"
976            CC="$(${XCRUN_FIND} clang)"
977            AR="$(${XCRUN_FIND} ar)"
978            LD="$(${XCRUN_FIND} ld)"
979            AS="$(${XCRUN_FIND} as)"
980            STRIP="$(${XCRUN_FIND} strip)"
981            NM="$(${XCRUN_FIND} nm)"
982            RANLIB="$(${XCRUN_FIND} ranlib)"
983            AS_SFX=.s
984
985            # ASFLAGS is written here instead of using check_add_asflags
986            # because we need to overwrite all of ASFLAGS and purge the
987            # options that were put in above
988            ASFLAGS="-arch ${tgt_isa} -g"
989
990            alt_libc="$(xcrun --sdk iphoneos --show-sdk-path)"
991            add_cflags -arch ${tgt_isa} -isysroot ${alt_libc}
992            add_ldflags -arch ${tgt_isa} -ios_version_min 7.0
993
994            for d in lib usr/lib usr/lib/system; do
995                try_dir="${alt_libc}/${d}"
996                [ -d "${try_dir}" ] && add_ldflags -L"${try_dir}"
997            done
998
999            asm_conversion_cmd="${source_path}/build/make/ads2gas_apple.pl"
1000         ;;
1001
1002        linux*)
1003            enable_feature linux
1004            if enabled rvct; then
1005                # Check if we have CodeSourcery GCC in PATH. Needed for
1006                # libraries
1007                hash arm-none-linux-gnueabi-gcc 2>&- || \
1008                  die "Couldn't find CodeSourcery GCC from PATH"
1009
1010                # Use armcc as a linker to enable translation of
1011                # some gcc specific options such as -lm and -lpthread.
1012                LD="armcc --translate_gcc"
1013
1014                # create configuration file (uses path to CodeSourcery GCC)
1015                armcc --arm_linux_configure --arm_linux_config_file=arm_linux.cfg
1016
1017                add_cflags --arm_linux_paths --arm_linux_config_file=arm_linux.cfg
1018                add_asflags --no_hide_all --apcs=/interwork
1019                add_ldflags --arm_linux_paths --arm_linux_config_file=arm_linux.cfg
1020                enabled pic && add_cflags --apcs=/fpic
1021                enabled pic && add_asflags --apcs=/fpic
1022                enabled shared && add_cflags --shared
1023            fi
1024        ;;
1025
1026        esac
1027    ;;
1028    mips*)
1029        link_with_cc=gcc
1030        setup_gnu_toolchain
1031        tune_cflags="-mtune="
1032        if enabled dspr2; then
1033            check_add_cflags -mips32r2 -mdspr2
1034            disable_feature fast_unaligned
1035        fi
1036        check_add_cflags -march=${tgt_isa}
1037        check_add_asflags -march=${tgt_isa}
1038        check_add_asflags -KPIC
1039    ;;
1040    ppc*)
1041        enable_feature ppc
1042        bits=${tgt_isa##ppc}
1043        link_with_cc=gcc
1044        setup_gnu_toolchain
1045        add_asflags -force_cpusubtype_ALL -I"\$(dir \$<)darwin"
1046        soft_enable altivec
1047        enabled altivec && add_cflags -maltivec
1048
1049        case "$tgt_os" in
1050        linux*)
1051            add_asflags -maltivec -mregnames -I"\$(dir \$<)linux"
1052        ;;
1053        darwin*)
1054            darwin_arch="-arch ppc"
1055            enabled ppc64 && darwin_arch="${darwin_arch}64"
1056            add_cflags  ${darwin_arch} -m${bits} -fasm-blocks
1057            add_asflags ${darwin_arch} -force_cpusubtype_ALL -I"\$(dir \$<)darwin"
1058            add_ldflags ${darwin_arch} -m${bits}
1059            enabled altivec && add_cflags -faltivec
1060        ;;
1061        esac
1062    ;;
1063    x86*)
1064        case  ${tgt_os} in
1065            win*)
1066                enabled gcc && add_cflags -fno-common
1067                ;;
1068            solaris*)
1069                CC=${CC:-${CROSS}gcc}
1070                CXX=${CXX:-${CROSS}g++}
1071                LD=${LD:-${CROSS}gcc}
1072                CROSS=${CROSS:-g}
1073                ;;
1074            os2)
1075                AS=${AS:-nasm}
1076                ;;
1077        esac
1078
1079        AS="${alt_as:-${AS:-auto}}"
1080        case  ${tgt_cc} in
1081            icc*)
1082                CC=${CC:-icc}
1083                LD=${LD:-icc}
1084                setup_gnu_toolchain
1085                add_cflags -use-msasm  # remove -use-msasm too?
1086                # add -no-intel-extensions to suppress warning #10237
1087                # refer to http://software.intel.com/en-us/forums/topic/280199
1088                add_ldflags -i-static -no-intel-extensions
1089                enabled x86_64 && add_cflags -ipo -static -O3 -no-prec-div
1090                enabled x86_64 && AR=xiar
1091                case ${tune_cpu} in
1092                    atom*)
1093                        tune_cflags="-x"
1094                        tune_cpu="SSE3_ATOM"
1095                    ;;
1096                    *)
1097                        tune_cflags="-march="
1098                    ;;
1099                esac
1100            ;;
1101            gcc*)
1102                link_with_cc=gcc
1103                tune_cflags="-march="
1104                setup_gnu_toolchain
1105                #for 32 bit x86 builds, -O3 did not turn on this flag
1106                enabled optimizations && disabled gprof && check_add_cflags -fomit-frame-pointer
1107            ;;
1108            vs*)
1109                # When building with Microsoft Visual Studio the assembler is
1110                # invoked directly. Checking at configure time is unnecessary.
1111                # Skip the check by setting AS arbitrarily
1112                AS=msvs
1113                msvs_arch_dir=x86-msvs
1114                vc_version=${tgt_cc##vs}
1115                case $vc_version in
1116                    7|8|9|10)
1117                         echo "${tgt_cc} does not support avx/avx2, disabling....."
1118                         RTCD_OPTIONS="${RTCD_OPTIONS}--disable-avx --disable-avx2 "
1119                         soft_disable avx
1120                         soft_disable avx2
1121                    ;;
1122                esac
1123            ;;
1124        esac
1125
1126        bits=32
1127        enabled x86_64 && bits=64
1128        check_cpp <<EOF && bits=x32
1129#ifndef __ILP32__
1130#error "not x32"
1131#endif
1132EOF
1133        case ${tgt_cc} in
1134            gcc*)
1135                add_cflags -m${bits}
1136                add_ldflags -m${bits}
1137            ;;
1138        esac
1139
1140        soft_enable runtime_cpu_detect
1141        # We can't use 'check_cflags' until the compiler is configured and CC is
1142        # populated.
1143        check_gcc_machine_option mmx
1144        check_gcc_machine_option sse
1145        check_gcc_machine_option sse2
1146        check_gcc_machine_option sse3
1147        check_gcc_machine_option ssse3
1148        check_gcc_machine_option sse4 sse4_1
1149        check_gcc_machine_option avx
1150        check_gcc_machine_option avx2
1151
1152        case "${AS}" in
1153            auto|"")
1154                which nasm >/dev/null 2>&1 && AS=nasm
1155                which yasm >/dev/null 2>&1 && AS=yasm
1156                [ "${AS}" = auto ] || [ -z "${AS}" ] \
1157                    && die "Neither yasm nor nasm have been found"
1158            ;;
1159        esac
1160        log_echo "  using $AS"
1161        [ "${AS##*/}" = nasm ] && add_asflags -Ox
1162        AS_SFX=.asm
1163        case  ${tgt_os} in
1164            win32)
1165                add_asflags -f win32
1166                enabled debug && add_asflags -g cv8
1167                EXE_SFX=.exe
1168            ;;
1169            win64)
1170                add_asflags -f x64
1171                enabled debug && add_asflags -g cv8
1172                EXE_SFX=.exe
1173            ;;
1174            linux*|solaris*|android*)
1175                add_asflags -f elf${bits}
1176                enabled debug && [ "${AS}" = yasm ] && add_asflags -g dwarf2
1177                enabled debug && [ "${AS}" = nasm ] && add_asflags -g
1178                [ "${AS##*/}" = nasm ] && check_asm_align
1179            ;;
1180            darwin*)
1181                add_asflags -f macho${bits}
1182                enabled x86 && darwin_arch="-arch i386" || darwin_arch="-arch x86_64"
1183                add_cflags  ${darwin_arch}
1184                add_ldflags ${darwin_arch}
1185                # -mdynamic-no-pic is still a bit of voodoo -- it was required at
1186                # one time, but does not seem to be now, and it breaks some of the
1187                # code that still relies on inline assembly.
1188                # enabled icc && ! enabled pic && add_cflags -fno-pic -mdynamic-no-pic
1189                enabled icc && ! enabled pic && add_cflags -fno-pic
1190            ;;
1191            iphonesimulator)
1192                add_asflags -f macho${bits}
1193                enabled x86 && sim_arch="-arch i386" || sim_arch="-arch x86_64"
1194                add_cflags  ${sim_arch}
1195                add_ldflags ${sim_arch}
1196           ;;
1197            os2)
1198                add_asflags -f aout
1199                enabled debug && add_asflags -g
1200                EXE_SFX=.exe
1201            ;;
1202            *) log "Warning: Unknown os $tgt_os while setting up $AS flags"
1203            ;;
1204        esac
1205    ;;
1206    universal*|*-gcc|generic-gnu)
1207        link_with_cc=gcc
1208        enable_feature gcc
1209    setup_gnu_toolchain
1210    ;;
1211    esac
1212
1213    # Try to enable CPU specific tuning
1214    if [ -n "${tune_cpu}" ]; then
1215        if [ -n "${tune_cflags}" ]; then
1216            check_add_cflags ${tune_cflags}${tune_cpu} || \
1217                die "Requested CPU '${tune_cpu}' not supported by compiler"
1218        fi
1219    if [ -n "${tune_asflags}" ]; then
1220            check_add_asflags ${tune_asflags}${tune_cpu} || \
1221                die "Requested CPU '${tune_cpu}' not supported by assembler"
1222        fi
1223    if [ -z "${tune_cflags}${tune_asflags}" ]; then
1224            log_echo "Warning: CPU tuning not supported by this toolchain"
1225        fi
1226    fi
1227
1228    if enabled debug; then
1229        check_add_cflags -g && check_add_ldflags -g
1230    else
1231        check_add_cflags -DNDEBUG
1232    fi
1233
1234    enabled gprof && check_add_cflags -pg && check_add_ldflags -pg
1235    enabled gcov &&
1236        check_add_cflags -fprofile-arcs -ftest-coverage &&
1237        check_add_ldflags -fprofile-arcs -ftest-coverage
1238
1239    if enabled optimizations; then
1240        if enabled rvct; then
1241            enabled small && check_add_cflags -Ospace || check_add_cflags -Otime
1242        else
1243            enabled small && check_add_cflags -O2 ||  check_add_cflags -O3
1244        fi
1245    fi
1246
1247    tgt_os_no_version=$(echo "${tgt_os}" | tr -d "[0-9]")
1248    # Default use_x86inc to yes when we are 64 bit, non-pic, or on any
1249    # non-Darwin target.
1250    if [ "${tgt_isa}" = "x86_64" ] || [ "${pic}" != "yes" ] || \
1251            [ "${tgt_os_no_version}" != "darwin" ]; then
1252        soft_enable use_x86inc
1253    fi
1254
1255    # Position Independent Code (PIC) support, for building relocatable
1256    # shared objects
1257    enabled gcc && enabled pic && check_add_cflags -fPIC
1258
1259    # Work around longjmp interception on glibc >= 2.11, to improve binary
1260    # compatibility. See http://code.google.com/p/webm/issues/detail?id=166
1261    enabled linux && check_add_cflags -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=0
1262
1263    # Check for strip utility variant
1264    ${STRIP} -V 2>/dev/null | grep GNU >/dev/null && enable_feature gnu_strip
1265
1266    # Try to determine target endianness
1267    check_cc <<EOF
1268    unsigned int e = 'O'<<24 | '2'<<16 | 'B'<<8 | 'E';
1269EOF
1270    [ -f "${TMP_O}" ] && od -A n -t x1 "${TMP_O}" | tr -d '\n' |
1271        grep '4f *32 *42 *45' >/dev/null 2>&1 && enable_feature big_endian
1272
1273    # Try to find which inline keywords are supported
1274    check_cc <<EOF && INLINE="inline"
1275    static inline function() {}
1276EOF
1277    check_cc <<EOF && INLINE="__inline__ __attribute__((always_inline))"
1278    static __attribute__((always_inline)) function() {}
1279EOF
1280
1281    # Almost every platform uses pthreads.
1282    if enabled multithread; then
1283        case ${toolchain} in
1284            *-win*-vs*);;
1285            *-android-gcc);;
1286            *) check_header pthread.h && add_extralibs -lpthread
1287        esac
1288    fi
1289
1290    # only for MIPS platforms
1291    case ${toolchain} in
1292        mips*)
1293            if enabled dspr2; then
1294                if enabled big_endian; then
1295                    echo "dspr2 optimizations are available only for little endian platforms"
1296                    disable_feature dspr2
1297                fi
1298            fi
1299        ;;
1300    esac
1301
1302    # glibc needs these
1303    if enabled linux; then
1304        add_cflags -D_LARGEFILE_SOURCE
1305        add_cflags -D_FILE_OFFSET_BITS=64
1306    fi
1307
1308    # append any user defined extra cflags
1309    if [ -n "${extra_cflags}" ] ; then
1310        check_add_cflags ${extra_cflags} || \
1311        die "Requested extra CFLAGS '${extra_cflags}' not supported by compiler"
1312    fi
1313}
1314
1315process_toolchain() {
1316    process_common_toolchain
1317}
1318
1319print_config_mk() {
1320    saved_prefix="${prefix}"
1321    prefix=$1
1322    makefile=$2
1323    shift 2
1324    for cfg; do
1325        if enabled $cfg; then
1326            upname="`toupper $cfg`"
1327            echo "${prefix}_${upname}=yes" >> $makefile
1328        fi
1329    done
1330    prefix="${saved_prefix}"
1331}
1332
1333print_config_h() {
1334    saved_prefix="${prefix}"
1335    prefix=$1
1336    header=$2
1337    shift 2
1338    for cfg; do
1339        upname="`toupper $cfg`"
1340        if enabled $cfg; then
1341            echo "#define ${prefix}_${upname} 1" >> $header
1342        else
1343            echo "#define ${prefix}_${upname} 0" >> $header
1344        fi
1345    done
1346    prefix="${saved_prefix}"
1347}
1348
1349print_config_vars_h() {
1350    header=$1
1351    shift
1352    while [ $# -gt 0 ]; do
1353        upname="`toupper $1`"
1354        echo "#define ${upname} $2" >> $header
1355        shift 2
1356    done
1357}
1358
1359print_webm_license() {
1360    saved_prefix="${prefix}"
1361    destination=$1
1362    prefix="$2"
1363    suffix="$3"
1364    shift 3
1365    cat <<EOF > ${destination}
1366${prefix} Copyright (c) 2011 The WebM project authors. All Rights Reserved.${suffix}
1367${prefix} ${suffix}
1368${prefix} Use of this source code is governed by a BSD-style license${suffix}
1369${prefix} that can be found in the LICENSE file in the root of the source${suffix}
1370${prefix} tree. An additional intellectual property rights grant can be found${suffix}
1371${prefix} in the file PATENTS.  All contributing project authors may${suffix}
1372${prefix} be found in the AUTHORS file in the root of the source tree.${suffix}
1373EOF
1374    prefix="${saved_prefix}"
1375}
1376
1377process_targets() {
1378    true;
1379}
1380
1381process_detect() {
1382    true;
1383}
1384
1385enable_feature logging
1386logfile="config.log"
1387self=$0
1388process() {
1389    cmdline_args="$@"
1390    process_cmdline "$@"
1391    if enabled child; then
1392        echo "# ${self} $@" >> ${logfile}
1393    else
1394        echo "# ${self} $@" > ${logfile}
1395    fi
1396    post_process_common_cmdline
1397    post_process_cmdline
1398    process_toolchain
1399    process_detect
1400    process_targets
1401
1402    OOT_INSTALLS="${OOT_INSTALLS}"
1403    if enabled source_path_used; then
1404    # Prepare the PWD for building.
1405    for f in ${OOT_INSTALLS}; do
1406            install -D "${source_path}/$f" "$f"
1407    done
1408    fi
1409    cp "${source_path}/build/make/Makefile" .
1410
1411    clean_temp_files
1412    true
1413}
1414