1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
|
#!/usr/bin/env python3
# Copyright (C) 2023 Apple Inc. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
# THE POSSIBILITY OF SUCH DAMAGE.
import os
import re
from pathlib import Path
try:
import ply.lex as lex
import ply.yacc as yacc
except ImportError:
import sys
sys.exit('Please run `pip3 install ply`')
# --- Parse Tree
class Comment(str):
pass
class Variable(str):
pass
class Stmt:
def __init__(self, type, children=None):
self.type = type
if children:
self.children = children
else:
self.children = []
def __getitem__(self, index):
out = self.children[index]
return Stmt(self.type, children=out) if isinstance(index, slice) else out
def __len__(self):
return len(self.children)
def __repr__(self):
return f'({self.type})'
class Expr:
def __init__(self, type, *children):
self.type = type
self.children = children
def __getitem__(self, index):
out = self.children[index]
return Expr(self.type, children=out) if isinstance(index, slice) else out
def __len__(self):
return len(self.children)
def __repr__(self):
return f'({self.type}' + ' '.join([f'{child}' for child in self.children]) + ')'
# --- Parser
class Parser:
'''
Base class for a lexer/parser that has the rules defined as methods
'''
tokens = []
precedence = ()
def __init__(self, **kw):
self.debug = kw.get('debug', None)
self.names = {}
try:
modname = os.path.split(os.path.splitext(__file__)[0])[1]
except:
modname = 'parser'
modname += '_' + self.__class__.__name__
self.debugfile = modname + '.dbg'
# Build the lexer and parser
lex.lex(module=self, debug=self.debug)
yacc.yacc(module=self, debug=self.debug, debugfile=self.debugfile)
def parse(self, input):
return yacc.parse(input)
class GnParser(Parser):
reserved = {'if': 'IF', 'else': 'ELSE', 'true': 'TRUE', 'false': 'FALSE'}
tokens = [
'PLUS', 'PLUSEQ', 'MINUS', 'MINUSEQ', 'EQ', 'EQEQ', 'NOT', 'NOTEQ', 'GT', 'GTEQ', 'LT',
'LTEQ', 'ANDAND', 'OROR', 'DOT', 'COMMA', 'LBRACE', 'RBRACE', 'LBRACK', 'RBRACK', 'LPAREN',
'RPAREN', 'NAME', 'INTEGER', 'STRING', 'COMMENT'
] + list(reserved.values())
# Ignored characters
t_ignore = ' \r\t'
# The following character sequences represent punctuation:
t_PLUS = r'\+'
t_PLUSEQ = r'\+='
t_MINUS = r'-'
t_MINUSEQ = r'-='
t_EQ = r'='
t_EQEQ = r'=='
t_NOT = r'!'
t_NOTEQ = r'!='
t_GT = r'>'
t_GTEQ = r'>='
t_LT = r'<'
t_LTEQ = r'<='
t_ANDAND = r'&&'
t_OROR = r'\|\|'
t_DOT = r'\.'
t_COMMA = r','
t_LPAREN = r'\('
t_RPAREN = r'\)'
t_LBRACK = r'\['
t_RBRACK = r'\]'
t_LBRACE = r'{'
t_RBRACE = r'}'
t_STRING = r'\"([^\\\n]|(\\.))*?\"'
t_COMMENT = r'\#.*'
def t_NAME(self, t):
r'[a-zA-Z_][a-zA-Z0-9_]*'
t.type = self.reserved.get(t.value, 'NAME') # Check for reserved words
return t
def t_INTEGER(self, t):
r'\d+'
t.value = int(t.value)
return t
# Ignored token with an action associated with it
def t_ignore_newline(self, t):
r'\n+'
t.lexer.lineno += t.value.count('\n')
# Error handler for illegal characters
def t_error(self, t):
print(f'{t.lexer.lineno}: Illegal character {t.value[0]!r}')
t.lexer.skip(1)
precedence = (('left', 'OROR'), ('left', 'ANDAND'), ('left', 'EQEQ', 'NOTEQ'),
('left', 'GT', 'GTEQ', 'LT', 'LTEQ'), ('left', 'PLUS', 'MINUS'), ('right',
'NOT'))
def p_file(self, p):
'''file : statement_list'''
p[0] = p[1]
def p_statement(self, p):
'''statement : comment
| assignment
| call
| condition'''
p[0] = p[1]
def p_comment(self, p):
'''comment : COMMENT'''
p[0] = Comment(p[1])
def p_lvalue(self, p):
'''lvalue : NAME
| array_access
| scope_access'''
p[0] = p[1]
def p_assignment(self, p):
'''assignment : lvalue EQ expr
| lvalue PLUSEQ expr
| lvalue MINUSEQ expr'''
p[0] = Stmt(p[2], children=[p[1], p[3]])
def p_call_0(self, p):
'''call : NAME LPAREN RPAREN
| NAME LPAREN RPAREN block'''
block = p[4] if len(p) == 5 else None
p[0] = Stmt(p[1], children=[[], block])
def p_call_1(self, p):
'''call : NAME LPAREN expr_list RPAREN
| NAME LPAREN expr_list RPAREN block'''
block = p[5] if len(p) == 6 else None
p[0] = Stmt(p[1], [p[3], block])
def p_condition(self, p):
'''condition : IF LPAREN expr RPAREN block
| IF LPAREN expr RPAREN block ELSE condition
| IF LPAREN expr RPAREN block ELSE block'''
if len(p) == 8:
p[0] = Stmt('if', [p[3], p[5], p[7]])
else:
p[0] = Stmt('if', [p[3], p[5], None])
def p_block(self, p):
'''block : LBRACE RBRACE
| LBRACE statement_list RBRACE'''
if len(p) == 3:
p[0] = []
else:
p[0] = p[2]
def p_statement_list(self, p):
'''statement_list : statement
| statement_list statement'''
if len(p) == 2:
p[0] = [p[1]]
else:
p[0] = p[1] + [p[2]]
def p_array_access(self, p):
'''array_access : NAME LBRACK expr RBRACK'''
p[0] = ('array-access', p[1], p[3])
def p_scope_access(self, p):
'''scope_access : NAME DOT NAME'''
p[0] = ('scope-access', p[1], p[3])
def p_expr_primary(self, p):
'''expr : primary_expr'''
p[0] = p[1]
def p_expr_binary(self, p):
'''expr : expr PLUS expr
| expr MINUS expr
| expr GT expr
| expr GTEQ expr
| expr LT expr
| expr LTEQ expr
| expr EQEQ expr
| expr NOTEQ expr
| expr ANDAND expr
| expr OROR expr'''
p[0] = Expr(p[2], p[1], p[3])
def p_expr_unary(self, p):
'''expr : NOT expr'''
p[0] = Expr(p[1], p[2])
def p_primary_expr_name(self, p):
'''primary_expr : NAME'''
p[0] = Variable(p[1])
def p_primary_expr_sliteral(self, p):
'''primary_expr : STRING'''
p[0] = p[1][1:-1] # Trim leading & trailing "
def p_primary_expr(self, p):
'''primary_expr : INTEGER
| TRUE
| FALSE
| call
| array_access
| scope_access
| block
| LPAREN expr RPAREN'''
if len(p) == 2:
p[0] = p[1]
elif len(p) == 4:
p[0] = p[2]
def p_primary_expr_2(self, p):
'''primary_expr : LBRACK RBRACK
| LBRACK expr_list RBRACK
| LBRACK expr_list COMMA RBRACK'''
if len(p) == 3:
p[0] = []
else:
p[0] = p[2]
def p_expr_list(self, p):
'''expr_list : expr
| expr_list COMMA expr'''
if len(p) == 2:
p[0] = [p[1]]
else:
p[0] = p[1] + [p[3]]
def p_error(self, p):
print(f'{p.lineno}: Syntax error at {p.value!r}')
# --- Process importing GNI files
class ImportGni:
def __init__(self, cwd, exclude):
self.cwd = cwd
if type(exclude) is list:
self.exclude = exclude
else:
self.exclude = [exclude]
def _is_excluded(self, s):
for pat in self.exclude:
if re.fullmatch(pat, s):
return True
return False
def _import_gni(self, import_stmt):
assert (import_stmt.type == 'import')
args = import_stmt[0]
assert (len(args) == 1)
path = args[0]
if self._is_excluded(path):
return []
path = self.cwd / path
return load_gn(path, self.exclude)
def fold_list(self, list):
new_list = []
for child in list:
if isinstance(child, Stmt) and child.type == 'import':
imported = self._import_gni(child)
new_list.extend(imported)
else:
new_list.append(self.fold(child))
return new_list
def fold(self, ast):
if type(ast) is list:
return self.fold_list(ast)
elif isinstance(ast, Stmt):
ast.children = self.fold_list(ast.children)
return ast
def import_gni(ast, cwd, exclude):
'''
Import any gni files referenced in the AST.
Any import paths that match the patterns in `exclude` are skipped.
'''
importer = ImportGni(cwd, exclude)
return importer.fold(ast)
def load_gn(path, exclude):
'''
Load a gn/gni file, loading any imports not excluded by the patterns
in `exclude`.
'''
input = path.read_text()
ast = GnParser().parse(input)
ast = import_gni(ast, path.parent, exclude)
return ast
# --- Convert GN expression operators into CMake versions
class CMakeExprOps:
def fold(self, ast):
if type(ast) is list:
return [self.fold(child) for child in ast]
elif isinstance(ast, Stmt):
ast.children = [self.fold(child) for child in ast.children]
elif isinstance(ast, Expr):
ast.children = [self.fold(child) for child in ast.children]
# Rename GN binary operators into CMake
if ast.type == '==':
ast.type = ' STREQUAL '
elif ast.type == '&&':
ast.type = ' AND '
elif ast.type == '||':
ast.type = ' OR '
elif ast.type == '!':
ast.type = 'NOT '
# Convert list concatenation into CMake form
elif ast.type == '+':
# var + [ list ] -> [ ${var} list ]
if isinstance(ast[0], Variable) and type(ast[1]) is list:
ast = [Variable(f'${{{ast[0]}}}')] + ast[1]
# "" + var -> ${var}
elif len(ast[0]) == 0 and isinstance(ast[1], Variable):
ast = Variable(f'${{{ast[1]}}}')
return ast
def convert_to_cmake_ops(ast):
visitor = CMakeExprOps()
return visitor.fold(ast)
# --- Exclude nodes
class ExcludeStmts:
def __init__(self, exclude):
if type(exclude) is list:
self.exclude = exclude
else:
self.exclude = [exclude]
def _excluded(self, ast):
if isinstance(ast, Stmt):
return ast.type in self.exclude
return False
def fold(self, ast):
if type(ast) is list:
return [self.fold(child) for child in ast if not self._excluded(child)]
elif isinstance(ast, Stmt):
ast.children = self.fold(ast.children)
return ast
def exclude_stmts(ast, exclude):
visitor = ExcludeStmts(exclude)
return visitor.fold(ast)
# --- Write GN into CMake format
class CMakeWriter:
def __init__(self, path, prepend):
if not isinstance(path, Path):
path = Path(path)
self.prepend = prepend
self.output = path.open('w', encoding='utf-8')
self.writef('# This file was generated with the command:\n')
self.writef('# %s\n\n',
' '.join(['"' + arg.replace('"', '\\"') + '"' for arg in sys.argv]))
def __del__(self):
self.output.close()
def writef(self, fmt, *args):
self.output.write(fmt % args)
def visit(self, ast, indent=''):
nindent = indent + ' '
# Lists
if type(ast) is list:
[self.visit(child, indent) for child in ast]
# Comment
elif isinstance(ast, Comment):
self.writef('%s\n', ast)
# Variable
elif isinstance(ast, Variable):
self.writef('%s', ast)
# Strings (representing Paths)
elif isinstance(ast, str):
if self.prepend:
self.writef('"%s%s"', self.prepend, ast)
else:
self.writef('"%s"', ast)
# Expressions
elif isinstance(ast, Expr):
if len(ast) == 1:
self.writef('%s', ast.type)
self.visit(ast[0], indent)
else:
self.visit(ast[0], indent)
self.writef('%s', ast.type)
self.visit(ast[1], indent)
# Statements
elif isinstance(ast, Stmt):
# list assignment
if ast.type == '=':
if indent == '':
self.writef('\n')
self.writef('%sset(%s', indent, ast[0])
lst = []
if type(ast[1]) is list:
lst = ast[1]
if len(lst) == 1:
self.writef(' ')
self.visit(lst[0])
self.writef(')\n')
return
self.writef('\n')
for child in lst:
self.writef('%s', nindent)
self.visit(child, nindent)
self.writef('\n')
self.writef('%s)\n', indent)
# list concatenation
elif ast.type == '+=':
if indent == '':
self.writef('\n')
self.writef('%slist(APPEND %s', indent, ast[0])
if type(ast[1]) is list:
lst = ast[1]
if len(lst) == 1:
self.writef(' ')
self.visit(lst[0])
self.writef(')\n')
return
else:
self.writef('\n')
for child in lst:
self.writef('%s', nindent)
self.visit(child, nindent)
self.writef('\n')
self.writef('%s)\n', indent)
# if then else
elif ast.type == 'if':
if not len(ast[1]):
return
self.writef('\n%sif(', indent)
self.visit(ast[0], indent)
self.writef(f')\n')
self.visit(ast[1], nindent)
if ast[2]:
self.writef('%selse()\n', indent)
self.visit(ast[2], nindent)
self.writef('%sendif()\n', indent)
# foreach loop
elif ast.type == 'foreach':
self.writef('\n%sforeach(', indent)
self.visit(ast[0][0], indent)
self.writef(' IN LISTS ')
self.visit(ast[0][1], indent)
self.writef(f')\n')
self.visit(ast[1], nindent)
self.writef('%sendforeach()\n', indent)
# Unknown - These should have been removed by previous passes over the AST
# before writing the output CMake
else:
raise RuntimeError
def write_cmake(path, ast, prepend):
writer = CMakeWriter(path, prepend)
writer.visit(ast)
if __name__ == '__main__':
import sys
from argparse import ArgumentParser
parser = ArgumentParser(
prog='gni-to-cmake',
description='converts a .gn/.gni file to .cmake',
usage='%(prog)s [options')
parser.add_argument('gni', help='the .gn/gni file to parse')
parser.add_argument('cmake', help='the .cmake file to output')
parser.add_argument('--prepend', help='the path to prepend to each file name')
args = parser.parse_args()
path = Path(args.gni)
if not path.exists():
print(f'{path.fullname}: Not found')
# TODO:
ast = load_gn(path, exclude=[r'.*/angle\.gni$', r'//build/.*'])
ast = exclude_stmts(
ast, exclude=['assert', 'config', 'angle_source_set', 'pkg_config', 'declare_args'])
ast = convert_to_cmake_ops(ast)
write_cmake(args.cmake, ast, prepend=args.prepend)
|