@@ -37,12 +37,46 @@ def _strip_version(version):
3737
3838 return result
3939
40+ def _strip_or_pad_version (version , num_components ):
41+ """Strips or pads a version string to the given number of components.
42+
43+ If the version string contains fewer than the requested number of
44+ components, it will be padded with zeros.
45+
46+ Args:
47+ version: The version string.
48+ num_components: The desired number of components.
49+
50+ Returns:
51+ The version, stripped or padded to the requested number of components.
52+ """
53+ version_string = str (version )
54+ components = version_string .split ("." )
55+ if num_components <= len (components ):
56+ return "." .join (components [:num_components ])
57+ return version_string + (".0" * (num_components - len (components )))
58+
59+ _VERSION_PRECISION_COMPONENTS = {
60+ "major" : 1 ,
61+ "minor" : 2 ,
62+ "patch" : 3 ,
63+ }
64+
4065def _xcode_version_flag_impl (ctx ):
4166 """A rule that allows select() to differentiate between Xcode versions."""
4267 xcode_config = ctx .attr ._xcode_config [apple_common .XcodeVersionConfig ]
43- return config_common .FeatureFlagInfo (value = _strip_version (
44- xcode_config .xcode_version (),
45- ))
68+ xcode_version = xcode_config .xcode_version ()
69+ precision = ctx .attr .precision
70+
71+ if not precision :
72+ value = _strip_version (xcode_version )
73+ elif precision == "exact" :
74+ value = str (xcode_version )
75+ else :
76+ num_components = _VERSION_PRECISION_COMPONENTS [precision ]
77+ value = _strip_or_pad_version (xcode_version , num_components )
78+
79+ return config_common .FeatureFlagInfo (value = value )
4680
4781def _ios_sdk_version_flag_impl (ctx ):
4882 """A rule that allows select() to select based on the iOS SDK version."""
@@ -87,6 +121,58 @@ def _macos_sdk_version_flag_impl(ctx):
87121xcode_version_flag = rule (
88122 implementation = _xcode_version_flag_impl ,
89123 attrs = {
124+ "precision" : attr .string (
125+ doc = """\
126+ The desired precision with which the version number will be provided. The
127+ permitted values of this attribute are given below, with examples of the value
128+ that the rule would provide if the selected `xcode_version` target reported
129+ `version = "1.2.3.4X789"`:
130+
131+ - `major`: Provide only the major component of the version number (e.g., `1`).
132+ - `minor`: Provide only the major and minor components of the version number
133+ (e.g., `1.2`).
134+ - `patch`: Provide only the major, minor, and patch components of the version
135+ number (e.g., `1.2.3`).
136+ - `exact`: Provide the version number reported by the `xcode_version` target
137+ unmodified (e.g., `1.2.3.4X789`).
138+
139+ If `minor` or `patch` precision is requested and the version number reported by
140+ the `xcode_version` has a lower precision, the result will be padded with `.0`
141+ segments as necessary to ensure that it is consistent. For example, if the
142+ selected `xcode_version` reported `version = "1"`, then `minor` precision would
143+ be `1.0` and `patch` precision would be `1.0.0`.
144+
145+ Setting the precision lets you write `config_setting`s that test for more or
146+ less specific versions of Xcode. For example, if you want to handle all `11.*`
147+ versions of Xcode identically but do something different for Xcode 12.0 and
148+ 12.1, you can express that as follows:
149+
150+ ```
151+ config_setting(
152+ name = "xcode_11_any",
153+ flag_values = {
154+ "//tools/osx:xcode_version_flag_major": "11",
155+ },
156+ )
157+
158+ config_setting(
159+ name = "xcode_12_0",
160+ flag_values = {
161+ "//tools/osx:xcode_version_flag_minor": "12.0",
162+ },
163+ )
164+
165+ config_setting(
166+ name = "xcode_12_1",
167+ flag_values = {
168+ "//tools/osx:xcode_version_flag_minor": "12.1",
169+ },
170+ )
171+ ```
172+ """ ,
173+ mandatory = False ,
174+ values = ["major" , "minor" , "patch" , "exact" ],
175+ ),
90176 "_xcode_config" : attr .label (default = configuration_field (
91177 fragment = "apple" ,
92178 name = "xcode_config_label" ,
0 commit comments