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 { 41847a0c37SPatrick 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 -- "$@")" 52847a0c37SPatrick Williamseval set -- "$options" 53847a0c37SPatrick Williams 54*9ede18b4SPatrick 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 ;; 94847a0c37SPatrick Williams esac 95847a0c37SPatrick Williamsdone 96847a0c37SPatrick Williams 97847a0c37SPatrick Williams## Create an initially empty meson.build file. 98847a0c37SPatrick Williams## $1 - path to create meson.build at. 99847a0c37SPatrick Williamsfunction meson_empty_file { 100847a0c37SPatrick Williams mkdir -p "$1" 101847a0c37SPatrick Williams echo "# Generated file; do not modify." > "$1/meson.build" 102847a0c37SPatrick Williams} 103847a0c37SPatrick Williams 104847a0c37SPatrick Williams## Create the root-level meson.build 105847a0c37SPatrick Williams## 106847a0c37SPatrick Williams## Inserts rules to run the available version of this tool to ensure the 107847a0c37SPatrick Williams## version has not changed. 108847a0c37SPatrick Williamsfunction meson_create_root { 109847a0c37SPatrick Williams meson_empty_file "$outputdir" 110847a0c37SPatrick Williams 111847a0c37SPatrick Williams cat >> "$outputdir/meson.build" \ 112847a0c37SPatrick Williams << EOF 113847a0c37SPatrick Williamssdbuspp_gen_meson_ver = run_command( 114847a0c37SPatrick Williams sdbuspp_gen_meson_prog, 115847a0c37SPatrick Williams '--version', 116847a0c37SPatrick Williams).stdout().strip().split('\n')[0] 117847a0c37SPatrick Williams 118847a0c37SPatrick Williamsif sdbuspp_gen_meson_ver != '$tool_version' 119847a0c37SPatrick Williams warning('Generated meson files from wrong version of sdbus++-gen-meson.') 120847a0c37SPatrick Williams warning( 121847a0c37SPatrick Williams 'Expected "$tool_version", got:', 122847a0c37SPatrick Williams sdbuspp_gen_meson_ver 123847a0c37SPatrick Williams ) 124847a0c37SPatrick Williamsendif 125847a0c37SPatrick Williams 126847a0c37SPatrick WilliamsEOF 127847a0c37SPatrick Williams} 128847a0c37SPatrick Williams 129847a0c37SPatrick Williams## hash-tables to store: 130847a0c37SPatrick Williams## meson_paths - list of subdirectory paths for which an empty meson.build 131847a0c37SPatrick Williams## has already been created. 132847a0c37SPatrick Williams## interfaces - list of interface paths which a YAML has been found and 133847a0c37SPatrick Williams## which YAML types (interface, errors, etc.). 134847a0c37SPatrick Williamsdeclare -A meson_paths 135847a0c37SPatrick Williamsdeclare -A interfaces 136847a0c37SPatrick Williams 137847a0c37SPatrick Williams## Ensure the meson.build files to a path have been created. 138847a0c37SPatrick Williams## $1 - The path requiring to be created. 139847a0c37SPatrick Williamsfunction meson_create_path { 140847a0c37SPatrick Williams 141847a0c37SPatrick Williams meson_path="$outputdir" 142847a0c37SPatrick Williams prev_meson_path="" 143847a0c37SPatrick Williams 144847a0c37SPatrick Williams # Split the path into segments. 145*9ede18b4SPatrick Williams for part in $(echo "$1" | tr '/' '\n'); do 146847a0c37SPatrick Williams prev_meson_path="$meson_path" 147847a0c37SPatrick Williams meson_path="$meson_path/$part" 148847a0c37SPatrick Williams 149847a0c37SPatrick Williams # Create the meson.build for this segment if it doesn't already exist. 150*9ede18b4SPatrick Williams if [ "" == "${meson_paths[$meson_path]}" ]; then 151847a0c37SPatrick Williams meson_paths["$meson_path"]="1" 152847a0c37SPatrick Williams meson_empty_file "$meson_path" 153847a0c37SPatrick Williams 154847a0c37SPatrick Williams # Add the 'subdir' link into the parent's meson.build. 155847a0c37SPatrick Williams # We need to skip adding the links into the 'root' meson.build 156847a0c37SPatrick Williams # because most repositories want to selectively add TLDs based 157847a0c37SPatrick Williams # on config flags. Let them figure out their own logic for that. 158*9ede18b4SPatrick Williams if [ "$outputdir" != "$prev_meson_path" ]; then 159847a0c37SPatrick Williams echo "subdir('$part')" >> "$prev_meson_path/meson.build" 160847a0c37SPatrick Williams fi 161847a0c37SPatrick Williams fi 162847a0c37SPatrick Williams done 163847a0c37SPatrick Williams} 164847a0c37SPatrick Williams 165847a0c37SPatrick Williams## Generate the meson target for the source files (.cpp/.hpp) from a YAML 166847a0c37SPatrick Williams## interface. 167847a0c37SPatrick Williams## 168847a0c37SPatrick Williams## $1 - The interface to generate a target for. 169847a0c37SPatrick Williamsfunction meson_cpp_target { 17056eb1775SWilliam A. Kennington III mesondir="$outputdir/$1" 17156eb1775SWilliam A. Kennington III yamldir="$(realpath --relative-to="$mesondir" "$rootdir")" 172847a0c37SPatrick Williams 173847a0c37SPatrick Williams # Determine the source and output files based on the YAMLs present. 174847a0c37SPatrick Williams sources="" 175847a0c37SPatrick Williams outputs="" 176*9ede18b4SPatrick Williams for s in ${interfaces[$1]}; do 17756eb1775SWilliam A. Kennington III sources="'$yamldir/$1.$s', " 178847a0c37SPatrick Williams 179847a0c37SPatrick Williams case "$s" in 180847a0c37SPatrick Williams errors.yaml) 181847a0c37SPatrick Williams outputs="${outputs}'error.cpp', 'error.hpp', " 182847a0c37SPatrick Williams ;; 183847a0c37SPatrick Williams 184847a0c37SPatrick Williams interface.yaml) 185847a0c37SPatrick Williams outputs="${outputs}'server.cpp', 'server.hpp', " 186847a0c37SPatrick Williams outputs="${outputs}'client.hpp', " 187847a0c37SPatrick Williams ;; 188847a0c37SPatrick Williams esac 189847a0c37SPatrick Williams done 190847a0c37SPatrick Williams 191847a0c37SPatrick Williams # Create the target to generate the 'outputs'. 19256eb1775SWilliam A. Kennington III cat >> "$mesondir/meson.build" \ 193847a0c37SPatrick Williams << EOF 194847a0c37SPatrick Williamsgenerated_sources += custom_target( 195847a0c37SPatrick Williams '$1__cpp'.underscorify(), 196847a0c37SPatrick Williams input: [ $sources ], 197847a0c37SPatrick Williams output: [ $outputs ], 198847a0c37SPatrick Williams command: [ 199847a0c37SPatrick Williams sdbuspp_gen_meson_prog, '--command', 'cpp', 200847a0c37SPatrick Williams '--output', meson.current_build_dir(), 201847a0c37SPatrick Williams '--tool', sdbusplusplus_prog, 20256eb1775SWilliam A. Kennington III '--directory', meson.current_source_dir() / '$yamldir', 203847a0c37SPatrick Williams '$1', 204847a0c37SPatrick Williams ], 205847a0c37SPatrick Williams) 206847a0c37SPatrick Williams 207847a0c37SPatrick WilliamsEOF 208847a0c37SPatrick Williams} 209847a0c37SPatrick Williams 210847a0c37SPatrick Williams## Generate the meson target for the markdown files from a YAML interface. 211847a0c37SPatrick Williams## $1 - The interface to generate a target for. 212847a0c37SPatrick Williamsfunction meson_md_target { 21356eb1775SWilliam A. Kennington III mesondir="$outputdir/$(dirname "$1")" 21456eb1775SWilliam A. Kennington III yamldir="$(realpath --relative-to="$mesondir" "$rootdir")" 215847a0c37SPatrick Williams 216847a0c37SPatrick Williams # Determine the source files based on the YAMLs present. 217847a0c37SPatrick Williams sources="" 218*9ede18b4SPatrick Williams for s in ${interfaces[$1]}; do 21956eb1775SWilliam A. Kennington III sources="'$yamldir/$1.$s', " 220847a0c37SPatrick Williams done 221847a0c37SPatrick Williams 222847a0c37SPatrick Williams # Create the target to generate the interface.md file. 22356eb1775SWilliam A. Kennington III cat >> "$mesondir/meson.build" \ 224847a0c37SPatrick Williams << EOF 225847a0c37SPatrick Williamsgenerated_others += custom_target( 226847a0c37SPatrick Williams '$1__markdown'.underscorify(), 227847a0c37SPatrick Williams input: [ $sources ], 228847a0c37SPatrick Williams output: [ '$(basename "$1").md' ], 229847a0c37SPatrick Williams command: [ 230847a0c37SPatrick Williams sdbuspp_gen_meson_prog, '--command', 'markdown', 231847a0c37SPatrick Williams '--output', meson.current_build_dir(), 232847a0c37SPatrick Williams '--tool', sdbusplusplus_prog, 23356eb1775SWilliam A. Kennington III '--directory', meson.current_source_dir() / '$yamldir', 234847a0c37SPatrick Williams '$1', 235847a0c37SPatrick Williams ], 236847a0c37SPatrick Williams) 237847a0c37SPatrick Williams 238847a0c37SPatrick WilliamsEOF 239847a0c37SPatrick Williams} 240847a0c37SPatrick Williams 241847a0c37SPatrick Williams## Handle command=meson by generating the tree of meson.build files. 242847a0c37SPatrick Williamsfunction cmd_meson { 243847a0c37SPatrick Williams TLDs="com net org xyz" 244847a0c37SPatrick Williams yamls="" 245847a0c37SPatrick Williams 246847a0c37SPatrick Williams # Find all the YAML files in the TLD subdirectories. 247*9ede18b4SPatrick Williams for d in $TLDs; do 248847a0c37SPatrick Williams dir="$rootdir/$d" 249*9ede18b4SPatrick Williams if [ ! -d "$dir" ]; then 250847a0c37SPatrick Williams continue 251847a0c37SPatrick Williams fi 252847a0c37SPatrick Williams 253847a0c37SPatrick Williams yamls="\ 254847a0c37SPatrick Williams $yamls \ 255847a0c37SPatrick Williams $(find "$dir" -name '*.interface.yaml' -o -name '*.errors.yaml') \ 256847a0c37SPatrick Williams " 257847a0c37SPatrick Williams done 258847a0c37SPatrick Williams 259847a0c37SPatrick Williams # Sort YAMLs 260847a0c37SPatrick Williams yamls="$(echo "$yamls" | tr " " "\n" | sort)" 261847a0c37SPatrick Williams 262847a0c37SPatrick Williams # Assign the YAML files into the hash-table by interface name. 263*9ede18b4SPatrick Williams for y in $yamls; do 264847a0c37SPatrick Williams rel="$(realpath "--relative-to=$rootdir" "$y")" 265847a0c37SPatrick Williams dir="$(dirname "$rel")" 266847a0c37SPatrick Williams ext="${rel#*.}" 267847a0c37SPatrick Williams base="$(basename "$rel" ".$ext")" 268847a0c37SPatrick Williams 269*9ede18b4SPatrick Williams interfaces["$dir/$base"]="${interfaces[dir / base]} $ext" 270847a0c37SPatrick Williams done 271847a0c37SPatrick Williams 272847a0c37SPatrick Williams # Create the meson.build files. 273847a0c37SPatrick Williams meson_create_root 274847a0c37SPatrick Williams sorted_ifaces="$(echo "${!interfaces[@]}" | tr " " "\n" | sort)" 275*9ede18b4SPatrick Williams for i in ${sorted_ifaces}; do 276847a0c37SPatrick Williams meson_create_path "$i" 277847a0c37SPatrick Williams meson_cpp_target "$i" 278847a0c37SPatrick Williams meson_md_target "$i" 279847a0c37SPatrick Williams done 280847a0c37SPatrick Williams} 281847a0c37SPatrick Williams 282847a0c37SPatrick Williams## Handle command=cpp by calling sdbus++ as appropriate. 283847a0c37SPatrick Williams## $1 - interface to generate. 284847a0c37SPatrick Williams## 285847a0c37SPatrick Williams## For an interface foo/bar, the outputdir is expected to be foo/bar. 286847a0c37SPatrick Williamsfunction cmd_cpp { 287847a0c37SPatrick Williams 288*9ede18b4SPatrick Williams if [ "" == "$1" ]; then 289847a0c37SPatrick Williams show_usage 290847a0c37SPatrick Williams exit 1 291847a0c37SPatrick Williams fi 292847a0c37SPatrick Williams 293*9ede18b4SPatrick Williams if [ ! -e "$rootdir/$1.interface.yaml" ] && 294*9ede18b4SPatrick Williams [ ! -e "$rootdir/$1.errors.yaml" ]; then 295847a0c37SPatrick Williams echo "Missing YAML for $1." 296847a0c37SPatrick Williams exit 1 297847a0c37SPatrick Williams fi 298847a0c37SPatrick Williams 299847a0c37SPatrick Williams mkdir -p "$outputdir" 300847a0c37SPatrick Williams 301847a0c37SPatrick Williams sdbusppcmd="$sdbuspp -r $rootdir" 302847a0c37SPatrick Williams intf="${1//\//.}" 303847a0c37SPatrick Williams 304*9ede18b4SPatrick Williams if [ -e "$rootdir/$1.interface.yaml" ]; then 305847a0c37SPatrick Williams $sdbusppcmd interface server-header "$intf" > "$outputdir/server.hpp" 306847a0c37SPatrick Williams $sdbusppcmd interface server-cpp "$intf" > "$outputdir/server.cpp" 307847a0c37SPatrick Williams $sdbusppcmd interface client-header "$intf" > "$outputdir/client.hpp" 308847a0c37SPatrick Williams fi 309847a0c37SPatrick Williams 310*9ede18b4SPatrick Williams if [ -e "$rootdir/$1.errors.yaml" ]; then 311847a0c37SPatrick Williams $sdbusppcmd error exception-header "$intf" > "$outputdir/error.hpp" 312847a0c37SPatrick Williams $sdbusppcmd error exception-cpp "$intf" > "$outputdir/error.cpp" 313847a0c37SPatrick Williams fi 314847a0c37SPatrick Williams} 315847a0c37SPatrick Williams 316847a0c37SPatrick Williams## Handle command=markdown by calling sdbus++ as appropriate. 317847a0c37SPatrick Williams## $1 - interface to generate. 318847a0c37SPatrick Williams## 319847a0c37SPatrick Williams## For an interface foo/bar, the outputdir is expected to be foo. 320847a0c37SPatrick Williamsfunction cmd_markdown { 321847a0c37SPatrick Williams 322*9ede18b4SPatrick Williams if [ "" == "$1" ]; then 323847a0c37SPatrick Williams show_usage 324847a0c37SPatrick Williams exit 1 325847a0c37SPatrick Williams fi 326847a0c37SPatrick Williams 327*9ede18b4SPatrick Williams if [ ! -e "$rootdir/$1.interface.yaml" ] && 328*9ede18b4SPatrick Williams [ ! -e "$rootdir/$1.errors.yaml" ]; then 329847a0c37SPatrick Williams echo "Missing YAML for $1." 330847a0c37SPatrick Williams exit 1 331847a0c37SPatrick Williams fi 332847a0c37SPatrick Williams 333847a0c37SPatrick Williams mkdir -p "$outputdir" 334847a0c37SPatrick Williams 335847a0c37SPatrick Williams sdbusppcmd="$sdbuspp -r $rootdir" 336847a0c37SPatrick Williams intf="${1//\//.}" 337847a0c37SPatrick Williams base="$(basename "$1")" 338847a0c37SPatrick Williams 339847a0c37SPatrick Williams echo -n > "$outputdir/$base.md" 340*9ede18b4SPatrick Williams if [ -e "$rootdir/$1.interface.yaml" ]; then 341847a0c37SPatrick Williams $sdbusppcmd interface markdown "$intf" >> "$outputdir/$base.md" 342847a0c37SPatrick Williams fi 343847a0c37SPatrick Williams 344*9ede18b4SPatrick Williams if [ -e "$rootdir/$1.errors.yaml" ]; then 345847a0c37SPatrick Williams $sdbusppcmd error markdown "$intf" >> "$outputdir/$base.md" 346847a0c37SPatrick Williams fi 347847a0c37SPatrick Williams} 348847a0c37SPatrick Williams 349847a0c37SPatrick Williams## Handle command=version. 350847a0c37SPatrick Williamsfunction cmd_version { 351847a0c37SPatrick Williams show_version 352847a0c37SPatrick Williams} 353847a0c37SPatrick Williams 354847a0c37SPatrick Williams"cmd_$cmd" "$*" 355