Patrick Williams | 185d279 | 2020-05-19 16:41:45 -0500 | [diff] [blame] | 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | set -e |
| 4 | |
| 5 | function show_usage { |
| 6 | echo "Usage: $0 [options] <dirs>+" |
| 7 | echo |
| 8 | echo "Generate the sdbus++ sources from a directory path." |
| 9 | echo |
| 10 | echo "Options:" |
| 11 | echo " --tool <path> - path to processing tool (default 'sdbus++')." |
| 12 | echo " --output <path> - directory to place output files (default '.')." |
| 13 | echo " --list-all - include all generated files in stdout list." |
Patrick Williams | bb140d1 | 2020-06-05 16:29:10 -0500 | [diff] [blame] | 14 | echo " --jobs <N> - number to run in parallel (default: $(nproc))." |
Patrick Williams | 185d279 | 2020-05-19 16:41:45 -0500 | [diff] [blame] | 15 | echo " <dirs>+ - any number of subdirectories to generate." |
| 16 | echo |
| 17 | echo "The output on stdout is a list of generated files, which is intended" |
| 18 | echo "to be consumed by build systems, such as Meson." |
| 19 | echo |
| 20 | echo "This tool, by default, generates all files that are able to be" |
| 21 | echo "created by sdbus++. The output is a list of compilable sources that" |
| 22 | echo "were generated by the tool. The tool may generate outputs which are" |
| 23 | echo "not able to be compiled, such as documentation, but it does not put" |
| 24 | echo "them into stdout unless --list-all is given." |
| 25 | } |
| 26 | |
| 27 | sdbuspp="sdbus++" |
| 28 | outputdir="." |
| 29 | listall="no" |
Patrick Williams | e113b20 | 2020-06-09 14:10:22 -0500 | [diff] [blame] | 30 | parallel=$(nproc || cat /proc/cpuinfo | grep ^processor | wc -l) |
Patrick Williams | 185d279 | 2020-05-19 16:41:45 -0500 | [diff] [blame] | 31 | |
Patrick Williams | bb140d1 | 2020-06-05 16:29:10 -0500 | [diff] [blame] | 32 | options="$(getopt -o ho:t:j: --long help,list-all,output:,tool:,jobs: -- "$@")" |
Patrick Williams | 185d279 | 2020-05-19 16:41:45 -0500 | [diff] [blame] | 33 | eval set -- "$options" |
| 34 | |
Patrick Williams | bb140d1 | 2020-06-05 16:29:10 -0500 | [diff] [blame] | 35 | while true; |
| 36 | do |
Patrick Williams | 185d279 | 2020-05-19 16:41:45 -0500 | [diff] [blame] | 37 | case "$1" in |
| 38 | -h | --help) |
| 39 | show_usage |
| 40 | exit |
| 41 | ;; |
| 42 | |
Patrick Williams | bb140d1 | 2020-06-05 16:29:10 -0500 | [diff] [blame] | 43 | -j | --jobs) |
| 44 | shift |
| 45 | parallel="$1" |
| 46 | shift |
| 47 | ;; |
| 48 | |
Patrick Williams | 185d279 | 2020-05-19 16:41:45 -0500 | [diff] [blame] | 49 | --list-all) |
| 50 | listall="yes" |
| 51 | shift |
| 52 | ;; |
| 53 | |
| 54 | -o | --output) |
| 55 | shift |
Patrick Williams | bb140d1 | 2020-06-05 16:29:10 -0500 | [diff] [blame] | 56 | outputdir="$1" |
Patrick Williams | 185d279 | 2020-05-19 16:41:45 -0500 | [diff] [blame] | 57 | shift |
| 58 | ;; |
| 59 | |
| 60 | -t | --tool) |
| 61 | shift |
Patrick Williams | bb140d1 | 2020-06-05 16:29:10 -0500 | [diff] [blame] | 62 | sdbuspp="$1" |
Patrick Williams | 185d279 | 2020-05-19 16:41:45 -0500 | [diff] [blame] | 63 | shift |
| 64 | ;; |
| 65 | |
| 66 | --) |
| 67 | shift |
| 68 | break |
| 69 | ;; |
Patrick Williams | 185d279 | 2020-05-19 16:41:45 -0500 | [diff] [blame] | 70 | esac |
| 71 | done |
| 72 | |
| 73 | if [ "x" == "x$@" ]; |
| 74 | then |
| 75 | show_usage |
| 76 | exit 1 |
| 77 | fi |
| 78 | |
Patrick Williams | 0b2e48e | 2020-06-09 14:49:06 -0500 | [diff] [blame] | 79 | # generate_single -- make a single call to sdbus++. |
| 80 | # $1: sdbus++ TYPE |
| 81 | # $2: sdbus++ PROCESS |
| 82 | # $3: sdbus++ ITEM |
| 83 | # $4: relative output file |
| 84 | # $5: 'append-mode' if present. |
| 85 | function generate_single { |
| 86 | if [ "x" == "x$5" ]; |
| 87 | then |
| 88 | $sdbuspp $1 $2 $3 > $outputdir/$4 & |
| 89 | else |
| 90 | $sdbuspp $1 $2 $3 >> $outputdir/$4 & |
| 91 | fi |
| 92 | |
| 93 | # Always emit generated file name for foo-cpp and foo-header. |
| 94 | # Conditionally emit for everything else depending on $listall. |
| 95 | case "$2" in |
| 96 | *-cpp | *-header) |
| 97 | echo $outputdir/$4 |
| 98 | ;; |
| 99 | |
| 100 | *) |
| 101 | if [ "xyes" == "x$listall" ]; |
| 102 | then |
| 103 | echo $outputdir/$4 |
| 104 | fi |
| 105 | ;; |
| 106 | esac |
| 107 | |
| 108 | # Ensure that no more than ${parallel} jobs are running at a time and if so |
| 109 | # wait for at least one to finish. |
| 110 | while [[ $(jobs -r -p | wc -l) -ge $parallel ]]; |
| 111 | do |
| 112 | wait -n |
| 113 | done |
| 114 | } |
| 115 | |
Patrick Williams | 185d279 | 2020-05-19 16:41:45 -0500 | [diff] [blame] | 116 | for d in $@; |
| 117 | do |
| 118 | interfaces="$(find $d -name '*.interface.yaml')" |
| 119 | |
| 120 | for i in $interfaces; |
| 121 | do |
| 122 | path="${i%.interface.yaml}" |
| 123 | iface="${path//\//.}" |
| 124 | |
| 125 | mkdir -p $outputdir/$path |
| 126 | |
Patrick Williams | 0b2e48e | 2020-06-09 14:49:06 -0500 | [diff] [blame] | 127 | generate_single interface server-header $iface $path/server.hpp |
| 128 | generate_single interface server-cpp $iface $path/server.cpp |
| 129 | generate_single interface client-header $iface $path/client.hpp |
| 130 | generate_single interface markdown $iface $path.md |
Patrick Williams | 185d279 | 2020-05-19 16:41:45 -0500 | [diff] [blame] | 131 | |
Patrick Williams | 185d279 | 2020-05-19 16:41:45 -0500 | [diff] [blame] | 132 | done |
Patrick Williams | bb140d1 | 2020-06-05 16:29:10 -0500 | [diff] [blame] | 133 | wait # finish all before continuing |
Patrick Williams | 185d279 | 2020-05-19 16:41:45 -0500 | [diff] [blame] | 134 | |
| 135 | errors="$(find $d -name '*.errors.yaml')" |
| 136 | |
| 137 | for e in $errors; |
| 138 | do |
| 139 | path="${e%.errors.yaml}" |
| 140 | iface="${path//\//.}" |
| 141 | |
| 142 | mkdir -p $outputdir/$path |
| 143 | |
Patrick Williams | 0b2e48e | 2020-06-09 14:49:06 -0500 | [diff] [blame] | 144 | generate_single error exception-header $iface $path/error.hpp |
| 145 | generate_single error exception-cpp $iface $path/error.cpp |
| 146 | generate_single error markdown $iface $path.md append |
Patrick Williams | 185d279 | 2020-05-19 16:41:45 -0500 | [diff] [blame] | 147 | |
Patrick Williams | 185d279 | 2020-05-19 16:41:45 -0500 | [diff] [blame] | 148 | done |
Patrick Williams | bb140d1 | 2020-06-05 16:29:10 -0500 | [diff] [blame] | 149 | wait # finish all before continuing |
Patrick Williams | 185d279 | 2020-05-19 16:41:45 -0500 | [diff] [blame] | 150 | done |