~mmach/netext73/webkit2gtk

« back to all changes in this revision

Viewing changes to Source/ThirdParty/ANGLE/gni-to-cmake.py

  • Committer: mmach
  • Date: 2023-06-16 17:21:37 UTC
  • Revision ID: netbit73@gmail.com-20230616172137-2rqx6yr96ga9g3kp
1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
import sys
 
3
import re
 
4
 
 
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.
 
10
 
 
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.
 
15
 
 
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')
 
21
    exit(1)
 
22
 
 
23
file = open(sys.argv[1], 'rb').read()
 
24
 
 
25
# First translate gn single line list declaration:
 
26
file = re.sub(r'\[ ((?:"[^"]*",? )*)\]$', r' \1)', file, flags=re.M)
 
27
 
 
28
# Translate gn list declaration:
 
29
# variable_name = [
 
30
#   "file/name/foo.cpp",
 
31
# ]
 
32
# to cmake list declaration:
 
33
# set(variable_name
 
34
#     "file/name/foo.cpp"
 
35
# )
 
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)
 
39
 
 
40
# Translate list append fom gn to cmake
 
41
file = re.sub(r'^(\s*)(\w+) \+= ?\[?', r'\1\1list(APPEND \2', file, flags=re.M)
 
42
 
 
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)
 
48
 
 
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)
 
54
 
 
55
out = open(sys.argv[2], 'wb')
 
56
 
 
57
out.write('# This file was generated with the command:\n')
 
58
out.write('# ' + ' '.join(['"' + arg.replace('"', '\\"') + '"' for arg in sys.argv]))
 
59
out.write('\n\n')
 
60
out.write(file)