Pre-Compile.sh 10.7 KB
Newer Older
1 2 3 4 5 6
#!/bin/sh
#
# Pre-Compile.sh
#
# Script that install libvlc and its modules inside VLCKit.
#
7
# This is for some creepy reasons also used by legacy VLC.app or
8 9 10
# the moz plugin.

#
11
# We are building VLC.app or the moz plugin
12 13
#
if test "${ACTION}" = "release-makefile"; then
14 15
    echo "running Pre-Compile.sh in release-makefile mode"

16
    FULL_PRODUCT_NAME="${PRODUCT}"
17 18 19 20 21
    if [ "$FULL_PRODUCT_NAME" = "VLC-Plugin.plugin" ] ; then
        TARGET_BUILD_DIR="${src_dir}"
    else
        TARGET_BUILD_DIR="${build_dir}"
    fi
22 23 24 25
    CONTENTS_FOLDER_PATH="${FULL_PRODUCT_NAME}/Contents/MacOS"
    VLC_BUILD_DIR="${build_dir}"
    VLC_SRC_DIR="${src_dir}"
    ACTION="build"
26
    RELEASE_MAKEFILE="yes"
27 28 29 30
    use_archs="no"
    main_build_dir="${VLC_BUILD_DIR}"
else
    use_archs="yes"
31
    main_build_dir="${VLC_BUILD_DIR}/${ARCHS%% *}"
32
    echo "Building for $ARCHS"
33 34
fi

35 36 37 38 39
if test "${ACTION}" = "clean"; then
    rm -Rf "${VLC_BUILD_DIR}/tmp"
    exit 0
fi

40
if test "${ACTION}" != "build"; then
Felix Paul Kühne's avatar
Felix Paul Kühne committed
41
if test "${ACTION}" != "install"; then
42 43 44
    echo "This script is supposed to run from xcodebuild or Xcode"
    exit 1
fi
Felix Paul Kühne's avatar
Felix Paul Kühne committed
45
fi
46

47
lib="lib"
48
plugins="plugins"
49 50 51
share="share"
include="include"
target="${TARGET_BUILD_DIR}/${CONTENTS_FOLDER_PATH}"
52
target_bin="${target}/bin"
53
target_lib="${target}/${lib}"            # Should we consider using a different well-known folder like shared resources?
54
target_plugins="${target}/${plugins}"    # Should we consider using a different well-known folder like shared resources?
55
target_share="${target}/${share}"        # Should we consider using a different well-known folder like shared resources?
56
linked_libs=""
57
prefix=".libs"
58
suffix="dylib"
59
num_archs=$(echo `echo $ARCHS | wc -w`)
60 61

