1#!/usr/bin/tclsh
2
3set cachefile [lindex $argv 0]
4
5if { $cachefile == "" } {
6	puts stderr "Usage: [file tail $argv0] <existing CMakeCache.txt file>"
7	exit 1
8}
9
10set struct {
11	name
12	type
13	value
14	description
15}
16
17set fd [open $cachefile r]
18
19set cached ""
20
21set dbase ""
22
23while {[gets $fd line] != -1 } {
24	set line [string trim $line]
25
26	# Hash comment
27	if { [string index $line 0] == "#" } {
28		continue
29	}
30
31	# empty line
32	if { $line == "" } {
33		set cached ""
34		continue
35	}
36
37	if { [string range $line 0 1] == "//" } {
38		set linepart [string range $line 2 end]
39		# Variable description. Add to cache.
40		if { $cached != "" && [string index $cached end] != " " && [string index $linepart 0] != " " } {
41			append cached " "
42		}
43		append cached $linepart
44	}
45
46	# Possibly a variable
47	if [string is alpha [string index $line 0]] {
48		# Note: this skips variables starting grom underscore.
49
50		if { [string range $line 0 5] == "CMAKE_" } {
51			# Skip variables with CMAKE_ prefix, they are internal.
52			continue
53		}
54
55		lassign [split $line =] vartype value
56		lassign [split $vartype :] var type
57
58		# Store the variable now
59		set storage [list $var $type $value $cached]
60		set cached ""
61		lappend dbase $storage
62
63		continue
64	}
65
66	#puts stderr "Ignored line: $line"
67
68	# Ignored.
69}
70
71# Now look over the stored variables
72
73set lenlimit 80
74
75foreach stor $dbase {
76
77	lassign $stor {*}$struct
78
79	if { [string length $description] > $lenlimit } {
80		set description [string range $description 0 $lenlimit-2]...
81	}
82
83	if { $type in {STATIC INTERNAL} } {
84		continue
85	}
86
87	# Check special case of CXX to turn back to c++.
88	set pos [string first CXX $name]
89	if { $pos != -1 } {
90		# Check around, actually after XX should be no letter.
91		if { $pos+3 >= [string length $name] || ![string is alpha [string index $name $pos+3]] } {
92			set name [string replace $name $pos $pos+2 C++]
93		}
94	}
95
96	set optname [string tolower [string map {_ -} $name]]
97
98	# Variables of type bool are just empty.
99	# Variables of other types must have =<value> added.
100	# Lowercase cmake type will be used here.
101	set optassign ""
102	set def ""
103	if { $type != "BOOL" } {
104		set optassign "=<[string tolower $type]>"
105	} else {
106		# Supply default for boolean option
107		set def " (default: $value)"
108	}
109
110	puts "    $optname$optassign \"$description$def\""
111}
112
113
114
115
116