summaryrefslogtreecommitdiff
path: root/media/libaom/src/build/cmake/iosbuild.sh
blob: 167ece200a3a5b40954044e5b29ce4935fa10f7c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#!/bin/sh
## Copyright (c) 2016, Alliance for Open Media. All rights reserved
##
## This source code is subject to the terms of the BSD 2 Clause License and
## the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
## was not distributed with this source code in the LICENSE file, you can
## obtain it at www.aomedia.org/license/software. If the Alliance for Open
## Media Patent License 1.0 was not distributed with this source code in the
## PATENTS file, you can obtain it at www.aomedia.org/license/patent.
##
## This script generates 'AOM.framework'. An iOS app can encode and decode AVx
## video by including 'AOM.framework'.
##
## Run iosbuild.sh to create 'AOM.framework' in the current directory.
##
set -e
devnull='> /dev/null 2>&1'

BUILD_ROOT="_iosbuild"
CONFIGURE_ARGS="--disable-docs
                --disable-examples
                --disable-libyuv
                --disable-unit-tests"
DIST_DIR="_dist"
FRAMEWORK_DIR="AOM.framework"
FRAMEWORK_LIB="AOM.framework/AOM"
HEADER_DIR="${FRAMEWORK_DIR}/Headers/aom"
SCRIPT_DIR=$(dirname "$0")
LIBAOM_SOURCE_DIR=$(cd ${SCRIPT_DIR}/../..; pwd)
LIPO=$(xcrun -sdk iphoneos${SDK} -find lipo)
ORIG_PWD="$(pwd)"
ARM_TARGETS="arm64-darwin-gcc
             armv7-darwin-gcc
             armv7s-darwin-gcc"
SIM_TARGETS="x86-iphonesimulator-gcc
             x86_64-iphonesimulator-gcc"
OSX_TARGETS="x86-darwin16-gcc
             x86_64-darwin16-gcc"
TARGETS="${ARM_TARGETS} ${SIM_TARGETS}"

# Configures for the target specified by $1, and invokes make with the dist
# target using $ as the distribution output directory.
build_target() {
  local target="$1"
  local old_pwd="$(pwd)"
  local target_specific_flags=""

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

  case "${target}" in
    x86-*)
      target_specific_flags="--enable-pic"
      vlog "Enabled PIC for ${target}"
      ;;
  esac

  mkdir "${target}"
  cd "${target}"
  # TODO(tomfinegan@google.com): switch to cmake.
  eval "${LIBAOM_SOURCE_DIR}/configure" --target="${target}" \
    ${CONFIGURE_ARGS} ${EXTRA_CONFIGURE_ARGS} ${target_specific_flags} \
    ${devnull}
  export DIST_DIR
  eval make dist ${devnull}
  cd "${old_pwd}"

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

# Returns the preprocessor symbol for the target specified by $1.
target_to_preproc_symbol() {
  target="$1"
  case "${target}" in
    arm64-*)
      echo "__aarch64__"
      ;;
    armv7-*)
      echo "__ARM_ARCH_7A__"
      ;;
    armv7s-*)
      echo "__ARM_ARCH_7S__"
      ;;
    x86-*)
      echo "__i386__"
      ;;
    x86_64-*)
      echo "__x86_64__"
      ;;
    *)
      echo "#error ${target} unknown/unsupported"
      return 1
      ;;
  esac
}

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

  local file_header="/*
 *  Copyright (c) $(date +%Y), Alliance for Open Media. All rights reserved.
 *
 * This source code is subject to the terms of the BSD 2 Clause License and
 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
 * was not distributed with this source code in the LICENSE file, you can
 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
 * Media Patent License 1.0 was not distributed with this source code in the
 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
 */

