1#!/bin/bash
2#
3# Copyright (c) 2018 The Chromium Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7# This script has been modified for use in Android. It is used to generate .bp
8# files and files in the config/ directories needed to build libaom.
9#
10# Every time the upstream source code is updated this script must be run.
11#
12# Usage:
13# $ ./generate_config.sh
14# Requirements:
15# Install the following Debian packages.
16# - cmake3
17# - yasm or nasm
18# Toolchain for armv7:
19# - gcc-arm-linux-gnueabihf
20# - g++-arm-linux-gnueabihf
21# Toolchain for arm64:
22# - gcc-aarch64-linux-gnu
23# - g++-aarch64-linux-gnu
24# 32bit build environment for cmake. Including but potentially not limited to:
25# - lib32gcc-7-dev
26# - lib32stdc++-7-dev
27# Alternatively: treat 32bit builds like Windows and manually tweak aom_config.h
28
29set -eE
30
31# sort() consistently.
32export LC_ALL=C
33
34BASE=$(pwd)
35SRC="${BASE}/libaom"
36CFG="${BASE}/config"
37TMP=$(mktemp -d "${BASE}/build.XXXX")
38
39# Clean up and prepare config directory
40rm -rf "${CFG}"
41mkdir -p "${CFG}/config"
42
43function clean {
44  rm -rf "${TMP}"
45}
46
47# Create empty temp and config directories.
48# $1 - Header file directory.
49function reset_dirs {
50  cd "${BASE}"
51  rm -rf "${TMP}"
52  mkdir "${TMP}"
53  cd "${TMP}"
54
55  echo "Generate ${1} config files."
56  mkdir -p "${CFG}/${1}/config"
57}
58
59if [ $# -ne 0 ]; then
60  echo "Unknown option(s): ${@}"
61  exit 1
62fi
63
64# Missing function:
65# find_duplicates
66# We may have enough targets to avoid re-implementing this.
67
68# Generate Config files.
69# $1 - Header file directory.
70# $2 - cmake options.
71function gen_config_files {
72  cmake "${SRC}" ${2} &> cmake.txt
73
74  case "${1}" in
75    x86*)
76      egrep "#define [A-Z0-9_]+ [01]" config/aom_config.h | \
77        awk '{print "%define " $2 " " $3}' > config/aom_config.asm
78      ;;
79  esac
80
81  cp config/aom_config.{h,c,asm} "${CFG}/${1}/config/"
82
83  cp config/*_rtcd.h "${CFG}/${1}/config/"
84  #clang-format -i "${CFG}/${1}/config/"*_rtcd.h
85}
86
87function update_readme {
88  local IFS=$'\n'
89  # Split git log output '<date>\n<commit hash>' on the newline to produce 2
90  # array entries.
91  local vals=($(git -C "${SRC}" --no-pager log -1 --format="%cd%n%H" \
92    --date=format:"%A %B %d %Y"))
93  sed -E -i.bak \
94    -e "s/^(Date:)[[:space:]]+.*$/\1 ${vals[0]}/" \
95    -e "s/^(Commit:)[[:space:]]+[a-f0-9]{40}/\1 ${vals[1]}/" \
96    ${BASE}/README.android
97  rm ${BASE}/README.android.bak
98  cat <<EOF
99
100README.android updated with:
101Date: ${vals[0]}
102Commit: ${vals[1]}
103EOF
104}
105
106cd "${TMP}"
107
108# Scope 'trap' error reporting to configuration generation.
109(
110trap '{
111  [ -f ${TMP}/cmake.txt ] && cat ${TMP}/cmake.txt
112  echo "Build directory ${TMP} not removed automatically."
113}' ERR
114
115all_platforms="-DCONFIG_SIZE_LIMIT=1"
116all_platforms+=" -DDECODE_HEIGHT_LIMIT=16384 -DDECODE_WIDTH_LIMIT=16384"
117all_platforms+=" -DCONFIG_AV1_ENCODER=0"
118all_platforms+=" -DCONFIG_LOWBITDEPTH=1"
119all_platforms+=" -DCONFIG_MAX_DECODE_PROFILE=0"
120all_platforms+=" -DCONFIG_NORMAL_TILE_MODE=1"
121# Android requires ssse3. Simplify the build by disabling everything above that
122# and RTCD.
123all_platforms+=" -DENABLE_SSE4_1=0"
124all_platforms+=" -DCONFIG_RUNTIME_CPU_DETECT=0"
125
126toolchain="-DCMAKE_TOOLCHAIN_FILE=${SRC}/build/cmake/toolchains"
127
128reset_dirs x86
129gen_config_files x86 "${toolchain}/x86-linux.cmake ${all_platforms} -DCONFIG_PIC=1"
130
131# libaom_srcs.gni and aom_version.h are shared.
132cp libaom_srcs.gni "${BASE}"
133cp config/aom_version.h "${CFG}/config/"
134
135reset_dirs x86_64
136gen_config_files x86_64 "${all_platforms}"
137
138reset_dirs arm
139gen_config_files arm "${toolchain}/armv7-linux-gcc.cmake ${all_platforms}"
140
141reset_dirs arm64
142gen_config_files arm64 "${toolchain}/arm64-linux-gcc.cmake ${all_platforms}"
143)
144
145# This needs to be run by update_libaom.sh before the .git file is removed.
146#update_readme
147
148# libaom_srcs.gni was built for Chromium. Remove:
149# - the path prefix (//third_party/libaom/source/)
150# - comments (lines starting with #)
151# - header files
152# - perl scripts (rtcd)
153
154rm -f "${BASE}/Android.bp"
155(
156  echo "// THIS FILE IS AUTOGENERATED, DO NOT EDIT"
157  echo "// Generated from Android.bp.in, run ./generate_config.sh to regenerate"
158  echo
159  cat "${BASE}/libaom_srcs.gni" |
160    grep -v ^\# |
161    sed 's/\/\/third_party\/libaom\/source\///' |
162    grep -v h\",$ |
163    grep -v pl\",$
164  cat "${BASE}/Android.bp.in"
165) > "${BASE}/Android.bp"
166
167rm -f "${BASE}/libaom_srcs.gni"
168bpfmt -w "${BASE}/Android.bp" || echo "bpfmt not found: format Android.bp manually."
169
170
171clean
172