Skip to content
This repository has been archived by the owner on Apr 3, 2023. It is now read-only.

Commit

Permalink
Refactored the rules to handle dependencies that have shared dependen…
Browse files Browse the repository at this point in the history
…cies (#34)

Added spm_repositories so that all external Swift packages can be specified in a single rule so that SPM can be used to manage the intricacies of selecting package versions.
Removed spm_repository.
  • Loading branch information
cgrindel authored Aug 9, 2021
1 parent 6d06481 commit 2fdd7b1
Show file tree
Hide file tree
Showing 33 changed files with 1,537 additions and 699 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/bazel.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,9 @@ jobs:
run: |
cd examples/complex
bazelisk test //...
- name: Build Package With Deps Example
shell: bash
run: |
cd examples/package_with_deps
bazelisk test //...
29 changes: 15 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,27 +97,28 @@ rules_spm_dependencies()
### 2. Add external Swift packages as dependencies to your workspace

Add the following to download and build [apple/swift-log](https://github.com/apple/swift-log) to
your workspace.
your workspace. NOTE: It is imperative that the products that will be used by your product are
listed in the `products` list.

```python
load(
"@cgrindel_rules_spm//spm:spm.bzl",
"spm_repository",
)

spm_repository(
name = "apple_swift_log",
sha256 = "de51662b35f47764b6e12e9f1d43e7de28f6dd64f05bc30a318cf978cf3bc473",
strip_prefix = "swift-log-1.4.2",
urls = ["https://github.com/apple/swift-log/archive/1.4.2.tar.gz"],
load("@cgrindel_rules_spm//spm:spm.bzl", "spm_pkg", "spm_repositories")

spm_repositories(
name = "swift_pkgs",
dependencies = [
spm_pkg(
"https://github.com/apple/swift-log.git",
from_version = "1.0.0",
products = ["Logging"],
),
],
)
```


### 3. Use the module(s) defined in the Swift packages

Each Swift package can contain multiple Swift modules. A Bazel target is created for each Swift
module.
module which is exported by the products that were listed in the `spm_pkg` declaration.

The following shows a Bazel BUILD file with a `swift_binary` that depends upon the `Logging` module
defined in [apple/swift-log](https://github.com/apple/swift-log).
Expand All @@ -130,7 +131,7 @@ swift_binary(
srcs = ["main.swift"],
visibility = ["//swift:__subpackages__"],
deps = [
"@apple_swift_log//:Logging",
"@swift_pkgs//swift-log:Logging",
],
)
```
Expand Down
4 changes: 2 additions & 2 deletions examples/complex/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ swift_binary(
srcs = ["main.swift"],
visibility = ["//swift:__subpackages__"],
deps = [
"@apple_swift_log//:Logging",
"@apple_swift_nio//:NIO",
"@swift_pkgs//swift-log:Logging",
"@swift_pkgs//swift-nio:NIO",
],
)

Expand Down
33 changes: 16 additions & 17 deletions examples/complex/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,20 @@ load(

rules_spm_dependencies()

load(
"@cgrindel_rules_spm//spm:spm.bzl",
"spm_repository",
)

spm_repository(
name = "apple_swift_log",
sha256 = "de51662b35f47764b6e12e9f1d43e7de28f6dd64f05bc30a318cf978cf3bc473",
strip_prefix = "swift-log-1.4.2",
urls = ["https://github.com/apple/swift-log/archive/1.4.2.tar.gz"],
)

spm_repository(
name = "apple_swift_nio",
sha256 = "40b115e77e8af3ffbe84be344c30c0964763e318e1d3dfe0a80da0e2ae17d614",
strip_prefix = "swift-nio-2.30.0",
urls = ["https://github.com/apple/swift-nio/archive/2.30.0.tar.gz"],
load("@cgrindel_rules_spm//spm:spm.bzl", "spm_pkg", "spm_repositories")

spm_repositories(
name = "swift_pkgs",
dependencies = [
spm_pkg(
"https://github.com/apple/swift-log.git",
from_version = "1.0.0",
products = ["Logging"],
),
spm_pkg(
"https://github.com/apple/swift-nio.git",
from_version = "2.0.0",
products = ["NIO"],
),
],
)
6 changes: 3 additions & 3 deletions examples/package_with_deps/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ swift_binary(
srcs = ["main.swift"],
visibility = ["//swift:__subpackages__"],
deps = [
"@apple_swift_log//:Logging",
"@apple_swift_nio//:NIO",
"@cgrindel_pkg_with_deps//:FooKit",
"@swift_pkgs//foo-kit:FooKit",
"@swift_pkgs//swift-log:Logging",
"@swift_pkgs//swift-nio:NIO",
],
)

Expand Down
60 changes: 22 additions & 38 deletions examples/package_with_deps/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -34,42 +34,26 @@ load(

rules_spm_dependencies()

load(
"@cgrindel_rules_spm//spm:spm.bzl",
"spm_repository",
load("@cgrindel_rules_spm//spm:spm.bzl", "spm_pkg", "spm_repositories")

spm_repositories(
name = "swift_pkgs",
dependencies = [
spm_pkg(
"https://github.com/apple/swift-log.git",
from_version = "1.0.0",
products = ["Logging"],
),
spm_pkg(
"https://github.com/apple/swift-nio.git",
from_version = "2.0.0",
products = ["NIO"],
),
spm_pkg(
"https://github.com/cgrindel/rules_spm_example_pkg_with_deps.git",
name = "foo-kit",
from_version = "0.0.0",
products = ["FooKit"],
),
],
)

# spm_repository(
# name = "vapor_vapor",
# sha256 = "3d7bc389ef6ae13f3ade731c95cfb88c641004f2f482fe55768799ebbb28aa0c",
# strip_prefix = "vapor-4.48.2",
# urls = ["https://github.com/vapor/vapor/archive/4.48.2.tar.gz"],
# )

spm_repository(
name = "apple_swift_log",
sha256 = "de51662b35f47764b6e12e9f1d43e7de28f6dd64f05bc30a318cf978cf3bc473",
strip_prefix = "swift-log-1.4.2",
urls = ["https://github.com/apple/swift-log/archive/1.4.2.tar.gz"],
)

spm_repository(
name = "apple_swift_nio",
sha256 = "40b115e77e8af3ffbe84be344c30c0964763e318e1d3dfe0a80da0e2ae17d614",
strip_prefix = "swift-nio-2.30.0",
urls = ["https://github.com/apple/swift-nio/archive/2.30.0.tar.gz"],
)

spm_repository(
name = "cgrindel_pkg_with_deps",
sha256 = "3c527aa6d0ffdb82f7311118474c70aea8ae5fc7e147265f0c90d6e6ac113c0e",
strip_prefix = "rules_spm_example_pkg_with_deps-0.1.0",
urls = ["https://github.com/cgrindel/rules_spm_example_pkg_with_deps/archive/0.1.0.tar.gz"],
)

# spm_repository(
# name = "vapor_async_kit",
# sha256 = "db9f46e1f9728540269fe7fe3b76d9193851bc3a9af20d9ad98548c498e539ae",
# strip_prefix = "async-kit-1.3.1",
# urls = ["https://github.com/vapor/async-kit/archive/1.3.1.tar.gz"],
# )
5 changes: 2 additions & 3 deletions examples/package_with_deps/main.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// AsyncKit imports Logging and NIO for us.
import AsyncKit
import FooKit
import Logging
import NIO

Expand All @@ -12,7 +11,7 @@ defer {

let eventLoop = group.next()

// Use an AsyncKit extension
// Use a FooKit extension
let future = eventLoop.future()

try future.wait()
Expand Down
2 changes: 1 addition & 1 deletion examples/simple/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ swift_binary(
srcs = ["main.swift"],
visibility = ["//swift:__subpackages__"],
deps = [
"@apple_swift_log//:Logging",
"@swift_pkgs//swift-log:Logging",
],
)

Expand Down
21 changes: 11 additions & 10 deletions examples/simple/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@ load(

swift_rules_extra_dependencies()

load(
"@cgrindel_rules_spm//spm:spm.bzl",
"spm_repository",
)

spm_repository(
name = "apple_swift_log",
sha256 = "de51662b35f47764b6e12e9f1d43e7de28f6dd64f05bc30a318cf978cf3bc473",
strip_prefix = "swift-log-1.4.2",
urls = ["https://github.com/apple/swift-log/archive/1.4.2.tar.gz"],
load("@cgrindel_rules_spm//spm:spm.bzl", "spm_pkg", "spm_repositories")

spm_repositories(
name = "swift_pkgs",
dependencies = [
spm_pkg(
"https://github.com/apple/swift-log.git",
from_version = "1.0.0",
products = ["Logging"],
),
],
)
2 changes: 1 addition & 1 deletion examples/simple_c/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ swift_binary(
srcs = ["main.swift"],
visibility = ["//swift:__subpackages__"],
deps = [
"@apple_swift_nio//:CNIOSHA1",
"@swift_pkgs//swift-nio:CNIOSHA1",
],
)

Expand Down
21 changes: 11 additions & 10 deletions examples/simple_c/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,15 @@ load(

rules_spm_dependencies()

load(
"@cgrindel_rules_spm//spm:spm.bzl",
"spm_repository",
)

spm_repository(
name = "apple_swift_nio",
sha256 = "40b115e77e8af3ffbe84be344c30c0964763e318e1d3dfe0a80da0e2ae17d614",
strip_prefix = "swift-nio-2.30.0",
urls = ["https://github.com/apple/swift-nio/archive/2.30.0.tar.gz"],
load("@cgrindel_rules_spm//spm:spm.bzl", "spm_pkg", "spm_repositories")

spm_repositories(
name = "swift_pkgs",
dependencies = [
spm_pkg(
"https://github.com/apple/swift-nio.git",
from_version = "2.0.0",
products = ["NIOWebSocket"],
),
],
)
4 changes: 2 additions & 2 deletions spm/internal/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ bzl_library(
)

bzl_library(
name = "spm_repository",
srcs = ["spm_repository.bzl"],
name = "spm_repositories",
srcs = ["spm_repositories.bzl"],
visibility = ["//spm:__subpackages__"],
)

Expand Down
19 changes: 19 additions & 0 deletions spm/internal/Package.swift.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// swift-tools-version:{swift_tools_version}

import PackageDescription

let package = Package(
name: "placeholder",
{swift_platforms}
products: [
.library(name: "Placeholder", targets: ["Placeholder"]),
],
dependencies: [
{package_dependencies}
],
targets: [
.target( name: "Placeholder", dependencies: [
{target_dependencies}
]),
]
)
24 changes: 0 additions & 24 deletions spm/internal/files.bzl

This file was deleted.

59 changes: 0 additions & 59 deletions spm/internal/package_description.bzl

This file was deleted.

Loading

0 comments on commit 2fdd7b1

Please sign in to comment.