Skip to content

Commit

Permalink
fix: filter out maccatalyst SPM platform (#829)
Browse files Browse the repository at this point in the history
Bazel currently does not support `maccatalyst` as a platform. We were
mapping it to `ios` as they are Mac apps that use iOS frameworks.
However, this can lead to warnings if define values exist with the same
name mapped for `ios` and `maccatalyst` (e.g. Firebase).

This PR filters out `maccatalyst` conditions. 

Closes #801.
  • Loading branch information
cgrindel authored Jan 2, 2024
1 parent 1cc31be commit 7e4b05e
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 28 deletions.
13 changes: 9 additions & 4 deletions config_settings/spm/platform/platforms.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,6 @@ _PLATFORM_INFOS = [
_platform_info(spm = p, bzl = None, os = p)
for p in _NON_APPLE_PLATFORMS
] + [
# Treat `maccatalyst` as an alias of sorts for macos. This will be handled
# in the `platforms.label` function.
_platform_info(spm = "maccatalyst", bzl = None, os = None),
# Map `driverkit` as `macos`. This will be handled in the
# `platforms.label()` function.
_platform_info(spm = "driverkit", bzl = None, os = None),
Expand All @@ -67,11 +64,17 @@ def _label(name):
# There is currently no support Mac Catalyst in Bazel. These are Mac apps
# that use iOS frameworks. Treat it like iOS for now.
if name == "maccatalyst":
name = "ios"
fail("Unsupported swift package manager platform: maccatalyst.")
if name == "driverkit":
name = "macos"
return "@rules_swift_package_manager//config_settings/spm/platform:{}".format(name)

def _is_supported(name):
return name != "maccatalyst"

def _supported(names):
return [n for n in names if _is_supported(n)]

platforms = struct(
macos = "macos",
maccatalyst = "maccatalyst",
Expand All @@ -86,4 +89,6 @@ platforms = struct(
all_values = [pi.spm for pi in _PLATFORM_INFOS],
all_platform_infos = _PLATFORM_INFOS,
label = _label,
is_supported = _is_supported,
supported = _supported,
)
6 changes: 6 additions & 0 deletions config_settings/spm/tests/platform_tests/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
load("@cgrindel_bazel_starlib//bzlformat:defs.bzl", "bzlformat_pkg")
load(":platforms_tests.bzl", "platforms_test_suite")

bzlformat_pkg(name = "bzlformat")

platforms_test_suite()
57 changes: 57 additions & 0 deletions config_settings/spm/tests/platform_tests/platforms_tests.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
"""Tests for `platforms` module."""

load("@bazel_skylib//lib:unittest.bzl", "asserts", "unittest")
load("//config_settings/spm/platform:platforms.bzl", "platforms")

def _is_supported_test(ctx):
env = unittest.begin(ctx)

tests = [
struct(name = "maccatalyst", exp = False),
] + [
struct(name = name, exp = True)
for name in platforms.all_values
]
for t in tests:
actual = platforms.is_supported(t.name)
msg = getattr(t, "msg", t.name)
asserts.equals(env, t.exp, actual, msg)

return unittest.end(env)

is_supported_test = unittest.make(_is_supported_test)

def _supported_test(ctx):
env = unittest.begin(ctx)

tests = [
struct(
msg = "all valid names",
names = platforms.all_values,
exp = platforms.all_values,
),
struct(
msg = "some invalid names",
names = [platforms.ios, platforms.maccatalyst, platforms.macos],
exp = [platforms.ios, platforms.macos],
),
struct(
msg = "only invalid names",
names = [platforms.maccatalyst],
exp = [],
),
]
for t in tests:
actual = platforms.supported(t.names)
asserts.equals(env, t.exp, actual, t.msg)

return unittest.end(env)

supported_test = unittest.make(_supported_test)

def platforms_test_suite(name = "platforms_tests"):
return unittest.suite(
name,
is_supported_test,
supported_test,
)
8 changes: 4 additions & 4 deletions examples/firebase_example/MODULE.bazel.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 11 additions & 13 deletions swiftpkg/internal/bzl_selects.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,19 @@ def _new_from_build_setting(build_setting, values_map_fn = None):
for v in values
]

platforms_len = len(bsc.platforms)
supported_platforms = spm_platforms.supported(bsc.platforms)
platforms_len = len(supported_platforms)
if platforms_len > 0 and bsc.configuration != None:
conditions = [
spm_platform_configurations.label(p, bsc.configuration)
for p in bsc.platforms
for p in supported_platforms
]
elif platforms_len > 0:
conditions = [spm_platforms.label(p) for p in bsc.platforms]
conditions = [spm_platforms.label(p) for p in supported_platforms]
elif bsc.configuration != None:
conditions = [spm_configurations.label(bsc.configuration)]
else:
fail("""\
Found a build setting condition that had no platforms or a configuration. {}\
""".format(build_setting))
return []

return [
_new(kind = build_setting.kind, value = v, condition = c)
Expand All @@ -97,13 +96,12 @@ def _new_from_target_dependency_condition(kind, labels, condition = None):
"""
if condition == None:
return [_new(kind = kind, value = labels)]
platforms_len = len(condition.platforms)
if platforms_len > 0:
conditions = [spm_platforms.label(p) for p in condition.platforms]
else:
fail("""\
Found a target dependency condition that had no platforms. {}\
""".format(condition))

conditions = [
spm_platforms.label(p)
for p in spm_platforms.supported(condition.platforms)
]

return [
_new(kind = kind, value = labels, condition = c)
for c in conditions
Expand Down
8 changes: 1 addition & 7 deletions swiftpkg/internal/pkginfos.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -1157,13 +1157,7 @@ def _new_build_setting_condition(platforms = [], configuration = None):
if platforms == [] and configuration == None:
return None

for platform in platforms:
validations.in_list(
spm_platforms.all_values,
platform,
"Unrecognized platform. platform:",
)

platforms = spm_platforms.supported(platforms)
if configuration != None:
validations.in_list(
spm_configurations.all_values,
Expand Down

0 comments on commit 7e4b05e

Please sign in to comment.