xref: /aosp_15_r20/external/libpng/ci/ci_shellify.sh (revision a67afe4df73cf47866eedc69947994b8ff839aba)
1#!/usr/bin/env bash
2set -o errexit -o pipefail -o posix
3
4# Copyright (c) 2019-2024 Cosmin Truta.
5#
6# Use, modification and distribution are subject to the MIT License.
7# Please see the accompanying file LICENSE_MIT.txt
8#
9# SPDX-License-Identifier: MIT
10
11# shellcheck source=ci/lib/ci.lib.sh
12source "$(dirname "$0")/lib/ci.lib.sh"
13cd "$CI_TOPLEVEL_DIR"
14
15function ci_shellify_c {
16    # Convert C preprocessor text, specifically originating
17    # from png.h, to shell scripting text.
18    # Select only the easy-to-parse definitions of PNG_LIBPNG_*.
19    sed -n -e '/^\# *define * PNG_LIBPNG_[^ ]* * ["0-9A-Za-z_]/ p' |
20        sed -e 's/^\# *define * PNG\([^ ]*\) * \([^ ]*\)/PNG\1=\2/' \
21            -e 's/=PNG\([0-9A-Za-z_]*\)/=\${PNG\1}/' \
22            -e 's/^\([^ ]*=[^ ]*\).*$/export \1;/'
23}
24
25function ci_shellify_autoconf {
26    # Convert autoconf (M4) text, specifically originating
27    # from configure.ac, to shell scripting text.
28    # Select only the easy-to-parse definitions of PNGLIB_*.
29    sed -n -e '/^ *PNGLIB_[^ ]*=[$"0-9A-Za-z_]/ p' |
30        sed -e 's/^ *PNG\([0-9A-Za-z_]*\)=\([^# ]*\).*$/PNG\1=\2/' \
31            -e 's/^\([^ ]*=[^ ]*\).*$/export \1;/'
32}
33
34function ci_shellify_cmake {
35    # Convert CMake lists text, specifically originating
36    # from CMakeLists.txt, to shell scripting text.
37    # Select only the easy-to-parse definitions of PNGLIB_*.
38    sed -n -e '/^ *set *(PNGLIB_[^ ]* * [$"0-9A-Za-z_].*)/ p' |
39        sed -e 's/^ *set *(PNG\([^ ]*\) * \([^() ]*\)).*$/PNG\1=\2/' \
40            -e 's/^\([^ ]*=[^ ]*\).*$/export \1;/'
41}
42
43function ci_shellify {
44    local arg filename
45    for arg in "$@"
46    do
47        test -f "$arg" || ci_err "no such file: '$arg'"
48        filename="$(basename -- "$arg")"
49        case "$filename" in
50        ( *.[ch] )
51            [[ $filename == png.h ]] || {
52                ci_err "unable to shellify: '$filename' (expecting: 'png.h')"
53            }
54            ci_shellify_c <"$arg" ;;
55        ( config* | *.ac )
56            [[ $filename == configure.ac ]] || {
57                ci_err "unable to shellify: '$filename' (expecting: 'configure.ac')"
58            }
59            ci_shellify_autoconf <"$arg" ;;
60        ( *CMake* | *cmake* | *.txt )
61            [[ $filename == [Cc][Mm]ake[Ll]ists.txt ]] || {
62                ci_err "unable to shellify: '$filename' (expecting: 'CMakeLists.txt')"
63            }
64            ci_shellify_cmake <"$arg" ;;
65        ( * )
66            ci_err "unable to shellify: '$arg'" ;;
67        esac
68    done
69}
70
71function usage {
72    echo "usage: $CI_SCRIPT_NAME [<options>] <files>..."
73    echo "options: -?|-h|--help"
74    echo "files: png.h|configure.ac|CMakeLists.txt"
75    exit "${@:-0}"
76}
77
78function main {
79    local opt
80    while getopts ":" opt
81    do
82        # This ain't a while-loop. It only pretends to be.
83        [[ $1 == -[?h]* || $1 == --help || $1 == --help=* ]] && usage 0
84        ci_err "unknown option: '$1'"
85    done
86    shift $((OPTIND - 1))
87    [[ $# -eq 0 ]] && usage 2
88    # And... go!
89    ci_shellify "$@"
90}
91
92main "$@"
93