1#!/usr/bin/env python
2
3# Copyright 2021 the V8 project authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7import argparse
8import os
9import sys
10
11if (sys.version_info >= (3, 0)):
12  from io import StringIO
13else:
14  from io import BytesIO as StringIO
15
16
17def parse_args():
18  global args
19  parser = argparse.ArgumentParser()
20  parser.add_argument('-o', '--output', type=str, action='store',
21                      help='Location of header file to generate')
22  parser.add_argument('-p', '--positive-define', type=str, action='append',
23                      help='Externally visibile positive definition')
24  parser.add_argument('-n', '--negative-define', type=str, action='append',
25                      help='Externally visibile negative definition')
26  args = parser.parse_args()
27
28def generate_positive_definition(out, define):
29  out.write('''
30#ifndef {define}
31#define {define} 1
32#else
33#if {define} != 1
34#error "{define} defined but not set to 1"
35#endif
36#endif  // {define}
37'''.format(define=define))
38
39def generate_negative_definition(out, define):
40  out.write('''
41#ifdef {define}
42#error "{define} is defined but is disabled by V8's GN build arguments"
43#endif  // {define}
44'''.format(define=define))
45
46def generate_header(out):
47  out.write('''// AUTOMATICALLY GENERATED. DO NOT EDIT.
48
49// The following definitions were used when V8 itself was built, but also appear
50// in the externally-visible header files and so must be included by any
51// embedder. This will be done automatically if V8_GN_HEADER is defined.
52// Ready-compiled distributions of V8 will need to provide this generated header
53// along with the other headers in include.
54
55// This header must be stand-alone because it is used across targets without
56// introducing dependencies. It should only be included via v8config.h.
57''')
58  if args.positive_define:
59    for define in args.positive_define:
60      generate_positive_definition(out, define)
61
62  if args.negative_define:
63    for define in args.negative_define:
64      generate_negative_definition(out, define)
65
66def main():
67  parse_args()
68  header_stream = StringIO("")
69  generate_header(header_stream)
70  contents = header_stream.getvalue()
71  if args.output:
72    # Check if the contents has changed before writing so we don't cause build
73    # churn.
74    old_contents = None
75    if os.path.exists(args.output):
76      with open(args.output, 'r') as f:
77        old_contents = f.read()
78    if old_contents != contents:
79      with open(args.output, 'w') as f:
80        f.write(contents)
81  else:
82    print(contents)
83
84if __name__ == '__main__':
85  main()
86