Skip to content

Commit 20bce26

Browse files
committed
Add example with deps in swift_import
1 parent 646d6d2 commit 20bce26

File tree

5 files changed

+98
-4
lines changed

5 files changed

+98
-4
lines changed

swift/toolchains/config/compile_config.bzl

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1861,7 +1861,7 @@ def _disable_autolink_framework_copts(library_path):
18611861
)
18621862

18631863
def _swift_module_search_path_map_fn(module):
1864-
"""Returns the path to the directory containing a `.swiftmodule` file.
1864+
"""Returns the path to the directory containing a Swift module file.
18651865
18661866
This function is intended to be used as a mapping function for modules
18671867
passed into `Args.add_all`.
@@ -1872,10 +1872,16 @@ def _swift_module_search_path_map_fn(module):
18721872
modules of a `SwiftInfo` provider.
18731873
18741874
Returns:
1875-
The dirname of the module's `.swiftmodule` file.
1875+
The dirname of the module's `.swiftmodule` or `.swiftinterface` file.
18761876
"""
18771877
if module.swift:
1878-
search_path = module.swift.swiftmodule.dirname
1878+
# For modules with .swiftinterface files (like swift_import), we need
1879+
# to use the directory containing the .swiftinterface so that Swift
1880+
# can find it when compiling dependent module interfaces.
1881+
if module.swift.swiftinterface:
1882+
search_path = module.swift.swiftinterface.dirname
1883+
else:
1884+
search_path = module.swift.swiftmodule.dirname
18791885

18801886
# If the dirname also ends in .swiftmodule, remove it as well so that
18811887
# the compiler finds the module *directory*.

test/fixtures/module_interface/BUILD

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ swift_binary(
1313
name = "client",
1414
srcs = ["Client.swift"],
1515
tags = FIXTURE_TAGS,
16-
deps = [":toy_module"],
16+
deps = [
17+
":toy_module",
18+
":toy_module_consumer",
19+
],
1720
)
1821

1922
swift_import(
@@ -26,3 +29,15 @@ swift_import(
2629
swiftinterface = "//test/fixtures/module_interface/library:toy_outputs/ToyModule.swiftinterface",
2730
tags = FIXTURE_TAGS,
2831
)
32+
33+
swift_import(
34+
name = "toy_module_consumer",
35+
archives = [
36+
"//test/fixtures/module_interface/library_consumer:toy_consumer_outputs/libToyModuleConsumer.a",
37+
],
38+
module_name = "ToyModuleConsumer",
39+
swiftdoc = "//test/fixtures/module_interface/library_consumer:toy_consumer_outputs/ToyModuleConsumer.swiftdoc",
40+
swiftinterface = "//test/fixtures/module_interface/library_consumer:toy_consumer_outputs/ToyModuleConsumer.swiftinterface",
41+
tags = FIXTURE_TAGS,
42+
deps = [":toy_module"],
43+
)

test/fixtures/module_interface/Client.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@
1313
// limitations under the License.
1414

1515
import ToyModule
16+
import ToyModuleConsumer
1617

1718
@main
1819
struct Main {
1920
static func main() {
2021
let value = ToyValue(number: 10)
2122
print(value.stringValue)
2223
print(value.squared())
24+
printToyValue()
2325
}
2426
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
load("//swift:swift_library.bzl", "swift_library")
2+
load(
3+
"//test/fixtures:common.bzl",
4+
"FIXTURE_TAGS",
5+
)
6+
load(
7+
"//test/rules:swift_library_artifact_collector.bzl",
8+
"swift_library_artifact_collector",
9+
)
10+
11+
package(
12+
default_testonly = True,
13+
default_visibility = ["//test:__subpackages__"],
14+
)
15+
16+
licenses(["notice"])
17+
18+
# Checking in pre-built artifacts like a `.swiftinterface` and static libraries
19+
# would require different artifacts for every platform the test might run on.
20+
# Instead, build it on-demand but forward the outputs using the "artifact
21+
# collector" rule below to make them act as if they were pre-built outputs when
22+
# referenced by the `swift_import` rule.
23+
#
24+
# These must be in a separate package than the `swift_import` target because
25+
# that rule propagates its pre-built inputs in `DefaultInfo`.
26+
27+
swift_library(
28+
name = "toy_module_consumer",
29+
srcs = ["ToyConsumer.swift"],
30+
library_evolution = True,
31+
module_name = "ToyModuleConsumer",
32+
tags = FIXTURE_TAGS,
33+
deps = ["//test/fixtures/module_interface/library:toy_module_library"],
34+
)
35+
36+
swift_library_artifact_collector(
37+
name = "toy_module_consumer_artifact_collector",
38+
static_library = "toy_consumer_outputs/libToyModuleConsumer.a",
39+
swiftdoc = "toy_consumer_outputs/ToyModuleConsumer.swiftdoc",
40+
swiftinterface = "toy_consumer_outputs/ToyModuleConsumer.swiftinterface",
41+
tags = FIXTURE_TAGS,
42+
target = ":toy_module_consumer",
43+
target_compatible_with = ["@platforms//os:macos"],
44+
)
45+
46+
swift_library(
47+
name = "toy_module_consumer_without_library_evolution",
48+
srcs = ["ToyConsumer.swift"],
49+
library_evolution = False,
50+
module_name = "ToyModuleConsumerNoEvolution",
51+
tags = FIXTURE_TAGS,
52+
)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2024 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+
import ToyModule
16+
17+
public func printToyValue() {
18+
print(ToyValue(number: 42))
19+
}

0 commit comments

Comments
 (0)