5
# This is a very dumb regex based method of translating from
6
# gn variable declarations and simple if statements to the
7
# equivalent cmake code. It only supports a few constructs and
8
# assumes the code is formatted a certain way. ANGLE gn files
9
# are auto-formatted so this should work most of the time.
11
# The output may need a bit of manual fixup, but it beats
12
# translating the whole thing by hand. If new constructs
13
# are added to the ANGLE gn files, hopefully we can just add
14
# a few extra regexes here.
16
if len(sys.argv) != 3:
17
sys.stderr.write('Error: wrong number of arguments.\n\n')
18
sys.stderr.write('Two arguments are required. The first argument is the path\n')
19
sys.stderr.write('of the input .gni file, and the second argument is the path\n')
20
sys.stderr.write('of the output .cmake file.\n')
23
file = open(sys.argv[1], 'rb').read()
25
# First translate gn single line list declaration:
26
file = re.sub(r'\[ ((?:"[^"]*",? )*)\]$', r' \1)', file, flags=re.M)
28
# Translate gn list declaration:
30
# "file/name/foo.cpp",
32
# to cmake list declaration:
36
file = re.sub(r'^(\s*)(\w+) = ?\[?', r'\1\1set(\2', file, flags=re.M)
37
file = re.sub(r'^(\s*)("[^"]+"),$', r'\1\1\2', file, flags=re.M)
38
file = re.sub(r'^(\s*)]$', r'\1\1)', file, flags=re.M)
40
# Translate list append fom gn to cmake
41
file = re.sub(r'^(\s*)(\w+) \+= ?\[?', r'\1\1list(APPEND \2', file, flags=re.M)
43
# Translate if statements from gn to cmake
44
file = re.sub(r'^(\s*)((?:} else )?)if \((.+)\) {$', r'\1\1\2if(\3)', file, flags=re.M)
45
file = re.sub(r'^} else if$', r'elseif', file, flags=re.M)
46
file = re.sub(r'^(\s*)} else {$', r'\1\1else()', file, flags=re.M)
47
file = re.sub(r'^(\s*)}$', r'\1\1endif()', file, flags=re.M)
49
# Translate logic ops from gn to cmake
50
file = re.sub(r' \|\| ', r' OR ', file, flags=re.M)
51
file = re.sub(r' \&\& ', r' AND ', file, flags=re.M)
52
file = re.sub(r' == ', r' STREQUAL ', file, flags=re.M)
53
file = re.sub(r'!', r' NOT ', file, flags=re.M)
55
out = open(sys.argv[2], 'wb')
57
out.write('# This file was generated with the command:\n')
58
out.write('# ' + ' '.join(['"' + arg.replace('"', '\\"') + '"' for arg in sys.argv]))