Skip to content

Commit e4f26eb

Browse files
committedDec 2, 2018
[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
1 parent 3c469ea commit e4f26eb

File tree

1 file changed

+18
-27
lines changed

1 file changed

+18
-27
lines changed
 

‎llvm/utils/gn/build/write_cmake_config.py

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
#!/usr/bin/env python
2-
"""Processes a foo.h.cmake file and writes foo.h.
2+
"""Emulates the bits of CMake's configure_file() function needed in LLVM.
3+
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.
37
48
Takes a list of KEY=VALUE pairs (where VALUE can be empty).
59
6-
Handles these types of lines (note that FOO= sets the value of FOO to the empty
7-
string, which is falsy, but FOO=0 sets it to '0' which is truthy):
10+
On each line, replaces ${KEY} with VALUE.
11+
12+
After that, also handles these special cases (note that FOO= sets the value of
13+
FOO to the empty string, which is falsy, but FOO=0 sets it to '0' which is
14+
truthy):
815
916
1.) #cmakedefine01 FOO
1017
Checks if key FOO is set to a truthy value, and depending on that prints
@@ -13,29 +20,15 @@
1320
#define FOO 1
1421
#define FOO 0
1522
16-
2.) #cmakedefine FOO
23+
2.) #cmakedefine FOO [...]
1724
Checks if key FOO is set to a truthy in value, and depending on that prints
1825
one of the following two lines:
1926
20-
#define FOO
27+
#define FOO [...]
2128
/* #undef FOO */
2229
23-
3.) #cmakedefine FOO asdf${BAR}jkl
24-
Checks if key FOO is set to a truthy values, and if so replaces all
25-
variable references in `asdf${BAR}jkl` with their value and prints that
26-
(else it prints the same undef line as the previous form):
27-
28-
#define FOO asdfBAR_VALjkl
29-
/* #undef FOO */
30-
31-
4.) #define FOO asdf{BAR}jkl
32-
Always gets its variable values filled in, independent of FOO's value being
33-
set:
34-
35-
#define FOO asdfBAR_VALjkl
36-
3730
Fails if any of the KEY=VALUE arguments aren't needed for processing the
38-
.h.cmake file, or if the .h.cmake file has unreplaces ${VAR} references after
31+
.h.cmake file, or if the .h.cmake file has unreplaced ${VAR} references after
3932
processing all values.
4033
"""
4134

@@ -70,9 +63,10 @@ def main():
7063
def repl(m):
7164
unused_values.discard(m.group(1))
7265
return values[m.group(1)]
66+
in_line = var_re.sub(repl, in_line)
7367
if in_line.startswith('#cmakedefine01 '):
7468
_, var = in_line.split()
75-
out_lines.append('#define %s %d\n' % (var, 1 if values[var] else 0))
69+
in_line = '#define %s %d\n' % (var, 1 if values[var] else 0)
7670
unused_values.discard(var)
7771
elif in_line.startswith('#cmakedefine '):
7872
_, var = in_line.split(None, 1)
@@ -81,14 +75,11 @@ def repl(m):
8175
except:
8276
var, val = var.rstrip(), '\n'
8377
if values[var]:
84-
out_lines.append('#define %s %s' % (var,
85-
var_re.sub(repl, val)))
78+
in_line = '#define %s %s' % (var, val)
8679
else:
87-
out_lines.append('/* #undef %s */\n' % var)
80+
in_line = '/* #undef %s */\n' % var
8881
unused_values.discard(var)
89-
else:
90-
# In particular, handles `#define FOO ${FOO}` lines.
91-
out_lines.append(var_re.sub(repl, in_line))
82+
out_lines.append(in_line)
9283

9384
if unused_values:
9485
print >>sys.stderr, 'Unused --values args:'

0 commit comments

Comments
 (0)