Skip to content

Commit

Permalink
feat: add support for bzlmod
Browse files Browse the repository at this point in the history
This changeset adds support for Bzlmod, both in the sense that
Cap'n'Proto now uses Bzlmod for development dependencies, and also
in the sense that this codebase now defines a module which can be
published for Cap'n'Proto C++.

Nearly all dependencies for capnp are available on Bzlmod; the sole
exception being Brotli, which will still need to be added by
downstream projects.

New macros have been added so that Cap'n'Proto can be used in
projects downstream, either via `WORKSPACE.bazel` or Bzlmod.

- feat: add `capnp_repositories` in `defs/repositories.bzl`
- feat: add `capnp_workspace` in `defs/workspace.bzl`
- feat: add `MODULE.bazel` with dev and API dependencies
- feat: add support for `.bazelci` via presubmit config
- test: add integration test for `workspace` via bazel
- test: add integration test for bzlmod support
- feat: add bcr publishing flow (occurs on release)
- feat: add public target for runtime capnproto dependency
- docs: add doc page with instructions for use from bazel
- fix: make all capnproto sources private to this codebase
- chore: generate initial bzlmod dependency lockfile
- chore: move root `WORKSPACE` logic to `defs` calls
- chore: split `.bazelrc` into `base.bazelrc` to reuse settings
- chore: move `cc_capnp_library` to `internal`

Signed-off-by: Sam Gammon <[email protected]>
  • Loading branch information
sgammon committed Jan 16, 2024
1 parent fb473d1 commit e79814a
Show file tree
Hide file tree
Showing 46 changed files with 4,517 additions and 233 deletions.
29 changes: 29 additions & 0 deletions .bazelci/presubmit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
matrix:
bazel_version: ["7.0.0"]
platform: ["centos7", "debian10", "macos", "ubuntu2004"]

validate_config: 1
buildifier: latest

tasks:
build:
working_directory: c++
include_json_profile:
- build
- test
build_targets:
- "//..."
test_targets:
- "//..."

bcr_test_module:
module_path: "integration_tests/bzlmod"
matrix:
platform: ["debian10", "macos", "ubuntu2004", "centos7"]
tasks:
build_bzlmod_test:
name: "Build test module"
platform: ${{ platform }}
build_targets:
- "//:sample-bin"
2 changes: 2 additions & 0 deletions .bcr/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
moduleRoots: ["./c++"]
12 changes: 12 additions & 0 deletions .bcr/metadata.template.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"homepage": "https://github.com/capnproto/capnproto",
"maintainers": [
{
"github": "kentonv",
"name": "Kenton Varda"
}
],
"repository": ["github:capnproto/capnproto"],
"versions": [],
"yanked_versions": {}
}
11 changes: 11 additions & 0 deletions .bcr/presubmit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
bcr_test_module:
module_path: "integration_tests/bzlmod"
matrix:
platform: ["debian10", "macos", "ubuntu2004"]
tasks:
build_bzlmod_test:
name: "Build test module"
platform: ${{ platform }}
build_targets:
- "//:sample-bin"
5 changes: 5 additions & 0 deletions .bcr/source.template.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"integrity": "",
"strip_prefix": "{REPO}-{VERSION}/c++",
"url": "https://github.com/{OWNER}/{REPO}/releases/download/{TAG}/capnproto-cpp-{VERSION}.tar.gz"
}
8 changes: 8 additions & 0 deletions .github/workflows/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
load("@buildifier_prebuilt//:rules.bzl", "buildifier")

buildifier(
name = "buildifier.check",
exclude_patterns = ["./.git/*"],
lint_mode = "warn",
mode = "diff",
)
21 changes: 21 additions & 0 deletions .github/workflows/ci.bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# This file contains Bazel settings to apply on CI only.
# It is referenced with a --bazelrc option in the call to bazel in ci.yaml

# Debug where options came from
build --announce_rc

# Apply CI configurations
build --config=ci

# This directory is configured in GitHub actions to be persisted between runs.
# We do not enable the repository cache to cache downloaded external artifacts
# as these are generally faster to download again than to fetch them from the
# GitHub actions cache.
build --disk_cache=~/.cache/bazel

# Don't rely on test logs being easily accessible from the test runner,
# though it makes the log noisier.
test --test_output=errors

# Allows tests to run bazelisk-in-bazel, since this is the cache folder used
test --test_env=XDG_CACHE_HOME
29 changes: 29 additions & 0 deletions .github/workflows/module.bazelci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
name: Bazel CI

# Controls when the action will run.
"on":
workflow_dispatch: {}

permissions:
contents: read

concurrency:
# Cancel previous actions from the same revision
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
test:
uses: bazel-contrib/.github/.github/workflows/bazel.yaml@29e53247c6366e30acbedfc767f58f79fc05836c
with:
folders: |
[
"./c++",
]
exclude_windows: false
exclude: |
[
{"bazelversion": "7.0.0", "bzlmodEnabled": true},
{"bazelversion": "7.0.0", "bzlmodEnabled": false}
]
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# TODO(2.0): Support g++ once they fix coroutines. This also blocks MinGW and ManyLinux builds.

---
# TODO(2.0): Support g++ once they fix coroutines. This also blocks MinGW and ManyLinux builds.
name: Quick Tests

on:
"on":
workflow_dispatch: {}
pull_request:
paths-ignore:
- 'doc/**'
Expand Down
32 changes: 32 additions & 0 deletions .github/workflows/on.release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
name: Release

"on":
push:
tags:
- "v*.*.*"

