#!/usr/bin/env bash set -e # Locale can change behavior of utilities like 'sort' but we want the output # to be stable on all machines. Force the locale to 'C' for consistency. export LC_ALL=C function show_usage { cat \ <* Generate meson.build files from a directory tree containing YAML files and facilitate building the sdbus++ sources. Options: --help - Display this message --command - Command mode to execute (default 'meson'). --directory - Root directory of the YAML source (default '.'). --output - Root directory of the output (default '.'). --tool - Path to the processing tool (default 'sdbus++'). --version - Display this tool's version string. Commands: meson - Generate a tree of meson.build files corresponding to the source YAML files. cpp - Generate the source files from a YAML interface. markdown - Generate the markdown files from a YAML interface. version - Display this tool's version string. EOF } ## The version is somewhat arbitrary but is used to create a warning message ## if a repository contains old copies of the generated meson.build files and ## needs an update. We should increment the version number whenever the ## resulting meson.build would change. tool_version="sdbus++-gen-meson version 2" function show_version { echo "$tool_version" } # Set up defaults. sdbuspp="sdbus++" outputdir="." cmd="meson" rootdir="." # Parse options. options="$(getopt -o hc:d:o:t:v --long help,command:,directory:,output:,tool:,version -- "$@")" eval set -- "$options" while true; do case "$1" in -h | --help) show_usage exit ;; -c | --command) shift cmd="$1" shift ;; -d | --directory) shift rootdir="$1" shift ;; -o | --output) shift outputdir="$1" shift ;; -t | --tool) shift sdbuspp="$1" shift ;; -v | --version) show_version exit ;; --) shift break ;; esac done ## Create an initially empty meson.build file. ## $1 - path to create meson.build at. function meson_empty_file { mkdir -p "$1" echo "# Generated file; do not modify." > "$1/meson.build" } ## Create the root-level meson.build ## ## Inserts rules to run the available version of this tool to ensure the ## version has not changed. function meson_create_root { meson_empty_file "$outputdir" cat >> "$outputdir/meson.build" \ <> "$prev_meson_path/meson.build" fi fi done } ## Generate the meson target for the source files (.cpp/.hpp) from a YAML ## interface. ## ## $1 - The interface to generate a target for. function meson_cpp_target { # Determine the source and output files based on the YAMLs present. sources="" outputs="" for s in ${interfaces[$1]}; do sources="${sources}meson.project_source_root() / '$1.$s', " case "$s" in errors.yaml) outputs="${outputs}'error.cpp', 'error.hpp', " ;; interface.yaml) outputs="${outputs}'server.cpp', 'server.hpp', " outputs="${outputs}'client.hpp', " ;; esac done # Create the target to generate the 'outputs'. cat >> "$outputdir/$1/meson.build" \ <> "$outputdir/$(dirname "$1")/meson.build" \ < "$outputdir/server.hpp" $sdbusppcmd interface server-cpp "$intf" > "$outputdir/server.cpp" $sdbusppcmd interface client-header "$intf" > "$outputdir/client.hpp" fi if [ -e "$rootdir/$1.errors.yaml" ]; then $sdbusppcmd error exception-header "$intf" > "$outputdir/error.hpp" $sdbusppcmd error exception-cpp "$intf" > "$outputdir/error.cpp" fi } ## Handle command=markdown by calling sdbus++ as appropriate. ## $1 - interface to generate. ## ## For an interface foo/bar, the outputdir is expected to be foo. function cmd_markdown { if [ "x" == "x$1" ]; then show_usage exit 1 fi if [ ! -e "$rootdir/$1.interface.yaml" ] && \ [ ! -e "$rootdir/$1.errors.yaml" ]; then echo "Missing YAML for $1." exit 1 fi mkdir -p "$outputdir" sdbusppcmd="$sdbuspp -r $rootdir" intf="${1//\//.}" base="$(basename "$1")" echo -n > "$outputdir/$base.md" if [ -e "$rootdir/$1.interface.yaml" ]; then $sdbusppcmd interface markdown "$intf" >> "$outputdir/$base.md" fi if [ -e "$rootdir/$1.errors.yaml" ]; then $sdbusppcmd error markdown "$intf" >> "$outputdir/$base.md" fi } ## Handle command=version. function cmd_version { show_version } "cmd_$cmd" "$*"