blob: 2ad4e360c05997c9eb81715092bc405771326a81 [file] [log] [blame]
Patrick Williams185d2792020-05-19 16:41:45 -05001#!/usr/bin/env bash
2
3set -e
4
5function show_usage {
6 echo "Usage: $0 [options] <dirs>+"
7 echo
8 echo "Generate the sdbus++ sources from a directory path."
9 echo
10 echo "Options:"
11 echo " --tool <path> - path to processing tool (default 'sdbus++')."
12 echo " --output <path> - directory to place output files (default '.')."
13 echo " --list-all - include all generated files in stdout list."
Patrick Williamsbb140d12020-06-05 16:29:10 -050014 echo " --jobs <N> - number to run in parallel (default: $(nproc))."
Patrick Williams185d2792020-05-19 16:41:45 -050015 echo " <dirs>+ - any number of subdirectories to generate."
16 echo
17 echo "The output on stdout is a list of generated files, which is intended"
18 echo "to be consumed by build systems, such as Meson."
19 echo
20 echo "This tool, by default, generates all files that are able to be"
21 echo "created by sdbus++. The output is a list of compilable sources that"
22 echo "were generated by the tool. The tool may generate outputs which are"
23 echo "not able to be compiled, such as documentation, but it does not put"
24 echo "them into stdout unless --list-all is given."
25}
26
27sdbuspp="sdbus++"
28outputdir="."
29listall="no"
Patrick Williamsc65b3aa2020-07-16 07:43:16 -050030parallel=$(nproc || grep -c ^processor < /proc/cpuinfo)
Patrick Williams185d2792020-05-19 16:41:45 -050031
Patrick Williamsbb140d12020-06-05 16:29:10 -050032options="$(getopt -o ho:t:j: --long help,list-all,output:,tool:,jobs: -- "$@")"
Patrick Williams185d2792020-05-19 16:41:45 -050033eval set -- "$options"
34
Patrick Williams9ede18b2022-03-12 07:55:36 -060035while true; do
Patrick Williams185d2792020-05-19 16:41:45 -050036 case "$1" in
37 -h | --help)
38 show_usage
39 exit
40 ;;
41
Patrick Williamsbb140d12020-06-05 16:29:10 -050042 -j | --jobs)
43 shift
44 parallel="$1"
45 shift
46 ;;
47
Patrick Williams185d2792020-05-19 16:41:45 -050048 --list-all)
49 listall="yes"
50 shift
51 ;;
52
53 -o | --output)
54 shift
Patrick Williamsbb140d12020-06-05 16:29:10 -050055 outputdir="$1"
Patrick Williams185d2792020-05-19 16:41:45 -050056 shift
57 ;;
58
59 -t | --tool)
60 shift
Patrick Williamsbb140d12020-06-05 16:29:10 -050061 sdbuspp="$1"
Patrick Williams185d2792020-05-19 16:41:45 -050062 shift
63 ;;
64
65 --)
66 shift
67 break
68 ;;
Patrick Williams185d2792020-05-19 16:41:45 -050069 esac
70done
71
Patrick Williams9ede18b2022-03-12 07:55:36 -060072if [ $# -eq 0 ]; then
Patrick Williams185d2792020-05-19 16:41:45 -050073 show_usage
74 exit 1
75fi
76
Patrick Williams70bdfdd2020-07-09 15:53:43 -050077all_jobs=""
78
Patrick Williamsb2bc0c72020-06-19 09:52:50 -050079declare -A emitted_file_names
Patrick Williams0b2e48e2020-06-09 14:49:06 -050080# generate_single -- make a single call to sdbus++.
81# $1: sdbus++ TYPE
82# $2: sdbus++ PROCESS
83# $3: sdbus++ ITEM
84# $4: relative output file
85# $5: 'append-mode' if present.
86function generate_single {
Patrick Williams0b731e02020-06-19 09:49:50 -050087
Patrick Williams9ede18b2022-03-12 07:55:36 -060088 if [ "empty" == "$1" ]; then
Patrick Williamsc65b3aa2020-07-16 07:43:16 -050089 echo -n > "$outputdir/$4"
Patrick Williams9ede18b2022-03-12 07:55:36 -060090 elif [ "" == "$5" ]; then
Patrick Williamsc65b3aa2020-07-16 07:43:16 -050091 $sdbuspp "$1" "$2" "$3" > "$outputdir/$4" &
Patrick Williams70bdfdd2020-07-09 15:53:43 -050092 all_jobs="$all_jobs $!"
Patrick Williams0b2e48e2020-06-09 14:49:06 -050093 else
Patrick Williamsc65b3aa2020-07-16 07:43:16 -050094 $sdbuspp "$1" "$2" "$3" >> "$outputdir/$4" &
Patrick Williams70bdfdd2020-07-09 15:53:43 -050095 all_jobs="$all_jobs $!"
Patrick Williams0b2e48e2020-06-09 14:49:06 -050096 fi
97
Patrick Williamsb2bc0c72020-06-19 09:52:50 -050098 # Emit filename as needed.
99 filename=$outputdir/$4
Patrick Williams9ede18b2022-03-12 07:55:36 -0600100 if [ "x1" != "x${emitted_file_names[$filename]}" ]; then
Patrick Williamsb2bc0c72020-06-19 09:52:50 -0500101 emitted_file_names[$filename]="1"
Patrick Williams0b2e48e2020-06-09 14:49:06 -0500102
Patrick Williamsb2bc0c72020-06-19 09:52:50 -0500103 # Always emit generated file name for foo-cpp and foo-header.
104 # Conditionally emit for everything else depending on $listall.
105 case "$2" in
106 *-cpp | *-header)
Patrick Williamsc65b3aa2020-07-16 07:43:16 -0500107 echo "$filename"
Patrick Williamsb2bc0c72020-06-19 09:52:50 -0500108 ;;
109
110 *)
Patrick Williams9ede18b2022-03-12 07:55:36 -0600111 if [ "yes" == "$listall" ]; then
Patrick Williamsc65b3aa2020-07-16 07:43:16 -0500112 echo "$filename"
Patrick Williamsb2bc0c72020-06-19 09:52:50 -0500113 fi
114 ;;
115 esac
116 fi
Patrick Williams0b2e48e2020-06-09 14:49:06 -0500117
118 # Ensure that no more than ${parallel} jobs are running at a time and if so
Patrick Williams70bdfdd2020-07-09 15:53:43 -0500119 # wait for them to finish.
Patrick Williams9ede18b2022-03-12 07:55:36 -0600120 if [[ $(echo "$all_jobs" | wc -w) -ge $parallel ]]; then
Patrick Williams70bdfdd2020-07-09 15:53:43 -0500121 waitall
122 fi
123}
124
125function waitall {
Patrick Williams9ede18b2022-03-12 07:55:36 -0600126 for job in $all_jobs; do
Patrick Williamsc65b3aa2020-07-16 07:43:16 -0500127 wait "$job"
Patrick Williams0b2e48e2020-06-09 14:49:06 -0500128 done
Patrick Williams70bdfdd2020-07-09 15:53:43 -0500129 all_jobs=""
Patrick Williams0b2e48e2020-06-09 14:49:06 -0500130}
131
Patrick Williams9ede18b2022-03-12 07:55:36 -0600132for d in "$@"; do
Patrick Williamsc65b3aa2020-07-16 07:43:16 -0500133 interfaces="$(find "$d" -name '*.interface.yaml')"
134 errors="$(find "$d" -name '*.errors.yaml')"
Patrick Williams0b731e02020-06-19 09:49:50 -0500135
136 # Some files are created from multiple YAML sources, but we don't know
137 # which YAML sources are present. For these files, we need to first
138 # create an empty file to ensure the file does not carry old data from
139 # a previous run.
Patrick Williams9ede18b2022-03-12 07:55:36 -0600140 for y in $interfaces $errors; do
Patrick Williams0b731e02020-06-19 09:49:50 -0500141 path="${y%.interface.yaml}"
142 path="${path%.errors.yaml}"
143 iface="${path//\//.}"
144
Patrick Williamsc65b3aa2020-07-16 07:43:16 -0500145 mkdir -p "$outputdir/$path"
146 generate_single empty markdown "$iface" "$path.md"
Patrick Williams0b731e02020-06-19 09:49:50 -0500147 done
Patrick Williams185d2792020-05-19 16:41:45 -0500148
Patrick Williams9ede18b2022-03-12 07:55:36 -0600149 for i in $interfaces; do
Patrick Williams185d2792020-05-19 16:41:45 -0500150 path="${i%.interface.yaml}"
151 iface="${path//\//.}"
152
Patrick Williamsc65b3aa2020-07-16 07:43:16 -0500153 generate_single interface server-header "$iface" "$path/server.hpp"
154 generate_single interface server-cpp "$iface" "$path/server.cpp"
155 generate_single interface client-header "$iface" "$path/client.hpp"
156 generate_single interface markdown "$iface" "$path.md" append
Patrick Williams185d2792020-05-19 16:41:45 -0500157
Patrick Williams185d2792020-05-19 16:41:45 -0500158 done
Patrick Williams70bdfdd2020-07-09 15:53:43 -0500159 waitall # finish all before continuing
Patrick Williams185d2792020-05-19 16:41:45 -0500160
Patrick Williams9ede18b2022-03-12 07:55:36 -0600161 for e in $errors; do
Patrick Williams185d2792020-05-19 16:41:45 -0500162 path="${e%.errors.yaml}"
163 iface="${path//\//.}"
164
Patrick Williamsc65b3aa2020-07-16 07:43:16 -0500165 generate_single error exception-header "$iface" "$path/error.hpp"
166 generate_single error exception-cpp "$iface" "$path/error.cpp"
167 generate_single error markdown "$iface" "$path.md" append
Patrick Williams185d2792020-05-19 16:41:45 -0500168
Patrick Williams185d2792020-05-19 16:41:45 -0500169 done
Patrick Williams70bdfdd2020-07-09 15:53:43 -0500170 waitall # finish all before continuing
Patrick Williams185d2792020-05-19 16:41:45 -0500171done