iosbuild.sh 7.97 KB
Newer Older
Tom Finegan's avatar
Tom Finegan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#!/bin/sh
##
##  Copyright (c) 2014 The WebM project authors. All Rights Reserved.
##
##  Use of this source code is governed by a BSD-style license
##  that can be found in the LICENSE file in the root of the source
##  tree. An additional intellectual property rights grant can be found
##  in the file PATENTS.  All contributing project authors may
##  be found in the AUTHORS file in the root of the source tree.
##
##
## This script generates 'VPX.framework'. An iOS app can encode and decode VPx
## video by including 'VPX.framework'.
##
## Run iosbuild.sh to create 'VPX.framework' in the current directory.
##
set -e
devnull='> /dev/null 2>&1'

BUILD_ROOT="_iosbuild"
21 22 23 24
CONFIGURE_ARGS="--disable-docs
                --disable-examples
                --disable-libyuv
                --disable-unit-tests"
Tom Finegan's avatar
Tom Finegan committed
25 26 27
DIST_DIR="_dist"
FRAMEWORK_DIR="VPX.framework"
HEADER_DIR="${FRAMEWORK_DIR}/Headers/vpx"
28
SCRIPT_DIR=$(dirname "$0")
Yaowu Xu's avatar
Yaowu Xu committed
29
LIBAOM_SOURCE_DIR=$(cd ${SCRIPT_DIR}/../..; pwd)
Tom Finegan's avatar
Tom Finegan committed
30 31
LIPO=$(xcrun -sdk iphoneos${SDK} -find lipo)
ORIG_PWD="$(pwd)"
32 33 34 35 36 37 38 39
ARM_TARGETS="arm64-darwin-gcc
             armv7-darwin-gcc
             armv7s-darwin-gcc"
SIM_TARGETS="x86-iphonesimulator-gcc
             x86_64-iphonesimulator-gcc"
OSX_TARGETS="x86-darwin15-gcc
             x86_64-darwin15-gcc"
TARGETS="${ARM_TARGETS} ${SIM_TARGETS}"
Tom Finegan's avatar
Tom Finegan committed
40

41 42
# Configures for the target specified by $1, and invokes make with the dist
# target using $DIST_DIR as the distribution output directory.
Tom Finegan's avatar
Tom Finegan committed
43 44 45
build_target() {
  local target="$1"
  local old_pwd="$(pwd)"
46
  local target_specific_flags=""
Tom Finegan's avatar
Tom Finegan committed
47 48 49

  vlog "***Building target: ${target}***"

50 51 52 53 54 55 56
  case "${target}" in
    x86-*)
      target_specific_flags="--enable-pic"
      vlog "Enabled PIC for ${target}"
      ;;
  esac

Tom Finegan's avatar
Tom Finegan committed
57 58
  mkdir "${target}"
  cd "${target}"
Yaowu Xu's avatar
Yaowu Xu committed
59
  eval "${LIBAOM_SOURCE_DIR}/configure" --target="${target}" \
60 61
    ${CONFIGURE_ARGS} ${EXTRA_CONFIGURE_ARGS} ${target_specific_flags} \
    ${devnull}
Tom Finegan's avatar
Tom Finegan committed
62
  export DIST_DIR
63
  eval make dist ${devnull}
Tom Finegan's avatar
Tom Finegan committed
64 65 66 67 68
  cd "${old_pwd}"

  vlog "***Done building target: ${target}***"
}

69 70 71 72
# Returns the preprocessor symbol for the target specified by $1.
target_to_preproc_symbol() {
  target="$1"
  case "${target}" in
73 74 75
    arm64-*)
      echo "__aarch64__"
      ;;
76
    armv7-*)
77
      echo "__ARM_ARCH_7A__"
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
      ;;
    armv7s-*)
      echo "__ARM_ARCH_7S__"
      ;;
    x86-*)
      echo "__i386__"
      ;;
    x86_64-*)
      echo "__x86_64__"
      ;;
    *)
      echo "#error ${target} unknown/unsupported"
      return 1
      ;;
  esac
}

