summaryrefslogtreecommitdiff
path: root/tools/notsd-fixup--includes
blob: 9dfa7d06041037264fcc6263050bb8254d351247 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#!/usr/bin/env bash

# Requires Bash 4.2 or higher (for `test -v`).

# If you are thinking "this file looks gross!", it is.  It
# started out as a set of Bash one-liners.  Which got turned
# into a script.  Which grew somewhat organically.  Not huge,
# but given that it started as some one liners, that's not a
# very pretty several hundred lines.  So yes, it is gross.
# Rewrites welcome; just don't introduce any behavioral changes
# (easy since `tools/notsd-move` runs it on the entire repo and
# puts the results in git history).

################################################################
# Everything else in this program is just fluff and bookkeeping
# around around calling classify().

out() {
	_ret_class=$1
	_ret_path=$2
}

# Return a tuple of (class/group, path); which is a class that
# the header path belongs to, and a normalized path for it.
#
# There are a fixed number of classes that it may put a header
# in; in order of most-public to most-private:
#
#  system
#  linux
#  public
#  protected
#  private
#
# This uses the global variable `expensive`.
classify() {
	local current_file=$1
	local path=$2
	if [[ "$path" = linux/* ]]; then
		out linux "$path"
	elif expensive.exists "${current_file%/*}/${path}"; then
		out private "$path"
	elif [[ "$path" != systemd/* ]] &&
	     [[ "$path" != libudev.h ]] &&
	     expensive.cpp "$path"; then
		out system "$path"
	else
		case "$path" in
			*-to-name.h|*-from-name.h)
				base="${path##*/}"
				base="${base%-to-name.h}"
				base="${base%-from-name.h}"
				case "$base" in
					dns_type)	     d=src/grp-resolve/systemd-resolved;;
					keyboard-keys)	     d=src/grp-udev/libudev-core;;
					af|arphrd|cap|errno) d=src/libsystemd-basic/src;;
					audit_type)	     d=src/libsystemd/src/sd-journal;;
					*)
						>&2 printf 'Unknown gperf base: %q\n' "$base"
						>&2 printf 'Cannot figure out: %q\n' "$path"
						return 2
						;;
				esac
				file="$d/${path##*/}"
				if [[ "$current_file" = "$d"/* ]]; then
					out private "${file##*/}"
				elif [[ "$file" = */include/* ]]; then
					out protected "${file##*/include/}"
				else
					out protected "${file##*/}"
				fi
				;;
			asm/sgidefs.h|dbus/dbus.h|efi.h|efilib.h|gio/gio.h|glib.h|libmount.h)
				out system "$path"
				;;
			util.h|*/util.h)
				if [[ "$current_file" = */systemd-boot/* ]]; then
					out private util.h
				else
					out protected systemd-basic/util.h
				fi
				;;
			*)
				file=$(expensive.find "${path##*/}")
				if [[ -f "$file" ]]; then
					case "$file" in
						*/src/*)
							if [[ "${current_file%/*}" = "${file%/*}" ]]; then
								out private "${file##*/}"
							else
								out protected "${file##*/src/}"
							fi
							;;
						*/libsystemd/include/*|*/libudev/include/*)
							out public "${file##*/include/}"
							;;
						*/include/*)
							out protected "${file##*/include/}"
							;;
						*/include-staging/*)
							out protected "${file##*/include-staging/}"
							;;
						*)
							if [[ "${current_file%/*}" = "${file%/*}" ]]; then
								out private "${file##*/}"
							else
								out protected "${file##*/}"
							fi
							;;
					esac
				else
					>&2 printf 'Cannot figure out: %q\n' "$path"
					return 2
				fi
				;;
		esac
	fi
}

################################################################
# Cache expensive things

cache.init_cpp() {
	if ! [[ -v _cache_cpp[@] ]]; then
		if [[ -f "$0.cache/cpp" ]]; then
			. "$0.cache/cpp"
		else
			declare -gA _cache_cpp=()
		fi
	fi
}

cache.save_cpp() {
	cache.init_cpp
	mkdir -p "$0.cache"
	declare -p _cache_cpp | sed 's/-/-g/' > "$0.cache/cpp"
}


cache.init_fs() {
	if ! [[ -v _cache_fs ]]; then
		if ! [[ -f "$0.cache/fs" ]]; then
			>&2 echo expensive fs.find
			mkdir -p "$0.cache"
			find src -name '*.h' \( -type l -printf 'l %p\n' -o -type f -printf 'f %p\n' \) > "$0.cache/fs"
		fi
		declare -g _cache_fs=true
	fi
}

