Pre-Compile.sh 11 KB

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