Skip to content

Implement code generation as a three-step process #583

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 23, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 30 additions & 8 deletions build-script.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,20 +284,28 @@ def generate_syntax_node_template_gyb_files(
verbose=verbose,
)

# Gyb-files that should be generated before running code generation using SwiftSyntaxBuilder.
# Maps directories containing .gyb files to the directories the generated files should
# live in.
def first_stage_gyb_dir_mapping(
generateswiftsyntaxbuilder_destination: Optional[str] = None,
) -> Dict[str, Optional[str]]:
return {
GENERATESWIFTSYNTAXBUILDER_DIR: generateswiftsyntaxbuilder_destination,
}

# Gyb-files that should be generated after running code generation using SwiftSyntaxBuilder.
# Maps directories containing .gyb files to the directories the generated files should
# live in.
def gyb_dir_mapping(
def second_stage_gyb_dir_mapping(
swiftsyntax_destination: Optional[str] = None,
swiftsyntaxbuilder_destination: Optional[str] = None,
swiftsyntaxparser_destination: Optional[str] = None,
generateswiftsyntaxbuilder_destination: Optional[str] = None,
) -> Dict[str, Optional[str]]:
return {
SWIFTSYNTAX_DIR: swiftsyntax_destination,
SWIFTSYNTAXBUILDER_DIR: swiftsyntaxbuilder_destination,
SWIFTSYNTAXPARSER_DIR: swiftsyntaxparser_destination,
GENERATESWIFTSYNTAXBUILDER_DIR: generateswiftsyntaxbuilder_destination,
}


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


def verify_generated_files(gyb_exec: str, verbose: bool) -> None:
gyb_dirs = gyb_dir_mapping(
def verify_gyb_generated_files(gyb_exec: str, verbose: bool) -> None:
first_stage_gyb_dirs = first_stage_gyb_dir_mapping(
generateswiftsyntaxbuilder_destination=tempfile.mkdtemp(),
)
second_stage_gyb_dirs = second_stage_gyb_dir_mapping(
swiftsyntax_destination=tempfile.mkdtemp(),
swiftsyntaxbuilder_destination=tempfile.mkdtemp(),
swiftsyntaxparser_destination=tempfile.mkdtemp(),
generateswiftsyntaxbuilder_destination=tempfile.mkdtemp(),
)
gyb_dirs = first_stage_gyb_dirs
gyb_dirs.update(second_stage_gyb_dirs)

generate_gyb_files(
gyb_exec,
Expand Down Expand Up @@ -682,7 +694,7 @@ def generate_source_code_command(args: argparse.Namespace) -> None:
try:
generate_gyb_files(
args.gyb_exec,
gyb_dir_mapping=gyb_dir_mapping(),
gyb_dir_mapping=first_stage_gyb_dir_mapping(),
add_source_locations=args.add_source_locations,
verbose=args.verbose,
)
Expand All @@ -704,10 +716,20 @@ def generate_source_code_command(args: argparse.Namespace) -> None:
fail_for_called_process_error(
"Source generation using SwiftSyntaxBuilder failed", e)

try:
generate_gyb_files(
args.gyb_exec,
gyb_dir_mapping=second_stage_gyb_dir_mapping(),
add_source_locations=args.add_source_locations,
verbose=args.verbose,
)
except subprocess.CalledProcessError as e:
fail_for_called_process_error("Generating .gyb files failed", e)


def verify_source_code_command(args: argparse.Namespace) -> None:
try:
verify_generated_files(args.gyb_exec, verbose=args.verbose)
verify_gyb_generated_files(args.gyb_exec, verbose=args.verbose)

verify_code_generated_files(
toolchain=args.toolchain,
Expand Down