File: gen-v8-gn.py

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (98 lines) | stat: -rwxr-xr-x 2,920 bytes parent folder | download | duplicates (5)
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
#!/usr/bin/env python3

# Copyright 2021 the V8 project authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import argparse
import os
import sys

if (sys.version_info >= (3, 0)):
  from io import StringIO
else:
  from io import BytesIO as StringIO


def parse_args():
  global args
  parser = argparse.ArgumentParser()
  parser.add_argument('-o', '--output', type=str, action='store',
                      help='Location of header file to generate')
  parser.add_argument(
      '-p',
      '--positive-define',
      type=str,
      action='append',
      help='Externally visibile positive definition in format NAME or NAME=VALUE'
  )
  parser.add_argument('-n', '--negative-define', type=str, action='append',
                      help='Externally visibile negative definition')
  args = parser.parse_args()


def generate_positive_definition(out, define_str):
  # Split the define into name and value if it contains '='
  if '=' in define_str:
    define, value = define_str.split('=', 1)
  else:
    define = define_str
    value = "1"  # Default value if not specified

  out.write('''
#ifndef {define}
#define {define} {value}
#else
#if {define} != {value}
#error "{define} defined but not set to {value}"
#endif
#endif  // {define}
'''.format(define=define, value=value))

def generate_negative_definition(out, define):
  out.write('''
#ifdef {define}
#error "{define} is defined but is disabled by V8's GN build arguments"
#endif  // {define}
'''.format(define=define))

def generate_header(out):
  out.write('''// AUTOMATICALLY GENERATED. DO NOT EDIT.

// The following definitions were used when V8 itself was built, but also appear
// in the externally-visible header files and so must be included by any
// embedder. This will be done automatically if V8_GN_HEADER is defined.
// Ready-compiled distributions of V8 will need to provide this generated header
// along with the other headers in include.

// This header must be stand-alone because it is used across targets without
// introducing dependencies. It should only be included via v8config.h.
''')
  if args.positive_define:
    for define in args.positive_define:
      generate_positive_definition(out, define)

  if args.negative_define:
    for define in args.negative_define:
      generate_negative_definition(out, define)

def main():
  parse_args()
  header_stream = StringIO("")
  generate_header(header_stream)
  contents = header_stream.getvalue()
  if args.output:
    # Check if the contents has changed before writing so we don't cause build
    # churn.
    old_contents = None
    if os.path.exists(args.output):
      with open(args.output, 'r') as f:
        old_contents = f.read()
    if old_contents != contents:
      with open(args.output, 'w') as f:
        f.write(contents)
  else:
    print(contents)

if __name__ == '__main__':
  main()