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 { |
Patrick Williams | d77548a | 2022-04-29 14:43:15 -0500 | [diff] [blame] | 6 | proc=$(nproc) |
Patrick Williams | 185d279 | 2020-05-19 16:41:45 -0500 | [diff] [blame] | 7 | echo "Usage: $0 [options] <dirs>+" |
| 8 | echo |
| 9 | echo "Generate the sdbus++ sources from a directory path." |
| 10 | echo |
| 11 | echo "Options:" |
| 12 | echo " --tool <path> - path to processing tool (default 'sdbus++')." |
| 13 | echo " --output <path> - directory to place output files (default '.')." |
| 14 | echo " --list-all - include all generated files in stdout list." |
Patrick Williams | d77548a | 2022-04-29 14:43:15 -0500 | [diff] [blame] | 15 | echo " --jobs <N> - number to run in parallel (default: ${proc})." |
Patrick Williams | 185d279 | 2020-05-19 16:41:45 -0500 | [diff] [blame] | 16 | echo " <dirs>+ - any number of subdirectories to generate." |
| 17 | echo |
| 18 | echo "The output on stdout is a list of generated files, which is intended" |
| 19 | echo "to be consumed by build systems, such as Meson." |
| 20 | echo |
| 21 | echo "This tool, by default, generates all files that are able to be" |
| 22 | echo "created by sdbus++. The output is a list of compilable sources that" |
| 23 | echo "were generated by the tool. The tool may generate outputs which are" |
| 24 | echo "not able to be compiled, such as documentation, but it does not put" |
| 25 | echo "them into stdout unless --list-all is given." |
| 26 | } |
| 27 | |
| 28 | sdbuspp="sdbus++" |
| 29 | outputdir="." |
| 30 | listall="no" |
Patrick Williams | c65b3aa | 2020-07-16 07:43:16 -0500 | [diff] [blame] | 31 | parallel=$(nproc || grep -c ^processor < /proc/cpuinfo) |
Patrick Williams | 185d279 | 2020-05-19 16:41:45 -0500 | [diff] [blame] | 32 | |
Patrick Williams | bb140d1 | 2020-06-05 16:29:10 -0500 | [diff] [blame] | 33 | options="$(getopt -o ho:t:j: --long help,list-all,output:,tool:,jobs: -- "$@")" |
Patrick Williams | d77548a | 2022-04-29 14:43:15 -0500 | [diff] [blame] | 34 | eval set -- "${options}" |
Patrick Williams | 185d279 | 2020-05-19 16:41:45 -0500 | [diff] [blame] | 35 | |
Patrick Williams | 9ede18b | 2022-03-12 07:55:36 -0600 | [diff] [blame] | 36 | while true; 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 | d77548a | 2022-04-29 14:43:15 -0500 | [diff] [blame] | 70 | |
| 71 | *) |
| 72 | echo "Invalid argument: $1" |
| 73 | exit 1 |
| 74 | ;; |
Patrick Williams | 185d279 | 2020-05-19 16:41:45 -0500 | [diff] [blame] | 75 | esac |
| 76 | done |
| 77 | |
Patrick Williams | d77548a | 2022-04-29 14:43:15 -0500 | [diff] [blame] | 78 | if [[ $# -eq 0 ]]; then |
Patrick Williams | 185d279 | 2020-05-19 16:41:45 -0500 | [diff] [blame] | 79 | show_usage |
| 80 | exit 1 |
| 81 | fi |
| 82 | |
Patrick Williams | 70bdfdd | 2020-07-09 15:53:43 -0500 | [diff] [blame] | 83 | all_jobs="" |
| 84 | |
Patrick Williams | b2bc0c7 | 2020-06-19 09:52:50 -0500 | [diff] [blame] | 85 | declare -A emitted_file_names |
Patrick Williams | 0b2e48e | 2020-06-09 14:49:06 -0500 | [diff] [blame] | 86 | # generate_single -- make a single call to sdbus++. |
| 87 | # $1: sdbus++ TYPE |
| 88 | # $2: sdbus++ PROCESS |
| 89 | # $3: sdbus++ ITEM |
| 90 | # $4: relative output file |
| 91 | # $5: 'append-mode' if present. |
| 92 | function generate_single { |
Patrick Williams | 0b731e0 | 2020-06-19 09:49:50 -0500 | [diff] [blame] | 93 | |
Patrick Williams | d77548a | 2022-04-29 14:43:15 -0500 | [diff] [blame] | 94 | if [[ "empty" == "$1" ]]; then |
| 95 | echo -n > "${outputdir}/$4" |
| 96 | elif [[ "" == "$5" ]]; then |
| 97 | ${sdbuspp} "$1" "$2" "$3" > "${outputdir}/$4" & |
| 98 | all_jobs="${all_jobs} $!" |
Patrick Williams | 0b2e48e | 2020-06-09 14:49:06 -0500 | [diff] [blame] | 99 | else |
Patrick Williams | d77548a | 2022-04-29 14:43:15 -0500 | [diff] [blame] | 100 | ${sdbuspp} "$1" "$2" "$3" >> "${outputdir}/$4" & |
| 101 | all_jobs="${all_jobs} $!" |
Patrick Williams | 0b2e48e | 2020-06-09 14:49:06 -0500 | [diff] [blame] | 102 | fi |
| 103 | |
Patrick Williams | b2bc0c7 | 2020-06-19 09:52:50 -0500 | [diff] [blame] | 104 | # Emit filename as needed. |
Patrick Williams | d77548a | 2022-04-29 14:43:15 -0500 | [diff] [blame] | 105 | filename=${outputdir}/$4 |
| 106 | if [[ "x1" != "x${emitted_file_names[${filename}]}" ]]; then |
| 107 | emitted_file_names[${filename}]="1" |
Patrick Williams | 0b2e48e | 2020-06-09 14:49:06 -0500 | [diff] [blame] | 108 | |
Patrick Williams | b2bc0c7 | 2020-06-19 09:52:50 -0500 | [diff] [blame] | 109 | # Always emit generated file name for foo-cpp and foo-header. |
| 110 | # Conditionally emit for everything else depending on $listall. |
| 111 | case "$2" in |
| 112 | *-cpp | *-header) |
Patrick Williams | d77548a | 2022-04-29 14:43:15 -0500 | [diff] [blame] | 113 | echo "${filename}" |
Patrick Williams | b2bc0c7 | 2020-06-19 09:52:50 -0500 | [diff] [blame] | 114 | ;; |
| 115 | |
| 116 | *) |
Patrick Williams | d77548a | 2022-04-29 14:43:15 -0500 | [diff] [blame] | 117 | if [[ "yes" == "${listall}" ]]; then |
| 118 | echo "${filename}" |
Patrick Williams | b2bc0c7 | 2020-06-19 09:52:50 -0500 | [diff] [blame] | 119 | fi |
| 120 | ;; |
| 121 | esac |
| 122 | fi |
Patrick Williams | 0b2e48e | 2020-06-09 14:49:06 -0500 | [diff] [blame] | 123 | |
| 124 | # 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] | 125 | # wait for them to finish. |
Patrick Williams | d77548a | 2022-04-29 14:43:15 -0500 | [diff] [blame] | 126 | # shellcheck disable=SC2312 |
| 127 | if [[ $(echo "${all_jobs}" | wc -w) -ge ${parallel} ]]; then |
Patrick Williams | 70bdfdd | 2020-07-09 15:53:43 -0500 | [diff] [blame] | 128 | waitall |
| 129 | fi |
| 130 | } |
| 131 | |
| 132 | function waitall { |
Patrick Williams | d77548a | 2022-04-29 14:43:15 -0500 | [diff] [blame] | 133 | for job in ${all_jobs}; do |
| 134 | wait "${job}" |
Patrick Williams | 0b2e48e | 2020-06-09 14:49:06 -0500 | [diff] [blame] | 135 | done |
Patrick Williams | 70bdfdd | 2020-07-09 15:53:43 -0500 | [diff] [blame] | 136 | all_jobs="" |
Patrick Williams | 0b2e48e | 2020-06-09 14:49:06 -0500 | [diff] [blame] | 137 | } |
| 138 | |
Patrick Williams | 9ede18b | 2022-03-12 07:55:36 -0600 | [diff] [blame] | 139 | for d in "$@"; do |
Patrick Williams | d77548a | 2022-04-29 14:43:15 -0500 | [diff] [blame] | 140 | interfaces="$(find "${d}" -name '*.interface.yaml')" |
| 141 | errors="$(find "${d}" -name '*.errors.yaml')" |
Patrick Williams | 0b731e0 | 2020-06-19 09:49:50 -0500 | [diff] [blame] | 142 | |
| 143 | # Some files are created from multiple YAML sources, but we don't know |
| 144 | # which YAML sources are present. For these files, we need to first |
| 145 | # create an empty file to ensure the file does not carry old data from |
| 146 | # a previous run. |
Patrick Williams | d77548a | 2022-04-29 14:43:15 -0500 | [diff] [blame] | 147 | for y in ${interfaces} ${errors}; do |
Patrick Williams | 0b731e0 | 2020-06-19 09:49:50 -0500 | [diff] [blame] | 148 | path="${y%.interface.yaml}" |
| 149 | path="${path%.errors.yaml}" |
| 150 | iface="${path//\//.}" |
| 151 | |
Patrick Williams | d77548a | 2022-04-29 14:43:15 -0500 | [diff] [blame] | 152 | mkdir -p "${outputdir}/${path}" |
| 153 | generate_single empty markdown "${iface}" "${path}.md" |
Patrick Williams | 0b731e0 | 2020-06-19 09:49:50 -0500 | [diff] [blame] | 154 | done |
Patrick Williams | 185d279 | 2020-05-19 16:41:45 -0500 | [diff] [blame] | 155 | |
Patrick Williams | d77548a | 2022-04-29 14:43:15 -0500 | [diff] [blame] | 156 | for i in ${interfaces}; do |
Patrick Williams | 185d279 | 2020-05-19 16:41:45 -0500 | [diff] [blame] | 157 | path="${i%.interface.yaml}" |
| 158 | iface="${path//\//.}" |
| 159 | |
Patrick Williams | d77548a | 2022-04-29 14:43:15 -0500 | [diff] [blame] | 160 | generate_single interface server-header "${iface}" "${path}/server.hpp" |
| 161 | generate_single interface server-cpp "${iface}" "${path}/server.cpp" |
| 162 | generate_single interface client-header "${iface}" "${path}/client.hpp" |
| 163 | generate_single interface markdown "${iface}" "${path}.md" append |
Patrick Williams | 185d279 | 2020-05-19 16:41:45 -0500 | [diff] [blame] | 164 | |
Patrick Williams | 185d279 | 2020-05-19 16:41:45 -0500 | [diff] [blame] | 165 | done |
Patrick Williams | 70bdfdd | 2020-07-09 15:53:43 -0500 | [diff] [blame] | 166 | waitall # finish all before continuing |
Patrick Williams | 185d279 | 2020-05-19 16:41:45 -0500 | [diff] [blame] | 167 | |
Patrick Williams | d77548a | 2022-04-29 14:43:15 -0500 | [diff] [blame] | 168 | for e in ${errors}; do |
Patrick Williams | 185d279 | 2020-05-19 16:41:45 -0500 | [diff] [blame] | 169 | path="${e%.errors.yaml}" |
| 170 | iface="${path//\//.}" |
| 171 | |
Patrick Williams | d77548a | 2022-04-29 14:43:15 -0500 | [diff] [blame] | 172 | generate_single error exception-header "${iface}" "${path}/error.hpp" |
| 173 | generate_single error exception-cpp "${iface}" "${path}/error.cpp" |
| 174 | generate_single error markdown "${iface}" "${path}.md" append |
Patrick Williams | 185d279 | 2020-05-19 16:41:45 -0500 | [diff] [blame] | 175 | |
Patrick Williams | 185d279 | 2020-05-19 16:41:45 -0500 | [diff] [blame] | 176 | done |
Patrick Williams | 70bdfdd | 2020-07-09 15:53:43 -0500 | [diff] [blame] | 177 | waitall # finish all before continuing |
Patrick Williams | 185d279 | 2020-05-19 16:41:45 -0500 | [diff] [blame] | 178 | done |