Skip to content

Commit aea6d97

Browse files
authored
Merge pull request #29474 from Rostepher/build-script-sections
[NFC][Build System: build-script] Re-organize build-script into more logical sections.
2 parents 87e9bc7 + 23b936c commit aea6d97

File tree

1 file changed

+59
-31
lines changed

1 file changed

+59
-31
lines changed

utils/build-script

Lines changed: 59 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
#!/usr/bin/env python
2-
# utils/build-script - The ultimate tool for building Swift -*- python -*-
3-
#
2+
43
# This source file is part of the Swift.org open source project
54
#
6-
# Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
5+
# Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
76
# Licensed under Apache License v2.0 with Runtime Library Exception
87
#
98
# See https://swift.org/LICENSE.txt for license information
109
# See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
1110

12-
from __future__ import print_function
11+
12+
"""
13+
The ultimate tool for building Swift.
14+
"""
15+
16+
17+
from __future__ import absolute_import, print_function, unicode_literals
1318

1419
import json
1520
import os
@@ -26,13 +31,11 @@ from build_swift.build_swift import presets
2631

2732
import six
2833

29-
from swift_build_support.swift_build_support import (
30-
diagnostics,
31-
products,
32-
shell,
33-
targets,
34-
workspace
35-
)
34+
from swift_build_support.swift_build_support import diagnostics
35+
from swift_build_support.swift_build_support import products
36+
from swift_build_support.swift_build_support import shell
37+
from swift_build_support.swift_build_support import targets
38+
from swift_build_support.swift_build_support import workspace
3639
from swift_build_support.swift_build_support.SwiftBuildSupport import (
3740
HOME,
3841
SWIFT_BUILD_ROOT,
@@ -46,10 +49,30 @@ from swift_build_support.swift_build_support.targets import \
4649
StdlibDeploymentTarget
4750
from swift_build_support.swift_build_support.toolchain import host_toolchain
4851

49-
build_script_impl = os.path.join(
52+
53+
# -----------------------------------------------------------------------------
54+
# Constants
55+
56+
BUILD_SCRIPT_IMPL = os.path.join(
5057
SWIFT_SOURCE_ROOT, SWIFT_REPO_NAME, "utils", "build-script-impl")
5158

5259

60+
# -----------------------------------------------------------------------------
61+
# Helpers
62+
63+
def clean_delay():
64+
"""Provide a short delay so accidentally invoked clean builds can be
65+
canceled.
66+
"""
67+
68+
sys.stdout.write('Starting clean build in ')
69+
for i in range(3, 0, -1):
70+
sys.stdout.write('\b%d' % i)
71+
sys.stdout.flush()
72+
time.sleep(1)
73+
print('\b\b\b\bnow.')
74+
75+
5376
def exit_rejecting_arguments(message, parser=None):
5477
print(message, file=sys.stderr)
5578
if parser:
@@ -107,9 +130,12 @@ def tar(source, destination):
107130
shell.call(args + [source], stderr=shell.DEVNULL)
108131

109132

110-
class BuildScriptInvocation(object):
133+
# -----------------------------------------------------------------------------
134+
# Build Script Impl Wrapping
111135

112-
"""Represent a single build script invocation."""
136+
class BuildScriptInvocation(object):
137+
"""Represent a single build script invocation.
138+
"""
113139

114140
@staticmethod
115141
def validate_arguments(toolchain, args):
@@ -290,7 +316,7 @@ class BuildScriptInvocation(object):
290316
'-DSWIFT_DARWIN_MODULE_ARCHS:STRING={}'.format(
291317
args.swift_darwin_module_archs))
292318

293-
# ---
319+
# -------------------------------------------------------------------------
294320

295321
def __init__(self, toolchain, args):
296322
self.toolchain = toolchain
@@ -795,7 +821,7 @@ class BuildScriptInvocation(object):
795821
# `build-script-impl`.
796822
if self.args.legacy_impl:
797823
# Execute the underlying build script implementation.
798-
shell.call_without_sleeping([build_script_impl] + self.impl_args,
824+
shell.call_without_sleeping([BUILD_SCRIPT_IMPL] + self.impl_args,
799825
env=self.impl_env, echo=True)
800826
return
801827

@@ -917,23 +943,15 @@ class BuildScriptInvocation(object):
917943

918944
def _execute_action(self, action_name):
919945
shell.call_without_sleeping(
920-
[build_script_impl] + self.impl_args +
946+
[BUILD_SCRIPT_IMPL] + self.impl_args +
921947
["--only-execute", action_name],
922948
env=self.impl_env, echo=self.args.verbose_build)
923949

924950

925-
# Provide a short delay so accidentally invoked clean builds can be canceled.
926-
def clean_delay():
927-
sys.stdout.write('Starting clean build in ')
928-
for i in range(3, 0, -1):
929-
sys.stdout.write('\b%d' % i)
930-
sys.stdout.flush()
931-
time.sleep(1)
932-
print('\b\b\b\bnow.')
951+
# -----------------------------------------------------------------------------
952+
# Main (preset)
933953

934-
935-
# Main entry point for the preset mode.
936-
def main_preset():
954+
def parse_preset_args():
937955
parser = argparse.ArgumentParser(
938956
formatter_class=argparse.RawDescriptionHelpFormatter,
939957
description="""Builds Swift using a preset.""")
@@ -1006,7 +1024,13 @@ def main_preset():
10061024
help="Reconfigure all projects as we build",
10071025
action="store_true",
10081026
default=False)
1009-
args = parser.parse_args()
1027+
1028+
return parser.parse_args()
1029+
1030+
1031+
def main_preset():
1032+
args = parse_preset_args()
1033+
10101034
if len(args.preset_file_names) == 0:
10111035
args.preset_file_names = [
10121036
os.path.join(
@@ -1088,7 +1112,9 @@ def main_preset():
10881112
return 0
10891113

10901114

1091-
# Main entry point for the normal mode.
1115+
# -----------------------------------------------------------------------------
1116+
# Main (normal)
1117+
10921118
def main_normal():
10931119
parser = driver_arguments.create_argument_parser()
10941120

@@ -1098,7 +1124,7 @@ def main_normal():
10981124
# If we received any impl args, check if `build-script-impl` would
10991125
# accept them or not before any further processing.
11001126
try:
1101-
migration.check_impl_args(build_script_impl,
1127+
migration.check_impl_args(BUILD_SCRIPT_IMPL,
11021128
args.build_script_impl_args)
11031129
except ValueError as e:
11041130
exit_rejecting_arguments(e, parser)
@@ -1193,6 +1219,8 @@ def main_normal():
11931219
return 0
11941220

11951221

1222+
# -----------------------------------------------------------------------------
1223+
11961224
def main():
11971225
if not SWIFT_SOURCE_ROOT:
11981226
diagnostics.fatal(

0 commit comments

Comments
 (0)