Skip to content

Commit 4342207

Browse files
committed
Implemented a presets module which includes a more robust and easy to understand parser. Moved the swift-sdks migration code to a new migration module and added testing for both the presets and migration modules. Also converted build-script to use the new presets parser.
1 parent 1a02213 commit 4342207

File tree

6 files changed

+627
-13
lines changed

6 files changed

+627
-13
lines changed

utils/build-script

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import time
2020

2121
from build_swift import defaults
2222
from build_swift import driver_arguments
23+
from build_swift import presets
24+
from build_swift.migration import migrate_swift_sdks
2325

2426
from swift_build_support.swift_build_support import (
2527
arguments,
@@ -32,16 +34,12 @@ from swift_build_support.swift_build_support import (
3234
targets,
3335
workspace
3436
)
35-
3637
from swift_build_support.swift_build_support.SwiftBuildSupport import (
3738
HOME,
3839
SWIFT_BUILD_ROOT,
3940
SWIFT_REPO_NAME,
4041
SWIFT_SOURCE_ROOT,
41-
get_all_preset_names,
42-
get_preset_options,
4342
)
44-
4543
from swift_build_support.swift_build_support.cmake import CMake
4644
from swift_build_support.swift_build_support.targets import \
4745
StdlibDeploymentTarget
@@ -977,42 +975,63 @@ def main_preset():
977975

978976
if len(args.preset_file_names) == 0:
979977
args.preset_file_names = [
980-
os.path.join(HOME, ".swift-build-presets"),
981978
os.path.join(
982979
SWIFT_SOURCE_ROOT, SWIFT_REPO_NAME, "utils",
983980
"build-presets.ini")
984981
]
985982

983+
user_presets_file = os.path.join(HOME, '.swift-build-presets')
984+
if os.path.isfile(user_presets_file):
985+
args.preset_file_names.append(user_presets_file)
986+
987+
preset_parser = presets.PresetParser()
988+
989+
try:
990+
preset_parser.read(args.preset_file_names)
991+
except presets.UnparsedFilesError as e:
992+
diagnostics.fatal('unable to parse preset files {}'
993+
.format(e.message))
994+
986995
if args.show_presets:
987-
for name in sorted(get_all_preset_names(args.preset_file_names),
988-
key=str.lower):
996+
for name in sorted(preset_parser.preset_names(),
997+
key=lambda name: name.lower()):
989998
print(name)
990999
return 0
9911000

9921001
if not args.preset:
9931002
diagnostics.fatal("missing --preset option")
9941003

9951004
args.preset_substitutions = {}
996-
9971005
for arg in args.preset_substitutions_raw:
9981006
name, value = arg.split("=", 1)
9991007
args.preset_substitutions[name] = value
10001008

1001-
preset_args = get_preset_options(
1002-
args.preset_substitutions, args.preset_file_names, args.preset)
1009+
try:
1010+
preset = preset_parser.get_preset(args.preset,
1011+
vars=args.preset_substitutions)
1012+
except presets.InterpolationError as e:
1013+
diagnostics.fatal('missing value for substitution "{}" in preset "{}"'
1014+
.format(e.message, args.preset))
1015+
except presets.MissingOptionError as e:
1016+
diagnostics.fatal('missing option(s) for preset "{}": {}'
1017+
.format(args.preset, e.message))
1018+
except presets.PresetNotFoundError:
1019+
diagnostics.fatal('preset "{}" not found'.format(args.preset))
1020+
1021+
preset_args = migrate_swift_sdks(preset.format_args())
10031022

10041023
build_script_args = [sys.argv[0]]
10051024
if args.dry_run:
10061025
build_script_args += ["--dry-run"]
1026+
10071027
build_script_args += preset_args
10081028
if args.distcc:
10091029
build_script_args += ["--distcc"]
10101030
if args.build_jobs:
10111031
build_script_args += ["--jobs", str(args.build_jobs)]
10121032

1013-
diagnostics.note(
1014-
"using preset '" + args.preset + "', which expands to \n\n" +
1015-
shell.quote_command(build_script_args) + "\n")
1033+
diagnostics.note('using preset "{}", which expands to \n\n{}\n'.format(
1034+
args.preset, shell.quote_command(build_script_args)))
10161035

10171036
if args.expand_build_script_invocation:
10181037
return 0

utils/build_swift/migration.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# This source file is part of the Swift.org open source project
2+
#
3+
# Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
4+
# Licensed under Apache License v2.0 with Runtime Library Exception
5+
#
6+
# See https://swift.org/LICENSE.txt for license information
7+
# See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
8+
9+
10+
"""
11+
Temporary module with functionaly used to migrate away from build-script-impl.
12+
"""
13+
14+
15+
from swift_build_support.swift_build_support.targets import \
16+
StdlibDeploymentTarget
17+
18+
19+
__all__ = [
20+
'UnknownSDKError',
21+
'migrate_swift_sdks',
22+
]
23+
24+
25+
_SDK_TARGETS = {
26+
'OSX': StdlibDeploymentTarget.OSX.targets,
27+
'IOS': StdlibDeploymentTarget.iOS.targets,
28+
'IOS_SIMULATOR': StdlibDeploymentTarget.iOSSimulator.targets,
29+
'TVOS': StdlibDeploymentTarget.AppleTV.targets,
30+
'TVOS_SIMULATOR': StdlibDeploymentTarget.AppleTVSimulator.targets,
31+
'WATCHOS': StdlibDeploymentTarget.AppleWatch.targets,
32+
'WATCHOS_SIMULATOR': StdlibDeploymentTarget.AppleWatchSimulator.targets,
33+
}
34+
35+
36+
# -----------------------------------------------------------------------------
37+
38+
class UnknownSDKError(Exception):
39+
"""Error indicating an unknown SDK was encountered when migrating to target
40+
triples.
41+
"""
42+
43+
pass
44+
45+
46+
def _swift_sdks_to_stdlib_targets(swift_sdks):
47+
stdlib_targets = []
48+
for sdk in swift_sdks:
49+
sdk_targets = _SDK_TARGETS.get(sdk, None)
50+
if sdk_targets is None:
51+
raise UnknownSDKError(sdk)
52+
stdlib_targets += sdk_targets
53+
54+
return stdlib_targets
55+
56+
57+
def migrate_swift_sdks(args):
58+
"""Migrate usages of the now deprecated `--swift-sdks` option to the new
59+
`--stdlib-deployment-targets` option, converting Swift SDKs to the
60+
corresponding targets. Since argument parsing is a last-wins scenario, only
61+
the last `--swift-sdks` option is migrated, all others are removed.
62+
63+
This function is a stop-gap to replacing all instances of `--swift-sdks`.
64+
"""
65+
66+
swift_sdks_args = [arg for arg in args if arg.startswith('--swift-sdks')]
67+
68+
if len(swift_sdks_args) < 1:
69+
return args
70+
71+
# Only get the last --swift-sdks arg since last-wins
72+
swift_sdks_arg = swift_sdks_args[-1]
73+
74+
sdk_list = swift_sdks_arg.split('=')[1].split(';')
75+
stdlib_targets = _swift_sdks_to_stdlib_targets(sdk_list)
76+
77+
target_names = ' '.join([target.name for target in stdlib_targets])
78+
stdlib_targets_arg = '--stdlib-deployment-targets=' + target_names
79+
80+
args = [arg for arg in args if not arg.startswith('--swift-sdks')]
81+
args.append(stdlib_targets_arg)
82+
83+
return args

0 commit comments

Comments
 (0)