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 | c65b3aa | 2020-07-16 07:43:16 -0500 | [diff] [blame] | 30 | parallel=$(nproc || grep -c ^processor < /proc/cpuinfo) |
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 | 9ede18b | 2022-03-12 07:55:36 -0600 | [diff] [blame] | 35 | while true; do |
Patrick Williams | 185d279 | 2020-05-19 16:41:45 -0500 | [diff] [blame] | 36 | case "$1" in |
| 37 | -h | --help) |
| 38 | show_usage |
| 39 | exit |
| 40 | ;; |
| 41 | |
Patrick Williams | bb140d1 | 2020-06-05 16:29:10 -0500 | [diff] [blame] | 42 | -j | --jobs) |
| 43 | shift |
| 44 | parallel="$1" |
| 45 | shift |
| 46 | ;; |
| 47 | |
Patrick Williams | 185d279 | 2020-05-19 16:41:45 -0500 | [diff] [blame] | 48 | --list-all) |
| 49 | listall="yes" |
| 50 | shift |
| 51 | ;; |
| 52 | |
| 53 | -o | --output) |
| 54 | shift |
Patrick Williams | bb140d1 | 2020-06-05 16:29:10 -0500 | [diff] [blame] | 55 | outputdir="$1" |
Patrick Williams | 185d279 | 2020-05-19 16:41:45 -0500 | [diff] [blame] | 56 | shift |
| 57 | ;; |
| 58 | |
| 59 | -t | --tool) |
| 60 | shift |
Patrick Williams | bb140d1 | 2020-06-05 16:29:10 -0500 | [diff] [blame] | 61 | sdbuspp="$1" |
Patrick Williams | 185d279 | 2020-05-19 16:41:45 -0500 | [diff] [blame] | 62 | shift |
| 63 | ;; |
| 64 | |
| 65 | --) |
| 66 | shift |
| 67 | break |
| 68 | ;; |
Patrick Williams | 185d279 | 2020-05-19 16:41:45 -0500 | [diff] [blame] | 69 | esac |
| 70 | done |
| 71 | |
Patrick Williams | 9ede18b | 2022-03-12 07:55:36 -0600 | [diff] [blame] | 72 | if [ $# -eq 0 ]; then |
Patrick Williams | 185d279 | 2020-05-19 16:41:45 -0500 | [diff] [blame] | 73 | show_usage |
| 74 | exit 1 |
| 75 | fi |
| 76 | |
Patrick Williams | 70bdfdd | 2020-07-09 15:53:43 -0500 | [diff] [blame] | 77 | all_jobs="" |
| 78 | |
Patrick Williams | b2bc0c7 | 2020-06-19 09:52:50 -0500 | [diff] [blame] | 79 | declare -A emitted_file_names |
Patrick Williams | 0b2e48e | 2020-06-09 14:49:06 -0500 | [diff] [blame] | 80 | # generate_single -- make a single call to sdbus++. |
| 81 | # $1: sdbus++ TYPE |
| 82 | # $2: sdbus++ PROCESS |
| 83 | # $3: sdbus++ ITEM |
| 84 | # $4: relative output file |
| 85 | # $5: 'append-mode' if present. |
| 86 | function generate_single { |
Patrick Williams | 0b731e0 | 2020-06-19 09:49:50 -0500 | [diff] [blame] | 87 | |
Patrick Williams | 9ede18b | 2022-03-12 07:55:36 -0600 | [diff] [blame] | 88 | if [ "empty" == "$1" ]; then |
Patrick Williams | c65b3aa | 2020-07-16 07:43:16 -0500 | [diff] [blame] | 89 | echo -n > "$outputdir/$4" |
Patrick Williams | 9ede18b | 2022-03-12 07:55:36 -0600 | [diff] [blame] | 90 | elif [ "" == "$5" ]; then |
Patrick Williams | c65b3aa | 2020-07-16 07:43:16 -0500 | [diff] [blame] | 91 | $sdbuspp "$1" "$2" "$3" > "$outputdir/$4" & |
Patrick Williams | 70bdfdd | 2020-07-09 15:53:43 -0500 | [diff] [blame] | 92 | all_jobs="$all_jobs $!" |
Patrick Williams | 0b2e48e | 2020-06-09 14:49:06 -0500 | [diff] [blame] | 93 | else |
Patrick Williams | c65b3aa | 2020-07-16 07:43:16 -0500 | [diff] [blame] | 94 | $sdbuspp "$1" "$2" "$3" >> "$outputdir/$4" & |
Patrick Williams | 70bdfdd | 2020-07-09 15:53:43 -0500 | [diff] [blame] | 95 | all_jobs="$all_jobs $!" |
Patrick Williams | 0b2e48e | 2020-06-09 14:49:06 -0500 | [diff] [blame] | 96 | fi |
| 97 | |
Patrick Williams | b2bc0c7 | 2020-06-19 09:52:50 -0500 | [diff] [blame] | 98 | # Emit filename as needed. |
| 99 | filename=$outputdir/$4 |
Patrick Williams | 9ede18b | 2022-03-12 07:55:36 -0600 | [diff] [blame] | 100 | if [ "x1" != "x${emitted_file_names[$filename]}" ]; then |
Patrick Williams | b2bc0c7 | 2020-06-19 09:52:50 -0500 | [diff] [blame] | 101 | emitted_file_names[$filename]="1" |
Patrick Williams | 0b2e48e | 2020-06-09 14:49:06 -0500 | [diff] [blame] | 102 | |
Patrick Williams | b2bc0c7 | 2020-06-19 09:52:50 -0500 | [diff] [blame] | 103 | # Always emit generated file name for foo-cpp and foo-header. |
| 104 | # Conditionally emit for everything else depending on $listall. |
| 105 | case "$2" in |
| 106 | *-cpp | *-header) |
Patrick Williams | c65b3aa | 2020-07-16 07:43:16 -0500 | [diff] [blame] | 107 | echo "$filename" |
Patrick Williams | b2bc0c7 | 2020-06-19 09:52:50 -0500 | [diff] [blame] | 108 | ;; |
| 109 | |
| 110 | *) |
Patrick Williams | 9ede18b | 2022-03-12 07:55:36 -0600 | [diff] [blame] | 111 | if [ "yes" == "$listall" ]; then |
Patrick Williams | c65b3aa | 2020-07-16 07:43:16 -0500 | [diff] [blame] | 112 | echo "$filename" |
Patrick Williams | b2bc0c7 | 2020-06-19 09:52:50 -0500 | [diff] [blame] | 113 | fi |
| 114 | ;; |
| 115 | esac |
| 116 | fi |
Patrick Williams | 0b2e48e | 2020-06-09 14:49:06 -0500 | [diff] [blame] | 117 | |
| 118 | # Ensure that no more than ${parallel} jobs are running at a time and if so |
Patrick Williams | 70bdfdd | 2020-07-09 15:53:43 -0500 | [diff] [blame] | 119 | # wait for them to finish. |
Patrick Williams | 9ede18b | 2022-03-12 07:55:36 -0600 | [diff] [blame] | 120 | if [[ $(echo "$all_jobs" | wc -w) -ge $parallel ]]; then |
Patrick Williams | 70bdfdd | 2020-07-09 15:53:43 -0500 | [diff] [blame] | 121 | waitall |
| 122 | fi |
| 123 | } |
| 124 | |
| 125 | function waitall { |
Patrick Williams | 9ede18b | 2022-03-12 07:55:36 -0600 | [diff] [blame] | 126 | for job in $all_jobs; do |
Patrick Williams | c65b3aa | 2020-07-16 07:43:16 -0500 | [diff] [blame] | 127 | wait "$job" |
Patrick Williams | 0b2e48e | 2020-06-09 14:49:06 -0500 | [diff] [blame] | 128 | done |
Patrick Williams | 70bdfdd | 2020-07-09 15:53:43 -0500 | [diff] [blame] | 129 | all_jobs="" |
Patrick Williams | 0b2e48e | 2020-06-09 14:49:06 -0500 | [diff] [blame] | 130 | } |
| 131 | |
Patrick Williams | 9ede18b | 2022-03-12 07:55:36 -0600 | [diff] [blame] | 132 | for d in "$@"; do |
Patrick Williams | c65b3aa | 2020-07-16 07:43:16 -0500 | [diff] [blame] | 133 | interfaces="$(find "$d" -name '*.interface.yaml')" |
| 134 | errors="$(find "$d" -name '*.errors.yaml')" |
Patrick Williams | 0b731e0 | 2020-06-19 09:49:50 -0500 | [diff] [blame] | 135 | |
| 136 | # Some files are created from multiple YAML sources, but we don't know |
| 137 | # which YAML sources are present. For these files, we need to first |
| 138 | # create an empty file to ensure the file does not carry old data from |
| 139 | # a previous run. |
Patrick Williams | 9ede18b | 2022-03-12 07:55:36 -0600 | [diff] [blame] | 140 | for y in $interfaces $errors; do |
Patrick Williams | 0b731e0 | 2020-06-19 09:49:50 -0500 | [diff] [blame] | 141 | path="${y%.interface.yaml}" |
| 142 | path="${path%.errors.yaml}" |
| 143 | iface="${path//\//.}" |
| 144 | |
Patrick Williams | c65b3aa | 2020-07-16 07:43:16 -0500 | [diff] [blame] | 145 | mkdir -p "$outputdir/$path" |
| 146 | generate_single empty markdown "$iface" "$path.md" |
Patrick Williams | 0b731e0 | 2020-06-19 09:49:50 -0500 | [diff] [blame] | 147 | done |
Patrick Williams | 185d279 | 2020-05-19 16:41:45 -0500 | [diff] [blame] | 148 | |
Patrick Williams | 9ede18b | 2022-03-12 07:55:36 -0600 | [diff] [blame] | 149 | for i in $interfaces; do |
Patrick Williams | 185d279 | 2020-05-19 16:41:45 -0500 | [diff] [blame] | 150 | path="${i%.interface.yaml}" |
| 151 | iface="${path//\//.}" |
| 152 | |
Patrick Williams | c65b3aa | 2020-07-16 07:43:16 -0500 | [diff] [blame] | 153 | generate_single interface server-header "$iface" "$path/server.hpp" |
| 154 | generate_single interface server-cpp "$iface" "$path/server.cpp" |
| 155 | generate_single interface client-header "$iface" "$path/client.hpp" |
| 156 | generate_single interface markdown "$iface" "$path.md" append |
Patrick Williams | 185d279 | 2020-05-19 16:41:45 -0500 | [diff] [blame] | 157 | |
Patrick Williams | 185d279 | 2020-05-19 16:41:45 -0500 | [diff] [blame] | 158 | done |
Patrick Williams | 70bdfdd | 2020-07-09 15:53:43 -0500 | [diff] [blame] | 159 | waitall # finish all before continuing |
Patrick Williams | 185d279 | 2020-05-19 16:41:45 -0500 | [diff] [blame] | 160 | |
Patrick Williams | 9ede18b | 2022-03-12 07:55:36 -0600 | [diff] [blame] | 161 | for e in $errors; do |
Patrick Williams | 185d279 | 2020-05-19 16:41:45 -0500 | [diff] [blame] | 162 | path="${e%.errors.yaml}" |
| 163 | iface="${path//\//.}" |
| 164 | |
Patrick Williams | c65b3aa | 2020-07-16 07:43:16 -0500 | [diff] [blame] | 165 | generate_single error exception-header "$iface" "$path/error.hpp" |
| 166 | generate_single error exception-cpp "$iface" "$path/error.cpp" |
| 167 | generate_single error markdown "$iface" "$path.md" append |
Patrick Williams | 185d279 | 2020-05-19 16:41:45 -0500 | [diff] [blame] | 168 | |
Patrick Williams | 185d279 | 2020-05-19 16:41:45 -0500 | [diff] [blame] | 169 | done |
Patrick Williams | 70bdfdd | 2020-07-09 15:53:43 -0500 | [diff] [blame] | 170 | waitall # finish all before continuing |
Patrick Williams | 185d279 | 2020-05-19 16:41:45 -0500 | [diff] [blame] | 171 | done |