{"id":19274516,"url":"https://github.com/cgrindel/rules_swift_package_manager","last_synced_at":"2025-04-05T06:09:43.819Z","repository":{"id":63363087,"uuid":"563984289","full_name":"cgrindel/rules_swift_package_manager","owner":"cgrindel","description":"Collection of utilities and Bazel rules to aid in the development and maintenance of Swift repositories using Bazel.","archived":false,"fork":false,"pushed_at":"2025-03-25T13:44:16.000Z","size":5995,"stargazers_count":90,"open_issues_count":103,"forks_count":35,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-03-25T14:23:05.324Z","etag":null,"topics":["bazel","bazel-rules","swift","swift-package-manager"],"latest_commit_sha":null,"homepage":"","language":"Starlark","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cgrindel.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":"CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-11-09T18:43:35.000Z","updated_at":"2025-03-25T13:44:19.000Z","dependencies_parsed_at":"2023-09-23T00:08:15.389Z","dependency_job_id":"905e8193-f0b4-41e7-af24-7b2aaee22a70","html_url":"https://github.com/cgrindel/rules_swift_package_manager","commit_stats":{"total_commits":774,"total_committers":18,"mean_commits":43.0,"dds":0.5891472868217054,"last_synced_commit":"b89c5521c43a71faf12cd080c76259b9bda914b5"},"previous_names":["cgrindel/swift_bazel"],"tags_count":76,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cgrindel%2Frules_swift_package_manager","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cgrindel%2Frules_swift_package_manager/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cgrindel%2Frules_swift_package_manager/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cgrindel%2Frules_swift_package_manager/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cgrindel","download_url":"https://codeload.github.com/cgrindel/rules_swift_package_manager/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247294540,"owners_count":20915340,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bazel","bazel-rules","swift","swift-package-manager"],"created_at":"2024-11-09T20:46:24.562Z","updated_at":"2025-04-05T06:09:43.813Z","avatar_url":"https://github.com/cgrindel.png","language":"Starlark","funding_links":[],"categories":["Starlark"],"sub_categories":[],"readme":"# Swift Package Manager Rules for Bazel\n\n[![Build](https://github.com/cgrindel/rules_swift_package_manager/actions/workflows/ci.yml/badge.svg?event=schedule)](https://github.com/cgrindel/rules_swift_package_manager/actions/workflows/ci.yml)\n\nThis repository contains a Bazel ruleset that can be used to download, build, and consume Swift\npackages. The rules in this repository build the external Swift packages using [rules_swift],\n[rules_apple] and native C/C++ rulesets making the Swift package products and targets available as\nBazel targets.\n\nThis repository is designed to fully replace [rules_spm] and provide utilities to ease Swift\ndevelopment inside a Bazel workspace.\n\n## Table of Contents\n\n\u003c!-- MARKDOWN TOC: BEGIN --\u003e\n* [Documentation](#documentation)\n* [Prerequisites](#prerequisites)\n  * [Mac OS](#mac-os)\n  * [Linux](#linux)\n* [Quickstart](#quickstart)\n  * [1. Enable bzlmod](#1-enable-bzlmod)\n  * [2. Configure your `MODULE.bazel` to use rules_swift_package_manager.](#2-configure-your-modulebazel-to-use-rules_swift_package_manager)\n    * [(Optional) Use `swift_package` repository for updating packages](#optional-use-swift_package-repository-for-updating-packages)\n    * [(Optional) Enable `swift_deps_info` generation for the Gazelle plugin](#optional-enable-swift_deps_info-generation-for-the-gazelle-plugin)\n  * [3. Create a minimal `Package.swift` file.](#3-create-a-minimal-packageswift-file)\n  * [4. Run `swift package update`](#4-run-swift-package-update)\n  * [5. Run `bazel mod tidy`.](#5-run-bazel-mod-tidy)\n  * [6. Build and test your project.](#6-build-and-test-your-project)\n  * [7. Check in `Package.swift`, `Package.resolved`, and `MODULE.bazel`.](#7-check-in-packageswift-packageresolved-and-modulebazel)\n  * [8. Start coding](#8-start-coding)\n* [Using a Swift package registry](#using-a-swift-package-registry)\n* [Tips and Tricks](#tips-and-tricks)\n\u003c!-- MARKDOWN TOC: END --\u003e\n\n## Documentation\n\n- [Rules and API documentation](/docs/README.md)\n- [High-level design](/docs/design/high-level.md)\n- [Frequently Asked Questions](/docs/faq.md)\n\n## Prerequisites\n\n### Mac OS\n\nBe sure to install Xcode.\n\n### Linux\n\nYou will need to [install Swift](https://swift.org/getting-started/#installing-swift). Make sure\nthat running `swift --version` works properly.\n\nDon't forget that `rules_swift` [expects the use of\n`clang`](https://github.com/bazelbuild/rules_swift#3-additional-configuration-linux-only). Hence,\nyou will need to specify `CC=clang` before running Bazel.\n\nFinally, help [rules_swift] and [rules_swift_package_manager] find the Swift toolchain by ensuring\nthat a `PATH` that includes the Swift binary is available in the Bazel actions.\n\n```sh\ncat \u003e\u003elocal.bazelrc \u003c\u003cEOF\nbuild --action_env=PATH\nEOF\n```\n\nThis approach is necessary to successfully execute the examples on an Ubuntu runner using Github\nactions. See the [CI GitHub workflow] for more details.\n\n## Quickstart\n\nThe following provides a quick introduction on how to set up and use the features in this\nrepository. These instructions assume that you are using [Bazel modules] to load your external\ndependencies. If you are using Bazel's legacy external dependency management, we recommend using\n[Bazel's hybrid mode], then follow the steps in this quickstart guide.\n\nAlso, check out the [examples] for more information.\n\n### 1. Enable bzlmod\n\nThis repository supports [bzlmod].\n\n```\ncommon --enable_bzlmod\n```\n\n### 2. Configure your `MODULE.bazel` to use [rules_swift_package_manager].\n\nAdd a dependency on `rules_swift_package_manager`.\n\n\u003c!-- BEGIN MODULE SNIPPET --\u003e\n```python\nbazel_dep(name = \"rules_swift_package_manager\", version = \"1.0.0-rc1\")\n```\n\u003c!-- END MODULE SNIPPET --\u003e\n\nIn addition, add the following to load the external dependencies described in your `Package.swift`\nand `Package.resolved` files.\n\n```bazel\nswift_deps = use_extension(\n    \"@rules_swift_package_manager//:extensions.bzl\",\n    \"swift_deps\",\n)\nswift_deps.from_package(\n    resolved = \"//:Package.resolved\",\n    swift = \"//:Package.swift\",\n)\nuse_repo(\n    swift_deps,\n    \"swift_deps_info\",  # This is generated by the ruleset.\n    # The name of the Swift package repositories will be added to this declaration in step 4 after\n    # running `bazel mod tidy`.\n    # NOTE: The name of the Bazel external repository for a Swift package is `swiftpkg_xxx` where\n    # `xxx` is the Swift package identity, lowercase, with punctuation replaced by `hyphen`. For\n    # example, the repository name for apple/swift-nio is `swiftpkg_swift_nio`.\n)\n```\n\nYou will also need to add a dependency on [rules_swift].\n\nNOTE: Some Swift package manager features (e.g., resources) use rules from [rules_apple]. It is a\ndependency for `rules_swift_package_manager`. However, you do not need to declare it unless you use\nany of the rules in your project.\n\n#### (Optional) Use `swift_package` repository for updating packages\n\nThe `swift_deps` module extension will by default generate a `swift_package` repository which can be used to execute `swift package` commands.\nThis is useful if you'd like to control the flags and behavior of `swift package`, as well as for using the correct `swift` binary according to the Bazel configured toolchain.\n\nFor example, to resolve the `Package.swift` file:\n\n```sh\nbazel run @swift_package//:resolve\n```\n\nTo update packages to their latest supported version:\n\n```sh\nbazel run @swift_package//:update\n```\n\nBoth targets support passing arguments as well, so for example, you could update a single package:\n\n```sh\nbazel run @swift_package//:update -- MyPackage\n```\n\nThese targets will update the `Package.resolved` file defined in `swift_deps.from_package`.\nThe targets come with default flags applied to enable the best Bazel compatibility, if you wish to configure it further, you can do so with `configure_swift_package`:\n\n```starlark\n# MODULE.bazel\n\nswift_deps.configure_swift_package(\n    build_path = \"spm-build\",\n    cache_path = \"spm-cache\",\n    dependency_caching = \"false\",\n    manifest_cache = \"none\",\n    manifest_caching = \"false\",\n)\n```\n\nIf you do not want to use the `swift_package` repository you can disable it in the `swift_deps.from_package` call:\n\n```starlark\nswift_deps.from_package(\n    declare_swift_package = False,  # \u003c=== Disable the `swift_package` repository\n    resolved = \"//:Package.resolved\",\n    swift = \"//:Package.swift\",\n)\n```\n\n#### (Optional) Enable `swift_deps_info` generation for the Gazelle plugin\n\nIf you will be using the [Gazelle plugin for Swift], you will need to enable the generation of\nthe `swift_deps_info` repository by enabling `declare_swift_deps_info`.\n\n```bazel\nswift_deps.from_package(\n    declare_swift_deps_info = True, # \u003c=== Enable swift_deps_info generation for the Gazelle plugin\n    resolved = \"//:Package.resolved\",\n    swift = \"//:Package.swift\",\n)\n```\n\n### 3. Create a minimal `Package.swift` file.\n\nCreate a minimal `Package.swift` file that only contains the external dependencies that are directly\nused by your Bazel workspace.\n\n```swift\n// swift-tools-version: 5.7\n\nimport PackageDescription\n\nlet package = Package(\n    name: \"my-project\",\n    dependencies: [\n        // Replace these entries with your dependencies.\n        .package(url: \"https://github.com/apple/swift-argument-parser\", from: \"1.2.0\"),\n        .package(url: \"https://github.com/apple/swift-log\", from: \"1.4.4\"),\n    ]\n)\n```\n\nThe name of the package can be whatever you like. It is required for the manifest, but it is not\nused by [rules_swift_package_manager]. If your project is published and consumed as a Swift package,\nfeel free to populate the rest of the manifest so that your package works properly by Swift package\nmanager. Just note that the [Swift Gazelle plugin] does not use the manifest to generate Bazel build\nfiles.\n\n### 4. Run `swift package update`\n\nThis will invoke Swift Package Manager and resolve all dependencies resulting in creation of\n`Package.resolved` file.\n\n### 5. Run `bazel mod tidy`.\n\nThis will update your `MODULE.bazel` with the correct `use_repo` declaration.\n\n### 6. Build and test your project.\n\nBuild and test your project.\n\n```sh\nbazel test //...\n```\n\n### 7. Check in `Package.swift`, `Package.resolved`, and `MODULE.bazel`.\n\n- The `Package.swift` file is used by `rules_swift_package_manager` to generate information about\n  your project's dependencies.\n- The `Package.resolved` file specifies that exact versions of the downloaded dependencies that were\n  identified.\n- The `MODULE.bazel` contains the declarations for your external dependencies.\n\n### 8. Start coding\n\nYou are ready to start coding.\n\n## Using a Swift package registry\n\nSee [our document on using a Swift package registry](/docs/swift_package_registry.md).\n\n## Tips and Tricks\n\nThe following are a few tips to consider as you work with your repository:\n\n- Are you trying to use a Swift package and it just won't build under Bazel? If you can figure out\n  how to fix it, you can patch the Swift package. Check out [our document on patching Swift packages].\n\n\u003c!-- Links --\u003e\n\n[Bazel modules]: https://bazel.build/external/module\n[Bazel's hybrid mode]: https://bazel.build/external/migration#hybrid-mode\n[bzlmod]: https://bazel.build/external/overview#bzlmod\n[our document on patching Swift packages]: docs/patch_swift_package.md\n[CI GitHub workflow]: .github/workflows/ci.yml\n[Gazelle plugin]: https://github.com/bazelbuild/bazel-gazelle/blob/master/extend.md\n[Gazelle]: https://github.com/bazelbuild/bazel-gazelle\n[Gazelle plugin for Swift]: https://github.com/cgrindel/swift_gazelle_plugin\n[Swift Gazelle plugin]: https://github.com/cgrindel/swift_gazelle_plugin\n[examples]: examples/\n[rules_apple]: https://github.com/bazelbuild/rules_apple\n[rules_spm]: https://github.com/cgrindel/rules_spm\n[rules_swift]: https://github.com/bazelbuild/rules_swift\n[rules_swift_package_manager]: https://github.com/cgrindel/rules_swift_package_manager\n[tidy]: https://github.com/cgrindel/bazel-starlib/blob/main/doc/bzltidy/rules_and_macros_overview.md#tidy\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcgrindel%2Frules_swift_package_manager","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcgrindel%2Frules_swift_package_manager","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcgrindel%2Frules_swift_package_manager/lists"}