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