summaryrefslogtreecommitdiff
path: root/build/genif.sh
blob: 697bef95912e46f8bc8e6c7759de4322c94eaa34 (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
#!/bin/sh
#
# Generate internal functions file content based on the provided extensions.
#
# SYNOPSIS:
#   genif.sh <template> <extensions>
#
# ARGUMENTS:
#   template    Path to internal functions template file.
#   extensions  Space delimited list of provided extensions and their locations.
#
# ENVIRONMENT:
#   The following optional variables are supported:
#
#   AWK         Path to the awk program or its command name.
#               AWK=/path/to/awk genif.sh ...
#
# USAGE EXAMPLE:
#   AWK=nawk ./build/genif.sh ./main/internal_functions.c.in "date;ext/date spl;ext/spl" > ./main/internal_functions.c

AWK=${AWK:-awk}
template=$1
shift
extensions="$@"

if test -z "$template"; then
  echo "Please supply template." >&2
  exit 1
fi

header_list=
olddir=$(pwd)

# Go to project root.
cd $(CDPATH= cd -- "$(dirname -- "$0")/../" && pwd -P)

module_ptrs="$(echo $extensions | $AWK -f ./build/order_by_dep.awk)"

for ext in $extensions; do
  ext_dir=$(echo "$ext" | cut -d ';' -f 2)
  header_list="$header_list $ext_dir/*.h*"
done

includes=$($AWK -f ./build/print_include.awk $header_list)

cd $olddir

cat $template | \
  sed \
    -e "s'@EXT_INCLUDE_CODE@'$includes'" \
    -e "s'@EXT_MODULE_PTRS@'$module_ptrs'" \
    -e 's/@NEWLINE@/\
/g'