workflow_dispatch:
inputs:
prerelease:
description: Pre-release
type: boolean
default: false
artifact:
description: Artifact
type: string
default: "capnp-cpp"
dry:
description: Dry-run
type: boolean
default: false

permissions:
contents: read

jobs:
release:
name: "Release: BCR"
uses: bazel-contrib/.github/.github/workflows/release_ruleset.yaml@v2
with:
release_files: capnp-cpp-*.tar.gz
37 changes: 37 additions & 0 deletions .github/workflows/release_prep.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env bash

set -o errexit -o nounset -o pipefail

# Set by GH actions, see
# https://docs.github.com/en/actions/learn-github-actions/environment-variables#default-environment-variables
TAG=${GITHUB_REF_NAME}
# The prefix is chosen to match what GitHub generates for source archives
PREFIX="capnp-cpp-${TAG:1}"
ARCHIVE="capnp-cpp-$TAG.tar.gz"
git archive --format=tar --prefix=${PREFIX}/ ${TAG} | gzip > $ARCHIVE
SHA=$(shasum -a 256 $ARCHIVE | awk '{print $1}')

cat << EOF
1. Enable with \`common --enable_bzlmod\` in \`.bazelrc\`.
2. Add to your \`MODULE.bazel\` file:
\`\`\`starlark
bazel_dep(name = "capnp-cpp", version = "${TAG:1}")
\`\`\`
## Using WORKSPACE
Paste this snippet into your `WORKSPACE.bazel` file:
\`\`\`starlark
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name = "capnp-cpp",
sha256 = "${SHA}",
strip_prefix = "${PREFIX}",
url = "https://github.com/capnproto/capnp-cpp/releases/download/${TAG}/${ARCHIVE}",
)
EOF

awk 'f;/--SNIP--/{f=1}' integration_tests/workspace/WORKSPACE.bazel
echo "\`\`\`"
36 changes: 2 additions & 34 deletions c++/.bazelrc
Original file line number Diff line number Diff line change
@@ -1,38 +1,6 @@
common --noenable_bzlmod
common --enable_platform_specific_config
common --enable_bzlmod

build:unix --cxxopt='-std=c++20' --host_cxxopt='-std=c++20' --force_pic --verbose_failures
build:unix --cxxopt='-Wall' --host_cxxopt='-Wall'
build:unix --cxxopt='-Wextra' --host_cxxopt='-Wextra'
build:unix --cxxopt='-Wno-strict-aliasing' --host_cxxopt='-Wno-strict-aliasing'
build:unix --cxxopt='-Wno-sign-compare' --host_cxxopt='-Wno-sign-compare'
build:unix --cxxopt='-Wno-unused-parameter' --host_cxxopt='-Wno-unused-parameter'
build:unix --cxxopt='-Wno-deprecated-this-capture' --host_cxxopt='-Wno-deprecated-this-capture'

# I needed these magic spells to build locally with clang-11 and clang-12 on Ubuntu. clang-13 and up
# work out-of-the-box.
# TODO(2.0): Remove this when we support g++ again.
build:linux --action_env=CXXFLAGS=-stdlib=libc++
build:linux --action_env=LDFLAGS=-stdlib=libc++
build:linux --action_env=BAZEL_CXXOPTS=-stdlib=libc++
build:linux --action_env=BAZEL_LINKOPTS=-lc++:-lm

build:linux --config=unix
build:macos --config=unix

# See https://bazel.build/configure/windows#symlink
startup --windows_enable_symlinks
# We use LLVM's MSVC-compatible compiler driver to compile our code on Windows
# under Bazel. MSVC is natively supported when using CMake builds.
build:windows --compiler=clang-cl

build:windows --cxxopt='/std:c++20' --host_cxxopt='/std:c++20' --verbose_failures
# The `/std:c++20` argument is unused during boringssl compilation and we don't
# want a warning when compiling each file.
build:windows --cxxopt='-Wno-unused-command-line-argument' --host_cxxopt='-Wno-unused-command-line-argument'
# MSVC disappointingly sets __cplusplus to 199711L by default. Defining /Zc:__cplusplus makes it
# set the correct value.
build:windows --cxxopt='/Zc:__cplusplus' --host_cxxopt='/Zc:__cplusplus'
import %workspace%/../tools/bazel/base.bazelrc

# build with ssl, zlib and bazel by default
build --//src/kj:openssl=True
Expand Down
60 changes: 60 additions & 0 deletions c++/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
"Cap'n'Proto for C++"

module(
name = "capnp-cpp",
version = "1.0.2", # keep in sync with current version
)


##
## Dependencies: API
##

bazel_dep(
name = "platforms",
version = "0.0.7",
)
bazel_dep(
name = "bazel_skylib",
version = "1.4.2",
)
bazel_dep(
name = "zlib",
version = "1.3",
)
bazel_dep(
name = "boringssl",
version = "0.0.0-20230215-5c22014",
repo_name = "ssl",
)

##
## Dependencies: Development
##

bazel_dep(
name = "rules_license",
version = "0.0.7",
dev_dependency = True,
)
bazel_dep(
name = "rules_testing",
version = "0.4.0",
dev_dependency = True,
)
bazel_dep(
name = "aspect_bazel_lib",
version = "1.34.1",
dev_dependency = True,
)
bazel_dep(
name = "stardoc",
version = "0.6.2",
dev_dependency = True,
repo_name = "io_bazel_stardoc",
)
bazel_dep(
name = "buildifier_prebuilt",
version = "6.3.3",
dev_dependency = True,
)
Loading

0 comments on commit e79814a

Please sign in to comment.