xref: /openbmc/sdbusplus/tools/sdbus++-gen-meson (revision d77548a1)
1847a0c37SPatrick Williams#!/usr/bin/env bash
2847a0c37SPatrick Williams
3847a0c37SPatrick Williamsset -e
4847a0c37SPatrick Williams
5a4c9edc1SPatrick Williams# Locale can change behavior of utilities like 'sort' but we want the output
6a4c9edc1SPatrick Williams# to be stable on all machines.  Force the locale to 'C' for consistency.
7a4c9edc1SPatrick Williamsexport LC_ALL=C
8a4c9edc1SPatrick Williams
9847a0c37SPatrick Williamsfunction show_usage {
10847a0c37SPatrick Williams    cat \
11847a0c37SPatrick Williams        << EOF
12847a0c37SPatrick WilliamsUsage: $(basename "$0") [options] <command-args>*
13847a0c37SPatrick Williams
14847a0c37SPatrick WilliamsGenerate meson.build files from a directory tree containing YAML files and
15847a0c37SPatrick Williamsfacilitate building the sdbus++ sources.
16847a0c37SPatrick Williams
17847a0c37SPatrick WilliamsOptions:
18847a0c37SPatrick Williams    --help              - Display this message
19847a0c37SPatrick Williams    --command <cmd>     - Command mode to execute (default 'meson').
20847a0c37SPatrick Williams    --directory <path>  - Root directory of the YAML source (default '.').
21847a0c37SPatrick Williams    --output <path>     - Root directory of the output (default '.').
22847a0c37SPatrick Williams    --tool <path>       - Path to the processing tool (default 'sdbus++').
23847a0c37SPatrick Williams    --version           - Display this tool's version string.
24847a0c37SPatrick Williams
25847a0c37SPatrick WilliamsCommands:
26847a0c37SPatrick Williams    meson               - Generate a tree of meson.build files corresponding
27847a0c37SPatrick Williams                          to the source YAML files.
28847a0c37SPatrick Williams    cpp <intf>          - Generate the source files from a YAML interface.
29847a0c37SPatrick Williams    markdown <intf>     - Generate the markdown files from a YAML interface.
30847a0c37SPatrick Williams    version             - Display this tool's version string.
31847a0c37SPatrick Williams
32847a0c37SPatrick WilliamsEOF
33847a0c37SPatrick Williams}
34847a0c37SPatrick Williams
35847a0c37SPatrick Williams## The version is somewhat arbitrary but is used to create a warning message
36847a0c37SPatrick Williams## if a repository contains old copies of the generated meson.build files and
37847a0c37SPatrick Williams## needs an update.  We should increment the version number whenever the
38847a0c37SPatrick Williams## resulting meson.build would change.
39a42e97a6SWilliam A. Kennington IIItool_version="sdbus++-gen-meson version 3"
40847a0c37SPatrick Williamsfunction show_version {
41*d77548a1SPatrick Williams    echo "${tool_version}"
42847a0c37SPatrick Williams}
43847a0c37SPatrick Williams
44847a0c37SPatrick Williams# Set up defaults.
45847a0c37SPatrick Williamssdbuspp="sdbus++"
46847a0c37SPatrick Williamsoutputdir="."
47847a0c37SPatrick Williamscmd="meson"
48847a0c37SPatrick Williamsrootdir="."
49847a0c37SPatrick Williams
50847a0c37SPatrick Williams# Parse options.
51847a0c37SPatrick Williamsoptions="$(getopt -o hc:d:o:t:v --long help,command:,directory:,output:,tool:,version -- "$@")"
52*d77548a1SPatrick Williamseval set -- "${options}"
53847a0c37SPatrick Williams
549ede18b4SPatrick Williamswhile true; do
55847a0c37SPatrick Williams    case "$1" in
56847a0c37SPatrick Williams        -h | --help)
57847a0c37SPatrick Williams            show_usage
58847a0c37SPatrick Williams            exit
59847a0c37SPatrick Williams            ;;
60847a0c37SPatrick Williams
61847a0c37SPatrick Williams        -c | --command)
62847a0c37SPatrick Williams            shift
63847a0c37SPatrick Williams            cmd="$1"
64847a0c37SPatrick Williams            shift
65847a0c37SPatrick Williams            ;;
66847a0c37SPatrick Williams
67847a0c37SPatrick Williams        -d | --directory)
68847a0c37SPatrick Williams            shift
69847a0c37SPatrick Williams            rootdir="$1"
70847a0c37SPatrick Williams            shift
71847a0c37SPatrick Williams            ;;
72847a0c37SPatrick Williams
73847a0c37SPatrick Williams        -o | --output)
74847a0c37SPatrick Williams            shift
75847a0c37SPatrick Williams            outputdir="$1"
76847a0c37SPatrick Williams            shift
77847a0c37SPatrick Williams            ;;
78847a0c37SPatrick Williams
79847a0c37SPatrick Williams        -t | --tool)
80847a0c37SPatrick Williams            shift
81847a0c37SPatrick Williams            sdbuspp="$1"
82847a0c37SPatrick Williams            shift
83847a0c37SPatrick Williams            ;;
84847a0c37SPatrick Williams
85847a0c37SPatrick Williams        -v | --version)
86847a0c37SPatrick Williams            show_version
87847a0c37SPatrick Williams            exit
88847a0c37SPatrick Williams            ;;
89847a0c37SPatrick Williams
90847a0c37SPatrick Williams        --)
91847a0c37SPatrick Williams            shift
92847a0c37SPatrick Williams            break
93847a0c37SPatrick Williams            ;;
94*d77548a1SPatrick Williams
95*d77548a1SPatrick Williams        *)
96*d77548a1SPatrick Williams            echo "Invalid argument $1"
97*d77548a1SPatrick Williams            exit 1
98*d77548a1SPatrick Williams            ;;
99847a0c37SPatrick Williams    esac
100847a0c37SPatrick Williamsdone
101847a0c37SPatrick Williams
102847a0c37SPatrick Williams## Create an initially empty meson.build file.
103847a0c37SPatrick Williams## $1 - path to create meson.build at.
104847a0c37SPatrick Williamsfunction meson_empty_file {
105847a0c37SPatrick Williams    mkdir -p "$1"
106847a0c37SPatrick Williams    echo "# Generated file; do not modify." > "$1/meson.build"
107847a0c37SPatrick Williams}
108847a0c37SPatrick Williams
109847a0c37SPatrick Williams## Create the root-level meson.build
110847a0c37SPatrick Williams##
111847a0c37SPatrick Williams## Inserts rules to run the available version of this tool to ensure the
112847a0c37SPatrick Williams## version has not changed.
113847a0c37SPatrick Williamsfunction meson_create_root {
114*d77548a1SPatrick Williams    meson_empty_file "${outputdir}"
115847a0c37SPatrick Williams
116*d77548a1SPatrick Williams    cat >> "${outputdir}/meson.build" \
117847a0c37SPatrick Williams        << EOF
118847a0c37SPatrick Williamssdbuspp_gen_meson_ver = run_command(
119847a0c37SPatrick Williams    sdbuspp_gen_meson_prog,
120847a0c37SPatrick Williams    '--version',
121847a0c37SPatrick Williams).stdout().strip().split('\n')[0]
122847a0c37SPatrick Williams
123*d77548a1SPatrick Williamsif sdbuspp_gen_meson_ver != '${tool_version}'
124847a0c37SPatrick Williams    warning('Generated meson files from wrong version of sdbus++-gen-meson.')
125847a0c37SPatrick Williams    warning(
126*d77548a1SPatrick Williams        'Expected "${tool_version}", got:',
127847a0c37SPatrick Williams        sdbuspp_gen_meson_ver
128847a0c37SPatrick Williams    )
129847a0c37SPatrick Williamsendif
130847a0c37SPatrick Williams
131847a0c37SPatrick WilliamsEOF
132847a0c37SPatrick Williams}
133847a0c37SPatrick Williams
134847a0c37SPatrick Williams## hash-tables to store:
135847a0c37SPatrick Williams##      meson_paths - list of subdirectory paths for which an empty meson.build
136847a0c37SPatrick Williams##                    has already been created.
137847a0c37SPatrick Williams##      interfaces - list of interface paths which a YAML has been found and
138847a0c37SPatrick Williams##                   which YAML types (interface, errors, etc.).
139847a0c37SPatrick Williamsdeclare -A meson_paths
140847a0c37SPatrick Williamsdeclare -A interfaces
141847a0c37SPatrick Williams
142847a0c37SPatrick Williams## Ensure the meson.build files to a path have been created.
143847a0c37SPatrick Williams## $1 - The path requiring to be created.
144847a0c37SPatrick Williamsfunction meson_create_path {
145847a0c37SPatrick Williams
146*d77548a1SPatrick Williams    meson_path="${outputdir}"
147847a0c37SPatrick Williams    prev_meson_path=""
148847a0c37SPatrick Williams
149847a0c37SPatrick Williams    # Split the path into segments.
1509ede18b4SPatrick Williams    for part in $(echo "$1" | tr '/' '\n'); do
151*d77548a1SPatrick Williams        prev_meson_path="${meson_path}"
152*d77548a1SPatrick Williams        meson_path="${meson_path}/${part}"
153847a0c37SPatrick Williams
154847a0c37SPatrick Williams        # Create the meson.build for this segment if it doesn't already exist.
155*d77548a1SPatrick Williams        if [[ "" == "${meson_paths[${meson_path}]}" ]]; then
156*d77548a1SPatrick Williams            meson_paths["${meson_path}"]="1"
157*d77548a1SPatrick Williams            meson_empty_file "${meson_path}"
158847a0c37SPatrick Williams
159847a0c37SPatrick Williams            # Add the 'subdir' link into the parent's meson.build.
160847a0c37SPatrick Williams            # We need to skip adding the links into the 'root' meson.build
161847a0c37SPatrick Williams            # because most repositories want to selectively add TLDs based
162847a0c37SPatrick Williams            # on config flags.  Let them figure out their own logic for that.
163*d77548a1SPatrick Williams            if [[ ${outputdir} != "${prev_meson_path}" ]]; then
164*d77548a1SPatrick Williams                echo "subdir('${part}')" >> "${prev_meson_path}/meson.build"
165847a0c37SPatrick Williams            fi
166847a0c37SPatrick Williams        fi
167847a0c37SPatrick Williams    done
168847a0c37SPatrick Williams}
169847a0c37SPatrick Williams
170847a0c37SPatrick Williams## Generate the meson target for the source files (.cpp/.hpp) from a YAML
171847a0c37SPatrick Williams## interface.
172847a0c37SPatrick Williams##
173847a0c37SPatrick Williams## $1 - The interface to generate a target for.
174847a0c37SPatrick Williamsfunction meson_cpp_target {
175*d77548a1SPatrick Williams    mesondir="${outputdir}/$1"
176*d77548a1SPatrick Williams    yamldir="$(realpath --relative-to="${mesondir}" "${rootdir}")"
177847a0c37SPatrick Williams
178847a0c37SPatrick Williams    # Determine the source and output files based on the YAMLs present.
179847a0c37SPatrick Williams    sources=""
180847a0c37SPatrick Williams    outputs=""
1819ede18b4SPatrick Williams    for s in ${interfaces[$1]}; do
182*d77548a1SPatrick Williams        sources="'${yamldir}/$1.${s}', "
183847a0c37SPatrick Williams
184*d77548a1SPatrick Williams        case "${s}" in
185847a0c37SPatrick Williams            errors.yaml)
186847a0c37SPatrick Williams                outputs="${outputs}'error.cpp', 'error.hpp', "
187847a0c37SPatrick Williams                ;;
188847a0c37SPatrick Williams
189847a0c37SPatrick Williams            interface.yaml)
190847a0c37SPatrick Williams                outputs="${outputs}'server.cpp', 'server.hpp', "
191847a0c37SPatrick Williams                outputs="${outputs}'client.hpp', "
192847a0c37SPatrick Williams                ;;
193*d77548a1SPatrick Williams
194*d77548a1SPatrick Williams            *)
195*d77548a1SPatrick Williams                echo "Unknown interface type: ${s}"
196*d77548a1SPatrick Williams                exit 1
197*d77548a1SPatrick Williams                ;;
198847a0c37SPatrick Williams        esac
199847a0c37SPatrick Williams    done
200847a0c37SPatrick Williams
201847a0c37SPatrick Williams    # Create the target to generate the 'outputs'.
202*d77548a1SPatrick Williams    cat >> "${mesondir}/meson.build" \
203847a0c37SPatrick Williams        << EOF
204847a0c37SPatrick Williamsgenerated_sources += custom_target(
205847a0c37SPatrick Williams    '$1__cpp'.underscorify(),
206*d77548a1SPatrick Williams    input: [ ${sources} ],
207*d77548a1SPatrick Williams    output: [ ${outputs} ],
208847a0c37SPatrick Williams    command: [
209847a0c37SPatrick Williams        sdbuspp_gen_meson_prog, '--command', 'cpp',
210847a0c37SPatrick Williams        '--output', meson.current_build_dir(),
211847a0c37SPatrick Williams        '--tool', sdbusplusplus_prog,
212*d77548a1SPatrick Williams        '--directory', meson.current_source_dir() / '${yamldir}',
213847a0c37SPatrick Williams        '$1',
214847a0c37SPatrick Williams    ],
215847a0c37SPatrick Williams)
216847a0c37SPatrick Williams
217847a0c37SPatrick WilliamsEOF
218847a0c37SPatrick Williams}
219847a0c37SPatrick Williams
220847a0c37SPatrick Williams## Generate the meson target for the markdown files from a YAML interface.
221847a0c37SPatrick Williams## $1 - The interface to generate a target for.
222847a0c37SPatrick Williamsfunction meson_md_target {
223*d77548a1SPatrick Williams    mesondir="${outputdir}/$(dirname "$1")"
224*d77548a1SPatrick Williams    yamldir="$(realpath --relative-to="${mesondir}" "${rootdir}")"
225847a0c37SPatrick Williams
226847a0c37SPatrick Williams    # Determine the source files based on the YAMLs present.
227847a0c37SPatrick Williams    sources=""
2289ede18b4SPatrick Williams    for s in ${interfaces[$1]}; do
229*d77548a1SPatrick Williams        sources="'${yamldir}/$1.${s}', "
230847a0c37SPatrick Williams    done
231847a0c37SPatrick Williams
232847a0c37SPatrick Williams    # Create the target to generate the interface.md file.
233*d77548a1SPatrick Williams    cat >> "${mesondir}/meson.build" \
234847a0c37SPatrick Williams        << EOF
235847a0c37SPatrick Williamsgenerated_others += custom_target(
236847a0c37SPatrick Williams    '$1__markdown'.underscorify(),
237*d77548a1SPatrick Williams    input: [ ${sources} ],
238847a0c37SPatrick Williams    output: [ '$(basename "$1").md' ],
239847a0c37SPatrick Williams    command: [
240847a0c37SPatrick Williams        sdbuspp_gen_meson_prog, '--command', 'markdown',
241847a0c37SPatrick Williams        '--output', meson.current_build_dir(),
242847a0c37SPatrick Williams        '--tool', sdbusplusplus_prog,
243*d77548a1SPatrick Williams        '--directory', meson.current_source_dir() / '${yamldir}',
244847a0c37SPatrick Williams        '$1',
245847a0c37SPatrick Williams    ],
246847a0c37SPatrick Williams)
247847a0c37SPatrick Williams
248847a0c37SPatrick WilliamsEOF
249847a0c37SPatrick Williams}
250847a0c37SPatrick Williams
251847a0c37SPatrick Williams## Handle command=meson by generating the tree of meson.build files.
252847a0c37SPatrick Williamsfunction cmd_meson {
253847a0c37SPatrick Williams    TLDs="com net org xyz"
254847a0c37SPatrick Williams    yamls=""
255847a0c37SPatrick Williams
256847a0c37SPatrick Williams    # Find all the YAML files in the TLD subdirectories.
257*d77548a1SPatrick Williams    for d in ${TLDs}; do
258*d77548a1SPatrick Williams        dir="${rootdir}/${d}"
259*d77548a1SPatrick Williams        if [[ ! -d ${dir} ]]; then
260847a0c37SPatrick Williams            continue
261847a0c37SPatrick Williams        fi
262847a0c37SPatrick Williams
263847a0c37SPatrick Williams        yamls="\
264*d77548a1SPatrick Williams            ${yamls} \
265*d77548a1SPatrick Williams            $(find "${dir}" -name '*.interface.yaml' -o -name '*.errors.yaml') \
266847a0c37SPatrick Williams            "
267847a0c37SPatrick Williams    done
268847a0c37SPatrick Williams
269847a0c37SPatrick Williams    # Sort YAMLs
270*d77548a1SPatrick Williams    # shellcheck disable=SC2312
271*d77548a1SPatrick Williams    yamls="$(echo "${yamls}" | tr " " "\n" | sort)"
272847a0c37SPatrick Williams
273847a0c37SPatrick Williams    # Assign the YAML files into the hash-table by interface name.
274*d77548a1SPatrick Williams    for y in ${yamls}; do
275*d77548a1SPatrick Williams        rel="$(realpath "--relative-to=${rootdir}" "${y}")"
276*d77548a1SPatrick Williams        dir="$(dirname "${rel}")"
277847a0c37SPatrick Williams        ext="${rel#*.}"
278*d77548a1SPatrick Williams        base="$(basename "${rel}" ".${ext}")"
279*d77548a1SPatrick Williams        key="${dir}/${base}"
280847a0c37SPatrick Williams
281*d77548a1SPatrick Williams        interfaces["${key}"]="${interfaces[${key}]} ${ext}"
282847a0c37SPatrick Williams    done
283847a0c37SPatrick Williams
284847a0c37SPatrick Williams    # Create the meson.build files.
285847a0c37SPatrick Williams    meson_create_root
286*d77548a1SPatrick Williams    # shellcheck disable=SC2312
287847a0c37SPatrick Williams    sorted_ifaces="$(echo "${!interfaces[@]}" | tr " " "\n" | sort)"
2889ede18b4SPatrick Williams    for i in ${sorted_ifaces}; do
289*d77548a1SPatrick Williams        meson_create_path "${i}"
290*d77548a1SPatrick Williams        meson_cpp_target "${i}"
291*d77548a1SPatrick Williams        meson_md_target "${i}"
292847a0c37SPatrick Williams    done
293847a0c37SPatrick Williams}
294847a0c37SPatrick Williams
295847a0c37SPatrick Williams## Handle command=cpp by calling sdbus++ as appropriate.
296847a0c37SPatrick Williams## $1 - interface to generate.
297847a0c37SPatrick Williams##
298847a0c37SPatrick Williams## For an interface foo/bar, the outputdir is expected to be foo/bar.
299847a0c37SPatrick Williamsfunction cmd_cpp {
300847a0c37SPatrick Williams
301*d77548a1SPatrick Williams    if [[ "" == "$1" ]]; then
302847a0c37SPatrick Williams        show_usage
303847a0c37SPatrick Williams        exit 1
304847a0c37SPatrick Williams    fi
305847a0c37SPatrick Williams
306*d77548a1SPatrick Williams    if [[ ! -e "${rootdir}/$1.interface.yaml" ]] &&
307*d77548a1SPatrick Williams        [[ ! -e "${rootdir}/$1.errors.yaml" ]]; then
308847a0c37SPatrick Williams        echo "Missing YAML for $1."
309847a0c37SPatrick Williams        exit 1
310847a0c37SPatrick Williams    fi
311847a0c37SPatrick Williams
312*d77548a1SPatrick Williams    mkdir -p "${outputdir}"
313847a0c37SPatrick Williams
314*d77548a1SPatrick Williams    sdbusppcmd="${sdbuspp} -r ${rootdir}"
315847a0c37SPatrick Williams    intf="${1//\//.}"
316847a0c37SPatrick Williams
317*d77548a1SPatrick Williams    if [[ -e "${rootdir}/$1.interface.yaml" ]]; then
318*d77548a1SPatrick Williams        ${sdbusppcmd} interface server-header "${intf}" > "${outputdir}/server.hpp"
319*d77548a1SPatrick Williams        ${sdbusppcmd} interface server-cpp "${intf}" > "${outputdir}/server.cpp"
320*d77548a1SPatrick Williams        ${sdbusppcmd} interface client-header "${intf}" > "${outputdir}/client.hpp"
321847a0c37SPatrick Williams    fi
322847a0c37SPatrick Williams
323*d77548a1SPatrick Williams    if [[ -e "${rootdir}/$1.errors.yaml" ]]; then
324*d77548a1SPatrick Williams        ${sdbusppcmd} error exception-header "${intf}" > "${outputdir}/error.hpp"
325*d77548a1SPatrick Williams        ${sdbusppcmd} error exception-cpp "${intf}" > "${outputdir}/error.cpp"
326847a0c37SPatrick Williams    fi
327847a0c37SPatrick Williams}
328847a0c37SPatrick Williams
329847a0c37SPatrick Williams## Handle command=markdown by calling sdbus++ as appropriate.
330847a0c37SPatrick Williams## $1 - interface to generate.
331847a0c37SPatrick Williams##
332847a0c37SPatrick Williams## For an interface foo/bar, the outputdir is expected to be foo.
333847a0c37SPatrick Williamsfunction cmd_markdown {
334847a0c37SPatrick Williams
335*d77548a1SPatrick Williams    if [[ "" == "$1" ]]; then
336847a0c37SPatrick Williams        show_usage
337847a0c37SPatrick Williams        exit 1
338847a0c37SPatrick Williams    fi
339847a0c37SPatrick Williams
340*d77548a1SPatrick Williams    if [[ ! -e "${rootdir}/$1.interface.yaml" ]] &&
341*d77548a1SPatrick Williams        [[ ! -e "${rootdir}/$1.errors.yaml" ]]; then
342847a0c37SPatrick Williams        echo "Missing YAML for $1."
343847a0c37SPatrick Williams        exit 1
344847a0c37SPatrick Williams    fi
345847a0c37SPatrick Williams
346*d77548a1SPatrick Williams    mkdir -p "${outputdir}"
347847a0c37SPatrick Williams
348*d77548a1SPatrick Williams    sdbusppcmd="${sdbuspp} -r ${rootdir}"
349847a0c37SPatrick Williams    intf="${1//\//.}"
350847a0c37SPatrick Williams    base="$(basename "$1")"
351847a0c37SPatrick Williams
352*d77548a1SPatrick Williams    echo -n > "${outputdir}/${base}.md"
353*d77548a1SPatrick Williams    if [[ -e "${rootdir}/$1.interface.yaml" ]]; then
354*d77548a1SPatrick Williams        ${sdbusppcmd} interface markdown "${intf}" >> "${outputdir}/${base}.md"
355847a0c37SPatrick Williams    fi
356847a0c37SPatrick Williams
357*d77548a1SPatrick Williams    if [[ -e "${rootdir}/$1.errors.yaml" ]]; then
358*d77548a1SPatrick Williams        ${sdbusppcmd} error markdown "${intf}" >> "${outputdir}/${base}.md"
359847a0c37SPatrick Williams    fi
360847a0c37SPatrick Williams}
361847a0c37SPatrick Williams
362847a0c37SPatrick Williams## Handle command=version.
363847a0c37SPatrick Williamsfunction cmd_version {
364847a0c37SPatrick Williams    show_version
365847a0c37SPatrick Williams}
366847a0c37SPatrick Williams
367*d77548a1SPatrick Williams"cmd_${cmd}" "$*"
368