blob: 518d43bdde2688c6ee5a4b2d0dbe5f0000705312 [file] [log] [blame]
Patrick Williams185d2792020-05-19 16:41:45 -05001#!/usr/bin/env bash
2
3set -e
4
5function 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 Williamsbb140d12020-06-05 16:29:10 -050014 echo " --jobs <N> - number to run in parallel (default: $(nproc))."
Patrick Williams185d2792020-05-19 16:41:45 -050015 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
27sdbuspp="sdbus++"
28outputdir="."
29listall="no"
Patrick Williamse113b202020-06-09 14:10:22 -050030parallel=$(nproc || cat /proc/cpuinfo | grep ^processor | wc -l)
Patrick Williams185d2792020-05-19 16:41:45 -050031
Patrick Williamsbb140d12020-06-05 16:29:10 -050032options="$(getopt -o ho:t:j: --long help,list-all,output:,tool:,jobs: -- "$@")"
Patrick Williams185d2792020-05-19 16:41:45 -050033eval set -- "$options"
34
Patrick Williamsbb140d12020-06-05 16:29:10 -050035while true;
36do
Patrick Williams185d2792020-05-19 16:41:45 -050037 case "$1" in
38 -h | --help)
39 show_usage
40 exit
41 ;;
42
Patrick Williamsbb140d12020-06-05 16:29:10 -050043 -j | --jobs)
44 shift
45 parallel="$1"
46 shift
47 ;;
48
Patrick Williams185d2792020-05-19 16:41:45 -050049 --list-all)
50 listall="yes"
51 shift
52 ;;
53
54 -o | --output)
55 shift
Patrick Williamsbb140d12020-06-05 16:29:10 -050056 outputdir="$1"
Patrick Williams185d2792020-05-19 16:41:45 -050057 shift
58 ;;
59
60 -t | --tool)
61 shift
Patrick Williamsbb140d12020-06-05 16:29:10 -050062 sdbuspp="$1"
Patrick Williams185d2792020-05-19 16:41:45 -050063 shift
64 ;;
65
66 --)
67 shift
68 break
69 ;;
Patrick Williams185d2792020-05-19 16:41:45 -050070 esac
71done
72
73if [ "x" == "x$@" ];
74then
75 show_usage
76 exit 1
77fi
78
Patrick Williams0b2e48e2020-06-09 14:49:06 -050079# 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.
85function generate_single {
Patrick Williams0b731e02020-06-19 09:49:50 -050086
87 if [ "xempty" == "x$1" ];
88 then
89 echo -n > $outputdir/$4
90 elif [ "x" == "x$5" ];
Patrick Williams0b2e48e2020-06-09 14:49:06 -050091 then
92 $sdbuspp $1 $2 $3 > $outputdir/$4 &
93 else
94 $sdbuspp $1 $2 $3 >> $outputdir/$4 &
95 fi
96
97 # Always emit generated file name for foo-cpp and foo-header.
98 # Conditionally emit for everything else depending on $listall.
99 case "$2" in
100 *-cpp | *-header)
101 echo $outputdir/$4
102 ;;
103
104 *)
105 if [ "xyes" == "x$listall" ];
106 then
107 echo $outputdir/$4
108 fi
109 ;;
110 esac
111
112 # Ensure that no more than ${parallel} jobs are running at a time and if so
113 # wait for at least one to finish.
114 while [[ $(jobs -r -p | wc -l) -ge $parallel ]];
115 do
116 wait -n
117 done
118}
119
Patrick Williams185d2792020-05-19 16:41:45 -0500120for d in $@;
121do
122 interfaces="$(find $d -name '*.interface.yaml')"
Patrick Williams0b731e02020-06-19 09:49:50 -0500123 errors="$(find $d -name '*.errors.yaml')"
124
125 # Some files are created from multiple YAML sources, but we don't know
126 # which YAML sources are present. For these files, we need to first
127 # create an empty file to ensure the file does not carry old data from
128 # a previous run.
129 for y in $interfaces $errors;
130 do
131 path="${y%.interface.yaml}"
132 path="${path%.errors.yaml}"
133 iface="${path//\//.}"
134
135 mkdir -p $outputdir/$path
136 generate_single empty markdown $iface $path.md
137 done
Patrick Williams185d2792020-05-19 16:41:45 -0500138
139 for i in $interfaces;
140 do
141 path="${i%.interface.yaml}"
142 iface="${path//\//.}"
143
Patrick Williams0b2e48e2020-06-09 14:49:06 -0500144 generate_single interface server-header $iface $path/server.hpp
145 generate_single interface server-cpp $iface $path/server.cpp
146 generate_single interface client-header $iface $path/client.hpp
Patrick Williams0b731e02020-06-19 09:49:50 -0500147 generate_single interface markdown $iface $path.md append
Patrick Williams185d2792020-05-19 16:41:45 -0500148
Patrick Williams185d2792020-05-19 16:41:45 -0500149 done
Patrick Williamsbb140d12020-06-05 16:29:10 -0500150 wait # finish all before continuing
Patrick Williams185d2792020-05-19 16:41:45 -0500151
Patrick Williams185d2792020-05-19 16:41:45 -0500152
153 for e in $errors;
154 do
155 path="${e%.errors.yaml}"
156 iface="${path//\//.}"
157
Patrick Williams0b2e48e2020-06-09 14:49:06 -0500158 generate_single error exception-header $iface $path/error.hpp
159 generate_single error exception-cpp $iface $path/error.cpp
160 generate_single error markdown $iface $path.md append
Patrick Williams185d2792020-05-19 16:41:45 -0500161
Patrick Williams185d2792020-05-19 16:41:45 -0500162 done
Patrick Williamsbb140d12020-06-05 16:29:10 -0500163 wait # finish all before continuing
Patrick Williams185d2792020-05-19 16:41:45 -0500164done