# Create a vpx_config.h shim that, based on preprocessor settings for the
# current target CPU, includes the real vpx_config.h for the current target.
# $1 is the list of targets.
create_vpx_framework_config_shim() {
  local targets="$1"
  local config_file="${HEADER_DIR}/vpx_config.h"
  local preproc_symbol=""
  local target=""
  local include_guard="VPX_FRAMEWORK_HEADERS_VPX_VPX_CONFIG_H_"

  local file_header="/*
 *  Copyright (c) $(date +%Y) The WebM project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

/* GENERATED FILE: DO NOT EDIT! */

#ifndef ${include_guard}
#define ${include_guard}

#if defined"

  printf "%s" "${file_header}" > "${config_file}"
  for target in ${targets}; do
    preproc_symbol=$(target_to_preproc_symbol "${target}")
    printf " ${preproc_symbol}\n" >> "${config_file}"
126
    printf "#define VPX_FRAMEWORK_TARGET \"${target}\"\n" >> "${config_file}"
127 128 129 130 131 132 133 134 135 136 137 138 139
    printf "#include \"VPX/vpx/${target}/vpx_config.h\"\n" >> "${config_file}"
    printf "#elif defined" >> "${config_file}"
    mkdir "${HEADER_DIR}/${target}"
    cp -p "${BUILD_ROOT}/${target}/vpx_config.h" "${HEADER_DIR}/${target}"
  done

  # Consume the last line of output from the loop: We don't want it.
  sed -i '' -e '$d' "${config_file}"

  printf "#endif\n\n" >> "${config_file}"
  printf "#endif  // ${include_guard}" >> "${config_file}"
}

140 141
# Configures and builds each target specified by $1, and then builds
# VPX.framework.
142
build_framework() {
143
  local lib_list=""
Tom Finegan's avatar
Tom Finegan committed
144
  local targets="$1"
145 146
  local target=""
  local target_dist_dir=""
Tom Finegan's avatar
Tom Finegan committed
147 148 149 150 151 152 153 154 155 156 157 158

  # Clean up from previous build(s).
  rm -rf "${BUILD_ROOT}" "${FRAMEWORK_DIR}"

  # Create output dirs.
  mkdir -p "${BUILD_ROOT}"
  mkdir -p "${HEADER_DIR}"

  cd "${BUILD_ROOT}"

  for target in ${targets}; do
    build_target "${target}"
159
    target_dist_dir="${BUILD_ROOT}/${target}/${DIST_DIR}"
Yaowu Xu's avatar
Yaowu Xu committed
160
    lib_list="${lib_list} ${target_dist_dir}/lib/libaom.a"
Tom Finegan's avatar
Tom Finegan committed
161 162 163
  done

  cd "${ORIG_PWD}"
164

Yaowu Xu's avatar
Yaowu Xu committed
165
  # The basic libaom API includes are all the same; just grab the most recent
166
  # set.
167
  cp -p "${target_dist_dir}"/include/vpx/* "${HEADER_DIR}"
168 169

  # Build the fat library.
170 171
  ${LIPO} -create ${lib_list} -output ${FRAMEWORK_DIR}/VPX

172 173 174 175 176 177 178
  # Create the vpx_config.h shim that allows usage of vpx_config.h from
  # within VPX.framework.
  create_vpx_framework_config_shim "${targets}"

  # Copy in vpx_version.h.
  cp -p "${BUILD_ROOT}/${target}/vpx_version.h" "${HEADER_DIR}"

179 180 181 182 183 184 185
  vlog "Created fat library ${FRAMEWORK_DIR}/VPX containing:"
  for lib in ${lib_list}; do
    vlog "  $(echo ${lib} | awk -F / '{print $2, $NF}')"
  done

  # TODO(tomfinegan): Verify that expected targets are included within
  # VPX.framework/VPX via lipo -info.
Tom Finegan's avatar
Tom Finegan committed
186 187
}

188 189
# Trap function. Cleans up the subtree used to build all targets contained in
# $TARGETS.
Tom Finegan's avatar
Tom Finegan committed
190
cleanup() {
191
  local readonly res=$?
Tom Finegan's avatar
Tom Finegan committed
192 193
  cd "${ORIG_PWD}"

194 195 196 197
  if [ $res -ne 0 ]; then
    elog "build exited with error ($res)"
  fi

Tom Finegan's avatar
Tom Finegan committed
198 199 200 201 202
  if [ "${PRESERVE_BUILD_OUTPUT}" != "yes" ]; then
    rm -rf "${BUILD_ROOT}"
  fi
}

203 204 205 206 207 208 209 210 211
print_list() {
  local indent="$1"
  shift
  local list="$@"
  for entry in ${list}; do
    echo "${indent}${entry}"
  done
}

Tom Finegan's avatar
Tom Finegan committed
212 213 214 215
iosbuild_usage() {
cat << EOF
  Usage: ${0##*/} [arguments]
    --help: Display this message and exit.
