Skip to content

Commit

Permalink
feat: support storing Swift dependency files in a sub-package (#146)
Browse files Browse the repository at this point in the history
- Add `swift_deps_index` attribute to `swift_update_packages`.
- Update `vapor_example` to demonstrate storing the Swift dependency
files in a sub-package.

Closes #136.
  • Loading branch information
cgrindel committed Jan 13, 2023
1 parent a2a53f4 commit f860367
Show file tree
Hide file tree
Showing 17 changed files with 1,295 additions and 71 deletions.
4 changes: 2 additions & 2 deletions .bazelrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# To update these lines, execute
# `bazel run @contrib_rules_bazel_integration_test//tools:update_deleted_packages`
build --deleted_packages=examples/http_archive_ext_deps,examples/http_archive_ext_deps/Sources/MyDequeModule,examples/http_archive_ext_deps/Sources/PrintStuff,examples/http_archive_ext_deps/Tests/MyDequeModuleTests,examples/http_archive_ext_deps/third_party,examples/interesting_deps,examples/ios_sim,examples/ios_sim/Sources/Foo,examples/ios_sim/Tests/FooTests,examples/objc_code,examples/pkg_manifest_minimal,examples/pkg_manifest_minimal/Sources/MyExecutable,examples/pkg_manifest_minimal/Sources/MyLibrary,examples/pkg_manifest_minimal/Tests/MyLibraryTests,examples/pkg_manifest_minimal/third_party,examples/vapor_example,examples/vapor_example/Sources/App,examples/vapor_example/Sources/Run,examples/vapor_example/Tests/AppTests,examples/xcmetrics_example
query --deleted_packages=examples/http_archive_ext_deps,examples/http_archive_ext_deps/Sources/MyDequeModule,examples/http_archive_ext_deps/Sources/PrintStuff,examples/http_archive_ext_deps/Tests/MyDequeModuleTests,examples/http_archive_ext_deps/third_party,examples/interesting_deps,examples/ios_sim,examples/ios_sim/Sources/Foo,examples/ios_sim/Tests/FooTests,examples/objc_code,examples/pkg_manifest_minimal,examples/pkg_manifest_minimal/Sources/MyExecutable,examples/pkg_manifest_minimal/Sources/MyLibrary,examples/pkg_manifest_minimal/Tests/MyLibraryTests,examples/pkg_manifest_minimal/third_party,examples/vapor_example,examples/vapor_example/Sources/App,examples/vapor_example/Sources/Run,examples/vapor_example/Tests/AppTests,examples/xcmetrics_example
build --deleted_packages=examples/http_archive_ext_deps,examples/http_archive_ext_deps/Sources/MyDequeModule,examples/http_archive_ext_deps/Sources/PrintStuff,examples/http_archive_ext_deps/Tests/MyDequeModuleTests,examples/http_archive_ext_deps/third_party,examples/interesting_deps,examples/ios_sim,examples/ios_sim/Sources/Foo,examples/ios_sim/Tests/FooTests,examples/objc_code,examples/pkg_manifest_minimal,examples/pkg_manifest_minimal/Sources/MyExecutable,examples/pkg_manifest_minimal/Sources/MyLibrary,examples/pkg_manifest_minimal/Tests/MyLibraryTests,examples/pkg_manifest_minimal/third_party,examples/vapor_example,examples/vapor_example/Sources/App,examples/vapor_example/Sources/Run,examples/vapor_example/Tests/AppTests,examples/vapor_example/swift,examples/xcmetrics_example
query --deleted_packages=examples/http_archive_ext_deps,examples/http_archive_ext_deps/Sources/MyDequeModule,examples/http_archive_ext_deps/Sources/PrintStuff,examples/http_archive_ext_deps/Tests/MyDequeModuleTests,examples/http_archive_ext_deps/third_party,examples/interesting_deps,examples/ios_sim,examples/ios_sim/Sources/Foo,examples/ios_sim/Tests/FooTests,examples/objc_code,examples/pkg_manifest_minimal,examples/pkg_manifest_minimal/Sources/MyExecutable,examples/pkg_manifest_minimal/Sources/MyLibrary,examples/pkg_manifest_minimal/Tests/MyLibraryTests,examples/pkg_manifest_minimal/third_party,examples/vapor_example,examples/vapor_example/Sources/App,examples/vapor_example/Sources/Run,examples/vapor_example/Tests/AppTests,examples/vapor_example/swift,examples/xcmetrics_example

# Import Shared settings
import %workspace%/shared.bazelrc
Expand Down
7 changes: 7 additions & 0 deletions docs/faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* [Can I just manage my external Swift packages and not generate Bazel build files for my project?](#can-i-just-manage-my-external-swift-packages-and-not-generate-bazel-build-files-for-my-project)
* [After running //:swift\_update\_pkgs , I see a \.build directory\. What is it? Do I need it?](#after-running-swift_update_pkgs-i-see-a-build-directory-what-is-it-do-i-need-it)
* [Does the Gazelle plugin run Swift package manager with every execution?](#does-the-gazelle-plugin-run-swift-package-manager-with-every-execution)
* [Can I store the Swift dependency files in a sub\-package (i\.e\., not in the root of the workspace)?](#can-i-store-the-swift-dependency-files-in-a-sub-package-ie-not-in-the-root-of-the-workspace)
<!-- MARKDOWN TOC: END -->

## Why use Gazelle and Go?
Expand Down Expand Up @@ -83,9 +84,15 @@ update the version selection for a dependency). The `update` mode for the Gazell
Bazel build files for your project. It uses information written to the `swift_deps_index.json` and
the source files that exist in your project to generate the Bazel build files.

## Can I store the Swift dependency files in a sub-package (i.e., not in the root of the workspace)?

Yes. The [vapor example] demonstrates storing the Swift dependency files in a sub-package called
`swift`.


[loading phase]: https://bazel.build/run/build#loading
[quickstart]: https://github.com/cgrindel/swift_bazel/blob/main/README.md#quickstart
[rules_spm]: https://github.com/cgrindel/rules_spm/
[rules_swift]: https://github.com/bazelbuild/rules_swift
[Gazelle framework]: https://github.com/bazelbuild/bazel-gazelle/blob/master/extend.md
[vapor example]: /examples/vapor_example
23 changes: 5 additions & 18 deletions examples/vapor_example/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,46 +1,33 @@
load("@bazel_gazelle//:def.bzl", "gazelle", "gazelle_binary")
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load("@cgrindel_bazel_starlib//bzltidy:defs.bzl", "tidy")
load("@cgrindel_swift_bazel//swiftpkg:defs.bzl", "swift_update_packages")

tidy(
name = "tidy",
targets = [
":swift_update_pkgs",
"//swift:update_pkgs",
":update_build_files",
],
)

# MARK: - Gazelle

# Ignore the Swift build folder
# gazelle:exclude .build

gazelle_binary(
name = "gazelle_bin",
languages = [
"@bazel_skylib//gazelle/bzl",
"@cgrindel_swift_bazel//gazelle",
],
visibility = ["//:__subpackages__"],
)

gazelle(
name = "update_build_files",
args = [
"-swift_dependency_index=swift/deps_index.json",
],
gazelle = ":gazelle_bin",
)

swift_update_packages(
name = "swift_update_pkgs",
gazelle = ":gazelle_bin",
)

bzl_library(
name = "swift_deps",
srcs = ["swift_deps.bzl"],
visibility = ["//visibility:public"],
deps = ["@cgrindel_swift_bazel//swiftpkg:defs"],
)

# Vapor was not happy building under 13.2.1. Fix the version for now.
# See Keith's article for more details:
# https://www.smileykeith.com/2021/03/08/locking-xcode-in-bazel/
Expand Down
2 changes: 2 additions & 0 deletions examples/vapor_example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Swift and Clang dependencies. Some of their Clang modules
have custom module maps. This example exercises the `swift_bazel` code that processes custom module
maps and handles novel Clang module linking issues.

Also, this example demonstrates how to store the Swift-specific dependency files in a sub-package.


## Linux Prequisites

Expand Down
4 changes: 2 additions & 2 deletions examples/vapor_example/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ load(
"@build_bazel_rules_swift//swift:repositories.bzl",
"swift_rules_dependencies",
)
load("//:swift_deps.bzl", "swift_dependencies")
load("//swift:deps.bzl", "swift_dependencies")

# gazelle:repository_macro swift_deps.bzl%swift_dependencies
# gazelle:repository_macro swift/deps.bzl%swift_dependencies
swift_dependencies()

swift_rules_dependencies()
Expand Down
35 changes: 24 additions & 11 deletions examples/vapor_example/set_up_clean_test
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ find "${script_dir}" \
-exec rm {} \;

# Remove the SPM build folder
rm -rf .build
rm -rf "${script_dir}/swift/.build"

# Replace the swift_deps.bzl with no declarations.
cat > "${script_dir}/swift_deps.bzl" <<-EOF
# Replace the swift/deps.bzl with no declarations.
cat > "${script_dir}/swift/deps.bzl" <<-EOF
def swift_dependencies():
pass
EOF
Expand All @@ -29,37 +29,50 @@ load("@cgrindel_swift_bazel//swiftpkg:defs.bzl", "swift_update_packages")
tidy(
name = "tidy",
targets = [
":swift_update_pkgs",
"//swift:update_pkgs",
":update_build_files",
],
)
# MARK: - Gazelle
# Ignore the Swift build folder
# gazelle:exclude .build
gazelle_binary(
name = "gazelle_bin",
languages = [
"@bazel_skylib//gazelle/bzl",
"@cgrindel_swift_bazel//gazelle",
],
visibility = ["//:__subpackages__"],
)
gazelle(
name = "update_build_files",
args = [
"-swift_dependency_index=swift/deps_index.json",
],
gazelle = ":gazelle_bin",
)
EOF

cat > "${script_dir}/swift/BUILD.bazel" <<-EOF
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load("@cgrindel_swift_bazel//swiftpkg:defs.bzl", "swift_update_packages")
# Ignore the Swift build folder
# gazelle:exclude .build
swift_update_packages(
name = "swift_update_pkgs",
gazelle = ":gazelle_bin",
name = "update_pkgs",
gazelle = "//:gazelle_bin",
package_manifest = "swift/Package.swift",
swift_deps = "swift/deps.bzl",
swift_deps_index = "swift/deps_index.json",
)
bzl_library(
name = "swift_deps",
srcs = ["swift_deps.bzl"],
name = "deps",
srcs = ["deps.bzl"],
visibility = ["//visibility:public"],
deps = ["@cgrindel_swift_bazel//swiftpkg:defs"],
)
Expand Down
25 changes: 25 additions & 0 deletions examples/vapor_example/swift/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load("@cgrindel_swift_bazel//swiftpkg:defs.bzl", "swift_update_packages")

# NOTE: If you decide to move the Swift dependency files like this example, do
# not forget to update the `gazelle` declaration `//:update_build_files` to
# point at the new location of the Swift dependency index JSON file. Also, the
# `//:gazelle_bin target` must be visible to this package.

# Ignore the Swift build folder
# gazelle:exclude .build

swift_update_packages(
name = "update_pkgs",
gazelle = "//:gazelle_bin",
package_manifest = "swift/Package.swift",
swift_deps = "swift/deps.bzl",
swift_deps_index = "swift/deps_index.json",
)

bzl_library(
name = "deps",
srcs = ["deps.bzl"],
visibility = ["//visibility:public"],
deps = ["@cgrindel_swift_bazel//swiftpkg:defs"],
)
File renamed without changes.
File renamed without changes.

0 comments on commit f860367

Please sign in to comment.