Skip to content

Commit

Permalink
Add example with deps in swift_import
Browse files Browse the repository at this point in the history
  • Loading branch information
luispadron committed Nov 26, 2024
1 parent 61e2c46 commit 7b92e4d
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 1 deletion.
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,
)
19 changes: 19 additions & 0 deletions test/fixtures/module_interface/library_consumer/ToyConsumer.swift
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))
}

0 comments on commit 7b92e4d

Please sign in to comment.