summaryrefslogtreecommitdiff
path: root/contrib/ffmpeg-universal.sh
blob: 6e27c56b80e919e8ae087da2d51a69da7a3e1283 (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
#!/bin/sh

function usage {
    echo "usage: $0 <action> <source path> <arch list>"
    echo "where: <action> is one of:"
    echo "       -configure    do initial configuration for each architecture"
    echo "       -avcodec      build libavcodec.a for each architecture"
    echo "       -avutil       build libavutil.a for each architecture"
    echo "       -postproc     build libpostproc.a for each architecture"
    echo "       -clean        clean for each architecture"
    echo "       -distclean    distclean for each architecture"
    echo ""
    echo "Configuration must be done before any library builds.  Options to be passed"
    echo "to ffmpeg's configure command-line should be passed in the environment"
    echo "using the FFMPEG_CONFIGURE_OPTIONS environment variable."
    exit 1
}

function run_configure {
    local arch=$1
    local ffmpeg_topbuilddir="$2"

    # start over from scratch
    rm -rf "$ffmpeg_topbuilddir"
    mkdir -p "$ffmpeg_topbuilddir"
    pushd "$ffmpeg_topbuilddir" > /dev/null 2>&1

    # The Makefile should be passing FFMPEG_CONFIGURE_OPTIONS without passing
    # any --extra-cflags or --extra-ldflags options.  Both CFLAGS and LDFLAGS
    # should be in the environment in addition to FFMPEG_CONFIGURE_OPTIONS.

    local CROSS_OPTIONS=
    local EXTRA_CFLAGS=
    local EXTRA_LDFLAGS=

    if test $HOST_ARCH != $arch; then
        EXTRA_CFLAGS="$CFLAGS -isysroot /Developer/SDKs/MacOSX10.4u.sdk -arch $arch"
        EXTRA_LDFLAGS="$LDFLAGS -isysroot /Developer/SDKs/MacOSX10.4u.sdk -arch $arch"
        if test $arch = i386; then
            FFMPEG_ARCH=x86_32
        else
            FFMPEG_ARCH=$arch
        fi
        CROSS_OPTIONS="--cross-compile --arch=$FFMPEG_ARCH"
    fi

    echo "$SOURCE_PATH/configure" $CROSS_OPTIONS $FFMPEG_CONFIGURE_OPTIONS \
                                  --extra-cflags="$EXTRA_CFLAGS" \
                                  --extra-ldflags="$EXTRA_LDFLAGS"
    "$SOURCE_PATH/configure" $CROSS_OPTIONS $FFMPEG_CONFIGURE_OPTIONS \
                             --extra-cflags="$EXTRA_CFLAGS" \
                             --extra-ldflags="$EXTRA_LDFLAGS"
    local retval=$?

    # We're building ffmpeg to put it into a shared library, but ffmpeg thinks
    # it's building to put it into a static library or program image, so it
    # adds -mdynamic-no-pic to its list of options as an optimization.  This is
    # fine in Tiger if later linked with -Wl,-read_only_relocs,warning, but it
    # isn't fine on other versions of Mac OS X.
    if test -f config.mak; then
        cat config.mak | sed -e '/OPTFLAGS=/s/-mdynamic-no-pic//g' > config.tmp
        mv -f config.tmp config.mak
    fi

    popd > /dev/null 2>&1
    `exit $retval` || exit $retval
}

if test x"$*" = x""; then
    usage
fi
case "$1" in
    -configure)
        MODE=configure
        ;;
    -avcodec)
        MODE=avcodec
        ;;
    -avutil)
        MODE=avutil
        ;;
    -postproc)
        MODE=postproc
        ;;
    -clean)
        MODE=clean
        ;;
    -distclean)
        MODE=distclean
        ;;
    *)
        echo "Unrecognized mode: $1"
        usage
        ;;
esac
shift

if test x"$1" = x""; then
    echo "No source path specified!"
    usage
fi
if test ! -d "$1"; then
    echo "Source path $1 does not exist!"
    exit 1
fi
SOURCE_PATH="$1"
shift

TOP_BUILD_PATH="`pwd`/ffmpeg"
if test -f "$TOP_BUILD_PATH/configure"; then
    # Building in place.  Create a temporary build directory and use that
    # instead, but place the final output from lipo into the original
    # build directory.
    BUILD_PATH="$TOP_BUILD_PATH/build"
else
    BUILD_PATH="$TOP_BUILD_PATH"
fi
# This doesn't make sense for -configure, but it does for everything else.
# The -configure mode won't ever use it anyway.
OUTPUT_FILENAME="$BUILD_PATH/lib$MODE/lib$MODE.a"

HOST_ARCH=`arch`
UNIVERSAL_ARCHES=$*
if test x"$UNIVERSAL_ARCHES" = x""; then
    echo "No architecture(s) specified; using $HOST_ARCH only."
    UNIVERSAL_ARCHES=$HOST_ARCH
fi
CONFIG_FILES=
LIPO_CMDLINE="-create -output $OUTPUT_FILENAME"
for arch in $UNIVERSAL_ARCHES; do
    ffmpeg_topbuilddir="$BUILD_PATH/$arch"
    LIPO_CMDLINE="$LIPO_CMDLINE -arch $arch $ffmpeg_topbuilddir/lib$MODE/lib$MODE.a"
    case $MODE in
        configure)
            run_configure $arch "$ffmpeg_topbuilddir"
            CONFIG_FILES="$CONFIG_FILES $ffmpeg_topbuilddir/config.h"
            ;;
        avcodec)
            "$MAKE" -C "$ffmpeg_topbuilddir/libavcodec" libavcodec.a || exit $?
            ;;
        avutil)
            "$MAKE" -C "$ffmpeg_topbuilddir/libavutil" libavutil.a || exit $?
            ;;
        postproc)
            "$MAKE" -C "$ffmpeg_topbuilddir/libpostproc" libpostproc.a || exit $?
            ;;
        clean)
            "$MAKE" -C "$ffmpeg_topbuilddir" clean || exit $?
            ;;
        distclean)
            "$MAKE" -C "$ffmpeg_topbuilddir" distclean || exit $?
            ;;
    esac
done

case $MODE in
    configure)
        # Now that configuration is done, create config.h in the top-level
        # ffmpeg directory.  Pull out only what's needed by xine-lib, removing
        # any possible platform conflicts
        grep -h "define CONFIG_.*_DECODER" $CONFIG_FILES | uniq > "$BUILD_PATH/config.h"
        touch "$BUILD_PATH/config.mak"
        ;;
    clean)
        rm -f "$BUILD_PATH/libavcodec/libavcodec.a" "$BUILD_PATH/libavutil/libavutil.a" \
              "$BUILD_PATH/libpostproc/libpostproc.a"
        ;;
    distclean)
        rm -f "$BUILD_PATH/config.mak" "$BUILD_PATH/config.h"
        rm -f "$BUILD_PATH/libavcodec/libavcodec.a" "$BUILD_PATH/libavutil/libavutil.a" \
              "$BUILD_PATH/libpostproc/libpostproc.a"
        ;;
    *)
        mkdir -p "`dirname "$OUTPUT_FILENAME"`"
        lipo $LIPO_CMDLINE
        ;;
esac