1#!/bin/bash
2##
3##  configure
4##
5##  This script is the front-end to the build system. It provides a similar
6##  interface to standard configure scripts with some extra bits for dealing
7##  with toolchains that differ from the standard POSIX interface and
8##  for extracting subsets of the source tree. In theory, reusable parts
9##  of this script were intended to live in build/make/configure.sh,
10##  but in practice, the line is pretty blurry.
11##
12##  This build system is based in part on the FFmpeg configure script.
13##
14
15#source_path="`dirname \"$0\"`"
16source_path=${0%/*}
17. "${source_path}/build/make/configure.sh"
18
19show_help(){
20    show_help_pre
21    cat << EOF
22Advanced options:
23  ${toggle_libs}                  don't build libraries
24  ${toggle_examples}              don't build examples
25  --libc=PATH                     path to alternate libc
26  --as={yasm|nasm|auto}           use specified assembler [auto, yasm preferred]
27  ${toggle_fast_unaligned}        don't use unaligned accesses, even when
28                                  supported by hardware [auto]
29  ${toggle_codec_srcs}            in/exclude codec library source code
30  ${toggle_debug_libs}            in/exclude debug version of libraries
31  ${toggle_md5}                   support for output of checksum data
32  ${toggle_static_msvcrt}         use static MSVCRT (VS builds only)
33  ${toggle_vp8}                   VP8 codec support
34  ${toggle_psnr}                  output of PSNR data, if supported (encoders)
35  ${toggle_mem_tracker}           track memory usage
36  ${toggle_postproc}              postprocessing
37  ${toggle_multithread}           multithreaded encoding and decoding.
38  ${toggle_spatial_resampling}    spatial sampling (scaling) support
39  ${toggle_realtime_only}         enable this option while building for real-time encoding
40  ${toggle_runtime_cpu_detect}    runtime cpu detection
41  ${toggle_shared}                shared library support
42  ${toggle_small}                 favor smaller size over speed
43  ${toggle_postproc_visualizer}   macro block / block level visualizers
44
45Codecs:
46  Codecs can be selectively enabled or disabled individually, or by family:
47      --disable-<codec>
48  is equivalent to:
49      --disable-<codec>-encoder
50      --disable-<codec>-decoder
51
52  Codecs available in this distribution:
53EOF
54#restore editor state '
55
56    local family;
57    local last_family;
58    local c;
59    local str;
60    for c in ${CODECS}; do
61        family=${c%_*}
62        if [ "${family}" != "${last_family}" ]; then
63            [ -z "${str}" ] || echo "${str}"
64            str="$(printf '    %10s:' ${family})"
65        fi
66        str="${str} $(printf '%10s' ${c#*_})"
67        last_family=${family}
68    done
69    echo "${str}"
70    show_help_post
71}
72
73##
74## BEGIN APPLICATION SPECIFIC CONFIGURATION
75##
76
77# all_platforms is a list of all supported target platforms. Maintain
78# alphabetically by architecture, generic-gnu last.
79all_platforms="${all_platforms} armv5te-linux-rvct"
80all_platforms="${all_platforms} armv5te-linux-gcc"
81all_platforms="${all_platforms} armv5te-none-rvct"
82all_platforms="${all_platforms} armv5te-symbian-gcc"
83all_platforms="${all_platforms} armv6-darwin-gcc"
84all_platforms="${all_platforms} armv6-linux-rvct"
85all_platforms="${all_platforms} armv6-linux-gcc"
86all_platforms="${all_platforms} armv6-none-rvct"
87all_platforms="${all_platforms} armv6-symbian-gcc"
88all_platforms="${all_platforms} iwmmxt-linux-rvct"
89all_platforms="${all_platforms} iwmmxt-linux-gcc"
90all_platforms="${all_platforms} iwmmxt2-linux-rvct"
91all_platforms="${all_platforms} iwmmxt2-linux-gcc"
92all_platforms="${all_platforms} armv7-darwin-gcc"    #neon Cortex-A8
93all_platforms="${all_platforms} armv7-linux-rvct"    #neon Cortex-A8
94all_platforms="${all_platforms} armv7-linux-gcc"     #neon Cortex-A8
95all_platforms="${all_platforms} armv7-none-rvct"     #neon Cortex-A8
96all_platforms="${all_platforms} mips32-linux-gcc"
97all_platforms="${all_platforms} ppc32-darwin8-gcc"
98all_platforms="${all_platforms} ppc32-darwin9-gcc"
99all_platforms="${all_platforms} ppc32-linux-gcc"
100all_platforms="${all_platforms} ppc64-darwin8-gcc"
101all_platforms="${all_platforms} ppc64-darwin9-gcc"
102all_platforms="${all_platforms} ppc64-linux-gcc"
103all_platforms="${all_platforms} sparc-solaris-gcc"
104all_platforms="${all_platforms} x86-darwin8-gcc"
105all_platforms="${all_platforms} x86-darwin8-icc"
106all_platforms="${all_platforms} x86-darwin9-gcc"
107all_platforms="${all_platforms} x86-darwin9-icc"
108all_platforms="${all_platforms} x86-linux-gcc"
109all_platforms="${all_platforms} x86-linux-icc"
110all_platforms="${all_platforms} x86-solaris-gcc"
111all_platforms="${all_platforms} x86-win32-gcc"
112all_platforms="${all_platforms} x86-win32-vs7"
113all_platforms="${all_platforms} x86-win32-vs8"
114all_platforms="${all_platforms} x86-win32-vs9"
115all_platforms="${all_platforms} x86_64-darwin9-gcc"
116all_platforms="${all_platforms} x86_64-darwin10-gcc"
117all_platforms="${all_platforms} x86_64-linux-gcc"
118all_platforms="${all_platforms} x86_64-linux-icc"
119all_platforms="${all_platforms} x86_64-solaris-gcc"
120all_platforms="${all_platforms} x86_64-win64-vs8"
121all_platforms="${all_platforms} x86_64-win64-vs9"
122all_platforms="${all_platforms} universal-darwin8-gcc"
123all_platforms="${all_platforms} universal-darwin9-gcc"
124all_platforms="${all_platforms} generic-gnu"
125
126# all_targets is a list of all targets that can be configured
127# note that these should be in dependency order for now.
128all_targets="libs examples docs"
129
130# all targets available are enabled, by default.
131for t in ${all_targets}; do
132    [ -f ${source_path}/${t}.mk ] && enable ${t}
133done
134
135# check installed doxygen version
136doxy_version=$(doxygen --version 2>/dev/null)
137doxy_major=${doxy_version%%.*}
138if [ ${doxy_major:-0} -ge 1 ]; then
139    doxy_version=${doxy_version#*.}
140    doxy_minor=${doxy_version%%.*}
141    doxy_patch=${doxy_version##*.}
142
143    [ $doxy_major -gt 1 ] && enable doxygen
144    [ $doxy_minor -gt 5 ] && enable doxygen
145    [ $doxy_minor -eq 5 ] && [ $doxy_patch -ge 3 ] && enable doxygen
146fi
147
148# install everything except the sources, by default. sources will have
149# to be enabled when doing dist builds, since that's no longer a common
150# case.
151enabled doxygen && php -v >/dev/null 2>&1 && enable install_docs
152enable install_bins
153enable install_libs
154
155enable optimizations
156enable fast_unaligned #allow unaligned accesses, if supported by hw
157enable md5
158enable spatial_resampling
159enable multithread
160enable os_support
161
162[ -d ${source_path}/../include ] && enable alt_tree_layout
163for d in vp8; do
164    [ -d ${source_path}/${d} ] && disable alt_tree_layout;
165done
166
167if ! enabled alt_tree_layout; then
168# development environment
169[ -d ${source_path}/vp8 ] && CODECS="${CODECS} vp8_encoder vp8_decoder"
170else
171# customer environment
172[ -f ${source_path}/../include/vpx/vp8cx.h ] && CODECS="${CODECS} vp8_encoder"
173[ -f ${source_path}/../include/vpx/vp8dx.h ] && CODECS="${CODECS} vp8_decoder"
174
175[ -f ${source_path}/../lib/*/*mt.lib ] && soft_enable static_msvcrt
176fi
177
178CODECS="$(echo ${CODECS} | tr ' ' '\n')"
179CODEC_FAMILIES="$(for c in ${CODECS}; do echo ${c%_*}; done | sort | uniq)"
180
181ARCH_LIST="
182    arm
183    mips
184    x86
185    x86_64
186    ppc32
187    ppc64
188"
189ARCH_EXT_LIST="
190    armv5te
191    armv6
192    armv7
193    iwmmxt
194    iwmmxt2
195
196    mips32
197
198    mmx
199    sse
200    sse2
201    sse3
202    ssse3
203    sse4_1
204
205    altivec
206"
207HAVE_LIST="
208    ${ARCH_EXT_LIST}
209    vpx_ports
210    stdint_h
211    alt_tree_layout
212    pthread_h
213    sys_mman_h
214"
215CONFIG_LIST="
216    external_build
217    install_docs
218    install_bins
219    install_libs
220    install_srcs
221    debug
222    gprof
223    gcov
224    rvct
225    gcc
226    msvs
227    pic
228    big_endian
229
230    codec_srcs
231    debug_libs
232    fast_unaligned
233    mem_manager
234    mem_tracker
235    mem_checks
236    md5
237
238    dequant_tokens
239    dc_recon
240    runtime_cpu_detect
241    postproc
242    multithread
243    psnr
244    ${CODECS}
245    ${CODEC_FAMILIES}
246    encoders
247    decoders
248    static_msvcrt
249    spatial_resampling
250    realtime_only
251    shared
252    small
253    postproc_visualizer
254    os_support
255"
256CMDLINE_SELECT="
257    extra_warnings
258    werror
259    install_docs
260    install_bins
261    install_libs
262    install_srcs
263    debug
264    gprof
265    gcov
266    pic
267    optimizations
268    ccache
269    runtime_cpu_detect
270
271    libs
272    examples
273    libc
274    as
275    fast_unaligned
276    codec_srcs
277    debug_libs
278    md5
279
280    dequant_tokens
281    dc_recon
282    postproc
283    multithread
284    psnr
285    ${CODECS}
286    ${CODEC_FAMILIES}
287    static_msvcrt
288    mem_tracker
289    spatial_resampling
290    realtime_only
291    shared
292    small
293    postproc_visualizer
294"
295
296process_cmdline() {
297    for opt do
298        optval="${opt#*=}"
299        case "$opt" in
300        --disable-codecs) for c in ${CODECS}; do disable $c; done ;;
301        *) process_common_cmdline "$opt"
302        ;;
303        esac
304    done
305}
306
307post_process_cmdline() {
308    local c
309
310    # If the codec family is disabled, disable all components of that family.
311    # If the codec family is enabled, enable all components of that family.
312    log_echo "Configuring selected codecs"
313    for c in ${CODECS}; do
314        disabled ${c%%_*} && disable ${c}
315        enabled ${c%%_*} && enable ${c}
316    done
317
318    # Enable all detected codecs, if they haven't been disabled
319    for c in ${CODECS}; do soft_enable $c; done
320
321    # Enable the codec family if any component of that family is enabled
322    for c in ${CODECS}; do
323        enabled $c && enable ${c%_*}
324    done
325
326    # Set the {en,de}coders variable if any algorithm in that class is enabled
327    for c in ${CODECS}; do
328        enabled ${c} && enable ${c##*_}s
329    done
330}
331
332
333process_targets() {
334    enabled child || write_common_config_banner
335    enabled universal || write_common_target_config_h  ${BUILD_PFX}vpx_config.h
336
337    # TODO: add host tools target (obj_int_extract, etc)
338
339    # For fat binaries, call configure recursively to configure for each
340    # binary architecture to be included.
341    if enabled universal; then
342        # Call configure (ourselves) for each subarchitecture
343        for arch in $fat_bin_archs; do
344            BUILD_PFX=${arch}/ toolchain=${arch} $self --child $cmdline_args || exit $?
345        done
346    fi
347
348    # The write_common_config (config.mk) logic is deferred until after the
349    # recursive calls to configure complete, becuase we want our universal
350    # targets to be executed last.
351    write_common_config_targets
352    enabled universal && echo "FAT_ARCHS=${fat_bin_archs}" >> config.mk
353
354    # Calculate the default distribution name, based on the enabled features
355    local cf
356    local DIST_DIR=vpx
357    for cf in $CODEC_FAMILIES; do
358        if enabled ${cf}_encoder && enabled ${cf}_decoder; then
359            DIST_DIR="${DIST_DIR}-${cf}"
360        elif enabled ${cf}_encoder; then
361            DIST_DIR="${DIST_DIR}-${cf}cx"
362        elif enabled ${cf}_decoder; then
363            DIST_DIR="${DIST_DIR}-${cf}dx"
364        fi
365    done
366    enabled debug_libs && DIST_DIR="${DIST_DIR}-debug"
367    enabled codec_srcs && DIST_DIR="${DIST_DIR}-src"
368    ! enabled postproc && DIST_DIR="${DIST_DIR}-nopost"
369    ! enabled multithread && DIST_DIR="${DIST_DIR}-nomt"
370    ! enabled install_docs && DIST_DIR="${DIST_DIR}-nodocs"
371    DIST_DIR="${DIST_DIR}-${tgt_isa}-${tgt_os}"
372    case "${tgt_os}" in
373    win*) enabled static_msvcrt && DIST_DIR="${DIST_DIR}mt" || DIST_DIR="${DIST_DIR}md"
374          DIST_DIR="${DIST_DIR}-${tgt_cc}"
375          ;;
376    esac
377    if [ -f "${source_path}/build/make/version.sh" ]; then
378        local ver=`"$source_path/build/make/version.sh" --bare $source_path`
379        DIST_DIR="${DIST_DIR}-${ver}"
380        VERSION_STRING=${ver}
381        ver=${ver%%-*}
382        VERSION_PATCH=${ver##*.}
383        ver=${ver%.*}
384        VERSION_MINOR=${ver##*.}
385        ver=${ver#v}
386        VERSION_MAJOR=${ver%.*}
387    fi
388    enabled child || cat <<EOF >> config.mk
389
390PREFIX=${prefix}
391ifeq (\$(MAKECMDGOALS),dist)
392DIST_DIR?=${DIST_DIR}
393else
394DIST_DIR?=\$(DESTDIR)${prefix}
395endif
396LIBSUBDIR=${libdir##${prefix}/}
397
398VERSION_STRING=${VERSION_STRING}
399
400VERSION_MAJOR=${VERSION_MAJOR}
401VERSION_MINOR=${VERSION_MINOR}
402VERSION_PATCH=${VERSION_PATCH}
403
404CONFIGURE_ARGS=${CONFIGURE_ARGS}
405EOF
406    enabled child || echo "CONFIGURE_ARGS?=${CONFIGURE_ARGS}" >> config.mk
407
408    #
409    # Write makefiles for all enabled targets
410    #
411    for tgt in libs examples docs solution; do
412        local tgt_fn="$tgt-$toolchain.mk"
413
414        if enabled $tgt; then
415            echo "Creating makefiles for ${toolchain} ${tgt}"
416            write_common_target_config_mk $tgt_fn ${BUILD_PFX}vpx_config.h
417            #write_${tgt}_config
418        fi
419    done
420
421}
422
423process_detect() {
424    if enabled shared; then
425        # Can only build shared libs on a subset of platforms. Doing this check
426        # here rather than at option parse time because the target auto-detect
427        # magic happens after the command line has been parsed.
428        enabled linux || die "--enable-shared only supported on ELF for now"
429    fi
430    if [ -z "$CC" ]; then
431        echo "Bypassing toolchain for environment detection."
432        enable external_build
433        check_header() {
434            log fake_check_header "$@"
435            header=$1
436            shift
437            var=`echo $header | sed 's/[^A-Za-z0-9_]/_/g'`
438            disable $var
439            case $header in
440                stdio.h)
441                    true;
442                ;;
443                *)
444                    local result=false
445                    for d in "$@"; do
446                        [ -f "${d##-I}/$header" ] && result=true && break
447                    done
448                    ${result:-true}
449            esac && enable $var
450        }
451        check_ld() {
452            true
453        }
454    fi
455    check_header stdio.h || die "Unable to invoke compiler: ${CC} ${CFLAGS}"
456    check_ld <<EOF || die "Toolchain is unable to link executables"
457int main(void) {return 0;}
458EOF
459    # check system headers
460    check_header stdint.h
461    check_header pthread.h
462    check_header sys/mman.h
463
464    check_header vpx/vpx_integer.h -I${source_path} && enable vpx_ports
465}
466
467process_toolchain() {
468    process_common_toolchain
469
470    # Handle universal binaries for this architecture
471    case $toolchain in
472        universal-darwin*)
473            local darwin_ver=${tgt_os##darwin}
474            fat_bin_archs="$fat_bin_archs ppc32-${tgt_os}-gcc"
475
476            # Intel
477            fat_bin_archs="$fat_bin_archs x86-${tgt_os}-${tgt_cc}"
478            if [ $darwin_ver -gt 8 ]; then
479                fat_bin_archs="$fat_bin_archs x86_64-${tgt_os}-${tgt_cc}"
480            fi
481            ;;
482    esac
483
484
485    # Enable some useful compiler flags
486    if enabled gcc; then
487        enabled werror && check_add_cflags -Werror
488        check_add_cflags -Wall
489        check_add_cflags -Wdeclaration-after-statement
490        check_add_cflags -Wdisabled-optimization
491        check_add_cflags -Wpointer-arith
492        check_add_cflags -Wtype-limits
493        check_add_cflags -Wcast-qual
494        enabled extra_warnings || check_add_cflags -Wno-unused-function
495    fi
496
497    if enabled icc; then
498        enabled werror && check_add_cflags -Werror
499        check_add_cflags -Wall
500        check_add_cflags -Wpointer-arith
501
502        # ICC has a number of floating point optimizations that we disable
503        # in favor of deterministic output WRT to other compilers
504        add_cflags -fp-model precise
505    fi
506
507    # Enable extra, harmless warnings. These might provide additional insight
508    # to what the compiler is doing and why, but in general, but they shouldn't
509    # be treated as fatal, even if we're treating warnings as errors.
510    GCC_EXTRA_WARNINGS="
511        -Wdisabled-optimization
512        -Winline
513    "
514    enabled gcc && EXTRA_WARNINGS="${GCC_EXTRA_WARNINGS}"
515    RVCT_EXTRA_WARNINGS="
516        --remarks
517    "
518    enabled rvct && EXTRA_WARNINGS="${RVCT_EXTRA_WARNINGS}"
519    if enabled extra_warnings; then
520        for w in ${EXTRA_WARNINGS}; do
521            check_add_cflags ${w}
522            enabled gcc && enabled werror && check_add_cflags -Wno-error=${w}
523        done
524    fi
525
526    # ccache only really works on gcc toolchains
527    enabled gcc || soft_disable ccache
528    if enabled mips; then
529        enable dequant_tokens
530        enable dc_recon
531    fi
532
533    # Enable the postbuild target if building for visual studio.
534    case "$tgt_cc" in
535        vs*) enable msvs
536             enable solution
537             vs_version=${tgt_cc##vs}
538             all_targets="${all_targets} solution"
539        ;;
540    esac
541
542    # Other toolchain specific defaults
543    case $toolchain in x86*|ppc*|universal*) soft_enable postproc;; esac
544
545    if enabled postproc_visualizer; then
546        enabled postproc || die "postproc_visualizer requires postproc to be enabled"
547    fi
548}
549
550
551##
552## END APPLICATION SPECIFIC CONFIGURATION
553##
554CONFIGURE_ARGS="$@"
555process "$@"
556cat <<EOF > ${BUILD_PFX}vpx_config.c
557static const char* const cfg = "$CONFIGURE_ARGS";
558const char *vpx_codec_build_config(void) {return cfg;}
559EOF
560