Skip to content

Commit

Permalink
Fix to resolve error when compiling app intents indicating that it ex…
Browse files Browse the repository at this point in the history
…pects all values to be statically allocated and resolved at compile time.

This pulls all of the module names found from SwiftInfo when gathering the swiftconstfiles. This appears to work in the test targets set up, which do lean on the multiple module strategy.

PiperOrigin-RevId: 589938532
  • Loading branch information
nglevin authored and swiple-rules-gardener committed Dec 11, 2023
1 parent 5368f97 commit 5668eb4
Show file tree
Hide file tree
Showing 10 changed files with 174 additions and 5 deletions.
1 change: 1 addition & 0 deletions apple/internal/aspects/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ bzl_library(
"//apple/internal:cc_info_support",
"//apple/internal/providers:app_intents_info",
"@build_bazel_apple_support//lib:apple_support",
"@build_bazel_rules_swift//swift:providers",
],
)

Expand Down
9 changes: 9 additions & 0 deletions apple/internal/aspects/app_intents_aspect.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ load(
"@build_bazel_rules_apple//apple/internal/providers:app_intents_info.bzl",
"AppIntentsInfo",
)
load(
"@build_bazel_rules_swift//swift:providers.bzl",
"SwiftInfo",
)

visibility("//apple/internal/...")

Expand All @@ -39,12 +43,17 @@ def _app_intents_aspect_impl(target, ctx):
)

swiftconstvalues_files = []
module_names = []
xcode_version_config = ctx.attr._xcode_config[apple_common.XcodeVersionConfig]
if xcode_version_config.xcode_version() >= apple_common.dotted_version("15.0"):
# TODO(b/315847370): See if these names need to be deduplicated, if duplicate entries are at
# all possible.
module_names = [x.name for x in target[SwiftInfo].direct_modules if x.swift]
swiftconstvalues_files = target[OutputGroupInfo]["const_values"].to_list()

