1#!/bin/bash
2#
3# This script generates 'WebP.framework' and 'WebPDecoder.framework'. An iOS
4# app can decode WebP images by including 'WebPDecoder.framework' and both
5# encode and decode WebP images by including 'WebP.framework'.
6#
7# Run ./iosbuild.sh to generate the frameworks under the current directory
8# (the previous build will be erased if it exists).
9#
10# This script is inspired by the build script written by Carson McDonald.
11# (http://www.ioncannon.net/programming/1483/using-webp-to-reduce-native-ios-app-size/).
12
13set -e
14
15# Extract the latest SDK version from the final field of the form: iphoneosX.Y
16readonly SDK=$(xcodebuild -showsdks \
17  | grep iphoneos | sort | tail -n 1 | awk '{print substr($NF, 9)}'
18)
19# Extract Xcode version.
20readonly XCODE=$(xcodebuild -version | grep Xcode | cut -d " " -f2)
21if [[ -z "${XCODE}" ]]; then
22  echo "Xcode not available"
23  exit 1
24fi
25
26readonly OLDPATH=${PATH}
27
28# Add iPhoneOS-V6 to the list of platforms below if you need armv6 support.
29# Note that iPhoneOS-V6 support is not available with the iOS6 SDK.
30PLATFORMS="iPhoneSimulator iPhoneSimulator64"
31PLATFORMS+=" iPhoneOS-V7 iPhoneOS-V7s iPhoneOS-V7-arm64"
32readonly PLATFORMS
33readonly SRCDIR=$(dirname $0)
34readonly TOPDIR=$(pwd)
35readonly BUILDDIR="${TOPDIR}/iosbuild"
36readonly TARGETDIR="${TOPDIR}/WebP.framework"
37readonly DECTARGETDIR="${TOPDIR}/WebPDecoder.framework"
38readonly DEVELOPER=$(xcode-select --print-path)
39readonly PLATFORMSROOT="${DEVELOPER}/Platforms"
40readonly LIPO=$(xcrun -sdk iphoneos${SDK} -find lipo)
41LIBLIST=''
42DECLIBLIST=''
43
44if [[ -z "${SDK}" ]]; then
45  echo "iOS SDK not available"
46  exit 1
47elif [[ ${SDK%%.*} -gt 8 ]]; then
48  EXTRA_CFLAGS="-fembed-bitcode"
49elif [[ ${SDK} < 6.0 ]]; then
50  echo "You need iOS SDK version 6.0 or above"
51  exit 1
52else
53  echo "iOS SDK Version ${SDK}"
54fi
55
56rm -rf ${BUILDDIR} ${TARGETDIR} ${DECTARGETDIR}
57mkdir -p ${BUILDDIR} ${TARGETDIR}/Headers/ ${DECTARGETDIR}/Headers/
58
59if [[ ! -e ${SRCDIR}/configure ]]; then
60  if ! (cd ${SRCDIR} && sh autogen.sh); then
61    cat <<EOT
62Error creating configure script!
63This script requires the autoconf/automake and libtool to build. MacPorts can
64be used to obtain these:
65http://www.macports.org/install.php
66EOT
67    exit 1
68  fi
69fi
70
71for PLATFORM in ${PLATFORMS}; do
72  ARCH2=""
73  if [[ "${PLATFORM}" == "iPhoneOS-V7-arm64" ]]; then
74    PLATFORM="iPhoneOS"
75    ARCH="aarch64"
76    ARCH2="arm64"
77  elif [[ "${PLATFORM}" == "iPhoneOS-V7s" ]]; then
78    PLATFORM="iPhoneOS"
79    ARCH="armv7s"
80  elif [[ "${PLATFORM}" == "iPhoneOS-V7" ]]; then
81    PLATFORM="iPhoneOS"
82    ARCH="armv7"
83  elif [[ "${PLATFORM}" == "iPhoneOS-V6" ]]; then
84    PLATFORM="iPhoneOS"
85    ARCH="armv6"
86  elif [[ "${PLATFORM}" == "iPhoneSimulator64" ]]; then
87    PLATFORM="iPhoneSimulator"
88    ARCH="x86_64"
89  else
90    ARCH="i386"
91  fi
92
93  ROOTDIR="${BUILDDIR}/${PLATFORM}-${SDK}-${ARCH}"
94  mkdir -p "${ROOTDIR}"
95
96  DEVROOT="${DEVELOPER}/Toolchains/XcodeDefault.xctoolchain"
97  SDKROOT="${PLATFORMSROOT}/"
98  SDKROOT+="${PLATFORM}.platform/Developer/SDKs/${PLATFORM}${SDK}.sdk/"
99  CFLAGS="-arch ${ARCH2:-${ARCH}} -pipe -isysroot ${SDKROOT} -O3 -DNDEBUG"
100  CFLAGS+=" -miphoneos-version-min=6.0 ${EXTRA_CFLAGS}"
101
102  set -x
103  export PATH="${DEVROOT}/usr/bin:${OLDPATH}"
104  ${SRCDIR}/configure --host=${ARCH}-apple-darwin --prefix=${ROOTDIR} \
105    --build=$(${SRCDIR}/config.guess) \
106    --disable-shared --enable-static \
107    --enable-libwebpdecoder --enable-swap-16bit-csp \
108    CFLAGS="${CFLAGS}"
109  set +x
110
111  # run make only in the src/ directory to create libwebp.a/libwebpdecoder.a
112  cd src/
113  make V=0
114  make install
115
116  LIBLIST+=" ${ROOTDIR}/lib/libwebp.a"
117  DECLIBLIST+=" ${ROOTDIR}/lib/libwebpdecoder.a"
118
119  make clean
120  cd ..
121
122  export PATH=${OLDPATH}
123done
124
125cp -a ${SRCDIR}/src/webp/{decode,encode,types}.h ${TARGETDIR}/Headers/
126${LIPO} -create ${LIBLIST} -output ${TARGETDIR}/WebP
127
128cp -a ${SRCDIR}/src/webp/{decode,types}.h ${DECTARGETDIR}/Headers/
129${LIPO} -create ${DECLIBLIST} -output ${DECTARGETDIR}/WebPDecoder
130