• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/bash
2#
3# Copyright (c) 2012 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 is used to generate files in the <platform> directories needed to
8# build libvpx. Every time libvpx source code is updated run this script.
9#
10# The script depends on the bpfmt tool, which may need to be built with
11# m -j blueprint_tools
12#
13# For example, from the top of an Android tree:
14# $ source build/envsetup.sh
15# $ m -j blueprint_tools
16# $ external/libvpx/post_update.sh
17#
18# And this will update all the config files needed.
19
20set -e
21
22export LC_ALL=C
23cd $(dirname $0)
24BASE_DIR=$(pwd)
25LIBVPX_SRC_DIR="."
26LIBVPX_CONFIG_DIR="config"
27
28# Clean files from previous make.
29function make_clean {
30  make clean > /dev/null
31  rm -f libvpx_srcs.txt
32}
33
34# Lint a pair of vpx_config.h and vpx_config.asm to make sure they match.
35# $1 - Header file directory.
36function lint_config {
37  $BASE_DIR/lint_config.sh \
38    -h $BASE_DIR/$LIBVPX_CONFIG_DIR/$1/vpx_config.h \
39    -a $BASE_DIR/$LIBVPX_CONFIG_DIR/$1/vpx_config.asm
40}
41
42# Print the configuration.
43# $1 - Header file directory.
44function print_config {
45  $BASE_DIR/lint_config.sh -p \
46    -h $BASE_DIR/$LIBVPX_CONFIG_DIR/$1/vpx_config.h \
47    -a $BASE_DIR/$LIBVPX_CONFIG_DIR/$1/vpx_config.asm
48}
49
50# Print the configuration from Header file.
51# This function is an abridged version of print_config which does not use
52# lint_config and it does not require existence of vpx_config.asm.
53# $1 - Header file directory.
54function print_config_basic {
55  combined_config="$(cat $BASE_DIR/$LIBVPX_CONFIG_DIR/$1/vpx_config.h \
56                   | grep -E ' +[01] *$')"
57  combined_config="$(echo "$combined_config" | grep -v DO1STROUNDING)"
58  combined_config="$(echo "$combined_config" | sed 's/[ \t]//g')"
59  combined_config="$(echo "$combined_config" | sed 's/.*define//')"
60  combined_config="$(echo "$combined_config" | sed 's/0$/=no/')"
61  combined_config="$(echo "$combined_config" | sed 's/1$/=yes/')"
62  echo "$combined_config" | sort | uniq
63}
64
65# Generate *_rtcd.h files.
66# $1 - Header file directory.
67# $2 - Architecture.
68# $3 - Optional - any additional arguments to pass through.
69function gen_rtcd_header {
70  echo "Generate $LIBVPX_CONFIG_DIR/$1/*_rtcd.h files."
71
72  rm -rf $BASE_DIR/$TEMP_DIR/libvpx.config
73  $BASE_DIR/lint_config.sh -p \
74    -h $BASE_DIR/$LIBVPX_CONFIG_DIR/$1/vpx_config.h \
75    -a $BASE_DIR/$LIBVPX_CONFIG_DIR/$1/vpx_config.asm \
76    -o $BASE_DIR/$TEMP_DIR/libvpx.config
77
78  $BASE_DIR/$LIBVPX_SRC_DIR/build/make/rtcd.pl \
79    --arch=$2 \
80    --sym=vp8_rtcd $3 \
81    --config=$BASE_DIR/$TEMP_DIR/libvpx.config \
82    $BASE_DIR/$LIBVPX_SRC_DIR/vp8/common/rtcd_defs.pl \
83    > $BASE_DIR/$LIBVPX_CONFIG_DIR/$1/vp8_rtcd.h
84
85  $BASE_DIR/$LIBVPX_SRC_DIR/build/make/rtcd.pl \
86    --arch=$2 \
87    --sym=vp9_rtcd $3 \
88    --config=$BASE_DIR/$TEMP_DIR/libvpx.config \
89    $BASE_DIR/$LIBVPX_SRC_DIR/vp9/common/vp9_rtcd_defs.pl \
90    > $BASE_DIR/$LIBVPX_CONFIG_DIR/$1/vp9_rtcd.h
91
92  $BASE_DIR/$LIBVPX_SRC_DIR/build/make/rtcd.pl \
93    --arch=$2 \
94    --sym=vpx_scale_rtcd $3 \
95    --config=$BASE_DIR/$TEMP_DIR/libvpx.config \
96    $BASE_DIR/$LIBVPX_SRC_DIR/vpx_scale/vpx_scale_rtcd.pl \
97    > $BASE_DIR/$LIBVPX_CONFIG_DIR/$1/vpx_scale_rtcd.h
98
99  $BASE_DIR/$LIBVPX_SRC_DIR/build/make/rtcd.pl \
100    --arch=$2 \
101    --sym=vpx_dsp_rtcd $3 \
102    --config=$BASE_DIR/$TEMP_DIR/libvpx.config \
103    $BASE_DIR/$LIBVPX_SRC_DIR/vpx_dsp/vpx_dsp_rtcd_defs.pl \
104    > $BASE_DIR/$LIBVPX_CONFIG_DIR/$1/vpx_dsp_rtcd.h
105
106  rm -rf $BASE_DIR/$TEMP_DIR/libvpx.config
107}
108
109# Generate Config files. "--enable-external-build" must be set to skip
110# detection of capabilities on specific targets.
111# $1 - Header file directory.
112# $2 - Config command line.
113function gen_config_files {
114  ./configure $2 > /dev/null
115
116  # Generate vpx_config.asm for x86[_64].
117  if [[ "$1" == *x86* ]]; then
118    egrep "#define [A-Z0-9_]+ [01]" vpx_config.h \
119      | awk '{print "%define " $2 " " $3}' > vpx_config.asm
120  else
121    # vpx_config.asm is unused for arm[64] but is needed to pass lint_config.
122    egrep "#define [A-Z0-9_]+ [01]" vpx_config.h \
123      | awk '{print $2 " EQU " $3}' \
124      | perl $BASE_DIR/$LIBVPX_SRC_DIR/build/make/ads2gas.pl > vpx_config.asm
125  fi
126
127  # Generate vpx_version.h
128  $BASE_DIR/$LIBVPX_SRC_DIR/build/make/version.sh "$BASE_DIR/$LIBVPX_SRC_DIR" vpx_version.h
129
130  cp vpx_config.* vpx_version.h $BASE_DIR/$LIBVPX_CONFIG_DIR/$1
131  make_clean
132  rm -rf vpx_config.* vpx_version.h
133}
134
135# Generate a text file containing sources for a config
136# $1 - Config
137function gen_source_list {
138  make_clean
139  if [[ "$1" = "generic" ]]; then
140    config=$(print_config_basic $1)
141  else
142    config=$(print_config $1)
143  fi
144  make libvpx_srcs.txt libvpxrc_srcs.txt target=libs $config > /dev/null
145  mv libvpx_srcs.txt libvpx_srcs_$1.txt
146  mv libvpxrc_srcs.txt libvpxrc_srcs_$1.txt
147}
148
149# Extract a list of C sources from a libvpx_srcs.txt file
150# $1 - path to libvpx_srcs.txt
151# $2 - C file match pattern
152# $3 - Negative match pattern (default: none)
153function libvpx_srcs_txt_to_c_srcs {
154  local match_pattern="$2"
155  local negative_patterns=(-e "^vpx_config\\.c$")
156  if [[ -n "$3" ]]; then
157    negative_patterns+=(-e "$3")
158  fi
159  grep "${match_pattern}" $1 \
160    | grep -v "${negative_patterns[@]}" \
161    | awk '$0="\""$0"\","' \
162    | sort
163}
164
165# Extract a list of C++ sources from a libvpxrc_srcs.txt file
166# $1 - path to libvpxrc_srcs.txt
167function libvpxrc_srcs_txt_to_cc_srcs {
168  grep ".cc$" $1 | awk '$0="\""$0"\","' | sort
169}
170
171# Extract a list of ASM sources from a libvpx_srcs.txt file
172# $1 - path to libvpx_srcs.txt
173function libvpx_srcs_txt_to_asm_srcs {
174    grep ".asm$" $1 | awk '$0="\""$0"\","' | sort
175}
176
177# Extract a list of converted ASM sources from a libvpx_srcs.txt file
178# $1 - path to libvpx_srcs.txt
179function libvpx_srcs_txt_to_asm_S_srcs {
180    grep ".asm.S$" $1 | awk '$0="\""$0"\","' | sort
181}
182
183# Convert a list of sources to a blueprint file containing a variable
184# assignment.
185# $1 - Config
186function gen_bp_srcs {
187  (
188    # First collect the libvpx sources into variables.
189    varprefix=libvpx_${1//-/_}
190    local negative_pattern
191    # Optimization file suffixes that require explicit architecture flagging.
192    local arch_flag_req_suffixes=()
193    case "$1" in
194      arm64)
195        negative_pattern="\\(_neon_\\(dotprod\\|i8mm\\)\\|_sve\\|_sve2\\)\\.c"
196        arch_flag_req_suffixes=("neon_dotprod" "neon_i8mm" "sve" "sve2")
197        ;;
198      x86*)
199        negative_pattern="\\(_sse4\\|_avx\\|_avx2\\|_avx512\\)\\.c"
200        arch_flag_req_suffixes=("sse4" "avx" "avx2" "avx512")
201        ;;
202    esac
203    for suffix in "${arch_flag_req_suffixes[@]}"; do
204      echo "${varprefix}_${suffix}_c_srcs = ["
205      libvpx_srcs_txt_to_c_srcs libvpx_srcs_$1.txt "_${suffix}\\.c"
206      echo "]"
207      echo
208    done
209
210    echo "${varprefix}_c_srcs = ["
211    libvpx_srcs_txt_to_c_srcs libvpx_srcs_$1.txt "\\.c$" "${negative_pattern}"
212    echo "\"$LIBVPX_CONFIG_DIR/$1/vpx_config.c\","
213    echo "]"
214    if grep -qE ".asm(.S)?$" libvpx_srcs_$1.txt; then
215      echo
216      echo "${varprefix}_asm_srcs = ["
217      libvpx_srcs_txt_to_asm_srcs libvpx_srcs_$1.txt
218      libvpx_srcs_txt_to_asm_S_srcs libvpx_srcs_$1.txt
219      echo "]"
220    fi
221
222    # Now collect the libvpxrc sources into variables. Note that we're only
223    # interested in x86_64 for now, but this can be expanded later.
224    if [[ "$1" == "x86_64" ]]; then
225      varprefix=libvpxrc_${1//-/_}
226      echo
227      echo "${varprefix}_c_srcs = ["
228      # Note the architecture specific files only need to be filtered out, not
229      # added to libvpxrc specific variables (as is done with libvpx), because
230      # this library uses 'static_libs' to link to libvpx.
231      libvpx_srcs_txt_to_c_srcs libvpxrc_srcs_$1.txt "\\.c$" \
232        "${negative_pattern}"
233      echo "]"
234      echo
235      echo "${varprefix}_cc_srcs = ["
236      libvpxrc_srcs_txt_to_cc_srcs libvpxrc_srcs_$1.txt "\\.cc$" ""
237      echo "]"
238      echo
239      echo "${varprefix}_asm_srcs = ["
240      libvpx_srcs_txt_to_asm_srcs libvpxrc_srcs_$1.txt
241      libvpx_srcs_txt_to_asm_S_srcs libvpxrc_srcs_$1.txt
242      echo "]"
243    fi
244
245    echo
246  ) > config_$1.bp
247}
248
249# The ARM assembly sources must be converted from ADS to GAS compatible format.
250# This step is only required for ARM. MIPS uses intrinsics exclusively and x86
251# requires 'yasm' to pre-process its assembly files.
252function convert_arm_asm {
253  find $BASE_DIR/$LIBVPX_CONFIG_DIR/arm-neon -name '*.asm.S' | xargs -r rm
254  for src in $(grep ".asm$" libvpx_srcs_arm-neon.txt); do
255    newsrc=$LIBVPX_CONFIG_DIR/arm-neon/$src.S
256    mkdir -p $BASE_DIR/$(dirname $newsrc)
257    perl $BASE_DIR/$LIBVPX_SRC_DIR/build/make/ads2gas.pl <$BASE_DIR/$LIBVPX_SRC_DIR/$src >$BASE_DIR/$newsrc
258    echo $newsrc >>libvpx_srcs_arm-neon.txt
259  done
260  sed -i "/\.asm$/ d" libvpx_srcs_arm-neon.txt
261}
262
263echo "Create temporary directory."
264TEMP_DIR="../libvpx.temp"
265rm -rf $TEMP_DIR
266cp -R $LIBVPX_SRC_DIR $TEMP_DIR
267cd $TEMP_DIR
268
269echo "Generate config files."
270all_platforms="--enable-external-build --enable-realtime-only --enable-pic"
271all_platforms+=" --disable-install-docs"
272all_platforms+=" --size-limit=4096x3072 --enable-vp9-highbitdepth"
273gen_config_files x86 "--target=x86-linux-gcc ${all_platforms}"
274gen_config_files x86_64 "--target=x86_64-linux-gcc ${all_platforms}"
275gen_config_files arm-neon \
276  "--target=armv7-linux-gcc ${all_platforms} --disable-runtime-cpu-detect"
277gen_config_files arm64 "--target=armv8-linux-gcc ${all_platforms}"
278gen_config_files generic "--target=generic-gnu ${all_platforms}"
279
280echo "Remove temporary directory."
281cd $BASE_DIR
282rm -rf $TEMP_DIR
283
284echo "Lint libvpx configuration."
285lint_config x86
286lint_config x86_64
287lint_config arm-neon
288lint_config arm64
289lint_config generic
290
291echo "Create temporary directory."
292TEMP_DIR="../libvpx.temp"
293rm -rf $TEMP_DIR
294cp -R $LIBVPX_SRC_DIR $TEMP_DIR
295cd $TEMP_DIR
296
297gen_rtcd_header x86 x86
298gen_rtcd_header x86_64 x86_64
299gen_rtcd_header arm-neon armv7
300gen_rtcd_header arm64 armv8
301gen_rtcd_header generic generic
302
303echo "Prepare Makefile."
304./configure --target=generic-gnu > /dev/null
305make_clean
306
307echo "Generate source lists"
308gen_source_list x86
309gen_source_list x86_64
310gen_source_list arm-neon
311gen_source_list arm64
312gen_source_list generic
313
314echo "Convert ARM assembly format"
315convert_arm_asm
316
317echo "Convert to bp"
318gen_bp_srcs x86
319gen_bp_srcs x86_64
320gen_bp_srcs arm-neon
321gen_bp_srcs arm64
322gen_bp_srcs generic
323
324rm -f $BASE_DIR/Android.bp
325(
326  echo "// THIS FILE IS AUTOGENERATED, DO NOT EDIT"
327  echo "// Generated from Android.bp.in, run ./post_update.sh to regenerate"
328  echo
329  cat config_*.bp
330  cat $BASE_DIR/Android.bp.in
331) > $BASE_DIR/Android.bp
332bpfmt -s -w "${BASE_DIR}/Android.bp" \
333  || echo "bpfmt not found. Run 'm bpfmt' followed by" \
334          "'bpfmt -s -w ${BASE_DIR}/Android.bp'."
335
336echo "Remove temporary directory."
337cd $BASE_DIR
338rm -rf $TEMP_DIR
339