3 This script accepts a cmake lists file as an argument extracts all
4 `option` and `set(... CACHE ...)` variables. It then writes a
5 markdown table to stdout
9 from pathlib
import Path
15 p = argparse.ArgumentParser(description=__doc__)
17 p.add_argument(
"cmakefile", help=
"Input cmake lists file to parse")
19 "--prefix", default=
"ACTS_", help=
"Prefix to identify relevant variables to extract"
25 help=
"Width of second column generated from cmake doc strings",
30 help=
"Write table to this file, expects delimiters CMAKE_OPTS_{BEGIN,END}",
36 help=
"Only verify the target file contains the right table, don't write",
43 cmakefile = Path(args.cmakefile)
45 with cmakefile.open()
as fh:
50 rf
"option\( *({args.prefix}\w*) \"(.*)\" (ON|OFF|\${{\w+}})\ *\)", line
52 name, doc, default = m.groups()
54 if m := re.match(
r"\${(\w+)}", default):
57 default = f
"{lookup} -> {opts[lookup]}"
59 rf
"set\( *({args.prefix}\w*) \"(.*)\" CACHE (\w+) \"(.*)\"( FORCE)? *\)",
62 name, default, type, doc, _ = m.groups()
70 doc =
"<br>".
join(textwrap.wrap(doc, width=args.width))
71 rows.append((name, f
"{doc}<br> type: `{type}`, default: `{default}`"))
75 headers = (
"Option",
"Description")
76 column_lengths = [0] * len(rows[0])
80 column_lengths[i] = max(column_lengths[i], len(col))
84 output +=
" " + header.ljust(column_lengths[i]) +
" |"
88 for i
in range(len(column_lengths)):
89 output +=
"-" + (
"-" * column_lengths[i]) +
"-|"
96 output +=
" " + col.ljust(column_lengths[i]) +
" |"
99 output = output.strip()
101 if args.write
and args.write.exists():
102 source = args.write.read_text().split(
"\n")
104 begin = source.index(
"<!-- CMAKE_OPTS_BEGIN -->")
105 end = source.index(
"<!-- CMAKE_OPTS_END -->")
107 print(
"Markers not found in output file")
111 actual =
"\n".
join(source[begin + 1 : end])
113 print(
"MISMATCH:\n" +
"-" * 9 +
"\n")
116 difflib.unified_diff(
126 out = source[: begin + 1] + output.split(
"\n") + source[end:]
127 args.write.write_text(
"\n".
join(out))