]> git.sesse.net Git - vlc/blob - extras/package/macosx/build-package.sh
macosx: implemented A->B Loop (close #5187)
[vlc] / extras / package / macosx / build-package.sh
1 #!/bin/sh
2 #
3 # build-package.sh
4 #
5 # Script that installs libvlc to VLC.app
6
7 # We are building VLC.app or the moz plugin
8 #
9 if test "${ACTION}" = "release-makefile"; then
10     echo "running build-package.sh in release-makefile mode"
11
12     FULL_PRODUCT_NAME="${PRODUCT}"
13     if [ "$FULL_PRODUCT_NAME" = "VLC-Plugin.plugin" ] ; then
14         TARGET_BUILD_DIR="${src_dir}"
15     else
16         TARGET_BUILD_DIR="${build_dir}"
17     fi
18     CONTENTS_FOLDER_PATH="${FULL_PRODUCT_NAME}/Contents/MacOS"
19     VLC_BUILD_DIR="${build_dir}"
20     VLC_SRC_DIR="${src_dir}"
21     ACTION="build"
22     RELEASE_MAKEFILE="yes"
23     use_archs="no"
24     main_build_dir="${VLC_BUILD_DIR}"
25 else
26     use_archs="yes"
27     main_build_dir="${VLC_BUILD_DIR}/${ARCHS%% *}"
28     echo "Building for $ARCHS"
29 fi
30
31 if test "${ACTION}" = "clean"; then
32     rm -Rf "${VLC_BUILD_DIR}/tmp"
33     exit 0
34 fi
35
36 if test "${ACTION}" != "build"; then
37     echo "This script is supposed to run from xcodebuild or Xcode"
38     exit 1
39 fi
40
41 lib="lib"
42 plugins="plugins"
43 share="share"
44 include="include"
45 target="${TARGET_BUILD_DIR}/${CONTENTS_FOLDER_PATH}"
46 target_bin="${target}/bin"
47 target_lib="${target}/${lib}"            # Should we consider using a different well-known folder like shared resources?
48 target_plugins="${target}/${plugins}"    # Should we consider using a different well-known folder like shared resources?
49 target_share="${target}/${share}"        # Should we consider using a different well-known folder like shared resources?
50 linked_libs=""
51 prefix=".libs"
52 suffix="dylib"
53 num_archs=$(echo `echo $ARCHS | wc -w`)
54
55 ##########################
56 # @function vlc_install_object(src_lib, dest_dir, type, lib_install_prefix, destination_name, suffix)
57 # @description Installs the specified library into the destination folder, automatically changes the references to dependencies
58 # @param src_lib     source library to copy to the destination directory
59 # @param dest_dir    destination directory where the src_lib should be copied to
60 vlc_install_object() {
61     local src_lib=${1}
62     local dest_dir=${2}
63     local type=${3}
64     local lib_install_prefix=${4}
65     local destination_name=${5}
66     local suffix=${6}
67
68     if [ $type = "library" ]; then
69         local install_name="@loader_path/lib"
70     elif [ $type = "module" ]; then
71         local install_name="@loader_path/plugins"
72     fi
73     if [ "$destination_name" != "" ]; then
74         local lib_dest="$dest_dir/$destination_name$suffix"
75         local lib_name=`basename $destination_name`
76     else
77         local lib_dest="$dest_dir/`basename $src_lib`$suffix"
78         local lib_name=`basename $src_lib`
79     fi
80
81     if [ "x$lib_install_prefix" != "x" ]; then
82         local lib_install_prefix="$lib_install_prefix"
83     else
84         local lib_install_prefix="@loader_path/../lib"
85     fi
86
87     if test ! -e ${src_lib}; then
88         return
89     fi
90
91     if ( (test ! -e ${lib_dest}) || test ${src_lib} -nt ${lib_dest} ); then
92
93         mkdir -p ${dest_dir}
94
95         # Lets copy the library from the source folder to our new destination folder
96         if [ "${type}" = "bin" ]; then
97             install -m 755 ${src_lib} ${lib_dest}
98         else
99             install -m 644 ${src_lib} ${lib_dest}
100         fi
101
102         # Update the dynamic library so it will know where to look for the other libraries
103         echo "Installing ${type} `basename ${lib_dest}`"
104
105         if [ "${type}" = "library" ]; then
106             # Change the reference of libvlc.1 stored in the usr directory to libvlc.dylib in the framework's library directory
107             install_name_tool -id "${install_name}/${lib_name}" ${lib_dest} > /dev/null
108         fi
109
110         if [ "${type}" != "data" ]; then
111             # Iterate through each installed library and modify the references to other dynamic libraries to match the framework's library directory
112             for linked_lib in `otool -L ${lib_dest}  | grep '(' | sed 's/\((.*)\)//'`; do
113                 local name=`basename ${linked_lib}`
114                 case "${linked_lib}" in
115                     */vlc_build_dir/* | */vlc_install_dir/* | *vlc* | */extras/contrib/*)
116                         if test -e ${linked_lib}; then
117                             install_name_tool -change "$linked_lib" "${lib_install_prefix}/${name}" "${lib_dest}"
118                             linked_libs="${linked_libs} ${ref_lib}"
119                             vlc_install_object ${linked_lib} ${target_lib} "library"
120                         fi
121                         ;;
122                 esac
123             done
124         fi
125      fi
126 }
127 # @function vlc_install_object
128 ##########################
129
130 ##########################
131 # @function vlc_install(src_lib_dir, src_lib_name, dest_dir, type, lib_install_prefix)
132 # @description Installs the specified library into the destination folder, automatically changes the references to dependencies
133 # @param src_lib     source library to copy to the destination directory
134 # @param dest_dir    destination directory where the src_lib should be copied to
135 vlc_install() {
136     local src_dir=$1
137     local src=$2
138     local dest_dir=$3
139     local type=$4
140
141     if test "$use_archs" = "no"; then
142         vlc_install_object "$VLC_BUILD_DIR/$src_dir/$src" "$dest_dir" "$type" $5
143     else
144         if test $type = "data"; then
145             vlc_install_object "$main_build_dir/$src_dir/$src" "$dest_dir" "$type" $5
146         else
147             local fatdest="$dest_dir/$2"
148             local shouldUpdate="no"
149
150             # Determine what architectures are available in the destination image
151             local fatdest_archs=""
152             if [ -e ${fatdest} ]; then
153                 fatdest_archs=`lipo -info "${fatdest}" 2> /dev/null | sed -E -e 's/[[:space:]]+$//' -e 's/.+:[[:space:]]*//' -e 's/[^[:space:]]+/(&)/g'`
154
155                 # Check to see if the destination image needs to be reconstructed
156                 for arch in $ARCHS; do
157                     # Only install if the new image is newer than the one we have installed or the required arch is missing.
158                     if test $shouldUpdate = "no"  && (! [[ "$fatdest_archs" =~ \($arch\) ]] || test "$VLC_BUILD_DIR/$arch/$src_dir/$src" -nt "${fatdest}"); then
159                         shouldUpdate="yes"
160                     fi
161                     fatdest_archs=${fatdest_archs//\($arch\)/}
162                 done
163
164                 # Reconstruct the destination image, if the update flag is set or if there are more archs in the desintation then we need
165                 fatdest_archs=${fatdest_archs// /}
166             else
167                 # If the destination image does not exist, then we have to reconstruct it
168                 shouldUpdate="yes"
169             fi
170
171             # If we should update the destination image or if there were unexpected archs in the destination image, then reconstruct it
172             if test "$shouldUpdate" = "yes" || test -n "${fatdest_archs}"; then
173                 # If the destination image exists, get rid of it so we can copy over the newly constructed image
174                 if test -e ${fatdest}; then
175                     rm "$fatdest"
176                 fi
177
178                 if test "$num_archs" = "1"; then
179                     echo "Copying $ARCHS $type $fatdest"
180                     local arch_src="$VLC_BUILD_DIR/$ARCHS/$src_dir/$src"
181                     vlc_install_object "$arch_src" "$dest_dir" "$type" "$5" ""
182                 else
183                     # Create a temporary destination dir to store each ARCH object file
184                     local tmp_dest_dir="$VLC_BUILD_DIR/tmp/$type"
185                     rm -Rf "${tmp_dest_dir}/*"
186                     mkdir -p "$tmp_dest_dir"
187
188                     # Search for each ARCH object file used to construct a fat image
189                     local objects=""
190                     for arch in $ARCHS; do
191                         local arch_src="$VLC_BUILD_DIR/$arch/$src_dir/$src"
192                         vlc_install_object "$arch_src" "$tmp_dest_dir" "$type" "$5" "" ".$arch"
193                         local dest="$tmp_dest_dir/$src.$arch"
194                         if [ -e ${dest} ]; then
195                             objects="${dest} $objects"
196                         else
197                             echo "Warning: building $arch_src without $arch"
198                         fi
199                     done;
200
201                     echo "Creating fat $type $fatdest"
202                     lipo $objects -output "$fatdest" -create
203                 fi
204             fi
205         fi
206     fi
207 }
208 # @function vlc_install
209 ##########################
210
211 ##########################
212 # Create a symbolic link in the root of the framework
213 mkdir -p ${target_lib}
214 mkdir -p ${target_plugins}
215 mkdir -p ${target_bin}
216
217 if [ "$RELEASE_MAKEFILE" != "yes" ] ; then
218     pushd `pwd` > /dev/null
219     cd ${TARGET_BUILD_DIR}/${FULL_PRODUCT_NAME}
220
221     ln -sf Versions/Current/${lib} .
222     ln -sf Versions/Current/${plugins} .
223     ln -sf Versions/Current/${include} .
224     ln -sf Versions/Current/${share} .
225     ln -sf Versions/Current/bin .
226     ln -sf ../plugins Versions/Current/bin
227     ln -sf ../share Versions/Current/bin
228
229     popd > /dev/null
230 fi
231
232 ##########################
233 # Hack for VLC.app
234 if [ "$FULL_PRODUCT_NAME" = "VLC.app" ] ; then
235     vlc_install "bin/${prefix}" "vlc" "${target}" "bin" "@loader_path/lib"
236     mv ${target}/vlc ${target}/VLC
237     chmod +x ${target}/VLC
238 elif [ "$FULL_PRODUCT_NAME" = "VLC-Plugin.plugin" ] ; then
239     # install Safari webplugin
240     vlc_install "projects/mozilla/${prefix}" "npvlc.${suffix}" "${target}" "lib" "@loader_path/lib"
241     mv ${target}/npvlc.${suffix} "${target}/VLC Plugin"
242     chmod +x "${target}/VLC Plugin"
243 else
244     vlc_install "bin/${prefix}" "vlc" "${target}/bin" "bin" "@loader_path/../lib"
245 fi
246
247 ##########################
248 # Build the plugins folder
249 echo "Building plugins folder..."
250 # Figure out what plugins are available to install
251 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
252     # Check to see that the reported module actually exists
253     if test -n ${module}; then
254         vlc_install `dirname ${module}` `basename ${module}` ${target_plugins} "module"
255     fi
256 done
257
258 ##########################
259 # Build the lib folder
260 vlc_install "lib/${prefix}" "libvlc.5.dylib" "${target_lib}" "library"
261 vlc_install "src/${prefix}" "libvlccore.5.dylib" "${target_lib}" "library"
262 pushd `pwd` > /dev/null
263 cd ${target_lib}
264 ln -sf libvlc.5.dylib libvlc.dylib
265 ln -sf libvlccore.5.dylib libvlccore.dylib
266 popd > /dev/null
267
268 ##########################
269 # Build the share folder
270 if [ $PRODUCT != "VLC.app" ]; then
271     echo "Building share folder..."
272     pbxcp="/Developer/Library/PrivateFrameworks/DevToolsCore.framework/Resources/pbxcp -exclude .DS_Store -resolve-src-symlinks -v -V"
273     mkdir -p ${target_share}
274     if test "$use_archs" = "no"; then
275         $pbxcp ${VLC_BUILD_DIR}/share/lua ${target_share}
276     else
277         $pbxcp ${main_build_dir}/share/lua ${target_share}
278     fi
279 fi