/* 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}"
    printf "#define AOM_FRAMEWORK_TARGET \"${target}\"\n" >> "${config_file}"
    printf "#include \"AOM/aom/${target}/aom_config.h\"\n" >> "${config_file}"
    printf "#elif defined" >> "${config_file}"
    mkdir "${HEADER_DIR}/${target}"
    cp -p "${BUILD_ROOT}/${target}/aom_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}"
}

# Verifies that $FRAMEWORK_LIB fat library contains requested builds.
verify_framework_targets() {
  local requested_cpus=""
  local cpu=""

  # Extract CPU from full target name.
  for target; do
    cpu="${target%%-*}"
    if [ "${cpu}" = "x86" ]; then
      # lipo -info outputs i386 for libaom x86 targets.
      cpu="i386"
    fi
    requested_cpus="${requested_cpus}${cpu} "
  done

  # Get target CPUs present in framework library.
  local targets_built=$(${LIPO} -info ${FRAMEWORK_LIB})

  # $LIPO -info outputs a string like the following:
  #   Architectures in the fat file: $FRAMEWORK_LIB <architectures>
  # Capture only the architecture strings.
  targets_built=${targets_built##*: }

  # Sort CPU strings to make the next step a simple string compare.
  local actual=$(echo ${targets_built} | tr " " "\n" | sort | tr "\n" " ")
  local requested=$(echo ${requested_cpus} | tr " " "\n" | sort | tr "\n" " ")

  vlog "Requested ${FRAMEWORK_LIB} CPUs: ${requested}"
  vlog "Actual ${FRAMEWORK_LIB} CPUs: ${actual}"

  if [ "${requested}" != "${actual}" ]; then
    elog "Actual ${FRAMEWORK_LIB} targets do not match requested target list."
    elog "  Requested target CPUs: ${requested}"
    elog "  Actual target CPUs: ${actual}"
    return 1
  fi
}

# Configures and builds each target specified by $1, and then builds
# AOM.framework.
build_framework() {
  local lib_list=""
  local targets="$1"
  local target=""
  local target_dist_dir=""

  # 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}"
    target_dist_dir="${BUILD_ROOT}/${target}/${DIST_DIR}"
    if [ "${ENABLE_SHARED}" = "yes" ]; then
      local suffix="dylib"
    else
      local suffix="a"
    fi
    lib_list="${lib_list} ${target_dist_dir}/lib/libaom.${suffix}"
  done

  cd "${ORIG_PWD}"

  # The basic libaom API includes are all the same; just grab the most recent
  # set.
  cp -p "${target_dist_dir}"/include/aom/* "${HEADER_DIR}"

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

  # Create the aom_config.h shim that allows usage of aom_config.h from
  # within AOM.framework.
  create_aom_framework_config_shim "${targets}"

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

  if [ "${ENABLE_SHARED}" = "yes" ]; then
    # Adjust the dylib's name so dynamic linking in apps works as expected.
    install_name_tool -id '@rpath/AOM.framework/AOM' ${FRAMEWORK_DIR}/AOM

    # Copy in Info.plist.
    cat "${SCRIPT_DIR}/ios-Info.plist" \
      | sed "s/\${FULLVERSION}/${FULLVERSION}/g" \
      | sed "s/\${VERSION}/${VERSION}/g" \
      | sed "s/\${IOS_VERSION_MIN}/${IOS_VERSION_MIN}/g" \
      > "${FRAMEWORK_DIR}/Info.plist"
  fi

  # Confirm AOM.framework/AOM contains the targets requested.
  verify_framework_targets ${targets}

  vlog "Created fat library ${FRAMEWORK_LIB} containing:"
  for lib in ${lib_list}; do
    vlog "  $(echo ${lib} | awk -F / '{print $2, $NF}')"
  done
}

# Trap function. Cleans up the subtree used to build all targets contained in
# $TARGETS.
cleanup() {
  local res=$?
  cd "${ORIG_PWD}"

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

  if [ "${PRESERVE_BUILD_OUTPUT}" != "yes" ]; then
    rm -rf "${BUILD_ROOT}"
  fi
}

print_list() {
  local indent="$1"
  shift
  local list="$@"
  for entry in ${list}; do
    echo "${indent}${entry}"
  done
}

iosbuild_usage() {
cat << EOF
  Usage: ${0##*/} [arguments]
    --help: Display this message and exit.
    --enable-shared: Build a dynamic framework for use on iOS 8 or later.
    --extra-configure-args <args>: Extra args to pass when configuring libaom.
    --macosx: Uses darwin16 targets instead of iphonesimulator targets for x86
              and x86_64. Allows linking to framework when builds target MacOSX
              instead of iOS.
    --preserve-build-output: Do not delete the build directory.
    --show-build-output: Show output from each library build.
    --targets <targets>: Override default target list. Defaults:
