Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Collection of utilities and Bazel rules to aid in the development and maintenance of Swift repositories using Bazel.

License

NotificationsYou must be signed in to change notification settings

cgrindel/rules_swift_package_manager

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build

This repository contains a Bazel ruleset that can be used to download, build, and consume Swiftpackages. The rules in this repository build the external Swift packages usingrules_swift,rules_apple and native C/C++ rulesets making the Swift package products and targets available asBazel targets.

This repository is designed to fully replacerules_spm and provide utilities to ease Swiftdevelopment inside a Bazel workspace.

Table of Contents

Documentation

Prerequisites

Mac OS

Be sure to install Xcode.

Linux

You will need toinstall Swift. Make surethat runningswift --version works properly.

Don't forget thatrules_swiftexpects the use ofclang. Hence,you will need to specifyCC=clang before running Bazel.

Finally, helprules_swift andrules_swift_package_manager find the Swift toolchain by ensuringthat aPATH that includes the Swift binary is available in the Bazel actions.

cat>>local.bazelrc<<EOFbuild --action_env=PATHEOF

This approach is necessary to successfully execute the examples on an Ubuntu runner using Githubactions. See theCI GitHub workflow for more details.

Quickstart

The following provides a quick introduction on how to set up and use the features in thisrepository. These instructions assume that you are usingBazel modules to load your externaldependencies. If you are using Bazel's legacy external dependency management, we recommend usingBazel's hybrid mode, then follow the steps in this quickstart guide.

Also, check out theexamples for more information.

1. Enable bzlmod

This repository supportsbzlmod.

common --enable_bzlmod

2. Configure yourMODULE.bazel to userules_swift_package_manager.

Add a dependency onrules_swift_package_manager.

bazel_dep(name="rules_swift_package_manager",version="1.10.0")

In addition, add the following to load the external dependencies described in yourPackage.swiftandPackage.resolved files.

swift_deps=use_extension("@rules_swift_package_manager//:extensions.bzl","swift_deps",)swift_deps.from_package(resolved="//:Package.resolved",swift="//:Package.swift",)use_repo(swift_deps,"swift_deps_info",# This is generated by the ruleset.# The name of the Swift package repositories will be added to this declaration in step 4 after# running `bazel mod tidy`.# NOTE: The name of the Bazel external repository for a Swift package is `swiftpkg_xxx` where# `xxx` is the Swift package identity, lowercase, with punctuation replaced by `hyphen`. For# example, the repository name for apple/swift-nio is `swiftpkg_swift_nio`.)

You will also need to add a dependency onrules_swift.

NOTE: Some Swift package manager features (e.g., resources) use rules fromrules_apple. It is adependency forrules_swift_package_manager. However, you do not need to declare it unless you useany of the rules in your project.

(Optional) Useswift_package repository for updating packages

Theswift_deps module extension will by default generate aswift_package repository which can be used to executeswift package commands.This is useful if you'd like to control the flags and behavior ofswift package, as well as for using the correctswift binary according to the Bazel configured toolchain.

For example, to resolve thePackage.swift file:

bazel run @swift_package//:resolve

To update packages to their latest supported version:

bazel run @swift_package//:update

Both targets support passing arguments as well, so for example, you could update a single package:

bazel run @swift_package//:update -- MyPackage

These targets will update thePackage.resolved file defined inswift_deps.from_package.The targets come with default flags applied to enable the best Bazel compatibility, if you wish to configure it further, you can do so withconfigure_swift_package:

# MODULE.bazelswift_deps.configure_swift_package(build_path="spm-build",cache_path="spm-cache",dependency_caching="false",manifest_cache="none",manifest_caching="false",)

If you do not want to use theswift_package repository you can disable it in theswift_deps.from_package call:

swift_deps.from_package(declare_swift_package=False,# <=== Disable the `swift_package` repositoryresolved="//:Package.resolved",swift="//:Package.swift",)

(Optional) Enableswift_deps_info generation for the Gazelle plugin

If you will be using theGazelle plugin for Swift, you will need to enable the generation oftheswift_deps_info repository by enablingdeclare_swift_deps_info.

swift_deps.from_package(declare_swift_deps_info=True,# <=== Enable swift_deps_info generation for the Gazelle pluginresolved="//:Package.resolved",swift="//:Package.swift",)

3. Create a minimalPackage.swift file.

Create a minimalPackage.swift file that only contains the external dependencies that are directlyused by your Bazel workspace.

// swift-tools-version: 5.7import PackageDescriptionletpackage=Package(    name:"my-project",    dependencies:[        // Replace these entries with your dependencies..package(url:"https://github.com/apple/swift-argument-parser", from:"1.2.0"),.package(url:"https://github.com/apple/swift-log", from:"1.4.4"),])

The name of the package can be whatever you like. It is required for the manifest, but it is notused byrules_swift_package_manager. If your project is published and consumed as a Swift package,feel free to populate the rest of the manifest so that your package works properly by Swift packagemanager. Just note that theSwift Gazelle plugin does not use the manifest to generate Bazel buildfiles.

4. Runswift package update

This will invoke Swift Package Manager and resolve all dependencies resulting in creation ofPackage.resolved file.

5. Runbazel mod tidy.

This will update yourMODULE.bazel with the correctuse_repo declaration.

6. Build and test your project.

Build and test your project.

bazeltest //...

7. Check inPackage.swift,Package.resolved, andMODULE.bazel.

  • ThePackage.swift file is used byrules_swift_package_manager to generate information aboutyour project's dependencies.
  • ThePackage.resolved file specifies that exact versions of the downloaded dependencies that wereidentified.
  • TheMODULE.bazel contains the declarations for your external dependencies.

8. Start coding

You are ready to start coding.

Using a Swift package registry

Seeour document on using a Swift package registry.

Tips and Tricks

The following are a few tips to consider as you work with your repository:

About

Collection of utilities and Bazel rules to aid in the development and maintenance of Swift repositories using Bazel.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors30


[8]ページ先頭

©2009-2025 Movatter.jp