expensive.cpp() {
	local path=$1
	cache.init_cpp
	if [[ -z "${_cache_cpp[$path]}" ]]; then
		>&2 echo expensive cpp "$path"
		local r
		r=0; cpp -include "$path" <<<'' &>/dev/null || r=$?
		_cache_cpp[$path]=$r
	fi
	return ${_cache_cpp[$path]}
}

expensive.exists() {
	local path=$1
	cache.init_fs
	grep -qFx \
	     -e "l $path" \
	     -e "f $path" \
	     < "$0.cache/fs"
}

expensive.find() {
	local name=$1
	cache.init_fs
	sed -n "/^f .*\/${name//./\\.}\$/s/^f //p" < "$0.cache/fs"
}

################################################################
# Data structure for storing a chunk of `#include` lines.

includes.init() {
	_includes_trailing_nl=
	_includes_system=()
	_includes_linux=()
	_includes_public=()
	_includes_protected=()
	_includes_typedef=()
	_includes_typedef_last=true
	_includes_private=()
}
includes.print() {
	local b=:
	if [[ ${#_includes_system[@]} -gt 0 ]]; then
		printf '%s\n' "${_includes_system[@]}" | sort -u
		b=echo
	fi
	if [[ ${#_includes_linux[@]} -gt 0 ]]; then
		$b
		printf '%s\n' "${_includes_linux[@]}"
		b=echo
	fi
	if [[ ${#_includes_public[@]} -gt 0 ]]; then
		$b
		printf '%s\n' "${_includes_public[@]}" | sort -u
		b=echo
	fi
	if [[ ${#_includes_protected[@]} -gt 0 ]]; then
		$b
		printf '%s\n' "${_includes_protected[@]}" | sort -u
		b=echo
	fi
	if [[ ${#_includes_typedef[@]} -gt 0 ]] && ! $_includes_typedef_last; then
		$b
		printf '%s\n' "${typedef[@]}" | sort -u
		b=echo
	fi
	if [[ ${#_includes_private[@]} -gt 0 ]]; then
		$b
		printf '%s\n' "${_includes_private[@]}" | sort -u
		b=echo
	fi
	if [[ ${#_includes_typedef[@]} -gt 0 ]] && $_includes_typedef_last; then
		$b
		printf '%s\n' "${_includes_typedef[@]}"
	fi
	printf '%s' "$_includes_trailing_nl"
}
includes.add() {
	local class=$1
	local path=$2
	local extra=$3
	local line
	case "$class" in
		system)
			printf -v line '#include <%s>%s' "$path" "$extra"
			_includes_system+=("$line")
			;;
		linux)
			printf -v line '#include <%s>%s' "$path" "$extra"
			_includes_linux+=("$line")
			;;
		public)
			printf -v line '#include <%s>%s' "$path" "$extra"
			_includes_public+=("$line")
			;;
		protected)
			printf -v line '#include "%s"%s' "$path" "$extra"
			_includes_protected+=("$line")
			;;
		private)
			if [[ ${#typedef[@]} -gt 0 ]]; then
				_includes_typedef_last=false
			fi
			printf -v line '#include "%s"%s' "$path" "$extra"
			_includes_private+=("$line")
			;;
		*)
			>&2 printf 'Invalid include class: %q\n' "$class"
			return 2
			;;
	esac
}

################################################################
# The main program loop

panic() {
	>&2 echo panic
	exit 2
}

phase0() {
	phase=phase0
	hook=:
	local filename="$1"
	local line="$2"
	case "$line" in
		'#include'*|'typedef '*';')
			includes.init
			phase1 "$filename" "$line"
			;;
		*)
			printf '%s\n' "$line"
			;;
	esac
}

phase1() {
	phase=phase1
	hook=includes.print
	local filename="$1"
	local line="$2"
	case "$line" in
		'')
			_includes_trailing_nl+=$'\n'
			;;
		'#include'*)
			_includes_trailing_nl=''
			local re='^#include [<"]([^">]*)[">](.*)'
			if [[ "$line" =~ $re ]]; then
				# OK, this is gross, but we want to avoid creating a subshell
				local _ret_class _ret_path
				classify "$filename" "${BASH_REMATCH[1]}" || panic
				includes.add "$_ret_class" "$_ret_path" "${BASH_REMATCH[2]}" || panic
			else
				panic
			fi
			;;
		'typedef '*';')
			_includes_trailing_nl=''
			_includes_typedef+=("$line")
			;;
		*)
			includes.print
			phase0 "$filename" "$line"
			;;
	esac
}

phase=phase0
hook=:

main() {
	local filename="$1"
	>&2 printf ' => %q %q\n' "$0" "$filename"
	set -o pipefail
	{
		IFS=''
		while read -r line; do
			"$phase" "$filename" "$line"
			IFS=''
		done
		"$hook"
	} < "$filename"
	cache.save_cpp
}

main "$@"