blob: 25c4c0e7706b1dc3ff2b788f3418a9c93b2323bf [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 Williamsbb140d12020-06-05 16:29:10 -050035while true;
36do
Patrick Williams185d2792020-05-19 16:41:45 -050037 case "$1" in
38 -h | --help)
39 show_usage
40 exit
41 ;;
42
Patrick Williamsbb140d12020-06-05 16:29:10 -050043 -j | --jobs)
44 shift
45 parallel="$1"
46 shift
47 ;;
48
Patrick Williams185d2792020-05-19 16:41:45 -050049 --list-all)
50 listall="yes"
51 shift
52 ;;
53
54 -o | --output)
55 shift
Patrick Williamsbb140d12020-06-05 16:29:10 -050056 outputdir="$1"
Patrick Williams185d2792020-05-19 16:41:45 -050057 shift
58 ;;
59
60 -t | --tool)
61 shift
Patrick Williamsbb140d12020-06-05 16:29:10 -050062 sdbuspp="$1"
Patrick Williams185d2792020-05-19 16:41:45 -050063 shift
64 ;;
65
66 --)
67 shift
68 break
69 ;;
Patrick Williams185d2792020-05-19 16:41:45 -050070 esac
71done
72
Patrick Williamsc65b3aa2020-07-16 07:43:16 -050073if [ $# -eq 0 ];
Patrick Williams185d2792020-05-19 16:41:45 -050074then
75 show_usage
76 exit 1
77fi
78
Patrick Williams70bdfdd2020-07-09 15:53:43 -050079all_jobs=""
80
Patrick Williamsb2bc0c72020-06-19 09:52:50 -050081declare -A emitted_file_names
Patrick Williams0b2e48e2020-06-09 14:49:06 -050082# generate_single -- make a single call to sdbus++.
83# $1: sdbus++ TYPE
84# $2: sdbus++ PROCESS
85# $3: sdbus++ ITEM
86# $4: relative output file
87# $5: 'append-mode' if present.
88function generate_single {
Patrick Williams0b731e02020-06-19 09:49:50 -050089
90 if [ "xempty" == "x$1" ];
91 then
Patrick Williamsc65b3aa2020-07-16 07:43:16 -050092 echo -n > "$outputdir/$4"
Patrick Williams0b731e02020-06-19 09:49:50 -050093 elif [ "x" == "x$5" ];
Patrick Williams0b2e48e2020-06-09 14:49:06 -050094 then
Patrick Williamsc65b3aa2020-07-16 07:43:16 -050095 $sdbuspp "$1" "$2" "$3" > "$outputdir/$4" &
Patrick Williams70bdfdd2020-07-09 15:53:43 -050096 all_jobs="$all_jobs $!"
Patrick Williams0b2e48e2020-06-09 14:49:06 -050097 else
Patrick Williamsc65b3aa2020-07-16 07:43:16 -050098 $sdbuspp "$1" "$2" "$3" >> "$outputdir/$4" &
Patrick Williams70bdfdd2020-07-09 15:53:43 -050099 all_jobs="$all_jobs $!"
Patrick Williams0b2e48e2020-06-09 14:49:06 -0500100 fi
101
Patrick Williamsb2bc0c72020-06-19 09:52:50 -0500102 # Emit filename as needed.
103 filename=$outputdir/$4
104 if [ "x1" != "x${emitted_file_names[$filename]}" ];
105 then
106 emitted_file_names[$filename]="1"
Patrick Williams0b2e48e2020-06-09 14:49:06 -0500107
Patrick Williamsb2bc0c72020-06-19 09:52:50 -0500108 # Always emit generated file name for foo-cpp and foo-header.
109 # Conditionally emit for everything else depending on $listall.
110 case "$2" in
111 *-cpp | *-header)
Patrick Williamsc65b3aa2020-07-16 07:43:16 -0500112 echo "$filename"
Patrick Williamsb2bc0c72020-06-19 09:52:50 -0500113 ;;
114
115 *)
116 if [ "xyes" == "x$listall" ];
117 then
Patrick Williamsc65b3aa2020-07-16 07:43:16 -0500118 echo "$filename"
Patrick Williamsb2bc0c72020-06-19 09:52:50 -0500119 fi
120 ;;
121 esac
122 fi
Patrick Williams0b2e48e2020-06-09 14:49:06 -0500123
124 # Ensure that no more than ${parallel} jobs are running at a time and if so
Patrick Williams70bdfdd2020-07-09 15:53:43 -0500125 # wait for them to finish.
Patrick Williamsc65b3aa2020-07-16 07:43:16 -0500126 if [[ $(echo "$all_jobs" | wc -w) -ge $parallel ]];
Patrick Williams70bdfdd2020-07-09 15:53:43 -0500127 then
128 waitall
129 fi
130}
131
132function waitall {
133 for job in $all_jobs;
Patrick Williams0b2e48e2020-06-09 14:49:06 -0500134 do
Patrick Williamsc65b3aa2020-07-16 07:43:16 -0500135 wait "$job"
Patrick Williams0b2e48e2020-06-09 14:49:06 -0500136 done
Patrick Williams70bdfdd2020-07-09 15:53:43 -0500137 all_jobs=""
Patrick Williams0b2e48e2020-06-09 14:49:06 -0500138}
139
Patrick Williamsc65b3aa2020-07-16 07:43:16 -0500140for d in "$@";
Patrick Williams185d2792020-05-19 16:41:45 -0500141do
Patrick Williamsc65b3aa2020-07-16 07:43:16 -0500142 interfaces="$(find "$d" -name '*.interface.yaml')"
143 errors="$(find "$d" -name '*.errors.yaml')"
Patrick Williams0b731e02020-06-19 09:49:50 -0500144
145 # Some files are created from multiple YAML sources, but we don't know
146 # which YAML sources are present. For these files, we need to first
147 # create an empty file to ensure the file does not carry old data from
148 # a previous run.
149 for y in $interfaces $errors;
150 do
151 path="${y%.interface.yaml}"
152 path="${path%.errors.yaml}"
153 iface="${path//\//.}"
154
Patrick Williamsc65b3aa2020-07-16 07:43:16 -0500155 mkdir -p "$outputdir/$path"
156 generate_single empty markdown "$iface" "$path.md"
Patrick Williams0b731e02020-06-19 09:49:50 -0500157 done
Patrick Williams185d2792020-05-19 16:41:45 -0500158
159 for i in $interfaces;
160 do
161 path="${i%.interface.yaml}"
162 iface="${path//\//.}"
163
Patrick Williamsc65b3aa2020-07-16 07:43:16 -0500164 generate_single interface server-header "$iface" "$path/server.hpp"
165 generate_single interface server-cpp "$iface" "$path/server.cpp"
166 generate_single interface client-header "$iface" "$path/client.hpp"
167 generate_single interface 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 -0500171
Patrick Williams185d2792020-05-19 16:41:45 -0500172
173 for e in $errors;
174 do
175 path="${e%.errors.yaml}"
176 iface="${path//\//.}"
177
Patrick Williamsc65b3aa2020-07-16 07:43:16 -0500178 generate_single error exception-header "$iface" "$path/error.hpp"
179 generate_single error exception-cpp "$iface" "$path/error.cpp"
180 generate_single error markdown "$iface" "$path.md" append
Patrick Williams185d2792020-05-19 16:41:45 -0500181
Patrick Williams185d2792020-05-19 16:41:45 -0500182 done
Patrick Williams70bdfdd2020-07-09 15:53:43 -0500183 waitall # finish all before continuing
Patrick Williams185d2792020-05-19 16:41:45 -0500184done