6
6
import subprocess
7
7
import sys
8
8
import tempfile
9
+ from typing import Dict , List , Optional
9
10
10
11
11
12
# -----------------------------------------------------------------------------
47
48
# Xcode Projects Generation
48
49
49
50
50
- def xcode_gen (config ):
51
+ def xcode_gen (config : str ):
51
52
print ("** Generate SwiftSyntax as an Xcode project **" )
52
53
os .chdir (PACKAGE_DIR )
53
54
swiftpm_call = ["swift" , "package" , "generate-xcodeproj" ]
@@ -60,28 +61,28 @@ def xcode_gen(config):
60
61
# Helpers
61
62
62
63
63
- def printerr (message ):
64
+ def printerr (message : str ):
64
65
print (message , file = sys .stderr )
65
66
66
67
67
- def note (message ):
68
+ def note (message : str ):
68
69
print ("--- %s: note: %s" % (os .path .basename (sys .argv [0 ]), message ))
69
70
sys .stdout .flush ()
70
71
71
72
72
- def fatal_error (message ):
73
+ def fatal_error (message : str ):
73
74
printerr (message )
74
75
sys .exit (1 )
75
76
76
77
77
- def escapeCmdArg (arg ) :
78
+ def escapeCmdArg (arg : str ) -> str :
78
79
if '"' in arg or " " in arg :
79
80
return '"%s"' % arg .replace ('"' , '\\ "' )
80
81
else :
81
82
return arg
82
83
83
84
84
- def call (cmd , env = os .environ , stdout = None , stderr = subprocess .STDOUT , verbose = False ):
85
+ def call (cmd : List [ str ] , env : Dict [ str , str ] = dict ( os .environ ) , stdout = None , stderr = subprocess .STDOUT , verbose : bool = False ):
85
86
if verbose :
86
87
print (" " .join ([escapeCmdArg (arg ) for arg in cmd ]))
87
88
process = subprocess .Popen (cmd , env = env , stdout = stdout , stderr = stderr )
@@ -90,13 +91,13 @@ def call(cmd, env=os.environ, stdout=None, stderr=subprocess.STDOUT, verbose=Fal
90
91
return process .returncode
91
92
92
93
93
- def check_call (cmd , cwd = None , env = os .environ , verbose = False ):
94
+ def check_call (cmd : List [ str ] , cwd : Optional [ str ] = None , env : Dict [ str , str ] = dict ( os .environ ) , verbose : bool = False ):
94
95
if verbose :
95
96
print (" " .join ([escapeCmdArg (arg ) for arg in cmd ]))
96
- return subprocess .check_call (cmd , cwd = cwd , env = env , stderr = subprocess .STDOUT )
97
+ subprocess .check_call (cmd , cwd = cwd , env = env , stderr = subprocess .STDOUT )
97
98
98
99
99
- def realpath (path ) :
100
+ def realpath (path : Optional [ str ]) -> Optional [ str ] :
100
101
if path is None :
101
102
return None
102
103
return os .path .realpath (path )
@@ -106,7 +107,7 @@ def realpath(path):
106
107
# Generating gyb Files
107
108
108
109
109
- def check_gyb_exec (gyb_exec ):
110
+ def check_gyb_exec (gyb_exec : str ):
110
111
if not os .path .exists (gyb_exec ):
111
112
fatal_error (
112
113
"""
@@ -128,14 +129,14 @@ def check_rsync():
128
129
129
130
130
131
def generate_single_gyb_file (
131
- gyb_exec ,
132
- gyb_file ,
133
- output_file_name ,
134
- destination ,
135
- temp_files_dir ,
136
- add_source_locations ,
137
- additional_gyb_flags ,
138
- verbose ,
132
+ gyb_exec : str ,
133
+ gyb_file : str ,
134
+ output_file_name : str ,
135
+ destination : str ,
136
+ temp_files_dir : str ,
137
+ add_source_locations : bool ,
138
+ additional_gyb_flags : List [ str ] ,
139
+ verbose : bool ,
139
140
):
140
141
# Source locations are added by default by gyb, and cleared by passing
141
142
# `--line-directive=` (nothing following the `=`) to the generator. Our
@@ -171,11 +172,11 @@ def generate_single_gyb_file(
171
172
# `destination_dir`, otherwise they will be written to
172
173
# `sources_dir/gyb_generated`.
173
174
def generate_gyb_files_helper (
174
- sources_dir ,
175
- destination_dir ,
176
- gyb_exec ,
177
- add_source_locations ,
178
- verbose ,
175
+ sources_dir : str ,
176
+ destination_dir : Optional [ str ] ,
177
+ gyb_exec : str ,
178
+ add_source_locations : bool ,
179
+ verbose : bool ,
179
180
):
180
181
temp_files_dir = tempfile .gettempdir ()
181
182
make_dir_if_needed (temp_files_dir )
@@ -217,10 +218,10 @@ def generate_gyb_files_helper(
217
218
# `destination_dir/syntax_nodes`, otherwise they will be written to
218
219
# `sources_dir/gyb_generated/syntax_nodes`.
219
220
def generate_syntax_node_template_gyb_files (
220
- destination_dir ,
221
- gyb_exec ,
222
- add_source_locations ,
223
- verbose
221
+ destination_dir : Optional [ str ] ,
222
+ gyb_exec : str ,
223
+ add_source_locations : bool ,
224
+ verbose : bool
224
225
):
225
226
temp_files_dir = tempfile .gettempdir ()
226
227
make_dir_if_needed (temp_files_dir )
@@ -260,10 +261,11 @@ def generate_syntax_node_template_gyb_files(
260
261
261
262
262
263
def generate_gyb_files (
263
- gyb_exec , verbose , add_source_locations ,
264
- swiftsyntax_destination = None , swiftsyntaxbuilder_destination = None ,
265
- swiftsyntaxparser_destination = None ,
266
- swiftsyntaxbuildergenerator_destination = None
264
+ gyb_exec : str , verbose : bool , add_source_locations : bool ,
265
+ swiftsyntax_destination : Optional [str ] = None ,
266
+ swiftsyntaxbuilder_destination : Optional [str ] = None ,
267
+ swiftsyntaxparser_destination : Optional [str ] = None ,
268
+ swiftsyntaxbuildergenerator_destination : Optional [str ] = None
267
269
):
268
270
print ("** Generating gyb Files **" )
269
271
@@ -307,7 +309,7 @@ def generate_gyb_files(
307
309
308
310
print ("** Done Generating gyb Files **" )
309
311
310
- def run_code_generation (toolchain , build_dir , multiroot_data_file , release , verbose , swiftsyntaxbuilder_destination ) :
312
+ def run_code_generation (toolchain : str , build_dir : Optional [ str ] , multiroot_data_file : Optional [ str ] , release : bool , verbose : bool , swiftsyntaxbuilder_destination : str ) -> bool :
311
313
print ("** Running code generation **" )
312
314
swiftpm_call = get_swiftpm_invocation (
313
315
toolchain = toolchain ,
@@ -338,7 +340,7 @@ def make_dir_if_needed(path):
338
340
339
341
# Remove any files in the `gyb_generated` directory that no longer have a
340
342
# corresponding `.gyb` file in the `Sources` directory.
341
- def clear_gyb_files_from_previous_run (sources_dir , destination_dir , verbose ):
343
+ def clear_gyb_files_from_previous_run (sources_dir : str , destination_dir : str , verbose : bool ):
342
344
for previous_gyb_gen_file in os .listdir (destination_dir ):
343
345
if previous_gyb_gen_file .endswith (".swift" ):
344
346
gyb_file = os .path .join (
@@ -356,7 +358,7 @@ def clear_gyb_files_from_previous_run(sources_dir, destination_dir, verbose):
356
358
# Building SwiftSyntax
357
359
358
360
359
- def get_swiftpm_invocation (toolchain , action , build_dir , multiroot_data_file , release ) :
361
+ def get_swiftpm_invocation (toolchain : str , action : str , build_dir : Optional [ str ] , multiroot_data_file : Optional [ str ] , release : bool ) -> List [ str ] :
360
362
swift_exec = os .path .join (toolchain , "bin" , "swift" )
361
363
362
364
swiftpm_call = [swift_exec , action ]
@@ -374,14 +376,18 @@ def get_swiftpm_invocation(toolchain, action, build_dir, multiroot_data_file, re
374
376
375
377
376
378
class Builder (object ):
379
+ swiftpm_call : List [str ]
380
+ verbose : bool
381
+ toolchain : str
382
+
377
383
def __init__ (
378
384
self ,
379
- toolchain ,
380
- build_dir ,
381
- multiroot_data_file ,
382
- release ,
383
- verbose ,
384
- disable_sandbox = False ,
385
+ toolchain : str ,
386
+ build_dir : Optional [ str ] ,
387
+ multiroot_data_file : Optional [ str ] ,
388
+ release : bool ,
389
+ verbose : bool ,
390
+ disable_sandbox : bool = False ,
385
391
):
386
392
self .swiftpm_call = get_swiftpm_invocation (
387
393
toolchain = toolchain ,
@@ -397,7 +403,7 @@ def __init__(
397
403
self .verbose = verbose
398
404
self .toolchain = toolchain
399
405
400
- def build (self , product_name ):
406
+ def build (self , product_name : str ):
401
407
print ("** Building " + product_name + " **" )
402
408
command = list (self .swiftpm_call )
403
409
command .extend (["--product" , product_name ])
@@ -414,7 +420,7 @@ def build(self, product_name):
414
420
# Testing
415
421
416
422
417
- def verify_generated_files (gyb_exec , verbose ):
423
+ def verify_generated_files (gyb_exec : str , verbose : bool ):
418
424
user_swiftsyntax_generated_dir = os .path .join (
419
425
SWIFTSYNTAX_DIR , "gyb_generated"
420
426
)
@@ -453,7 +459,7 @@ def verify_generated_files(gyb_exec, verbose):
453
459
user_swiftsyntaxbuildergeneration_generated_dir )
454
460
455
461
456
- def verify_code_generated_files (toolchain , build_dir , multiroot_data_file , release , verbose ):
462
+ def verify_code_generated_files (toolchain : str , build_dir : Optional [ str ] , multiroot_data_file : Optional [ str ] , release : bool , verbose : bool ):
457
463
user_swiftsyntaxbuilder_generated_dir = os .path .join (
458
464
SWIFTSYNTAXBUILDER_DIR , "generated"
459
465
)
@@ -475,7 +481,7 @@ def verify_code_generated_files(toolchain, build_dir, multiroot_data_file, relea
475
481
user_swiftsyntaxbuilder_generated_dir )
476
482
477
483
478
- def check_generated_files_match (self_generated_dir , user_generated_dir ):
484
+ def check_generated_files_match (self_generated_dir : str , user_generated_dir : str ):
479
485
command = [
480
486
"diff" ,
481
487
"--recursive" ,
@@ -505,9 +511,9 @@ def verify_c_syntax_nodes_match():
505
511
506
512
507
513
def run_tests (
508
- toolchain , build_dir , multiroot_data_file , release , filecheck_exec ,
509
- skip_lit_tests , verbose
510
- ):
514
+ toolchain : str , build_dir : Optional [ str ] , multiroot_data_file : Optional [ str ] , release : bool , filecheck_exec : Optional [ str ] ,
515
+ skip_lit_tests : bool , verbose : bool
516
+ ) -> bool :
511
517
print ("** Running SwiftSyntax Tests **" )
512
518
513
519
if skip_lit_tests :
@@ -567,7 +573,7 @@ def check_incr_transfer_roundtrip_exec():
567
573
)
568
574
569
575
570
- def find_lit_test_helper_exec (toolchain , build_dir , release ):
576
+ def find_lit_test_helper_exec (toolchain : str , build_dir : Optional [ str ] , release : bool ):
571
577
swiftpm_call = get_swiftpm_invocation (
572
578
toolchain = toolchain ,
573
579
action = "build" ,
@@ -582,7 +588,7 @@ def find_lit_test_helper_exec(toolchain, build_dir, release):
582
588
return os .path .join (bin_dir .strip ().decode ('utf-8' ), "lit-test-helper" )
583
589
584
590
585
- def run_lit_tests (toolchain , build_dir , release , filecheck_exec , verbose ):
591
+ def run_lit_tests (toolchain : str , build_dir : Optional [ str ] , release : bool , filecheck_exec : Optional [ str ] , verbose : bool ):
586
592
print ("** Running lit-based tests **" )
587
593
588
594
check_lit_exec ()
@@ -615,7 +621,7 @@ def run_lit_tests(toolchain, build_dir, release, filecheck_exec, verbose):
615
621
# XCTest Tests
616
622
617
623
618
- def run_xctests (toolchain , build_dir , multiroot_data_file , release , verbose ):
624
+ def run_xctests (toolchain : str , build_dir : Optional [ str ] , multiroot_data_file : Optional [ str ] , release : bool , verbose : bool ):
619
625
print ("** Running XCTests **" )
620
626
swiftpm_call = get_swiftpm_invocation (
621
627
toolchain = toolchain ,
0 commit comments