eliperkins / rules_spm

Provide a means for integrating external Swift packages built by Swift Package Manager into Bazel build using rules_swift.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Swift Package Manager Rules for Bazel

Build

This repository contains rules for Bazel that can be used to download, build and consume Swift packages with rules_swift rules. The rules in this repository build the external Swift packages with Swift Package Manager, then make the outputs available to Bazel rules.

Table of Contents

Reference Documentation

Click here for reference documentation for the rules and other definitions in this repository.

Prerequisites

Mac OS

Be sure to install Xcode.

Linux

You will need to install Swift. Make sure that running swift --version works properly.

Don't forget that rules_swift expects the use of clang. Hence, you will need to specify CC=clang before running Bazel.

In addition, the Bazel toolchains want to use the Gold linker. While it may be installed on the system, it is possible that it will not be findable when using clang as mentioned previously. So, you will want to do one of the following:

Option #1: Create a symlink to the linker in the clang directory.

sudo ln -s $(which ld.gold) $(dirname $(which clang))

This option worked well on a fairly minimal install of Ubuntu 20.04.

Option #2:

Specify a custom PATH to Bazel via --action_env. The custom PATH should have the Swift bin directory as the first item.

swift_exec=$(which swift)
real_swift_exec=$(realpath $swift_exec)
real_swift_dir=$(dirname $real_swift_exec)
new_path="${real_swift_dir}:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
cat >>local.bazelrc <<EOF
build --action_env=PATH=${new_path}
EOF

This approach was necessary to successfully execute the examples on an Ubuntu runner using Github actions. See the Github workflow for more details.

Quickstart

The following provides a quick introduction on how to use the rules in this repository. Also, check out the examples directory for more information.

1. Configure your workspace to use rules_spm

Add the following to your WORKSPACE file to add this repository and its dependencies.

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
    name = "cgrindel_rules_spm",
    sha256 = "cbe5d5dccdc8d5aa300e1538c4214f44a1266895d9817e8279a9335bcbee2f1e",
    strip_prefix = "rules_spm-0.7.0",
    urls = [
        "http://github.com/cgrindel/rules_spm/archive/v0.7.0.tar.gz",
    ],
)

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

spm_rules_dependencies()

load(
    "@build_bazel_rules_swift//swift:repositories.bzl",
    "swift_rules_dependencies",
)

swift_rules_dependencies()

load(
    "@build_bazel_rules_swift//swift:extras.bzl",
    "swift_rules_extra_dependencies",
)

swift_rules_extra_dependencies()

2. Add external Swift packages as dependencies to your workspace

Add the following to download and build apple/swift-log to your workspace. NOTE: It is imperative that the products that will be used by your project are listed in the products list.

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

spm_repositories(
    name = "swift_pkgs",
    dependencies = [
        spm_pkg(
            "https://github.com/apple/swift-log.git",
            exact_version = "1.4.2",
            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 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.

load("@build_bazel_rules_swift//swift:swift.bzl", "swift_binary")

swift_binary(
    name = "simple",
    srcs = ["main.swift"],
    visibility = ["//swift:__subpackages__"],
    deps = [
        "@swift_pkgs//swift-log:Logging",
    ],
)

Lastly, import the Swift module and enjoy!

import Logging

let logger = Logger(label: "com.example.main")
logger.info("Hello World!")

About

Provide a means for integrating external Swift packages built by Swift Package Manager into Bazel build using rules_swift.

License:Apache License 2.0


Languages

Language:Starlark 98.4%Language:Shell 1.1%Language:Smarty 0.5%