[gn build] Slightly simplify write_cmake_config.

Before, the script had a bunch of special cases for #cmakedefine and
#cmakedefine01 and then did general variable substitution. Now, the script
always does general variable substitution for all lines and handles the special
cases afterwards.

This has no observable effect for the inputs we use, but is easier to explain
and slightly easier to implement.

Also mention to link to CMake's configure_file() in the docstring.

(The new behavior doesn't quite match CMake on lines like #cmakedefine ${FOO},
but nobody does that.)

Differential Revision: https://reviews.llvm.org/D55171

llvm-svn: 348106
This commit is contained in:
Nico Weber 2018-12-02 22:25:25 +00:00
parent 3c469ea731
commit e4f26eb49e
1 changed files with 18 additions and 27 deletions

View File

@ -1,10 +1,17 @@
#!/usr/bin/env python
"""Processes a foo.h.cmake file and writes foo.h.
"""Emulates the bits of CMake's configure_file() function needed in LLVM.
The CMake build uses configure_file() for several things. This emulates that
function for the GN build. In the GN build, this runs at build time, instead
of at generator time.
Takes a list of KEY=VALUE pairs (where VALUE can be empty).
Handles these types of lines (note that FOO= sets the value of FOO to the empty
string, which is falsy, but FOO=0 sets it to '0' which is truthy):
On each line, replaces ${KEY} with VALUE.
After that, also handles these special cases (note that FOO= sets the value of
FOO to the empty string, which is falsy, but FOO=0 sets it to '0' which is
truthy):
1.) #cmakedefine01 FOO
Checks if key FOO is set to a truthy value, and depending on that prints
@ -13,29 +20,15 @@ string, which is falsy, but FOO=0 sets it to '0' which is truthy):
#define FOO 1
#define FOO 0
2.) #cmakedefine FOO
2.) #cmakedefine FOO [...]
Checks if key FOO is set to a truthy in value, and depending on that prints
one of the following two lines:
#define FOO
#define FOO [...]
/* #undef FOO */
3.) #cmakedefine FOO asdf${BAR}jkl
Checks if key FOO is set to a truthy values, and if so replaces all
variable references in `asdf${BAR}jkl` with their value and prints that
(else it prints the same undef line as the previous form):
#define FOO asdfBAR_VALjkl
/* #undef FOO */
4.) #define FOO asdf{BAR}jkl
Always gets its variable values filled in, independent of FOO's value being
set:
#define FOO asdfBAR_VALjkl
Fails if any of the KEY=VALUE arguments aren't needed for processing the
.h.cmake file, or if the .h.cmake file has unreplaces ${VAR} references after
.h.cmake file, or if the .h.cmake file has unreplaced ${VAR} references after
processing all values.
"""
@ -70,9 +63,10 @@ def main():
def repl(m):
unused_values.discard(m.group(1))
return values[m.group(1)]
in_line = var_re.sub(repl, in_line)
if in_line.startswith('#cmakedefine01 '):
_, var = in_line.split()
out_lines.append('#define %s %d\n' % (var, 1 if values[var] else 0))
in_line = '#define %s %d\n' % (var, 1 if values[var] else 0)
unused_values.discard(var)
elif in_line.startswith('#cmakedefine '):
_, var = in_line.split(None, 1)
@ -81,14 +75,11 @@ def main():
except:
var, val = var.rstrip(), '\n'
if values[var]:
out_lines.append('#define %s %s' % (var,
var_re.sub(repl, val)))
in_line = '#define %s %s' % (var, val)
else:
out_lines.append('/* #undef %s */\n' % var)
in_line = '/* #undef %s */\n' % var
unused_values.discard(var)
else:
# In particular, handles `#define FOO ${FOO}` lines.
out_lines.append(var_re.sub(repl, in_line))
out_lines.append(in_line)
if unused_values:
print >>sys.stderr, 'Unused --values args:'