##########################
62
# @function vlc_install_object(src_lib, dest_dir, type, lib_install_prefix, destination_name, suffix)
63 64 65
# @description Installs the specified library into the destination folder, automatically changes the references to dependencies
# @param src_lib     source library to copy to the destination directory
# @param dest_dir    destination directory where the src_lib should be copied to
66
vlc_install_object() {
67 68 69 70 71
    local src_lib=${1}
    local dest_dir=${2}
    local type=${3}
    local lib_install_prefix=${4}
    local destination_name=${5}
72
    local suffix=${6}
73

74
    if [ $type = "library" ]; then
75 76
        local install_name="@loader_path/lib"
    elif [ $type = "module" ]; then
77
        local install_name="@loader_path/plugins"
78 79
    fi
    if [ "$destination_name" != "" ]; then
80
        local lib_dest="$dest_dir/$destination_name$suffix"
81
        local lib_name=`basename $destination_name`
82
    else
83
        local lib_dest="$dest_dir/`basename $src_lib`$suffix"
84
        local lib_name=`basename $src_lib`
85
    fi
86

87
    if [ "x$lib_install_prefix" != "x" ]; then
88 89 90 91
        local lib_install_prefix="$lib_install_prefix"
    else
        local lib_install_prefix="@loader_path/../lib"
    fi
92

93
    if test ! -e ${src_lib}; then
94 95
        return
    fi
96

97
    if ( (test ! -e ${lib_dest}) || test ${src_lib} -nt ${lib_dest} ); then
98

99
        mkdir -p ${dest_dir}
100

101 102 103 104 105 106
        # Lets copy the library from the source folder to our new destination folder
        if [ "${type}" = "bin" ]; then
            install -m 755 ${src_lib} ${lib_dest}
        else
            install -m 644 ${src_lib} ${lib_dest}
        fi
107

108 109
        # Update the dynamic library so it will know where to look for the other libraries
        echo "Installing ${type} `basename ${lib_dest}`"
110

111
        if [ "${type}" = "library" ]; then
112
            # Change the reference of libvlc.1 stored in the usr directory to libvlc.dylib in the framework's library directory
113
            install_name_tool -id "${install_name}/${lib_name}" ${lib_dest} > /dev/null
114
        fi
115

116 117 118 119 120
        if [ "${type}" != "data" ]; then
            # Iterate through each installed library and modify the references to other dynamic libraries to match the framework's library directory
            for linked_lib in `otool -L ${lib_dest}  | grep '(' | sed 's/\((.*)\)//'`; do
                local name=`basename ${linked_lib}`
                case "${linked_lib}" in
121
                    */vlc_build_dir/* | */vlc_install_dir/* | *vlc* | */extras/contrib/*)
122 123 124
                        if test -e ${linked_lib}; then
                            install_name_tool -change "$linked_lib" "${lib_install_prefix}/${name}" "${lib_dest}"
                            linked_libs="${linked_libs} ${ref_lib}"
125
                            vlc_install_object ${linked_lib} ${target_lib} "library"
126 127 128 129 130 131 132
                        fi
                        ;;
                esac
            done
        fi
     fi
}
133
# @function vlc_install_object
134 135
##########################

136 137 138 139 140 141
##########################
# @function vlc_install(src_lib_dir, src_lib_name, dest_dir, type, lib_install_prefix)
# @description Installs the specified library into the destination folder, automatically changes the references to dependencies
# @param src_lib     source library to copy to the destination directory
# @param dest_dir    destination directory where the src_lib should be copied to
vlc_install() {
142 143
    local src_dir=$1
    local src=$2
144 145 146 147
    local dest_dir=$3
    local type=$4

    if test "$use_archs" = "no"; then
148
        vlc_install_object "$VLC_BUILD_DIR/$src_dir/$src" "$dest_dir" "$type" $5
149 150
    else
        if test $type = "data"; then
151
            vlc_install_object "$main_build_dir/$src_dir/$src" "$dest_dir" "$type" $5
152
        else
153
            local fatdest="$dest_dir/$2"
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
            local shouldUpdate="no"

            # Determine what architectures are available in the destination image
            local fatdest_archs=""
            if [ -e ${fatdest} ]; then
                fatdest_archs=`lipo -info "${fatdest}" 2> /dev/null | sed -E -e 's/[[:space:]]+$//' -e 's/.+:[[:space:]]*//' -e 's/[^[:space:]]+/(&)/g'`

                # Check to see if the destination image needs to be reconstructed
                for arch in $ARCHS; do
                    # Only install if the new image is newer than the one we have installed or the required arch is missing.
                    if test $shouldUpdate = "no"  && (! [[ "$fatdest_archs" =~ \($arch\) ]] || test "$VLC_BUILD_DIR/$arch/$src_dir/$src" -nt "${fatdest}"); then
                        shouldUpdate="yes"
                    fi
                    fatdest_archs=${fatdest_archs//\($arch\)/}
                done

                # Reconstruct the destination image, if the update flag is set or if there are more archs in the desintation then we need
                fatdest_archs=${fatdest_archs// /}
            else
                # If the destination image does not exist, then we have to reconstruct it
                shouldUpdate="yes"
            fi
176

177 178 179 180 181 182
            # If we should update the destination image or if there were unexpected archs in the destination image, then reconstruct it
            if test "$shouldUpdate" = "yes" || test -n "${fatdest_archs}"; then
                # If the destination image exists, get rid of it so we can copy over the newly constructed image
                if test -e ${fatdest}; then
                    rm "$fatdest"
                fi
183

184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
                if test "$num_archs" = "1"; then
                    echo "Copying $ARCHS $type $fatdest"
                    local arch_src="$VLC_BUILD_DIR/$ARCHS/$src_dir/$src"
                    vlc_install_object "$arch_src" "$dest_dir" "$type" "$5" ""
                else
                    # Create a temporary destination dir to store each ARCH object file
                    local tmp_dest_dir="$VLC_BUILD_DIR/tmp/$type"
                    rm -Rf "${tmp_dest_dir}/*"
                    mkdir -p "$tmp_dest_dir"

                    # Search for each ARCH object file used to construct a fat image
                    local objects=""
                    for arch in $ARCHS; do
                        local arch_src="$VLC_BUILD_DIR/$arch/$src_dir/$src"
                        vlc_install_object "$arch_src" "$tmp_dest_dir" "$type" "$5" "" ".$arch"
                        local dest="$tmp_dest_dir/$src.$arch"
                        if [ -e ${dest} ]; then
                            objects="${dest} $objects"
                        else
                            echo "Warning: building $arch_src without $arch"
                        fi
                    done;
206

207 208 209
                    echo "Creating fat $type $fatdest"
                    lipo $objects -output "$fatdest" -create
                fi
210 211 212 213 214 215 216
            fi
        fi
    fi
}
# @function vlc_install
##########################

217 218 219
##########################
# Create a symbolic link in the root of the framework
mkdir -p ${target_lib}
220
mkdir -p ${target_plugins}
221 222 223 224 225 226 227
mkdir -p ${target_bin}

if [ "$RELEASE_MAKEFILE" != "yes" ] ; then
    pushd `pwd` > /dev/null
    cd ${TARGET_BUILD_DIR}/${FULL_PRODUCT_NAME}

    ln -sf Versions/Current/${lib} .
228
    ln -sf Versions/Current/${plugins} .
229 230 231
    ln -sf Versions/Current/${include} .
    ln -sf Versions/Current/${share} .
    ln -sf Versions/Current/bin .
232
    ln -sf ../plugins Versions/Current/bin
233 234 235 236 237
    ln -sf ../share Versions/Current/bin

    popd > /dev/null
fi

238
##########################
239 240
# Hack for VLC.app
if [ "$FULL_PRODUCT_NAME" = "VLC.app" ] ; then
241
    vlc_install "bin/${prefix}" "vlc" "${target}" "bin" "@loader_path/lib"
242 243 244 245
    mv ${target}/vlc ${target}/VLC
    chmod +x ${target}/VLC
elif [ "$FULL_PRODUCT_NAME" = "VLC-Plugin.plugin" ] ; then
    # install Safari webplugin
246
    vlc_install "projects/mozilla/${prefix}" "npvlc.${suffix}" "${target}" "lib" "@loader_path/lib"
247 248 249
    mv ${target}/npvlc.${suffix} "${target}/VLC Plugin"
    chmod +x "${target}/VLC Plugin"
else
250
    vlc_install "bin/${prefix}" "vlc" "${target}/bin" "bin" "@loader_path/../lib"
251
fi
252

253
##########################
254 255 256
# Build the plugins folder (Same as VLCKit.framework/plugins in Makefile)
echo "Building plugins folder..."
# Figure out what plugins are available to install
257
for module in `find ${main_build_dir}/modules -path "*dylib.dSYM*" -prune -o -name "lib*_plugin.dylib" -print | sed -e s:${main_build_dir}/::` ; do
258 259
    # Check to see that the reported module actually exists
    if test -n ${module}; then
260
        vlc_install `dirname ${module}` `basename ${module}` ${target_plugins} "module"
261 262
    fi
done
263

264
##########################
265
# Build the lib folder
266
vlc_install "lib/${prefix}" "libvlc.5.dylib" "${target_lib}" "library"
267
vlc_install "src/${prefix}" "libvlccore.8.dylib" "${target_lib}" "library"
268
pushd `pwd` > /dev/null
269
cd ${target_lib}
270
ln -sf libvlc.5.dylib libvlc.dylib
271
ln -sf libvlccore.8.dylib libvlccore.dylib
272 273 274 275
popd > /dev/null

##########################
# Build the share folder
276 277 278 279 280 281 282 283
echo "Building share folder..."
echo ${VLC_BUILD_DIR}
pbxcp="cp -R -L"
mkdir -p ${target_share}
if test "$use_archs" = "no"; then
    $pbxcp ${VLC_BUILD_DIR}/share/lua ${target_share}
else
    $pbxcp ${main_build_dir}/share/lua ${target_share}
284
fi