blob: 2de203295ebb6db210da28beb28e735c6958ff8c [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 Williams018b8ff2022-12-05 16:03:46 -06009function show_usage() {
Patrick Williams847a0c32020-06-24 15:18:10 -050010 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.
Patrick Williams6403d562023-08-18 11:34:43 -050039tool_version="sdbus++-gen-meson version 7"
Patrick Williams018b8ff2022-12-05 16:03:46 -060040function 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.
Patrick Williams018b8ff2022-12-05 16:03:46 -0600104function meson_empty_file() {
Patrick Williams847a0c32020-06-24 15:18:10 -0500105 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.
Patrick Williams018b8ff2022-12-05 16:03:46 -0600113function 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',
Ed Tanous60a94302023-01-06 13:52:45 -0800121 check: true,
Patrick Williams847a0c32020-06-24 15:18:10 -0500122).stdout().strip().split('\n')[0]
123
Patrick Williamsd77548a2022-04-29 14:43:15 -0500124if sdbuspp_gen_meson_ver != '${tool_version}'
Patrick Williams847a0c32020-06-24 15:18:10 -0500125 warning('Generated meson files from wrong version of sdbus++-gen-meson.')
126 warning(
Patrick Williamsd77548a2022-04-29 14:43:15 -0500127 'Expected "${tool_version}", got:',
Patrick Williams847a0c32020-06-24 15:18:10 -0500128 sdbuspp_gen_meson_ver
129 )
130endif
131
132EOF
133}
134
135## hash-tables to store:
136## meson_paths - list of subdirectory paths for which an empty meson.build
137## has already been created.
138## interfaces - list of interface paths which a YAML has been found and
139## which YAML types (interface, errors, etc.).
140declare -A meson_paths
141declare -A interfaces
142
143## Ensure the meson.build files to a path have been created.
144## $1 - The path requiring to be created.
Patrick Williams018b8ff2022-12-05 16:03:46 -0600145function meson_create_path() {
Patrick Williams847a0c32020-06-24 15:18:10 -0500146
Patrick Williamsd77548a2022-04-29 14:43:15 -0500147 meson_path="${outputdir}"
Patrick Williams847a0c32020-06-24 15:18:10 -0500148 prev_meson_path=""
149
150 # Split the path into segments.
Patrick Williams9ede18b2022-03-12 07:55:36 -0600151 for part in $(echo "$1" | tr '/' '\n'); do
Patrick Williamsd77548a2022-04-29 14:43:15 -0500152 prev_meson_path="${meson_path}"
153 meson_path="${meson_path}/${part}"
Patrick Williams847a0c32020-06-24 15:18:10 -0500154
155 # Create the meson.build for this segment if it doesn't already exist.
Patrick Williamsd77548a2022-04-29 14:43:15 -0500156 if [[ "" == "${meson_paths[${meson_path}]}" ]]; then
157 meson_paths["${meson_path}"]="1"
158 meson_empty_file "${meson_path}"
Patrick Williams847a0c32020-06-24 15:18:10 -0500159
160 # Add the 'subdir' link into the parent's meson.build.
161 # We need to skip adding the links into the 'root' meson.build
162 # because most repositories want to selectively add TLDs based
163 # on config flags. Let them figure out their own logic for that.
Patrick Williamsd77548a2022-04-29 14:43:15 -0500164 if [[ ${outputdir} != "${prev_meson_path}" ]]; then
165 echo "subdir('${part}')" >> "${prev_meson_path}/meson.build"
Patrick Williams847a0c32020-06-24 15:18:10 -0500166 fi
167 fi
168 done
169}
170
171## Generate the meson target for the source files (.cpp/.hpp) from a YAML
172## interface.
173##
174## $1 - The interface to generate a target for.
Patrick Williams018b8ff2022-12-05 16:03:46 -0600175function meson_cpp_target() {
Patrick Williamsd77548a2022-04-29 14:43:15 -0500176 mesondir="${outputdir}/$1"
177 yamldir="$(realpath --relative-to="${mesondir}" "${rootdir}")"
Patrick Williams847a0c32020-06-24 15:18:10 -0500178
179 # Determine the source and output files based on the YAMLs present.
180 sources=""
181 outputs=""
Patrick Williams9ede18b2022-03-12 07:55:36 -0600182 for s in ${interfaces[$1]}; do
Patrick Williamsd77548a2022-04-29 14:43:15 -0500183 sources="'${yamldir}/$1.${s}', "
Patrick Williams847a0c32020-06-24 15:18:10 -0500184
Patrick Williamsd77548a2022-04-29 14:43:15 -0500185 case "${s}" in
Patrick Williams847a0c32020-06-24 15:18:10 -0500186 errors.yaml)
187 outputs="${outputs}'error.cpp', 'error.hpp', "
188 ;;
189
190 interface.yaml)
Patrick Williams1caa5e82023-04-19 16:24:38 -0500191 outputs="${outputs}'common.hpp', "
Patrick Williams847a0c32020-06-24 15:18:10 -0500192 outputs="${outputs}'server.cpp', 'server.hpp', "
Patrick Williams6403d562023-08-18 11:34:43 -0500193 outputs="${outputs}'aserver.hpp', "
Patrick Williams847a0c32020-06-24 15:18:10 -0500194 outputs="${outputs}'client.hpp', "
195 ;;
Patrick Williamsd77548a2022-04-29 14:43:15 -0500196
197 *)
198 echo "Unknown interface type: ${s}"
199 exit 1
200 ;;
Patrick Williams847a0c32020-06-24 15:18:10 -0500201 esac
202 done
203
204 # Create the target to generate the 'outputs'.
Patrick Williamsd77548a2022-04-29 14:43:15 -0500205 cat >> "${mesondir}/meson.build" \
Patrick Williams9ede18b2022-03-12 07:55:36 -0600206 << EOF
Patrick Williams847a0c32020-06-24 15:18:10 -0500207generated_sources += custom_target(
208 '$1__cpp'.underscorify(),
Patrick Williamsd77548a2022-04-29 14:43:15 -0500209 input: [ ${sources} ],
210 output: [ ${outputs} ],
William A. Kennington III293c8a22022-09-02 14:35:54 -0700211 depend_files: sdbusplusplus_depfiles,
Patrick Williams847a0c32020-06-24 15:18:10 -0500212 command: [
213 sdbuspp_gen_meson_prog, '--command', 'cpp',
214 '--output', meson.current_build_dir(),
215 '--tool', sdbusplusplus_prog,
Patrick Williamsd77548a2022-04-29 14:43:15 -0500216 '--directory', meson.current_source_dir() / '${yamldir}',
Patrick Williams847a0c32020-06-24 15:18:10 -0500217 '$1',
218 ],
219)
220
221EOF
222}
223
224## Generate the meson target for the markdown files from a YAML interface.
225## $1 - The interface to generate a target for.
Patrick Williams018b8ff2022-12-05 16:03:46 -0600226function meson_md_target() {
Patrick Williamsd77548a2022-04-29 14:43:15 -0500227 mesondir="${outputdir}/$(dirname "$1")"
228 yamldir="$(realpath --relative-to="${mesondir}" "${rootdir}")"
Patrick Williams847a0c32020-06-24 15:18:10 -0500229
230 # Determine the source files based on the YAMLs present.
231 sources=""
Patrick Williams9ede18b2022-03-12 07:55:36 -0600232 for s in ${interfaces[$1]}; do
Patrick Williamsd77548a2022-04-29 14:43:15 -0500233 sources="'${yamldir}/$1.${s}', "
Patrick Williams847a0c32020-06-24 15:18:10 -0500234 done
235
236 # Create the target to generate the interface.md file.
Patrick Williamsd77548a2022-04-29 14:43:15 -0500237 cat >> "${mesondir}/meson.build" \
Patrick Williams9ede18b2022-03-12 07:55:36 -0600238 << EOF
Patrick Williams847a0c32020-06-24 15:18:10 -0500239generated_others += custom_target(
240 '$1__markdown'.underscorify(),
Patrick Williamsd77548a2022-04-29 14:43:15 -0500241 input: [ ${sources} ],
Patrick Williams847a0c32020-06-24 15:18:10 -0500242 output: [ '$(basename "$1").md' ],
William A. Kennington III293c8a22022-09-02 14:35:54 -0700243 depend_files: sdbusplusplus_depfiles,
Patrick Williams847a0c32020-06-24 15:18:10 -0500244 command: [
245 sdbuspp_gen_meson_prog, '--command', 'markdown',
246 '--output', meson.current_build_dir(),
247 '--tool', sdbusplusplus_prog,
Patrick Williamsd77548a2022-04-29 14:43:15 -0500248 '--directory', meson.current_source_dir() / '${yamldir}',
Patrick Williams847a0c32020-06-24 15:18:10 -0500249 '$1',
250 ],
Patrick Williams847a0c32020-06-24 15:18:10 -0500251)
252
253EOF
254}
255
256## Handle command=meson by generating the tree of meson.build files.
Patrick Williams018b8ff2022-12-05 16:03:46 -0600257function cmd_meson() {
Willam A. Kennington IIIce8d16d2022-09-07 15:46:40 -0700258 # Find and sort all the YAML files
259 yamls="$(find "${rootdir}" -name '*.interface.yaml' -o -name '*.errors.yaml')"
260 yamls="$(echo "${yamls}" | sort)"
Patrick Williams847a0c32020-06-24 15:18:10 -0500261
262 # Assign the YAML files into the hash-table by interface name.
Patrick Williamsd77548a2022-04-29 14:43:15 -0500263 for y in ${yamls}; do
264 rel="$(realpath "--relative-to=${rootdir}" "${y}")"
265 dir="$(dirname "${rel}")"
Patrick Williams847a0c32020-06-24 15:18:10 -0500266 ext="${rel#*.}"
Patrick Williamsd77548a2022-04-29 14:43:15 -0500267 base="$(basename "${rel}" ".${ext}")"
268 key="${dir}/${base}"
Patrick Williams847a0c32020-06-24 15:18:10 -0500269
Patrick Williamsd77548a2022-04-29 14:43:15 -0500270 interfaces["${key}"]="${interfaces[${key}]} ${ext}"
Patrick Williams847a0c32020-06-24 15:18:10 -0500271 done
272
273 # Create the meson.build files.
274 meson_create_root
Patrick Williamsd77548a2022-04-29 14:43:15 -0500275 # shellcheck disable=SC2312
Patrick Williams847a0c32020-06-24 15:18:10 -0500276 sorted_ifaces="$(echo "${!interfaces[@]}" | tr " " "\n" | sort)"
Patrick Williams9ede18b2022-03-12 07:55:36 -0600277 for i in ${sorted_ifaces}; do
Patrick Williamsd77548a2022-04-29 14:43:15 -0500278 meson_create_path "${i}"
279 meson_cpp_target "${i}"
280 meson_md_target "${i}"
Patrick Williams847a0c32020-06-24 15:18:10 -0500281 done
282}
283
284## Handle command=cpp by calling sdbus++ as appropriate.
285## $1 - interface to generate.
286##
287## For an interface foo/bar, the outputdir is expected to be foo/bar.
Patrick Williams018b8ff2022-12-05 16:03:46 -0600288function cmd_cpp() {
Patrick Williams847a0c32020-06-24 15:18:10 -0500289
Patrick Williamsd77548a2022-04-29 14:43:15 -0500290 if [[ "" == "$1" ]]; then
Patrick Williams847a0c32020-06-24 15:18:10 -0500291 show_usage
292 exit 1
293 fi
294
Patrick Williamsd77548a2022-04-29 14:43:15 -0500295 if [[ ! -e "${rootdir}/$1.interface.yaml" ]] &&
Patrick Williams018b8ff2022-12-05 16:03:46 -0600296 [[ ! -e "${rootdir}/$1.errors.yaml" ]]; then
Patrick Williams847a0c32020-06-24 15:18:10 -0500297 echo "Missing YAML for $1."
298 exit 1
299 fi
300
Patrick Williamsd77548a2022-04-29 14:43:15 -0500301 mkdir -p "${outputdir}"
Patrick Williams847a0c32020-06-24 15:18:10 -0500302
Patrick Williamsd77548a2022-04-29 14:43:15 -0500303 sdbusppcmd="${sdbuspp} -r ${rootdir}"
Patrick Williams847a0c32020-06-24 15:18:10 -0500304 intf="${1//\//.}"
305
Patrick Williamsd77548a2022-04-29 14:43:15 -0500306 if [[ -e "${rootdir}/$1.interface.yaml" ]]; then
Patrick Williams1caa5e82023-04-19 16:24:38 -0500307 ${sdbusppcmd} interface common-header "${intf}" > "${outputdir}/common.hpp"
Patrick Williamsd77548a2022-04-29 14:43:15 -0500308 ${sdbusppcmd} interface server-header "${intf}" > "${outputdir}/server.hpp"
309 ${sdbusppcmd} interface server-cpp "${intf}" > "${outputdir}/server.cpp"
310 ${sdbusppcmd} interface client-header "${intf}" > "${outputdir}/client.hpp"
Patrick Williams6403d562023-08-18 11:34:43 -0500311 ${sdbusppcmd} interface aserver-header "${intf}" > "${outputdir}/aserver.hpp"
Patrick Williams847a0c32020-06-24 15:18:10 -0500312 fi
313
Patrick Williamsd77548a2022-04-29 14:43:15 -0500314 if [[ -e "${rootdir}/$1.errors.yaml" ]]; then
315 ${sdbusppcmd} error exception-header "${intf}" > "${outputdir}/error.hpp"
316 ${sdbusppcmd} error exception-cpp "${intf}" > "${outputdir}/error.cpp"
Patrick Williams847a0c32020-06-24 15:18:10 -0500317 fi
318}
319
320## Handle command=markdown by calling sdbus++ as appropriate.
321## $1 - interface to generate.
322##
323## For an interface foo/bar, the outputdir is expected to be foo.
Patrick Williams018b8ff2022-12-05 16:03:46 -0600324function cmd_markdown() {
Patrick Williams847a0c32020-06-24 15:18:10 -0500325
Patrick Williamsd77548a2022-04-29 14:43:15 -0500326 if [[ "" == "$1" ]]; then
Patrick Williams847a0c32020-06-24 15:18:10 -0500327 show_usage
328 exit 1
329 fi
330
Patrick Williamsd77548a2022-04-29 14:43:15 -0500331 if [[ ! -e "${rootdir}/$1.interface.yaml" ]] &&
Patrick Williams018b8ff2022-12-05 16:03:46 -0600332 [[ ! -e "${rootdir}/$1.errors.yaml" ]]; then
Patrick Williams847a0c32020-06-24 15:18:10 -0500333 echo "Missing YAML for $1."
334 exit 1
335 fi
336
Patrick Williamsd77548a2022-04-29 14:43:15 -0500337 mkdir -p "${outputdir}"
Patrick Williams847a0c32020-06-24 15:18:10 -0500338
Patrick Williamsd77548a2022-04-29 14:43:15 -0500339 sdbusppcmd="${sdbuspp} -r ${rootdir}"
Patrick Williams847a0c32020-06-24 15:18:10 -0500340 intf="${1//\//.}"
341 base="$(basename "$1")"
342
Patrick Williamsd77548a2022-04-29 14:43:15 -0500343 echo -n > "${outputdir}/${base}.md"
344 if [[ -e "${rootdir}/$1.interface.yaml" ]]; then
345 ${sdbusppcmd} interface markdown "${intf}" >> "${outputdir}/${base}.md"
Patrick Williams847a0c32020-06-24 15:18:10 -0500346 fi
347
Patrick Williamsd77548a2022-04-29 14:43:15 -0500348 if [[ -e "${rootdir}/$1.errors.yaml" ]]; then
349 ${sdbusppcmd} error markdown "${intf}" >> "${outputdir}/${base}.md"
Patrick Williams847a0c32020-06-24 15:18:10 -0500350 fi
351}
352
353## Handle command=version.
Patrick Williams018b8ff2022-12-05 16:03:46 -0600354function cmd_version() {
Patrick Williams847a0c32020-06-24 15:18:10 -0500355 show_version
356}
357
Patrick Williamsd77548a2022-04-29 14:43:15 -0500358"cmd_${cmd}" "$*"