Pre-Compile.sh 11 KB

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