Git Product home page Git Product logo

rules_spm's Introduction

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 via --action_env

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 = "03718eb865a100ba4449ebcbca6d97bf6ea78fa17346ce6d55532312e8bf9aa8",
    strip_prefix = "rules_spm-0.11.0",
    urls = [
        "http://github.com/cgrindel/rules_spm/archive/v0.11.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!")

rules_spm's People

Contributors

cgrindel avatar cgrindel-app-token-generator[bot] avatar pomozoff avatar pswaminathan avatar marwanf1 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.