return [
AppIntentsInfo(
intent_module_names = module_names,
swift_source_files = ctx.rule.files.srcs,
swiftconstvalues_files = swiftconstvalues_files,
),
Expand Down
5 changes: 5 additions & 0 deletions apple/internal/partials/app_intents_metadata_bundle.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ def _app_intents_metadata_bundle_partial_impl(
for dep in deps[first_cc_toolchain_key]
for swiftconstvalues_file in dep[AppIntentsInfo].swiftconstvalues_files
],
intents_module_names = [
intent_module_name
for dep in deps[first_cc_toolchain_key]
for intent_module_name in dep[AppIntentsInfo].intent_module_names
],
label = label,
source_files = [
swift_source_file
Expand Down
2 changes: 2 additions & 0 deletions apple/internal/providers/app_intents_info.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ visibility("//apple/internal/...")
AppIntentsInfo = provider(
doc = "Private provider to propagate source files required by AppIntents processing.",
fields = {
"intent_module_names": """
A List with the module names where App Intents are expected to be found.""",
"swift_source_files": """
A List with the swift source Files to handle via app intents processing.""",
"swiftconstvalues_files": """
Expand Down
27 changes: 22 additions & 5 deletions apple/internal/resource_actions/app_intents.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ def generate_app_intents_metadata_bundle(
apple_fragment,
bundle_binary,
constvalues_files,
source_files,
intents_module_names,
label,
source_files,
target_triples,
xcode_version_config):
"""Process and generate AppIntents metadata bundle (Metadata.appintents).
Expand All @@ -37,8 +38,10 @@ def generate_app_intents_metadata_bundle(
bundle_binary: File referencing an application/extension/framework binary.
constvalues_files: List of swiftconstvalues files generated from Swift source files
implementing the AppIntents protocol.
source_files: List of Swift source files implementing the AppIntents protocol.
intents_module_names: List of Strings with the module names corresponding to the modules
found which have intents compiled.
label: Label for the current target (`ctx.label`).
source_files: List of Swift source files implementing the AppIntents protocol.
target_triples: List of Apple target triples from `CcToolchainInfo` providers.
xcode_version_config: The `apple_common.XcodeVersionConfig` provider from the current ctx.
Returns:
Expand All @@ -56,17 +59,31 @@ def generate_app_intents_metadata_bundle(
args.add("appintentsmetadataprocessor")

args.add("--binary-file", bundle_binary)
args.add("--module-name", label.name)
if len(intents_module_names) == 0:
# TODO(b/315847370): See why this works for Xcode 14.x, and if it should be an actual module
# name instead.
args.add("--module-name", label.name)
else:
args.add_all(
intents_module_names,
before_each = "--module-name",
)
args.add("--output", output.dirname)
args.add_all("--source-files", source_files)
args.add_all(
source_files,
before_each = "--source-files",
)
transitive_inputs = [depset(source_files)]
args.add("--sdk-root", apple_support.path_placeholders.sdkroot())
args.add_all(target_triples, before_each = "--target-triple")
args.add("--toolchain-dir", "{xcode_path}/Toolchains/XcodeDefault.xctoolchain".format(
xcode_path = apple_support.path_placeholders.xcode(),
))
if xcode_version_config.xcode_version() >= apple_common.dotted_version("15.0"):
args.add_all("--swift-const-vals", constvalues_files)
args.add_all(
constvalues_files,
before_each = "--swift-const-vals",
)
transitive_inputs.append(depset(constvalues_files))
args.add("--compile-time-extraction")

Expand Down
1 change: 1 addition & 0 deletions test/starlark_tests/common.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ _min_os_ios = struct(
oldest_supported = "11.0",
nplus1 = "13.0",
stable_swift_abi = "12.2",
widget_configuration_intents_support = "16.0",
)

_min_os_macos = struct(
Expand Down
28 changes: 28 additions & 0 deletions test/starlark_tests/ios_application_tests.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,34 @@ def ios_application_test_suite(name):
tags = [name],
)

# Test app with a Widget Configuration Intent with a computed property generates and bundles Metadata.appintents bundle.
archive_contents_test(
name = "{}_with_widget_configuration_intent_contains_app_intents_metadata_bundle_test".format(name),
build_type = "simulator",
target_under_test = "//test/starlark_tests/targets_under_test/ios:app_with_widget_configuration_intent",
contains = [
"$BUNDLE_ROOT/Metadata.appintents/extract.actionsdata",
"$BUNDLE_ROOT/Metadata.appintents/version.json",
],
tags = [
name,
],
)

# Test app with a Widget Configuration Intent with a computed property generates and bundles Metadata.appintents bundle.
archive_contents_test(
name = "{}_with_app_intent_and_widget_configuration_intent_contains_app_intents_metadata_bundle_test".format(name),
build_type = "simulator",
target_under_test = "//test/starlark_tests/targets_under_test/ios:app_with_app_intent_and_widget_configuration_intent",
contains = [
"$BUNDLE_ROOT/Metadata.appintents/extract.actionsdata",
"$BUNDLE_ROOT/Metadata.appintents/version.json",
],
tags = [
name,
],
)

# Test app with App Intents generates and bundles Metadata.appintents bundle for fat binaries.
archive_contents_test(
name = "{}_fat_build_contains_app_intents_metadata_bundle_test".format(name),
Expand Down
7 changes: 7 additions & 0 deletions test/starlark_tests/resources/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -760,3 +760,10 @@ swift_library(
srcs = ["app_intent.swift"],
tags = common.fixture_tags,
)

swift_library(
name = "widget_configuration_intent",
srcs = ["widget_configuration_intent.swift"],
module_name = "WidgetConfigurationIntent",
tags = common.fixture_tags,
)
59 changes: 59 additions & 0 deletions test/starlark_tests/resources/widget_configuration_intent.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright 2023 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import AppIntents

enum RefreshInterval: String, AppEnum {
case hourly, daily, weekly

static var typeDisplayRepresentation: TypeDisplayRepresentation = "Refresh Interval"
static var caseDisplayRepresentations: [RefreshInterval: DisplayRepresentation] = [
.hourly: "Every Hour",
.daily: "Every Day",
.weekly: "Every Week",
]
}

struct FavoriteSoup: WidgetConfigurationIntent {
static var title: LocalizedStringResource = "Favorite Soup"
static var description = IntentDescription("Shows a picture of your favorite soup!")

@Parameter(title: "Soup")
var name: String?

@Parameter(title: "Shuffle", default: true)
var shuffle: Bool

@Parameter(title: "Refresh", default: .daily)
var interval: RefreshInterval

static var parameterSummary: some ParameterSummary {
When(\.$shuffle, .equalTo, true) {
Summary {
\.$name
\.$shuffle
\.$interval
}
} otherwise: {
Summary {
\.$name
\.$shuffle
}
}
}

func perform() async throws -> some IntentResult & ProvidesDialog {
return .result(dialog: "This is an intent with a computed property!")
}
}
40 changes: 40 additions & 0 deletions test/starlark_tests/targets_under_test/ios/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -3403,6 +3403,46 @@ ios_application(
],
)

ios_application(
name = "app_with_widget_configuration_intent",
app_intents = [
"//test/starlark_tests/resources:widget_configuration_intent",
],
bundle_id = "com.google.example",
families = ["iphone"],
infoplists = [
"//test/starlark_tests/resources:Info.plist",
],
minimum_os_version = common.min_os_ios.widget_configuration_intents_support,
provisioning_profile = "//test/testdata/provisioning:integration_testing_ios.mobileprovision",
tags = common.fixture_tags,
deps = [
"//test/starlark_tests/resources:swift_uikit_appdelegate",
"//test/starlark_tests/resources:widget_configuration_intent",
],
)

ios_application(
name = "app_with_app_intent_and_widget_configuration_intent",
app_intents = [
"//test/starlark_tests/resources:app_intent",
"//test/starlark_tests/resources:widget_configuration_intent",
],
bundle_id = "com.google.example",
families = ["iphone"],
infoplists = [
"//test/starlark_tests/resources:Info.plist",
],
minimum_os_version = common.min_os_ios.widget_configuration_intents_support,
provisioning_profile = "//test/testdata/provisioning:integration_testing_ios.mobileprovision",
tags = common.fixture_tags,
deps = [
"//test/starlark_tests/resources:app_intent",
"//test/starlark_tests/resources:swift_uikit_appdelegate",
"//test/starlark_tests/resources:widget_configuration_intent",
],
)

# ---------------------------------------------------------------------------------------
# Targets for base_bundle_id and bundle_id flows with and without shared capabilities.

Expand Down

1 comment on commit 5668eb4

@keith
Copy link
Member

@keith keith commented on 5668eb4 Feb 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.