Skip to content

Add example with deps in swift_import #1460

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions swift/toolchains/config/compile_config.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -1861,7 +1861,7 @@ def _disable_autolink_framework_copts(library_path):
)

def _swift_module_search_path_map_fn(module):
"""Returns the path to the directory containing a `.swiftmodule` file.
"""Returns the path to the directory containing a Swift module file.

This function is intended to be used as a mapping function for modules
passed into `Args.add_all`.
Expand All @@ -1872,10 +1872,16 @@ def _swift_module_search_path_map_fn(module):
modules of a `SwiftInfo` provider.

Returns:
The dirname of the module's `.swiftmodule` file.
The dirname of the module's `.swiftmodule` or `.swiftinterface` file.
"""
if module.swift:
search_path = module.swift.swiftmodule.dirname
# For modules with .swiftinterface files (like swift_import), we need
# to use the directory containing the .swiftinterface so that Swift
# can find it when compiling dependent module interfaces.
if module.swift.swiftinterface:
search_path = module.swift.swiftinterface.dirname
else:
search_path = module.swift.swiftmodule.dirname

# If the dirname also ends in .swiftmodule, remove it as well so that
# the compiler finds the module *directory*.
Expand Down
17 changes: 16 additions & 1 deletion test/fixtures/module_interface/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ swift_binary(
name = "client",
srcs = ["Client.swift"],
tags = FIXTURE_TAGS,
deps = [":toy_module"],
deps = [
":toy_module",
":toy_module_consumer",
],
)

swift_import(
Expand All @@ -26,3 +29,15 @@ swift_import(
swiftinterface = "//test/fixtures/module_interface/library:toy_outputs/ToyModule.swiftinterface",
tags = FIXTURE_TAGS,
)

swift_import(
name = "toy_module_consumer",
archives = [
"//test/fixtures/module_interface/library_consumer:toy_consumer_outputs/libToyModuleConsumer.a",
],
module_name = "ToyModuleConsumer",
swiftdoc = "//test/fixtures/module_interface/library_consumer:toy_consumer_outputs/ToyModuleConsumer.swiftdoc",
swiftinterface = "//test/fixtures/module_interface/library_consumer:toy_consumer_outputs/ToyModuleConsumer.swiftinterface",
tags = FIXTURE_TAGS,
deps = [":toy_module"],
)
2 changes: 2 additions & 0 deletions test/fixtures/module_interface/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@
// limitations under the License.

import ToyModule
import ToyModuleConsumer

@main
struct Main {
static func main() {
let value = ToyValue(number: 10)
print(value.stringValue)
print(value.squared())
printToyValue()
}
}
52 changes: 52 additions & 0 deletions test/fixtures/module_interface/library_consumer/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
load("//swift:swift_library.bzl", "swift_library")
load(
"//test/fixtures:common.bzl",
"FIXTURE_TAGS",
)
load(
"//test/rules:swift_library_artifact_collector.bzl",
"swift_library_artifact_collector",
)

package(
default_testonly = True,
default_visibility = ["//test:__subpackages__"],
)

licenses(["notice"])

# Checking in pre-built artifacts like a `.swiftinterface` and static libraries
# would require different artifacts for every platform the test might run on.
# Instead, build it on-demand but forward the outputs using the "artifact
# collector" rule below to make them act as if they were pre-built outputs when
# referenced by the `swift_import` rule.
#
# These must be in a separate package than the `swift_import` target because
# that rule propagates its pre-built inputs in `DefaultInfo`.

swift_library(
name = "toy_module_consumer",
srcs = ["ToyConsumer.swift"],
library_evolution = True,
module_name = "ToyModuleConsumer",
tags = FIXTURE_TAGS,
deps = ["//test/fixtures/module_interface/library:toy_module_library"],
)

swift_library_artifact_collector(
name = "toy_module_consumer_artifact_collector",
static_library = "toy_consumer_outputs/libToyModuleConsumer.a",
swiftdoc = "toy_consumer_outputs/ToyModuleConsumer.swiftdoc",
swiftinterface = "toy_consumer_outputs/ToyModuleConsumer.swiftinterface",
tags = FIXTURE_TAGS,
target = ":toy_module_consumer",
target_compatible_with = ["@platforms//os:macos"],
)

swift_library(
name = "toy_module_consumer_without_library_evolution",
srcs = ["ToyConsumer.swift"],
library_evolution = False,
module_name = "ToyModuleConsumerNoEvolution",
tags = FIXTURE_TAGS,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2024 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 ToyModule

public func printToyValue() {
print(ToyValue(number: 42))
}