summaryrefslogtreecommitdiffstats
path: root/scripts/configure_helper.py
blob: fd7bed390d2c40dc535de5f9de6751841bea07ee (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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
#!/usr/bin/env python3
#
# Copyright (C) 2017 by Michael Olbrich <m.olbrich@pengutronix.de>
#
# See CREDITS for details about who has contributed to this project.
#
# For further information about the PTXdist project and license conditions
# see the README file.
#

import subprocess
import os
import sys
import re
import difflib
import argparse
import shlex
import json
import io

configure_blacklist = [
	"help",
	"version",
	"quiet",
	"silent",
	"cache-file",
	"config-cache",
	"no-create",
	"srcdir",
	"prefix",
	"exec-prefix",
	"bindir",
	"sbindir",
	"libexecdir",
	"sysconfdir",
	r".*statedir",
	"runstatedir",
	"libdir",
	"includedir",
	"oldincludedir",
	"datarootdir",
	"datadir",
	"infodir",
	"localedir",
	"mandir",
	"docdir",
	"htmldir",
	"dvidir",
	"pdfdir",
	"psdir",
	"program-prefix",
	"program-suffix",
	"program-transform-name",
	"build",
	"host",
	"target",
	"option-checking",
	"FEATURE",
	"silent-rules",
	"maintainer-mode",
	"dependency-tracking",
	"fast-install",
	"libtool-lock",

	"Bsymbolic",
	"znodelete",

	"PACKAGE",
	"libiconv",
	r".*prefix",
	"pic",
	"aix-soname",
	"gnu-ld",
	"sysroot",
	"html-dir",
	"xml-catalog",

	"bash-completion-dir",

	"pkg-config-path",
	"package-name",

	"autoconf",
	"autoheader",
	"automake",
	"aclocal",

	"pkgconfigdir",

	"x-includes",
	"x-libraries",
	"ld-version-script",

	"shared",
	"static",
]

meson_blacklist = [
	"c_std",
	"cpp_debugstl",
	"cpp_std",
	"b_asneeded",
	"b_colorout",
	"b_coverage",
	"b_lto",
	"b_lundef",
	"b_ndebug",
	"b_pch",
	"b_pgo",
	"b_sanitize",
	"b_staticpic",
	"bindir",
	"datadir",
	"default_library",
	"errorlogs",
	"includedir",
	"infodir",
	"layout",
	"libexecdir",
	"localedir",
	"localstatedir",
	"mandir",
	"sbindir",
	"sharedstatedir",
	"stdsplit",
	"strip",
	"sysconfdir",
	"unity",
	"warning_level",
	"werror",

	"bashcompletiondir",
]

def abort(message):
	print(message)
	print("\nSee '%s --help' for more details." % cmd)
	exit(1)

def ask_ptxdist(pkg):
	ptxdist = os.environ.get("ptxdist", "ptxdist")
	p = subprocess.Popen([ ptxdist, "-k", "make",
		"/print-%s_DIR" % pkg,
		"/print-%s_SUBDIR" % pkg,
		"/print-%s_CONF_OPT" % pkg,
		"/print-%s_AUTOCONF" % pkg,
		"/print-%s_CONF_TOOL" %pkg,
		"/print-CROSS_MESON_USR",
		"/print-CROSS_AUTOCONF_USR",
		"/print-PTXDIST_SYSROOT_HOST"],
		stdout=subprocess.PIPE,
		universal_newlines=True)

	d = p.stdout.readline().strip()
	subdir = p.stdout.readline().strip()
	opt = shlex.split(p.stdout.readline().strip()) + shlex.split(p.stdout.readline().strip())
	tool = p.stdout.readline().strip()
	if not tool and opt:
		tool = "autoconf"
	meson_default = shlex.split(p.stdout.readline().strip())
	autoconf_default = shlex.split(p.stdout.readline().strip())
	sysroot_host = p.stdout.readline().strip()
	if tool == "meson" and not opt:
		opt = meson_default
	if tool == "autoconf" and not opt:
		opt = autoconf_default
	if not d:
		abort("'%s' is not a valid package: %s_DIR is undefined" % (pkg, pkg))
	if not opt:
		abort("'%s' is not a autoconf/meson package: %s_CONF_OPT and %s_AUTOCONF are undefined" % (pkg, pkg, pkg))
	return (tool, d, subdir, opt, sysroot_host)

def blacklist_hit(name, blacklist):
	for e in blacklist:
		if re.fullmatch(e, name):
			return True
	return False

parse_args_re = re.compile("--((enable|disable|with|without|with\(out\))-)?\[?([^\[=]*)(([\[=]*)([^]]*)]?)?")
def parse_configure_args(args, blacklist):
	ret = []
	for arg in args:
		match = parse_args_re.match(arg)
		if not match:
			continue
		groups = match.groups()
		if not groups[2]:
			continue
		found = False
		for e in ret:
			if groups[2] == e["name"]:
				found = True
				break
		if found:
			continue
		ret.append({"name": groups[2], "value": groups[1],
				"arg": None if not groups[4] else groups[5],
				"blacklist": blacklist_hit(groups[2], blacklist)})
	return ret

meson_parse_args_re = re.compile("-D(.*)=(.*)")
def parse_meson_args(args, blacklist):
	ret = []
	new_args = []
	last_args = []
	last = None
	for arg in args:
		if last != None:
			name = last
			value = arg
			# Most --* args show up as generic arguments at the end
			if name != "cross-file":
				last_args.append("-D%s=%s" % (name, value))
			last = None
		else:
			if arg.startswith("--"):
				last = arg[2:]
				continue
			else:
				last = None
			r = meson_parse_args_re.match(arg)
			if not r:
				continue
			groups = r.groups()
			name = groups[0]
			value = groups[1]
			new_args.append(arg)

		found = False
		for e in ret:
			if name == e["name"]:
				found = True
				break
		if found:
			continue
		try:
			value = json.load(io.StringIO(value))
		except:
			pass
		ret.append({"name": name, "value": value,
				"blacklist": blacklist_hit(name, blacklist)})
	return (ret, new_args + sorted(last_args))

def build_args(parsed):
	build = []
	for arg in parsed:
		try:
			i = next(index for (index, d) in enumerate(parsed_pkg_conf_opt) if d["name"] == arg["name"])
			arg["blacklist"] = False
			arg["value"] = parsed_pkg_conf_opt[i]["value"]
			arg["arg"] = parsed_pkg_conf_opt[i]["arg"]
		except:
			pass
		if arg["blacklist"]:
			continue
		arg_str = "--"
		if arg["value"]:
			arg_str += arg["value"] + "-"
		arg_str += arg["name"]
		if arg["arg"] != None:
			arg_str += "=" + arg["arg"]
		build.append("\t" + arg_str + "\n")
	return build

def handle_dir(d, subdir):
	if not d:
		return (None, None, None)

	if subdir:
		d = os.path.join(d, subdir)

	d = os.path.normpath(d)
	if not os.path.exists(d):
		abort("'%s' does not exist" % d)

	configure = d + "/configure"
	meson_build = d + "/meson.build"
	if not os.path.exists(configure) and not os.path.exists(meson_build):
		abort("not a autoconf/meson package: configure script / meson.build file not found in '%s'" % d)
		exit(1)

	if os.path.exists(configure) and tool != "meson":
		return handle_dir_configure(d, configure)
	if os.path.exists(meson_build):
		return handle_dir_meson(d)

def handle_dir_configure(d, configure):
	configure_args = []
	p = subprocess.Popen([ "./" + os.path.basename(configure), "--help" ],
			stdout=subprocess.PIPE, universal_newlines=True,
			cwd=os.path.dirname(configure))
	lines = p.stdout.read().splitlines()
	for line in lines:
		if not re.match("^\s.*", line):
			continue
		try:
			word = re.split("[\s,]", line.strip())[0]
		except:
			continue
		if word[:2] == "--":
			configure_args.append(word)
		elif ptx_pkg == "host-qemu" and re.match("  [a-z].*", line):
			configure_args.append("--enable-" + word)

	parsed = parse_configure_args(configure_args, configure_blacklist)
	args = build_args(parsed)
	label = os.path.basename(d)
	return (parsed, args, label)

def handle_dir_meson(d):
	meson_builddir = d + "-build"
	if not os.path.exists(meson_builddir):
		abort("package must be configured")
		exit(1)
	args = []
	p = subprocess.Popen([ os.path.join(sysroot_host, "bin", "meson"), "introspect", meson_builddir, "--buildoptions" ], stdout=subprocess.PIPE, universal_newlines=True)
	options = json.load(p.stdout)
	for option in options:
		try:
			i = next(index for (index, d) in enumerate(parsed_pkg_conf_opt) if d["name"] == option["name"])
			option["blacklist"] = False
			option["value"] = parsed_pkg_conf_opt[i]["value"]
		except:
			pass
		option["blacklist"] = blacklist_hit(option["name"], meson_blacklist)
		if option["blacklist"]:
			continue

		value = option["value"]
		if not isinstance(value, str) or value.count(" ") > 0:
			i = io.StringIO()
			json.dump(value, i)
			value = i.getvalue()

		args.append("\t-D%s=%s\n" % (option["name"], value))

	label = os.path.basename(d)
	return (options, args, label)

def show_diff(old_opt, old_label, new_opt, new_label):
	if args.sort:
		sys.stdout.writelines(difflib.unified_diff(
			sorted(old_opt), sorted(new_opt),
			fromfile=old_label, tofile=new_label))
	else:
		sys.stdout.writelines(difflib.unified_diff(
			old_opt, new_opt,
			fromfile=old_label, tofile=new_label))

cmd = os.path.basename(sys.argv[0])
epilog = """
There are several diffent ways to configure arguments:

$ %s --pkg <pkg>
This will compare the available configure arguments of the current version
with those specified in PTXdist

$ %s --only-src /path/to/src --pkg <pkg>
This will compare the available configure arguments of the specified source
with those specified in PTXdist

$ %s --old-src /path/to/old-src --pkg <pkg>
$ %s --new-src /path/to/new-src --pkg <pkg>
This will compare the available configure arguments of the current version
with those of the specified old/new version

$ %s --new-src /path/to/new-src --old-src /path/to/old-src
This will compare the available configure arguments of the old and new
versions.

Note: this tool contains a blacklist with all options that usually don't
need to be added.

If '--pkg' is used, then the script must be called in the BSP workspace.
The environment variable 'ptxdist' can be used to specify the ptxdist
version to use.
""" % (cmd, cmd, cmd, cmd, cmd)

parser = argparse.ArgumentParser(epilog=epilog,
	formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument("-p", "--pkg", help="the ptxdist package to check",
	dest="pkg")
parser.add_argument("-o", "--old-src", help="the old source directory",
	dest="old")
parser.add_argument("-n", "--new-src", help="the new source directory",
	dest="new")
parser.add_argument("-s", "--only-src", help="the only source directory",
	dest="only")
parser.add_argument("--sort", help="sort the options before comparing",
	dest="sort", action="store_true")

args = parser.parse_args()

if len(sys.argv) == 1:
	parser.print_help()
	exit(1)

old_dir = args.old if args.old else None
new_dir = args.new if args.new else None

if (old_dir or new_dir) and args.only:
	abort("'--old-src' and '--new-src' make no sense in combination with '--only-src'")

if args.only:
	new_dir = args.only

if args.pkg:
	ptx_pkg = args.pkg.lower().replace('_', "-")
	ptx_PKG = args.pkg.upper().replace('-', "_")
else:
	ptx_pkg = None
	ptx_PKG = None

ptx_pkg_conf_opt = []
pkg_subdir = ""
tool = None
if args.pkg:
	(tool, d, pkg_subdir, pkg_conf_opt, sysroot_host) = ask_ptxdist(ptx_PKG)
	ptx_pkg_label = "rules/%s.make" % ptx_pkg
	if tool == "autoconf":
		parsed_pkg_conf_opt = parse_configure_args(pkg_conf_opt, [])
	if tool == "meson":
		(parsed_pkg_conf_opt, pkg_conf_opt) = parse_meson_args(pkg_conf_opt, [])

	if args.only:
		pass
	elif not new_dir:
		new_dir = d
	elif not old_dir:
		old_dir = d

	for arg in pkg_conf_opt:
		ptx_pkg_conf_opt.append("\t" + arg + "\n")

else:
	parsed_pkg_conf_opt = []
	if not old_dir or not new_dir:
		abort("If no package is given, then both '--old-src' and '--new-src' must be specified")

(old_parsed_configure_args, old_pkg_conf_opt, old_pkg_label) = handle_dir(old_dir, pkg_subdir)
(new_parsed_configure_args, new_pkg_conf_opt, new_pkg_label) = handle_dir(new_dir, pkg_subdir)

if not old_pkg_conf_opt:
	show_diff(ptx_pkg_conf_opt, ptx_pkg_label, new_pkg_conf_opt, new_pkg_label)
elif not new_pkg_conf_opt:
	show_diff(old_pkg_conf_opt, old_pkg_label, ptx_pkg_conf_opt, ptx_pkg_label)
else:
	show_diff(old_pkg_conf_opt, old_pkg_label, new_pkg_conf_opt, new_pkg_label)