11cb0ef41Sopenharmony_ci#!/usr/bin/env python 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ci# Copyright 2021 the V8 project authors. All rights reserved. 41cb0ef41Sopenharmony_ci# Use of this source code is governed by a BSD-style license that can be 51cb0ef41Sopenharmony_ci# found in the LICENSE file. 61cb0ef41Sopenharmony_ci 71cb0ef41Sopenharmony_ciimport argparse 81cb0ef41Sopenharmony_ciimport os 91cb0ef41Sopenharmony_ciimport sys 101cb0ef41Sopenharmony_ci 111cb0ef41Sopenharmony_ciif (sys.version_info >= (3, 0)): 121cb0ef41Sopenharmony_ci from io import StringIO 131cb0ef41Sopenharmony_cielse: 141cb0ef41Sopenharmony_ci from io import BytesIO as StringIO 151cb0ef41Sopenharmony_ci 161cb0ef41Sopenharmony_ci 171cb0ef41Sopenharmony_cidef parse_args(): 181cb0ef41Sopenharmony_ci global args 191cb0ef41Sopenharmony_ci parser = argparse.ArgumentParser() 201cb0ef41Sopenharmony_ci parser.add_argument('-o', '--output', type=str, action='store', 211cb0ef41Sopenharmony_ci help='Location of header file to generate') 221cb0ef41Sopenharmony_ci parser.add_argument('-p', '--positive-define', type=str, action='append', 231cb0ef41Sopenharmony_ci help='Externally visibile positive definition') 241cb0ef41Sopenharmony_ci parser.add_argument('-n', '--negative-define', type=str, action='append', 251cb0ef41Sopenharmony_ci help='Externally visibile negative definition') 261cb0ef41Sopenharmony_ci args = parser.parse_args() 271cb0ef41Sopenharmony_ci 281cb0ef41Sopenharmony_cidef generate_positive_definition(out, define): 291cb0ef41Sopenharmony_ci out.write(''' 301cb0ef41Sopenharmony_ci#ifndef {define} 311cb0ef41Sopenharmony_ci#define {define} 1 321cb0ef41Sopenharmony_ci#else 331cb0ef41Sopenharmony_ci#if {define} != 1 341cb0ef41Sopenharmony_ci#error "{define} defined but not set to 1" 351cb0ef41Sopenharmony_ci#endif 361cb0ef41Sopenharmony_ci#endif // {define} 371cb0ef41Sopenharmony_ci'''.format(define=define)) 381cb0ef41Sopenharmony_ci 391cb0ef41Sopenharmony_cidef generate_negative_definition(out, define): 401cb0ef41Sopenharmony_ci out.write(''' 411cb0ef41Sopenharmony_ci#ifdef {define} 421cb0ef41Sopenharmony_ci#error "{define} is defined but is disabled by V8's GN build arguments" 431cb0ef41Sopenharmony_ci#endif // {define} 441cb0ef41Sopenharmony_ci'''.format(define=define)) 451cb0ef41Sopenharmony_ci 461cb0ef41Sopenharmony_cidef generate_header(out): 471cb0ef41Sopenharmony_ci out.write('''// AUTOMATICALLY GENERATED. DO NOT EDIT. 481cb0ef41Sopenharmony_ci 491cb0ef41Sopenharmony_ci// The following definitions were used when V8 itself was built, but also appear 501cb0ef41Sopenharmony_ci// in the externally-visible header files and so must be included by any 511cb0ef41Sopenharmony_ci// embedder. This will be done automatically if V8_GN_HEADER is defined. 521cb0ef41Sopenharmony_ci// Ready-compiled distributions of V8 will need to provide this generated header 531cb0ef41Sopenharmony_ci// along with the other headers in include. 541cb0ef41Sopenharmony_ci 551cb0ef41Sopenharmony_ci// This header must be stand-alone because it is used across targets without 561cb0ef41Sopenharmony_ci// introducing dependencies. It should only be included via v8config.h. 571cb0ef41Sopenharmony_ci''') 581cb0ef41Sopenharmony_ci if args.positive_define: 591cb0ef41Sopenharmony_ci for define in args.positive_define: 601cb0ef41Sopenharmony_ci generate_positive_definition(out, define) 611cb0ef41Sopenharmony_ci 621cb0ef41Sopenharmony_ci if args.negative_define: 631cb0ef41Sopenharmony_ci for define in args.negative_define: 641cb0ef41Sopenharmony_ci generate_negative_definition(out, define) 651cb0ef41Sopenharmony_ci 661cb0ef41Sopenharmony_cidef main(): 671cb0ef41Sopenharmony_ci parse_args() 681cb0ef41Sopenharmony_ci header_stream = StringIO("") 691cb0ef41Sopenharmony_ci generate_header(header_stream) 701cb0ef41Sopenharmony_ci contents = header_stream.getvalue() 711cb0ef41Sopenharmony_ci if args.output: 721cb0ef41Sopenharmony_ci # Check if the contents has changed before writing so we don't cause build 731cb0ef41Sopenharmony_ci # churn. 741cb0ef41Sopenharmony_ci old_contents = None 751cb0ef41Sopenharmony_ci if os.path.exists(args.output): 761cb0ef41Sopenharmony_ci with open(args.output, 'r') as f: 771cb0ef41Sopenharmony_ci old_contents = f.read() 781cb0ef41Sopenharmony_ci if old_contents != contents: 791cb0ef41Sopenharmony_ci with open(args.output, 'w') as f: 801cb0ef41Sopenharmony_ci f.write(contents) 811cb0ef41Sopenharmony_ci else: 821cb0ef41Sopenharmony_ci print(contents) 831cb0ef41Sopenharmony_ci 841cb0ef41Sopenharmony_ciif __name__ == '__main__': 851cb0ef41Sopenharmony_ci main() 86