[yaml2obj/obj2yaml] - Add support for .stack_sizes sections.
[llvm-complete.git] / utils / gn / build / write_cmake_config.py
blobd57435285d1a00f60d529d9d13b6e568b1f4a747
1 #!/usr/bin/env python
2 """Emulates the bits of CMake's configure_file() function needed in LLVM.
4 The CMake build uses configure_file() for several things. This emulates that
5 function for the GN build. In the GN build, this runs at build time instead
6 of at generator time.
8 Takes a list of KEY=VALUE pairs (where VALUE can be empty).
10 The sequence `\` `n` in each VALUE is replaced by a newline character.
12 On each line, replaces '${KEY}' or '@KEY@' with VALUE.
14 Then, handles these special cases (note that FOO= sets the value of FOO to the
15 empty string, which is falsy, but FOO=0 sets it to '0' which is truthy):
17 1.) #cmakedefine01 FOO
18 Checks if key FOO is set to a truthy value, and depending on that prints
19 one of the following two lines:
21 #define FOO 1
22 #define FOO 0
24 2.) #cmakedefine FOO [...]
25 Checks if key FOO is set to a truthy in value, and depending on that prints
26 one of the following two lines:
28 #define FOO [...]
29 /* #undef FOO */
31 Fails if any of the KEY=VALUE arguments aren't needed for processing the
32 input file, or if the input file references keys that weren't passed in.
33 """
35 from __future__ import print_function
37 import argparse
38 import os
39 import re
40 import sys
43 def main():
44 parser = argparse.ArgumentParser(
45 epilog=__doc__,
46 formatter_class=argparse.RawDescriptionHelpFormatter)
47 parser.add_argument('input', help='input file')
48 parser.add_argument('values', nargs='*', help='several KEY=VALUE pairs')
49 parser.add_argument('-o', '--output', required=True,
50 help='output file')
51 args = parser.parse_args()
53 values = {}
54 for value in args.values:
55 key, val = value.split('=', 1)
56 if key in values:
57 print('duplicate key "%s" in args' % key, file=sys.stderr)
58 return 1
59 values[key] = val.replace('\\n', '\n')
60 unused_values = set(values.keys())
62 # Matches e.g. '${FOO}' or '@FOO@' and captures FOO in group 1 or 2.
63 var_re = re.compile(r'\$\{([^}]*)\}|@([^@]*)@')
65 in_lines = open(args.input).readlines()
66 out_lines = []
67 for in_line in in_lines:
68 def repl(m):
69 key = m.group(1) or m.group(2)
70 unused_values.discard(key)
71 return values[key]
72 in_line = var_re.sub(repl, in_line)
73 if in_line.startswith('#cmakedefine01 '):
74 _, var = in_line.split()
75 in_line = '#define %s %d\n' % (var, 1 if values[var] else 0)
76 unused_values.discard(var)
77 elif in_line.startswith('#cmakedefine '):
78 _, var = in_line.split(None, 1)
79 try:
80 var, val = var.split(None, 1)
81 in_line = '#define %s %s' % (var, val) # val ends in \n.
82 except:
83 var = var.rstrip()
84 in_line = '#define %s\n' % var
85 if not values[var]:
86 in_line = '/* #undef %s */\n' % var
87 unused_values.discard(var)
88 out_lines.append(in_line)
90 if unused_values:
91 print('unused values args:', file=sys.stderr)
92 print(' ' + '\n '.join(unused_values), file=sys.stderr)
93 return 1
95 output = ''.join(out_lines)
97 leftovers = var_re.findall(output)
98 if leftovers:
99 print(
100 'unprocessed values:\n',
101 '\n'.join([x[0] or x[1] for x in leftovers]),
102 file=sys.stderr)
103 return 1
105 if not os.path.exists(args.output) or open(args.output).read() != output:
106 open(args.output, 'w').write(output)
107 os.chmod(args.output, os.stat(args.input).st_mode & 0o777)
110 if __name__ == '__main__':
111 sys.exit(main())