blob: 5f750e931d1082c0f8c8cf5f999c0d4bc966e4ed [file] [log] [blame]
lberki57a2ad72017-10-18 09:30:09 +02001# Copyright 2017 The Bazel Authors. All rights reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15"""Rules that allows select() to differentiate between Apple OS versions."""
16
lberkidc8b43c2017-10-19 19:58:04 +020017def _strip_version(version):
vladmos20a042f2018-06-01 04:51:21 -070018 """Strip trailing characters that aren't digits or '.' from version names.
lberkidc8b43c2017-10-19 19:58:04 +020019
vladmos20a042f2018-06-01 04:51:21 -070020 Some OS versions look like "9.0gm", which is not useful for select()
21 statements. Thus, we strip the trailing "gm" part.
lberkidc8b43c2017-10-19 19:58:04 +020022
vladmos20a042f2018-06-01 04:51:21 -070023 Args:
24 version: the version string
lberkidc8b43c2017-10-19 19:58:04 +020025
vladmos20a042f2018-06-01 04:51:21 -070026 Returns:
27 The version with trailing letters stripped.
28 """
29 result = ""
cushon2d4917c2018-07-06 03:19:54 -070030 string = str(version)
31 for i in range(len(string)):
32 ch = string[i]
vladmos20a042f2018-06-01 04:51:21 -070033 if not ch.isdigit() and ch != ".":
34 break
lberkidc8b43c2017-10-19 19:58:04 +020035
vladmos20a042f2018-06-01 04:51:21 -070036 result += ch
lberkidc8b43c2017-10-19 19:58:04 +020037
vladmos20a042f2018-06-01 04:51:21 -070038 return result
lberkidc8b43c2017-10-19 19:58:04 +020039
lberki57a2ad72017-10-18 09:30:09 +020040def _xcode_version_flag_impl(ctx):
vladmos20a042f2018-06-01 04:51:21 -070041 """A rule that allows select() to differentiate between Xcode versions."""
42 xcode_config = ctx.attr._xcode_config[apple_common.XcodeVersionConfig]
cparsons3f87a952019-04-19 12:13:36 -070043 return config_common.FeatureFlagInfo(value = _strip_version(
44 xcode_config.xcode_version(),
45 ))
lberki57a2ad72017-10-18 09:30:09 +020046
47def _ios_sdk_version_flag_impl(ctx):
vladmos20a042f2018-06-01 04:51:21 -070048 """A rule that allows select() to select based on the iOS SDK version."""
49 xcode_config = ctx.attr._xcode_config[apple_common.XcodeVersionConfig]
lberki57a2ad72017-10-18 09:30:09 +020050
cparsons3f87a952019-04-19 12:13:36 -070051 return config_common.FeatureFlagInfo(value = _strip_version(
52 xcode_config.sdk_version_for_platform(
53 apple_common.platform.ios_device,
54 ),
55 ))
lberki57a2ad72017-10-18 09:30:09 +020056
57def _tvos_sdk_version_flag_impl(ctx):
vladmos20a042f2018-06-01 04:51:21 -070058 """A rule that allows select() to select based on the tvOS SDK version."""
59 xcode_config = ctx.attr._xcode_config[apple_common.XcodeVersionConfig]
lberki57a2ad72017-10-18 09:30:09 +020060
cparsons3f87a952019-04-19 12:13:36 -070061 return config_common.FeatureFlagInfo(value = _strip_version(
62 xcode_config.sdk_version_for_platform(
63 apple_common.platform.tvos_device,
64 ),
65 ))
lberki57a2ad72017-10-18 09:30:09 +020066
67def _watchos_sdk_version_flag_impl(ctx):
vladmos20a042f2018-06-01 04:51:21 -070068 """A rule that allows select() to select based on the watchOS SDK version."""
69 xcode_config = ctx.attr._xcode_config[apple_common.XcodeVersionConfig]
lberki57a2ad72017-10-18 09:30:09 +020070
cparsons3f87a952019-04-19 12:13:36 -070071 return config_common.FeatureFlagInfo(value = _strip_version(
72 xcode_config.sdk_version_for_platform(
73 apple_common.platform.watchos_device,
74 ),
75 ))
lberki57a2ad72017-10-18 09:30:09 +020076
77def _macos_sdk_version_flag_impl(ctx):
vladmos20a042f2018-06-01 04:51:21 -070078 """A rule that allows select() to select based on the macOS SDK version."""
79 xcode_config = ctx.attr._xcode_config[apple_common.XcodeVersionConfig]
lberki57a2ad72017-10-18 09:30:09 +020080
cparsons3f87a952019-04-19 12:13:36 -070081 return config_common.FeatureFlagInfo(value = _strip_version(
82 xcode_config.sdk_version_for_platform(
83 apple_common.platform.macos,
84 ),
85 ))
lberki57a2ad72017-10-18 09:30:09 +020086
87xcode_version_flag = rule(
88 implementation = _xcode_version_flag_impl,
89 attrs = {
vladmos20a042f2018-06-01 04:51:21 -070090 "_xcode_config": attr.label(default = configuration_field(
91 fragment = "apple",
92 name = "xcode_config_label",
93 )),
94 },
95)
lberki57a2ad72017-10-18 09:30:09 +020096
97ios_sdk_version_flag = rule(
98 implementation = _ios_sdk_version_flag_impl,
99 attrs = {
vladmos20a042f2018-06-01 04:51:21 -0700100 "_xcode_config": attr.label(default = configuration_field(
101 fragment = "apple",
102 name = "xcode_config_label",
103 )),
104 },
105)
lberki57a2ad72017-10-18 09:30:09 +0200106
107tvos_sdk_version_flag = rule(
108 implementation = _tvos_sdk_version_flag_impl,
109 attrs = {
vladmos20a042f2018-06-01 04:51:21 -0700110 "_xcode_config": attr.label(default = configuration_field(
111 fragment = "apple",
112 name = "xcode_config_label",
113 )),
114 },
115)
lberki57a2ad72017-10-18 09:30:09 +0200116
117watchos_sdk_version_flag = rule(
118 implementation = _watchos_sdk_version_flag_impl,
119 attrs = {
vladmos20a042f2018-06-01 04:51:21 -0700120 "_xcode_config": attr.label(default = configuration_field(
121 fragment = "apple",
122 name = "xcode_config_label",
123 )),
124 },
125)
lberki57a2ad72017-10-18 09:30:09 +0200126
127macos_sdk_version_flag = rule(
128 implementation = _macos_sdk_version_flag_impl,
129 attrs = {
vladmos20a042f2018-06-01 04:51:21 -0700130 "_xcode_config": attr.label(default = configuration_field(
131 fragment = "apple",
132 name = "xcode_config_label",
133 )),
134 },
135)