Skip to content

Commit 48ea92b

Browse files
committed
Tests: Introduce lit substitutions for target triples corresponding to Swift releases.
For example, tests can now invoke the frontend passing `-target %target-swift-abi-5.3-triple` to compile with a deployment target coresponding to the Swift 5.3 release on the test `run_os`.
1 parent e101b22 commit 48ea92b

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

test/lit.cfg

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,15 +426,55 @@ swift_version = lit_config.params.get('swift-version',
426426
lit_config.note('Compiling with -swift-version ' + swift_version)
427427
config.swift_test_options = '-swift-version ' + swift_version
428428

429-
# Load availability macros for known stdlib releases.
429+
# Loads availability macros for known stdlib releases.
430430
def load_availability_macros():
431431
path = os.path.join(os.path.dirname(__file__), "../utils/availability-macros.def")
432432
lines = open(path, 'r').read().splitlines()
433433
pattern = re.compile(r"\s*(#.*)?")
434434
return filter(lambda l: pattern.fullmatch(l) is None, lines)
435+
436+
# Returns a tuple with the Swift ABI version (e.g. '5.0') and the corresponding
437+
# target triple (e.g. 'arm64-apple-ios12.2')
438+
def availability_macro_to_swift_abi_target_triple(macro):
439+
# Use a regex and split to pull out the components of an availability macro
440+
# that is formatted like this:
441+
# SwiftStdlib 5.0:macOS 10.14.4, iOS 12.2, watchOS 5.2, tvOS 12.2
442+
matches = re.search(r'^[\s]*SwiftStdlib[\s]+([0-9\.]+)[\s]*:[\s]*(.+)', macro)
443+
stdlib_vers = matches.group(1)
444+
vers_by_platform = {}
445+
for platform_vers in matches.group(2).split(', '):
446+
components = platform_vers.split(' ')
447+
vers_by_platform[components[0]] = components[1]
448+
449+
platform = {
450+
'macosx': 'macOS',
451+
'ios': 'iOS',
452+
'maccatalyst': 'iOS',
453+
'tvos': 'tvOS',
454+
'watchos': 'watchOS'
455+
}.get(run_os)
456+
457+
if platform is None:
458+
return stdlib_vers, config.variant_triple
459+
460+
os_vers = vers_by_platform.get(platform)
461+
if os_vers is None:
462+
return stdlib_vers, config.variant_triple
463+
464+
if run_os == 'maccatalyst':
465+
return stdlib_vers, '%s-%s-ios%s-macabi' % (run_cpu, run_vendor, os_vers)
466+
467+
return stdlib_vers, '%s-%s-%s%s%s' % (run_cpu, run_vendor, run_os,
468+
os_vers, run_environment)
469+
470+
# Add availability macros to the default frontend/driver flags and create target
471+
# triple substitutions for each stdlib version.
435472
for macro in load_availability_macros():
436473
config.swift_frontend_test_options += " -define-availability '{0}'".format(macro)
437474
config.swift_driver_test_options += " -Xfrontend -define-availability -Xfrontend '{0}'".format(macro)
475+
(stdlib_vers, triple) = availability_macro_to_swift_abi_target_triple(macro)
476+
config.substitutions.append(('%target-swift-abi-{}-triple'.format(stdlib_vers), triple))
477+
438478

439479
differentiable_programming = lit_config.params.get('differentiable_programming', None)
440480
if differentiable_programming is not None:

0 commit comments

Comments
 (0)