Skip to content

Commit 3b1de47

Browse files
authored
[build-script] Presets Module (#14422) (#14834)
* 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. * Switched the expansion algorithm to resolve mixins in-place (little known feature) and also changed the parser to skip all non-preset sections. Tests are included for these two behaviors. * Re-worked the presets error hierarchy to have more descriptive and information packed exception classes. Also re-worked the PresetParser to catch duplicate preset declarations and duplicate options in a single preset. There's some special shim-code to handle the disconnect between the Python 2 ConfigParser module and the Python 3 update which adds DuplicateOptionError.
1 parent a0a3270 commit 3b1de47

File tree

6 files changed

+826
-13
lines changed

6 files changed

+826
-13
lines changed

utils/build-script

Lines changed: 25 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
@@ -974,42 +972,56 @@ def main_preset():
974972

975973
if len(args.preset_file_names) == 0:
976974
args.preset_file_names = [
977-
os.path.join(HOME, ".swift-build-presets"),
978975
os.path.join(
979976
SWIFT_SOURCE_ROOT, SWIFT_REPO_NAME, "utils",
980977
"build-presets.ini")
981978
]
982979

980+
user_presets_file = os.path.join(HOME, '.swift-build-presets')
981+
if os.path.isfile(user_presets_file):
982+
args.preset_file_names.append(user_presets_file)
983+
984+
preset_parser = presets.PresetParser()
985+
986+
try:
987+
preset_parser.read(args.preset_file_names)
988+
except presets.Error as e:
989+
diagnostics.fatal(e.message)
990+
983991
if args.show_presets:
984-
for name in sorted(get_all_preset_names(args.preset_file_names),
985-
key=str.lower):
992+
for name in sorted(preset_parser.preset_names(),
993+
key=lambda name: name.lower()):
986994
print(name)
987995
return 0
988996

989997
if not args.preset:
990998
diagnostics.fatal("missing --preset option")
991999

9921000
args.preset_substitutions = {}
993-
9941001
for arg in args.preset_substitutions_raw:
9951002
name, value = arg.split("=", 1)
9961003
args.preset_substitutions[name] = value
9971004

998-
preset_args = get_preset_options(
999-
args.preset_substitutions, args.preset_file_names, args.preset)
1005+
try:
1006+
preset = preset_parser.get_preset(args.preset,
1007+
vars=args.preset_substitutions)
1008+
except presets.Error as e:
1009+
diagnostics.fatal(e.message)
1010+
1011+
preset_args = migrate_swift_sdks(preset.format_args())
10001012

10011013
build_script_args = [sys.argv[0]]
10021014
if args.dry_run:
10031015
build_script_args += ["--dry-run"]
1016+
10041017
build_script_args += preset_args
10051018
if args.distcc:
10061019
build_script_args += ["--distcc"]
10071020
if args.build_jobs:
10081021
build_script_args += ["--jobs", str(args.build_jobs)]
10091022

1010-
diagnostics.note(
1011-
"using preset '" + args.preset + "', which expands to \n\n" +
1012-
shell.quote_command(build_script_args) + "\n")
1023+
diagnostics.note('using preset "{}", which expands to \n\n{}\n'.format(
1024+
args.preset, shell.quote_command(build_script_args)))
10131025

10141026
if args.expand_build_script_invocation:
10151027
return 0

utils/build_swift/migration.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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+
from __future__ import absolute_import, unicode_literals
15+
16+
from swift_build_support.swift_build_support.targets import \
17+
StdlibDeploymentTarget
18+
19+
20+
__all__ = [
21+
'UnknownSDKError',
22+
'migrate_swift_sdks',
23+
]
24+
25+
26+
_SDK_TARGETS = {
27+
'OSX': StdlibDeploymentTarget.OSX.targets,
28+
'IOS': StdlibDeploymentTarget.iOS.targets,
29+
'IOS_SIMULATOR': StdlibDeploymentTarget.iOSSimulator.targets,
30+
'TVOS': StdlibDeploymentTarget.AppleTV.targets,
31+
'TVOS_SIMULATOR': StdlibDeploymentTarget.AppleTVSimulator.targets,
32+
'WATCHOS': StdlibDeploymentTarget.AppleWatch.targets,
33+
'WATCHOS_SIMULATOR': StdlibDeploymentTarget.AppleWatchSimulator.targets,
34+
}
35+
36+
37+
# -----------------------------------------------------------------------------
38+
39+
class UnknownSDKError(Exception):
40+
"""Error indicating an unknown SDK was encountered when migrating to target
41+
triples.
42+
"""
43+
44+
pass
45+
46+
47+
def _swift_sdks_to_stdlib_targets(swift_sdks):
48+
stdlib_targets = []
49+
for sdk in swift_sdks:
50+
sdk_targets = _SDK_TARGETS.get(sdk, None)
51+
if sdk_targets is None:
52+
raise UnknownSDKError(sdk)
53+
stdlib_targets += sdk_targets
54+
55+
return stdlib_targets
56+
57+
58+
def migrate_swift_sdks(args):
59+
"""Migrate usages of the now deprecated `--swift-sdks` option to the new
60+
`--stdlib-deployment-targets` option, converting Swift SDKs to the
61+
corresponding targets. Since argument parsing is a last-wins scenario, only
62+
the last `--swift-sdks` option is migrated, all others are removed.
63+
64+
This function is a stop-gap to replacing all instances of `--swift-sdks`.
65+
"""
66+
67+
swift_sdks_args = [arg for arg in args if arg.startswith('--swift-sdks')]
68+
69+
if len(swift_sdks_args) < 1:
70+
return args
71+
72+
# Only get the last --swift-sdks arg since last-wins
73+
swift_sdks_arg = swift_sdks_args[-1]
74+
75+
sdk_list = swift_sdks_arg.split('=')[1].split(';')
76+
stdlib_targets = _swift_sdks_to_stdlib_targets(sdk_list)
77+
78+
target_names = ' '.join([target.name for target in stdlib_targets])
79+
stdlib_targets_arg = '--stdlib-deployment-targets=' + target_names
80+
81+
args = [arg for arg in args if not arg.startswith('--swift-sdks')]
82+
args.append(stdlib_targets_arg)
83+
84+
return args

0 commit comments

Comments
 (0)