Yaowu Xu's avatar
Yaowu Xu committed
216
    --extra-configure-args <args>: Extra args to pass when configuring libaom.
217 218 219
    --macosx: Uses darwin15 targets instead of iphonesimulator targets for x86
              and x86_64. Allows linking to framework when builds target MacOSX
              instead of iOS.
Tom Finegan's avatar
Tom Finegan committed
220 221
    --preserve-build-output: Do not delete the build directory.
    --show-build-output: Show output from each library build.
222
    --targets <targets>: Override default target list. Defaults:
223
$(print_list "        " ${TARGETS})
224 225
    --test-link: Confirms all targets can be linked. Functionally identical to
                 passing --enable-examples via --extra-configure-args.
Tom Finegan's avatar
Tom Finegan committed
226 227 228 229 230
    --verbose: Output information about the environment and each stage of the
               build.
EOF
}

231 232 233 234
elog() {
  echo "${0##*/} failed because: $@" 1>&2
}

Tom Finegan's avatar
Tom Finegan committed
235 236 237 238 239 240 241 242 243 244 245
vlog() {
  if [ "${VERBOSE}" = "yes" ]; then
    echo "$@"
  fi
}

trap cleanup EXIT

# Parse the command line.
while [ -n "$1" ]; do
  case "$1" in
246 247 248 249
    --extra-configure-args)
      EXTRA_CONFIGURE_ARGS="$2"
      shift
      ;;
Tom Finegan's avatar
Tom Finegan committed
250 251 252 253 254 255 256 257 258 259
    --help)
      iosbuild_usage
      exit
      ;;
    --preserve-build-output)
      PRESERVE_BUILD_OUTPUT=yes
      ;;
    --show-build-output)
      devnull=
      ;;
260 261 262
    --test-link)
      EXTRA_CONFIGURE_ARGS="${EXTRA_CONFIGURE_ARGS} --enable-examples"
      ;;
263 264 265 266
    --targets)
      TARGETS="$2"
      shift
      ;;
267 268 269
    --macosx)
      TARGETS="${ARM_TARGETS} ${OSX_TARGETS}"
      ;;
Tom Finegan's avatar
Tom Finegan committed
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
    --verbose)
      VERBOSE=yes
      ;;
    *)
      iosbuild_usage
      exit 1
      ;;
  esac
  shift
done

if [ "${VERBOSE}" = "yes" ]; then
cat << EOF
  BUILD_ROOT=${BUILD_ROOT}
  DIST_DIR=${DIST_DIR}
285
  CONFIGURE_ARGS=${CONFIGURE_ARGS}
286
  EXTRA_CONFIGURE_ARGS=${EXTRA_CONFIGURE_ARGS}
Tom Finegan's avatar
Tom Finegan committed
287 288
  FRAMEWORK_DIR=${FRAMEWORK_DIR}
  HEADER_DIR=${HEADER_DIR}
Yaowu Xu's avatar
Yaowu Xu committed
289
  LIBAOM_SOURCE_DIR=${LIBAOM_SOURCE_DIR}
Tom Finegan's avatar
Tom Finegan committed
290
  LIPO=${LIPO}
291
  MAKEFLAGS=${MAKEFLAGS}
Tom Finegan's avatar
Tom Finegan committed
292
  ORIG_PWD=${ORIG_PWD}
293
  PRESERVE_BUILD_OUTPUT=${PRESERVE_BUILD_OUTPUT}
294 295 296
  TARGETS="$(print_list "" ${TARGETS})"
  OSX_TARGETS="${OSX_TARGETS}"
  SIM_TARGETS="${SIM_TARGETS}"
Tom Finegan's avatar
Tom Finegan committed
297 298 299
EOF
fi

300
build_framework "${TARGETS}"
301
echo "Successfully built '${FRAMEWORK_DIR}' for:"
302
print_list "" ${TARGETS}