Skip to content

Commit 69f8b2a

Browse files
authored
Merge pull request #606 from ahoppen/pr/build-script-generation-clean-up
Simplify a few parameters in gyb-generation of build-script.py
2 parents 7012611 + 03efd13 commit 69f8b2a

File tree

1 file changed

+37
-30
lines changed

1 file changed

+37
-30
lines changed

build-script.py

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -191,16 +191,13 @@ def generate_single_gyb_file(
191191
# `sources_dir/gyb_generated`.
192192
def generate_gyb_files_helper(
193193
sources_dir: str,
194-
destination_dir: Optional[str],
194+
destination_dir: str,
195195
gyb_exec: str,
196196
add_source_locations: bool,
197197
verbose: bool,
198198
) -> None:
199199
temp_files_dir = tempfile.gettempdir()
200200
make_dir_if_needed(temp_files_dir)
201-
202-
if destination_dir is None:
203-
destination_dir = os.path.join(sources_dir, "gyb_generated")
204201
make_dir_if_needed(destination_dir)
205202

206203
# Clear any *.swift files that are relics from the previous run.
@@ -284,35 +281,48 @@ def generate_syntax_node_template_gyb_files(
284281
verbose=verbose,
285282
)
286283

287-
# Gyb-files that should be generated before running code generation using SwiftSyntaxBuilder.
284+
285+
# If `temp_directories` is True, creates a dictionary that maps every source dir in
286+
# `source_dirs` to a unique temporary directory.
287+
# If `temp_directories` is False, it maps each source dir to the corresponding
288+
# gyb_generated directory.
289+
def gyb_dir_mapping(temp_directories: bool, source_dirs: List[str]) -> Dict[str, str]:
290+
mapping = {}
291+
for source_dir in source_dirs:
292+
if temp_directories:
293+
mapping[source_dir] = tempfile.mkdtemp()
294+
else:
295+
mapping[source_dir] = os.path.join(source_dir, "gyb_generated")
296+
return mapping
297+
298+
299+
# Gyb-files that should be generated before running generate-swift-syntax-builder.
288300
# Maps directories containing .gyb files to the directories the generated files should
289301
# live in.
290302
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-
}
303+
temp_directories: bool
304+
) -> Dict[str, str]:
305+
return gyb_dir_mapping(temp_directories=temp_directories, source_dirs=[
306+
GENERATESWIFTSYNTAXBUILDER_DIR
307+
])
296308

297-
# Gyb-files that should be generated after running code generation using SwiftSyntaxBuilder.
309+
310+
# Gyb-files that should be generated after running generate-swift-syntax-builder.
298311
# Maps directories containing .gyb files to the directories the generated files should
299312
# live in.
300313
def second_stage_gyb_dir_mapping(
301-
swiftsyntax_destination: Optional[str] = None,
302-
swiftsyntax_raw_destination: Optional[str] = None,
303-
swiftsyntaxbuilder_destination: Optional[str] = None,
304-
swiftsyntaxparser_destination: Optional[str] = None,
305-
) -> Dict[str, Optional[str]]:
306-
return {
307-
SWIFTSYNTAX_DIR: swiftsyntax_destination,
308-
os.path.join(SWIFTSYNTAX_DIR, "Raw"): swiftsyntax_raw_destination,
309-
SWIFTSYNTAXBUILDER_DIR: swiftsyntaxbuilder_destination,
310-
SWIFTSYNTAXPARSER_DIR: swiftsyntaxparser_destination,
311-
}
314+
temp_directories: bool
315+
) -> Dict[str, str]:
316+
return gyb_dir_mapping(temp_directories=temp_directories, source_dirs=[
317+
SWIFTSYNTAX_DIR,
318+
os.path.join(SWIFTSYNTAX_DIR, "Raw"),
319+
SWIFTSYNTAXBUILDER_DIR,
320+
SWIFTSYNTAXPARSER_DIR,
321+
])
312322

313323

314324
def generate_gyb_files(
315-
gyb_exec: str, gyb_dir_mapping: Dict[str, Optional[str]],
325+
gyb_exec: str, gyb_dir_mapping: Dict[str, str],
316326
add_source_locations: bool, verbose: bool,
317327
) -> None:
318328
print("** Generating gyb Files **")
@@ -463,13 +473,10 @@ def build(self, product_name: str) -> None:
463473

464474
def verify_gyb_generated_files(gyb_exec: str, verbose: bool) -> None:
465475
first_stage_gyb_dirs = first_stage_gyb_dir_mapping(
466-
generateswiftsyntaxbuilder_destination=tempfile.mkdtemp(),
476+
temp_directories=True
467477
)
468478
second_stage_gyb_dirs = second_stage_gyb_dir_mapping(
469-
swiftsyntax_destination=tempfile.mkdtemp(),
470-
swiftsyntax_raw_destination=tempfile.mkdtemp(),
471-
swiftsyntaxbuilder_destination=tempfile.mkdtemp(),
472-
swiftsyntaxparser_destination=tempfile.mkdtemp(),
479+
temp_directories=True
473480
)
474481
gyb_dirs = first_stage_gyb_dirs
475482
gyb_dirs.update(second_stage_gyb_dirs)
@@ -697,7 +704,7 @@ def generate_source_code_command(args: argparse.Namespace) -> None:
697704
try:
698705
generate_gyb_files(
699706
args.gyb_exec,
700-
gyb_dir_mapping=first_stage_gyb_dir_mapping(),
707+
gyb_dir_mapping=first_stage_gyb_dir_mapping(temp_directories=False),
701708
add_source_locations=args.add_source_locations,
702709
verbose=args.verbose,
703710
)
@@ -722,7 +729,7 @@ def generate_source_code_command(args: argparse.Namespace) -> None:
722729
try:
723730
generate_gyb_files(
724731
args.gyb_exec,
725-
gyb_dir_mapping=second_stage_gyb_dir_mapping(),
732+
gyb_dir_mapping=second_stage_gyb_dir_mapping(temp_directories=False),
726733
add_source_locations=args.add_source_locations,
727734
verbose=args.verbose,
728735
)

0 commit comments

Comments
 (0)