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