Skip to content

Commit d5fdbbb

Browse files
committed
Implement code generation as a three-step process
To make sure code generation works when adding new nodes, implement it as a three-stage process. 1. Re-generate the gyb files in SwiftSyntaxBuilderGeneration. This only re-generates the Swift files which essentially match the Python files in gyb_syntax_support. This does not generate any new syntax nodes etc. 2. Run code generation using SwiftSyntaxBuilder (i.e. run SwiftSyntaxBuilderGeneration). This still compiles because stage 1 did not modify any types in SwiftSyntax 3. Generate the remaining gyb files to match the files that were generated using SwiftSyntaxBuilderGeneration in step 2. Since this is Python, it will always pass. Resolves #577
1 parent cdf111e commit d5fdbbb

File tree

1 file changed

+30
-8
lines changed

1 file changed

+30
-8
lines changed

build-script.py

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -284,20 +284,28 @@ def generate_syntax_node_template_gyb_files(
284284
verbose=verbose,
285285
)
286286

287+
# Gyb-files that should be generated before running code generation using SwiftSyntaxBuilder.
288+
# Maps directories containing .gyb files to the directories the generated files should
289+
# live in.
290+
def first_stage_gyb_dir_mapping(
291+
generateswiftsyntaxbuilder_destination: Optional[str] = None,
292+
) -> Dict[str, Optional[str]]:
293+
return {
294+
GENERATESWIFTSYNTAXBUILDER_DIR: generateswiftsyntaxbuilder_destination,
295+
}
287296

297+
# Gyb-files that should be generated after running code generation using SwiftSyntaxBuilder.
288298
# Maps directories containing .gyb files to the directories the generated files should
289299
# live in.
290-
def gyb_dir_mapping(
300+
def second_stage_gyb_dir_mapping(
291301
swiftsyntax_destination: Optional[str] = None,
292302
swiftsyntaxbuilder_destination: Optional[str] = None,
293303
swiftsyntaxparser_destination: Optional[str] = None,
294-
generateswiftsyntaxbuilder_destination: Optional[str] = None,
295304
) -> Dict[str, Optional[str]]:
296305
return {
297306
SWIFTSYNTAX_DIR: swiftsyntax_destination,
298307
SWIFTSYNTAXBUILDER_DIR: swiftsyntaxbuilder_destination,
299308
SWIFTSYNTAXPARSER_DIR: swiftsyntaxparser_destination,
300-
GENERATESWIFTSYNTAXBUILDER_DIR: generateswiftsyntaxbuilder_destination,
301309
}
302310

303311

@@ -451,13 +459,17 @@ def build(self, product_name: str) -> None:
451459
# Testing
452460

453461

454-
def verify_generated_files(gyb_exec: str, verbose: bool) -> None:
455-
gyb_dirs = gyb_dir_mapping(
462+
def verify_gyb_generated_files(gyb_exec: str, verbose: bool) -> None:
463+
first_stage_gyb_dirs = first_stage_gyb_dir_mapping(
464+
generateswiftsyntaxbuilder_destination=tempfile.mkdtemp(),
465+
)
466+
second_stage_gyb_dirs = second_stage_gyb_dir_mapping(
456467
swiftsyntax_destination=tempfile.mkdtemp(),
457468
swiftsyntaxbuilder_destination=tempfile.mkdtemp(),
458469
swiftsyntaxparser_destination=tempfile.mkdtemp(),
459-
generateswiftsyntaxbuilder_destination=tempfile.mkdtemp(),
460470
)
471+
gyb_dirs = first_stage_gyb_dirs
472+
gyb_dirs.update(second_stage_gyb_dirs)
461473

462474
generate_gyb_files(
463475
gyb_exec,
@@ -682,7 +694,7 @@ def generate_source_code_command(args: argparse.Namespace) -> None:
682694
try:
683695
generate_gyb_files(
684696
args.gyb_exec,
685-
gyb_dir_mapping=gyb_dir_mapping(),
697+
gyb_dir_mapping=first_stage_gyb_dir_mapping(),
686698
add_source_locations=args.add_source_locations,
687699
verbose=args.verbose,
688700
)
@@ -704,10 +716,20 @@ def generate_source_code_command(args: argparse.Namespace) -> None:
704716
fail_for_called_process_error(
705717
"Source generation using SwiftSyntaxBuilder failed", e)
706718

719+
try:
720+
generate_gyb_files(
721+
args.gyb_exec,
722+
gyb_dir_mapping=second_stage_gyb_dir_mapping(),
723+
add_source_locations=args.add_source_locations,
724+
verbose=args.verbose,
725+
)
726+
except subprocess.CalledProcessError as e:
727+
fail_for_called_process_error("Generating .gyb files failed", e)
728+
707729

708730
def verify_source_code_command(args: argparse.Namespace) -> None:
709731
try:
710-
verify_generated_files(args.gyb_exec, verbose=args.verbose)
732+
verify_gyb_generated_files(args.gyb_exec, verbose=args.verbose)
711733

712734
verify_code_generated_files(
713735
toolchain=args.toolchain,

0 commit comments

Comments
 (0)