$(print_list "        " ${TARGETS})
    --test-link: Confirms all targets can be linked. Functionally identical to
                 passing --enable-examples via --extra-configure-args.
    --verbose: Output information about the environment and each stage of the
               build.
EOF
}

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

vlog() {
  if [ "${VERBOSE}" = "yes" ]; then
    echo "$@"
  fi
}

trap cleanup EXIT

# Parse the command line.
while [ -n "$1" ]; do
  case "$1" in
    --extra-configure-args)
      EXTRA_CONFIGURE_ARGS="$2"
      shift
      ;;
    --help)
      iosbuild_usage
      exit
      ;;
    --enable-shared)
      ENABLE_SHARED=yes
      ;;
    --preserve-build-output)
      PRESERVE_BUILD_OUTPUT=yes
      ;;
    --show-build-output)
      devnull=
      ;;
    --test-link)
      EXTRA_CONFIGURE_ARGS="${EXTRA_CONFIGURE_ARGS} --enable-examples"
      ;;
    --targets)
      TARGETS="$2"
      shift
      ;;
    --macosx)
      TARGETS="${ARM_TARGETS} ${OSX_TARGETS}"
      ;;
    --verbose)
      VERBOSE=yes
      ;;
    *)
      iosbuild_usage
      exit 1
      ;;
  esac
  shift
done

if [ "${ENABLE_SHARED}" = "yes" ]; then
  CONFIGURE_ARGS="--enable-shared ${CONFIGURE_ARGS}"
fi

FULLVERSION=$("${SCRIPT_DIR}"/version.sh --bare "${LIBAOM_SOURCE_DIR}")
VERSION=$(echo "${FULLVERSION}" | sed -E 's/^v([0-9]+\.[0-9]+\.[0-9]+).*$/\1/')

if [ "$ENABLE_SHARED" = "yes" ]; then
  IOS_VERSION_OPTIONS="--enable-shared"
  IOS_VERSION_MIN="8.0"
else
  IOS_VERSION_OPTIONS=""
  IOS_VERSION_MIN="6.0"
fi

if [ "${VERBOSE}" = "yes" ]; then
cat << EOF
  BUILD_ROOT=${BUILD_ROOT}
  DIST_DIR=${DIST_DIR}
  CONFIGURE_ARGS=${CONFIGURE_ARGS}
  EXTRA_CONFIGURE_ARGS=${EXTRA_CONFIGURE_ARGS}
  FRAMEWORK_DIR=${FRAMEWORK_DIR}
  FRAMEWORK_LIB=${FRAMEWORK_LIB}
  HEADER_DIR=${HEADER_DIR}
  LIBAOM_SOURCE_DIR=${LIBAOM_SOURCE_DIR}
  LIPO=${LIPO}
  MAKEFLAGS=${MAKEFLAGS}
  ORIG_PWD=${ORIG_PWD}
  PRESERVE_BUILD_OUTPUT=${PRESERVE_BUILD_OUTPUT}
  TARGETS="$(print_list "" ${TARGETS})"
  ENABLE_SHARED=${ENABLE_SHARED}
  OSX_TARGETS="${OSX_TARGETS}"
  SIM_TARGETS="${SIM_TARGETS}"
  SCRIPT_DIR="${SCRIPT_DIR}"
  FULLVERSION="${FULLVERSION}"
  VERSION="${VERSION}"
  IOS_VERSION_MIN="${IOS_VERSION_MIN}"
EOF
fi

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