blob: 7e46f9c13ed25386f24fbf8788a308d46afd8eef [file] [log] [blame]
Patrick Williams847a0c32020-06-24 15:18:10 -05001#!/usr/bin/env bash
2
3set -e
4
Patrick Williamsa4c9edc2020-12-17 21:02:36 -06005# Locale can change behavior of utilities like 'sort' but we want the output
6# to be stable on all machines. Force the locale to 'C' for consistency.
7export LC_ALL=C
8
Patrick Williams847a0c32020-06-24 15:18:10 -05009function show_usage {
10 cat \
Patrick Williams9ede18b2022-03-12 07:55:36 -060011 << EOF
Patrick Williams847a0c32020-06-24 15:18:10 -050012Usage: $(basename "$0") [options] <command-args>*
13
14Generate meson.build files from a directory tree containing YAML files and
15facilitate building the sdbus++ sources.
16
17Options:
18 --help - Display this message
19 --command <cmd> - Command mode to execute (default 'meson').
20 --directory <path> - Root directory of the YAML source (default '.').
21 --output <path> - Root directory of the output (default '.').
22 --tool <path> - Path to the processing tool (default 'sdbus++').
23 --version - Display this tool's version string.
24
25Commands:
26 meson - Generate a tree of meson.build files corresponding
27 to the source YAML files.
28 cpp <intf> - Generate the source files from a YAML interface.
29 markdown <intf> - Generate the markdown files from a YAML interface.
30 version - Display this tool's version string.
31
32EOF
33}
34
35## The version is somewhat arbitrary but is used to create a warning message
36## if a repository contains old copies of the generated meson.build files and
37## needs an update. We should increment the version number whenever the
38## resulting meson.build would change.
William A. Kennington III293c8a22022-09-02 14:35:54 -070039tool_version="sdbus++-gen-meson version 4"
Patrick Williams847a0c32020-06-24 15:18:10 -050040function show_version {
Patrick Williamsd77548a2022-04-29 14:43:15 -050041 echo "${tool_version}"
Patrick Williams847a0c32020-06-24 15:18:10 -050042}
43
44# Set up defaults.
45sdbuspp="sdbus++"
46outputdir="."
47cmd="meson"
48rootdir="."
49
50# Parse options.
51options="$(getopt -o hc:d:o:t:v --long help,command:,directory:,output:,tool:,version -- "$@")"
Patrick Williamsd77548a2022-04-29 14:43:15 -050052eval set -- "${options}"
Patrick Williams847a0c32020-06-24 15:18:10 -050053
Patrick Williams9ede18b2022-03-12 07:55:36 -060054while true; do
Patrick Williams847a0c32020-06-24 15:18:10 -050055 case "$1" in
56 -h | --help)
57 show_usage
58 exit
59 ;;
60
61 -c | --command)
62 shift
63 cmd="$1"
64 shift
65 ;;
66
67 -d | --directory)
68 shift
69 rootdir="$1"
70 shift
71 ;;
72
73 -o | --output)
74 shift
75 outputdir="$1"
76 shift
77 ;;
78
79 -t | --tool)
80 shift
81 sdbuspp="$1"
82 shift
83 ;;
84
85 -v | --version)
86 show_version
87 exit
88 ;;
89
90 --)
91 shift
92 break
93 ;;
Patrick Williamsd77548a2022-04-29 14:43:15 -050094
95 *)
96 echo "Invalid argument $1"
97 exit 1
98 ;;
Patrick Williams847a0c32020-06-24 15:18:10 -050099 esac
100done
101
102## Create an initially empty meson.build file.
103## $1 - path to create meson.build at.
104function meson_empty_file {
105 mkdir -p "$1"
106 echo "# Generated file; do not modify." > "$1/meson.build"
107}
108
109## Create the root-level meson.build
110##
111## Inserts rules to run the available version of this tool to ensure the
112## version has not changed.
113function meson_create_root {
Patrick Williamsd77548a2022-04-29 14:43:15 -0500114 meson_empty_file "${outputdir}"
Patrick Williams847a0c32020-06-24 15:18:10 -0500115
Patrick Williamsd77548a2022-04-29 14:43:15 -0500116 cat >> "${outputdir}/meson.build" \
Patrick Williams9ede18b2022-03-12 07:55:36 -0600117 << EOF
Patrick Williams847a0c32020-06-24 15:18:10 -0500118sdbuspp_gen_meson_ver = run_command(
119 sdbuspp_gen_meson_prog,
120 '--version',
121).stdout().strip().split('\n')[0]
122
Patrick Williamsd77548a2022-04-29 14:43:15 -0500123if sdbuspp_gen_meson_ver != '${tool_version}'
Patrick Williams847a0c32020-06-24 15:18:10 -0500124 warning('Generated meson files from wrong version of sdbus++-gen-meson.')
125 warning(
Patrick Williamsd77548a2022-04-29 14:43:15 -0500126 'Expected "${tool_version}", got:',
Patrick Williams847a0c32020-06-24 15:18:10 -0500127 sdbuspp_gen_meson_ver
128 )
129endif
130
131EOF
132}
133
134## hash-tables to store:
135## meson_paths - list of subdirectory paths for which an empty meson.build
136## has already been created.
137## interfaces - list of interface paths which a YAML has been found and
138## which YAML types (interface, errors, etc.).
139declare -A meson_paths
140declare -A interfaces
141
142## Ensure the meson.build files to a path have been created.
143## $1 - The path requiring to be created.
144function meson_create_path {
145
Patrick Williamsd77548a2022-04-29 14:43:15 -0500146 meson_path="${outputdir}"
Patrick Williams847a0c32020-06-24 15:18:10 -0500147 prev_meson_path=""
148
149 # Split the path into segments.
Patrick Williams9ede18b2022-03-12 07:55:36 -0600150 for part in $(echo "$1" | tr '/' '\n'); do
Patrick Williamsd77548a2022-04-29 14:43:15 -0500151 prev_meson_path="${meson_path}"
152 meson_path="${meson_path}/${part}"
Patrick Williams847a0c32020-06-24 15:18:10 -0500153
154 # Create the meson.build for this segment if it doesn't already exist.
Patrick Williamsd77548a2022-04-29 14:43:15 -0500155 if [[ "" == "${meson_paths[${meson_path}]}" ]]; then
156 meson_paths["${meson_path}"]="1"
157 meson_empty_file "${meson_path}"
Patrick Williams847a0c32020-06-24 15:18:10 -0500158
159 # Add the 'subdir' link into the parent's meson.build.
160 # We need to skip adding the links into the 'root' meson.build
161 # because most repositories want to selectively add TLDs based
162 # on config flags. Let them figure out their own logic for that.
Patrick Williamsd77548a2022-04-29 14:43:15 -0500163 if [[ ${outputdir} != "${prev_meson_path}" ]]; then
164 echo "subdir('${part}')" >> "${prev_meson_path}/meson.build"
Patrick Williams847a0c32020-06-24 15:18:10 -0500165 fi
166 fi
167 done
168}
169
170## Generate the meson target for the source files (.cpp/.hpp) from a YAML
171## interface.
172##
173## $1 - The interface to generate a target for.
174function meson_cpp_target {
Patrick Williamsd77548a2022-04-29 14:43:15 -0500175 mesondir="${outputdir}/$1"
176 yamldir="$(realpath --relative-to="${mesondir}" "${rootdir}")"
Patrick Williams847a0c32020-06-24 15:18:10 -0500177
178 # Determine the source and output files based on the YAMLs present.
179 sources=""
180 outputs=""
Patrick Williams9ede18b2022-03-12 07:55:36 -0600181 for s in ${interfaces[$1]}; do
Patrick Williamsd77548a2022-04-29 14:43:15 -0500182 sources="'${yamldir}/$1.${s}', "
Patrick Williams847a0c32020-06-24 15:18:10 -0500183
Patrick Williamsd77548a2022-04-29 14:43:15 -0500184 case "${s}" in
Patrick Williams847a0c32020-06-24 15:18:10 -0500185 errors.yaml)
186 outputs="${outputs}'error.cpp', 'error.hpp', "
187 ;;
188
189 interface.yaml)
190 outputs="${outputs}'server.cpp', 'server.hpp', "
191 outputs="${outputs}'client.hpp', "
192 ;;
Patrick Williamsd77548a2022-04-29 14:43:15 -0500193
194 *)
195 echo "Unknown interface type: ${s}"
196 exit 1
197 ;;
Patrick Williams847a0c32020-06-24 15:18:10 -0500198 esac
199 done
200
201 # Create the target to generate the 'outputs'.
Patrick Williamsd77548a2022-04-29 14:43:15 -0500202 cat >> "${mesondir}/meson.build" \
Patrick Williams9ede18b2022-03-12 07:55:36 -0600203 << EOF
Patrick Williams847a0c32020-06-24 15:18:10 -0500204generated_sources += custom_target(
205 '$1__cpp'.underscorify(),
Patrick Williamsd77548a2022-04-29 14:43:15 -0500206 input: [ ${sources} ],
207 output: [ ${outputs} ],
William A. Kennington III293c8a22022-09-02 14:35:54 -0700208 depend_files: sdbusplusplus_depfiles,
Patrick Williams847a0c32020-06-24 15:18:10 -0500209 command: [
210 sdbuspp_gen_meson_prog, '--command', 'cpp',
211 '--output', meson.current_build_dir(),
212 '--tool', sdbusplusplus_prog,
Patrick Williamsd77548a2022-04-29 14:43:15 -0500213 '--directory', meson.current_source_dir() / '${yamldir}',
Patrick Williams847a0c32020-06-24 15:18:10 -0500214 '$1',
215 ],
216)
217
218EOF
219}
220
221## Generate the meson target for the markdown files from a YAML interface.
222## $1 - The interface to generate a target for.
223function meson_md_target {
Patrick Williamsd77548a2022-04-29 14:43:15 -0500224 mesondir="${outputdir}/$(dirname "$1")"
225 yamldir="$(realpath --relative-to="${mesondir}" "${rootdir}")"
Patrick Williams847a0c32020-06-24 15:18:10 -0500226
227 # Determine the source files based on the YAMLs present.
228 sources=""
Patrick Williams9ede18b2022-03-12 07:55:36 -0600229 for s in ${interfaces[$1]}; do
Patrick Williamsd77548a2022-04-29 14:43:15 -0500230 sources="'${yamldir}/$1.${s}', "
Patrick Williams847a0c32020-06-24 15:18:10 -0500231 done
232
233 # Create the target to generate the interface.md file.
Patrick Williamsd77548a2022-04-29 14:43:15 -0500234 cat >> "${mesondir}/meson.build" \
Patrick Williams9ede18b2022-03-12 07:55:36 -0600235 << EOF
Patrick Williams847a0c32020-06-24 15:18:10 -0500236generated_others += custom_target(
237 '$1__markdown'.underscorify(),
Patrick Williamsd77548a2022-04-29 14:43:15 -0500238 input: [ ${sources} ],
Patrick Williams847a0c32020-06-24 15:18:10 -0500239 output: [ '$(basename "$1").md' ],
William A. Kennington III293c8a22022-09-02 14:35:54 -0700240 depend_files: sdbusplusplus_depfiles,
Patrick Williams847a0c32020-06-24 15:18:10 -0500241 command: [
242 sdbuspp_gen_meson_prog, '--command', 'markdown',
243 '--output', meson.current_build_dir(),
244 '--tool', sdbusplusplus_prog,
Patrick Williamsd77548a2022-04-29 14:43:15 -0500245 '--directory', meson.current_source_dir() / '${yamldir}',
Patrick Williams847a0c32020-06-24 15:18:10 -0500246 '$1',
247 ],
Patrick Williams847a0c32020-06-24 15:18:10 -0500248)
249
250EOF
251}
252
253## Handle command=meson by generating the tree of meson.build files.
254function cmd_meson {
255 TLDs="com net org xyz"
256 yamls=""
257
258 # Find all the YAML files in the TLD subdirectories.
Patrick Williamsd77548a2022-04-29 14:43:15 -0500259 for d in ${TLDs}; do
260 dir="${rootdir}/${d}"
261 if [[ ! -d ${dir} ]]; then
Patrick Williams847a0c32020-06-24 15:18:10 -0500262 continue
263 fi
264
265 yamls="\
Patrick Williamsd77548a2022-04-29 14:43:15 -0500266 ${yamls} \
267 $(find "${dir}" -name '*.interface.yaml' -o -name '*.errors.yaml') \
Patrick Williams847a0c32020-06-24 15:18:10 -0500268 "
269 done
270
271 # Sort YAMLs
Patrick Williamsd77548a2022-04-29 14:43:15 -0500272 # shellcheck disable=SC2312
273 yamls="$(echo "${yamls}" | tr " " "\n" | sort)"
Patrick Williams847a0c32020-06-24 15:18:10 -0500274
275 # Assign the YAML files into the hash-table by interface name.
Patrick Williamsd77548a2022-04-29 14:43:15 -0500276 for y in ${yamls}; do
277 rel="$(realpath "--relative-to=${rootdir}" "${y}")"
278 dir="$(dirname "${rel}")"
Patrick Williams847a0c32020-06-24 15:18:10 -0500279 ext="${rel#*.}"
Patrick Williamsd77548a2022-04-29 14:43:15 -0500280 base="$(basename "${rel}" ".${ext}")"
281 key="${dir}/${base}"
Patrick Williams847a0c32020-06-24 15:18:10 -0500282
Patrick Williamsd77548a2022-04-29 14:43:15 -0500283 interfaces["${key}"]="${interfaces[${key}]} ${ext}"
Patrick Williams847a0c32020-06-24 15:18:10 -0500284 done
285
286 # Create the meson.build files.
287 meson_create_root
Patrick Williamsd77548a2022-04-29 14:43:15 -0500288 # shellcheck disable=SC2312
Patrick Williams847a0c32020-06-24 15:18:10 -0500289 sorted_ifaces="$(echo "${!interfaces[@]}" | tr " " "\n" | sort)"
Patrick Williams9ede18b2022-03-12 07:55:36 -0600290 for i in ${sorted_ifaces}; do
Patrick Williamsd77548a2022-04-29 14:43:15 -0500291 meson_create_path "${i}"
292 meson_cpp_target "${i}"
293 meson_md_target "${i}"
Patrick Williams847a0c32020-06-24 15:18:10 -0500294 done
295}
296
297## Handle command=cpp by calling sdbus++ as appropriate.
298## $1 - interface to generate.
299##
300## For an interface foo/bar, the outputdir is expected to be foo/bar.
301function cmd_cpp {
302
Patrick Williamsd77548a2022-04-29 14:43:15 -0500303 if [[ "" == "$1" ]]; then
Patrick Williams847a0c32020-06-24 15:18:10 -0500304 show_usage
305 exit 1
306 fi
307
Patrick Williamsd77548a2022-04-29 14:43:15 -0500308 if [[ ! -e "${rootdir}/$1.interface.yaml" ]] &&
309 [[ ! -e "${rootdir}/$1.errors.yaml" ]]; then
Patrick Williams847a0c32020-06-24 15:18:10 -0500310 echo "Missing YAML for $1."
311 exit 1
312 fi
313
Patrick Williamsd77548a2022-04-29 14:43:15 -0500314 mkdir -p "${outputdir}"
Patrick Williams847a0c32020-06-24 15:18:10 -0500315
Patrick Williamsd77548a2022-04-29 14:43:15 -0500316 sdbusppcmd="${sdbuspp} -r ${rootdir}"
Patrick Williams847a0c32020-06-24 15:18:10 -0500317 intf="${1//\//.}"
318
Patrick Williamsd77548a2022-04-29 14:43:15 -0500319 if [[ -e "${rootdir}/$1.interface.yaml" ]]; then
320 ${sdbusppcmd} interface server-header "${intf}" > "${outputdir}/server.hpp"
321 ${sdbusppcmd} interface server-cpp "${intf}" > "${outputdir}/server.cpp"
322 ${sdbusppcmd} interface client-header "${intf}" > "${outputdir}/client.hpp"
Patrick Williams847a0c32020-06-24 15:18:10 -0500323 fi
324
Patrick Williamsd77548a2022-04-29 14:43:15 -0500325 if [[ -e "${rootdir}/$1.errors.yaml" ]]; then
326 ${sdbusppcmd} error exception-header "${intf}" > "${outputdir}/error.hpp"
327 ${sdbusppcmd} error exception-cpp "${intf}" > "${outputdir}/error.cpp"
Patrick Williams847a0c32020-06-24 15:18:10 -0500328 fi
329}
330
331## Handle command=markdown by calling sdbus++ as appropriate.
332## $1 - interface to generate.
333##
334## For an interface foo/bar, the outputdir is expected to be foo.
335function cmd_markdown {
336
Patrick Williamsd77548a2022-04-29 14:43:15 -0500337 if [[ "" == "$1" ]]; then
Patrick Williams847a0c32020-06-24 15:18:10 -0500338 show_usage
339 exit 1
340 fi
341
Patrick Williamsd77548a2022-04-29 14:43:15 -0500342 if [[ ! -e "${rootdir}/$1.interface.yaml" ]] &&
343 [[ ! -e "${rootdir}/$1.errors.yaml" ]]; then
Patrick Williams847a0c32020-06-24 15:18:10 -0500344 echo "Missing YAML for $1."
345 exit 1
346 fi
347
Patrick Williamsd77548a2022-04-29 14:43:15 -0500348 mkdir -p "${outputdir}"
Patrick Williams847a0c32020-06-24 15:18:10 -0500349
Patrick Williamsd77548a2022-04-29 14:43:15 -0500350 sdbusppcmd="${sdbuspp} -r ${rootdir}"
Patrick Williams847a0c32020-06-24 15:18:10 -0500351 intf="${1//\//.}"
352 base="$(basename "$1")"
353
Patrick Williamsd77548a2022-04-29 14:43:15 -0500354 echo -n > "${outputdir}/${base}.md"
355 if [[ -e "${rootdir}/$1.interface.yaml" ]]; then
356 ${sdbusppcmd} interface markdown "${intf}" >> "${outputdir}/${base}.md"
Patrick Williams847a0c32020-06-24 15:18:10 -0500357 fi
358
Patrick Williamsd77548a2022-04-29 14:43:15 -0500359 if [[ -e "${rootdir}/$1.errors.yaml" ]]; then
360 ${sdbusppcmd} error markdown "${intf}" >> "${outputdir}/${base}.md"
Patrick Williams847a0c32020-06-24 15:18:10 -0500361 fi
362}
363
364## Handle command=version.
365function cmd_version {
366 show_version
367}
368
Patrick Williamsd77548a2022-04-29 14:43:15 -0500369"cmd_${cmd}" "$*"