88import functools
99import itertools
1010import json
11+ import os
1112import subprocess
1213
1314
@@ -37,20 +38,21 @@ def __lt__(self, other: Version) -> bool:
3738def get_simulator_device_info (
3839 requested_runtime_platform : str = "iOS" ,
3940 requested_device_type_product_family : str = "iPhone" ,
40- max_runtime_version_str : str | None = None ,
41+ requested_runtime_version_str : str | None = None ,
4142) -> dict [str , str ]:
4243 """
4344 Retrieves simulator device information from Xcode.
4445 This simulator device should be appropriate for running tests on this machine.
4546
4647 :param requested_runtime_platform: The runtime platform to select.
4748 :param requested_device_type_product_family: The device type product family to select.
48- :param max_runtime_version_str : The maximum runtime version to allow .
49+ :param requested_runtime_version_str : The runtime version select. If unspecified, selects the latest one .
4950
5051 :return: A dictionary containing information about the selected simulator device.
5152 """
52- max_runtime_version = Version (max_runtime_version_str ) if max_runtime_version_str is not None else None
53-
53+ requested_runtime_version = (
54+ Version (requested_runtime_version_str ) if requested_runtime_version_str is not None else None
55+ )
5456 simctl_proc = subprocess .run (
5557 ["xcrun" , "simctl" , "list" , "--json" , "--no-escape-slashes" ],
5658 text = True ,
@@ -73,7 +75,7 @@ def runtime_filter(runtime) -> bool:
7375 if runtime ["platform" ] != requested_runtime_platform :
7476 return False
7577
76- if max_runtime_version is not None and Version (runtime ["version" ]) > max_runtime_version :
78+ if requested_runtime_version is not None and Version (runtime ["version" ]) != requested_runtime_version :
7779 return False
7880
7981 return True
@@ -108,6 +110,9 @@ def device_filter(device) -> bool:
108110 ):
109111 runtime_id_and_device_pairs .extend ((runtime_id , device ) for device in filter (device_filter , device_list ))
110112
113+ if len (runtime_id_and_device_pairs ) == 0 :
114+ raise ValueError ("Failed to find requested simulator device info." )
115+
111116 # sort key - tuple of (runtime version, device type min runtime version)
112117 # the secondary device type min runtime version value is to treat more recent device types as greater
113118 def runtime_id_and_device_pair_key (runtime_id_and_device_pair ):
@@ -137,15 +142,20 @@ def runtime_id_and_device_pair_key(runtime_id_and_device_pair):
137142
138143
139144def main ():
140- parser = argparse .ArgumentParser (description = "Gets simulator info from Xcode and prints it in JSON format." )
141- _ = parser .parse_args () # no args yet
145+ requested_runtime_version_environment_variable_name = "ORT_GET_SIMULATOR_DEVICE_INFO_REQUESTED_RUNTIME_VERSION"
142146
143- info = get_simulator_device_info (
144- # The macOS-15 hosted agent image has iOS 17 which is currently in beta. Limit it to 16.4 for now.
145- # See https://github.com/actions/runner-images/issues/8023
146- # TODO Remove max_runtime_version limit.
147- max_runtime_version_str = "16.4" ,
147+ parser = argparse .ArgumentParser (description = "Gets simulator info from Xcode and prints it in JSON format." )
148+ parser .add_argument (
149+ "--requested-runtime-version" ,
150+ default = os .environ .get (requested_runtime_version_environment_variable_name , None ),
151+ help = "The requested runtime version. "
152+ f"This may also be specified with the { requested_runtime_version_environment_variable_name } "
153+ "environment variable. The command line option takes precedence. "
154+ "An unspecified value means the latest available runtime version." ,
148155 )
156+ args = parser .parse_args () # no args yet
157+
158+ info = get_simulator_device_info (requested_runtime_version_str = args .requested_runtime_version )
149159
150160 print (json .dumps (info , indent = 2 ))
151161
0 commit comments