blob: c4f92ee70f726881ad3286b343462b0b7b498547 [file] [log] [blame]
Patrick Williams185d2792020-05-19 16:41:45 -05001#!/usr/bin/env bash
2
3set -e
4
5function show_usage {
Patrick Williamsd77548a2022-04-29 14:43:15 -05006 proc=$(nproc)
Patrick Williams185d2792020-05-19 16:41:45 -05007 echo "Usage: $0 [options] <dirs>+"
8 echo
9 echo "Generate the sdbus++ sources from a directory path."
10 echo
11 echo "Options:"
12 echo " --tool <path> - path to processing tool (default 'sdbus++')."
13 echo " --output <path> - directory to place output files (default '.')."
14 echo " --list-all - include all generated files in stdout list."
Patrick Williamsd77548a2022-04-29 14:43:15 -050015 echo " --jobs <N> - number to run in parallel (default: ${proc})."
Patrick Williams185d2792020-05-19 16:41:45 -050016 echo " <dirs>+ - any number of subdirectories to generate."
17 echo
18 echo "The output on stdout is a list of generated files, which is intended"
19 echo "to be consumed by build systems, such as Meson."
20 echo
21 echo "This tool, by default, generates all files that are able to be"
22 echo "created by sdbus++. The output is a list of compilable sources that"
23 echo "were generated by the tool. The tool may generate outputs which are"
24 echo "not able to be compiled, such as documentation, but it does not put"
25 echo "them into stdout unless --list-all is given."
26}
27
28sdbuspp="sdbus++"
29outputdir="."
30listall="no"
Patrick Williamsc65b3aa2020-07-16 07:43:16 -050031parallel=$(nproc || grep -c ^processor < /proc/cpuinfo)
Patrick Williams185d2792020-05-19 16:41:45 -050032
Patrick Williamsbb140d12020-06-05 16:29:10 -050033options="$(getopt -o ho:t:j: --long help,list-all,output:,tool:,jobs: -- "$@")"
Patrick Williamsd77548a2022-04-29 14:43:15 -050034eval set -- "${options}"
Patrick Williams185d2792020-05-19 16:41:45 -050035
Patrick Williams9ede18b2022-03-12 07:55:36 -060036while true; do
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 Williamsd77548a2022-04-29 14:43:15 -050070
71 *)
72 echo "Invalid argument: $1"
73 exit 1
74 ;;
Patrick Williams185d2792020-05-19 16:41:45 -050075 esac
76done
77
Patrick Williamsd77548a2022-04-29 14:43:15 -050078if [[ $# -eq 0 ]]; then
Patrick Williams185d2792020-05-19 16:41:45 -050079 show_usage
80 exit 1
81fi
82
Patrick Williams70bdfdd2020-07-09 15:53:43 -050083all_jobs=""
84
Patrick Williamsb2bc0c72020-06-19 09:52:50 -050085declare -A emitted_file_names
Patrick Williams0b2e48e2020-06-09 14:49:06 -050086# generate_single -- make a single call to sdbus++.
87# $1: sdbus++ TYPE
88# $2: sdbus++ PROCESS
89# $3: sdbus++ ITEM
90# $4: relative output file
91# $5: 'append-mode' if present.
92function generate_single {
Patrick Williams0b731e02020-06-19 09:49:50 -050093
Patrick Williamsd77548a2022-04-29 14:43:15 -050094 if [[ "empty" == "$1" ]]; then
95 echo -n > "${outputdir}/$4"
96 elif [[ "" == "$5" ]]; then
97 ${sdbuspp} "$1" "$2" "$3" > "${outputdir}/$4" &
98 all_jobs="${all_jobs} $!"
Patrick Williams0b2e48e2020-06-09 14:49:06 -050099 else
Patrick Williamsd77548a2022-04-29 14:43:15 -0500100 ${sdbuspp} "$1" "$2" "$3" >> "${outputdir}/$4" &
101 all_jobs="${all_jobs} $!"
Patrick Williams0b2e48e2020-06-09 14:49:06 -0500102 fi
103
Patrick Williamsb2bc0c72020-06-19 09:52:50 -0500104 # Emit filename as needed.
Patrick Williamsd77548a2022-04-29 14:43:15 -0500105 filename=${outputdir}/$4
106 if [[ "x1" != "x${emitted_file_names[${filename}]}" ]]; then
107 emitted_file_names[${filename}]="1"
Patrick Williams0b2e48e2020-06-09 14:49:06 -0500108
Patrick Williamsb2bc0c72020-06-19 09:52:50 -0500109 # Always emit generated file name for foo-cpp and foo-header.
110 # Conditionally emit for everything else depending on $listall.
111 case "$2" in
112 *-cpp | *-header)
Patrick Williamsd77548a2022-04-29 14:43:15 -0500113 echo "${filename}"
Patrick Williamsb2bc0c72020-06-19 09:52:50 -0500114 ;;
115
116 *)
Patrick Williamsd77548a2022-04-29 14:43:15 -0500117 if [[ "yes" == "${listall}" ]]; then
118 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 Williamsd77548a2022-04-29 14:43:15 -0500126 # shellcheck disable=SC2312
127 if [[ $(echo "${all_jobs}" | wc -w) -ge ${parallel} ]]; then
Patrick Williams70bdfdd2020-07-09 15:53:43 -0500128 waitall
129 fi
130}
131
132function waitall {
Patrick Williamsd77548a2022-04-29 14:43:15 -0500133 for job in ${all_jobs}; do
134 wait "${job}"
Patrick Williams0b2e48e2020-06-09 14:49:06 -0500135 done
Patrick Williams70bdfdd2020-07-09 15:53:43 -0500136 all_jobs=""
Patrick Williams0b2e48e2020-06-09 14:49:06 -0500137}
138
Patrick Williams9ede18b2022-03-12 07:55:36 -0600139for d in "$@"; do
Patrick Williamsd77548a2022-04-29 14:43:15 -0500140 interfaces="$(find "${d}" -name '*.interface.yaml')"
141 errors="$(find "${d}" -name '*.errors.yaml')"
Patrick Williams0b731e02020-06-19 09:49:50 -0500142
143 # Some files are created from multiple YAML sources, but we don't know
144 # which YAML sources are present. For these files, we need to first
145 # create an empty file to ensure the file does not carry old data from
146 # a previous run.
Patrick Williamsd77548a2022-04-29 14:43:15 -0500147 for y in ${interfaces} ${errors}; do
Patrick Williams0b731e02020-06-19 09:49:50 -0500148 path="${y%.interface.yaml}"
149 path="${path%.errors.yaml}"
150 iface="${path//\//.}"
151
Patrick Williamsd77548a2022-04-29 14:43:15 -0500152 mkdir -p "${outputdir}/${path}"
153 generate_single empty markdown "${iface}" "${path}.md"
Patrick Williams0b731e02020-06-19 09:49:50 -0500154 done
Patrick Williams185d2792020-05-19 16:41:45 -0500155
Patrick Williamsd77548a2022-04-29 14:43:15 -0500156 for i in ${interfaces}; do
Patrick Williams185d2792020-05-19 16:41:45 -0500157 path="${i%.interface.yaml}"
158 iface="${path//\//.}"
159
Patrick Williamsd77548a2022-04-29 14:43:15 -0500160 generate_single interface server-header "${iface}" "${path}/server.hpp"
161 generate_single interface server-cpp "${iface}" "${path}/server.cpp"
162 generate_single interface client-header "${iface}" "${path}/client.hpp"
163 generate_single interface markdown "${iface}" "${path}.md" append
Patrick Williams185d2792020-05-19 16:41:45 -0500164
Patrick Williams185d2792020-05-19 16:41:45 -0500165 done
Patrick Williams70bdfdd2020-07-09 15:53:43 -0500166 waitall # finish all before continuing
Patrick Williams185d2792020-05-19 16:41:45 -0500167
Patrick Williamsd77548a2022-04-29 14:43:15 -0500168 for e in ${errors}; do
Patrick Williams185d2792020-05-19 16:41:45 -0500169 path="${e%.errors.yaml}"
170 iface="${path//\//.}"
171
Patrick Williamsd77548a2022-04-29 14:43:15 -0500172 generate_single error exception-header "${iface}" "${path}/error.hpp"
173 generate_single error exception-cpp "${iface}" "${path}/error.cpp"
174 generate_single error markdown "${iface}" "${path}.md" append
Patrick Williams185d2792020-05-19 16:41:45 -0500175
Patrick Williams185d2792020-05-19 16:41:45 -0500176 done
Patrick Williams70bdfdd2020-07-09 15:53:43 -0500177 waitall # finish all before continuing
Patrick Williams185d2792020-05-19 16:41:45 -0500178done