Skip to content

Commit

Permalink
Upgrade to Bazel 7, Ubuntu 24.04 (fixes #152) (#154)
Browse files Browse the repository at this point in the history
* Upgrade to Bazel 7.

Signed-off-by: Steffen Smolka <[email protected]>

* Improve CI script.

Signed-off-by: Steffen Smolka <[email protected]>

* tweaks

Signed-off-by: Steffen Smolka <[email protected]>

* Fix formatting script.

Signed-off-by: Steffen Smolka <[email protected]>

---------

Signed-off-by: Steffen Smolka <[email protected]>
  • Loading branch information
smolkaj authored Dec 18, 2024
1 parent 10be77f commit d8c34b2
Show file tree
Hide file tree
Showing 12 changed files with 118 additions and 149 deletions.
13 changes: 11 additions & 2 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,14 @@
try-import user.bazelrc

# Use C++17.
common --cxxopt='-std=c++17'
common --host_cxxopt='-std=c++17'
build --cxxopt=-std=c++17
build --host_cxxopt=-std=c++17

# Use Clang.
build --action_env=CC=clang
build --action_env=CXX=clang++

# Use `WORKSPACE.bazel` instead of Bzlmod (`MODULE.bazel`).
# TODO - migrate to Bzlmod.
common --noenable_bzlmod
common --enable_workspace
2 changes: 1 addition & 1 deletion .bazelversion
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5.4.0
7.4.1
12 changes: 1 addition & 11 deletions .github/workflows/ci-format.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,9 @@ on:

jobs:
build:
runs-on: ubuntu-22.04
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
submodules: recursive

- name: Mount bazel cache
uses: actions/cache@v3
with:
path: "~/.cache/bazel" # See https://docs.bazel.build/versions/master/output_directories.html
key: format-${{ hashFiles('**/*_deps.bzl', '**/*.bazelrc') }}
restore-keys: |
format

- name: Run format.sh
run: ./format.sh
Expand Down
86 changes: 54 additions & 32 deletions .github/workflows/ci-native.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,50 @@ on:
branches: [ master ]
schedule:
# Run daily at 00:00 (https://crontab.guru/#0_0_*_*_*).
- cron: "0 0 * * *"
- cron: "0 0 * * *" # Allow manual triggering of the workflow.
# https://docs.github.com/en/actions/managing-workflow-runs-and-deployments/managing-workflow-runs/manually-running-a-workflow
workflow_dispatch:

jobs:
build:
runs-on: ubuntu-22.04

strategy:
matrix:
# We only test on the oldest version we want to support and latest.
# We trust that things also work for versions in the middle.
os: [ubuntu-22.04, ubuntu-latest]
# See Bazelisk README for legal values.
# TODO - Add `latest` once it is supported. Requires using Bzlmod.
bazel_version: [7.x]
# Don't abort other runs when one of them fails, to ease debugging.
fail-fast: false

runs-on: ${{ matrix.os }}

env:
XDG_CACHE_HOME: ~/.cache/bazel-repo
# This tells Bazelisk (installed as `bazel`) to use specified version.
# https://github.com/bazelbuild/bazelisk?tab=readme-ov-file#how-does-bazelisk-know-which-bazel-version-to-run
USE_BAZEL_VERSION: ${{ matrix.bazel_version }}
CACHE_KEY: ${{ matrix.os }}_bazel-${{ matrix.bazel_version }}

steps:
- uses: actions/checkout@v4
with:
submodules: recursive

- name: Install p4c system dependencies (Flex, Bison, GMP)
run: sudo apt-get update && sudo apt-get install bison flex libfl-dev libgmp-dev
run: sudo apt install bison flex libfl-dev libgmp-dev

- name: Mount bazel caches
uses: actions/cache/restore@v3
id: restore-cache
- name: Mount bazel cache
uses: actions/cache/restore@v4
with:
# See https://docs.bazel.build/versions/master/output_directories.html
path: "~/.cache/bazel"
key: bazel-${{ hashFiles('**/*_deps.bzl', '**/*.bazelrc') }}-${{ github.ref_name }}
# Create a new cache entry whenever Bazel files change.
# See https://docs.github.com/en/actions/guides/caching-dependencies-to-speed-up-workflows
key: ${{ env.CACHE_KEY }}-${{ hashFiles('**/*.bazel*', '**/*.bzl') }}
restore-keys: |
bazel-${{ hashFiles('**/*_deps.bzl', '**/*.bazelrc') }}
bazel
${{ env.CACHE_KEY }}
- name: Save start time
uses: josStorer/get-current-time@v2
Expand All @@ -38,44 +59,45 @@ jobs:
# Unix timestamp -- seconds since 1970.
format: X

- name: bazel build //...
run: |
bazel \
--bazelrc=$GITHUB_WORKSPACE/.github/workflows/ci.bazelrc \
build //...
- name: Build
run: bazel build --test_output=errors //...

- name: bazel test //...
run: |
bazel \
--bazelrc=$GITHUB_WORKSPACE/.github/workflows/ci.bazelrc \
test //...
- name: Test
run: bazel test --test_output=errors //...

- name: Save end time
# Always save the end time so we can calculate the build duration.
if: always()
uses: josStorer/get-current-time@v2
id: end-time
with:
# Unix timestamp -- seconds since 1970.
format: X

- name: Calculate build duration
# Always calculate the build duration so we can update the cache if needed.
if: always()
run: |
START=${{ steps.start-time.outputs.formattedTime }}
END=${{ steps.end-time.outputs.formattedTime }}
DURATION=$(( $END - $START ))
echo "duration=$DURATION" | tee "$GITHUB_ENV"
# Bazel's `--disk-cache` currently grows without bounds, so we remove files
# that haven't been accessed in 30+ days manually.
# github.com/bazelbuild/bazel/issues/5139 tracks fixing this in Bazel.
- name: Compress disk cache
run: find $HOME/.cache/bazel -type f -atime +30 -delete
- name: Compress cache
# Always compress the cache so we can update the cache if needed.
if: always()
run: rm -rf $(bazel info repository_cache)

- name: Save cache
uses: actions/cache/save@v3
# We create a new cache entry if either of the following is true:
# - We are on the master branch.
# - It took more than 5 minutes to build and test.
if: github.ref_name == 'master' || env.duration > 300
- name: Save bazel cache
uses: actions/cache/save@v4
# Only create a new cache entry if we're on the main branch or the build takes >3mins.
#
# NOTE: Even though `always()` evaluates to true, and `true && x == x`,
# the `always() &&` prefix is not redundant! The call to `always()` has a
# side effect, which is to override the default behavior of automagically
# canceling this step if a previous step failed.
# (Don't blame me, blame GitHub Actions!)
if: always() && (github.ref_name == 'main' || env.duration > 180)
with:
key: ${{ steps.restore-cache.outputs.cache-primary-key }}-${{ github.run_id }}
path: "~/.cache/bazel"
key: ${{ env.CACHE_KEY }}-${{ hashFiles('**/*.bazel*', '**/*.bzl') }}-${{ github.run_id }}
22 changes: 0 additions & 22 deletions .github/workflows/ci.bazelrc

This file was deleted.

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.DS_Store
bazel-*
user.bazelrc
buildifier
7 changes: 0 additions & 7 deletions BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
load("@com_github_bazelbuild_buildtools//buildifier:def.bzl", "buildifier")

package(licenses = ["notice"])

exports_files(["LICENSE"])

buildifier(
name = "buildifier",
lint_mode = "fix",
)
45 changes: 4 additions & 41 deletions WORKSPACE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps")

protobuf_deps()

load("@rules_proto//proto:repositories.bzl", "rules_proto_dependencies", "rules_proto_toolchains")
load("@rules_proto//proto:repositories.bzl", "rules_proto_dependencies")

rules_proto_dependencies()

rules_proto_toolchains()
load("@rules_proto//proto:setup.bzl", "rules_proto_setup")

rules_proto_setup()

# -- P4{C, Runtime} + transitive dependencies ----------------------------------

Expand Down Expand Up @@ -48,45 +50,6 @@ load("@com_github_grpc_grpc//bazel:grpc_extra_deps.bzl", "grpc_extra_deps")

grpc_extra_deps()

# -- Buildifier (Bazel file formatter) + transitive dependencies ---------------

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

http_archive(
name = "io_bazel_rules_go",
sha256 = "2b1641428dff9018f9e85c0384f03ec6c10660d935b750e3fa1492a281a53b0f",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.29.0/rules_go-v0.29.0.zip",
"https://github.com/bazelbuild/rules_go/releases/download/v0.29.0/rules_go-v0.29.0.zip",
],
)

load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")

go_rules_dependencies()

go_register_toolchains()

http_archive(
name = "bazel_gazelle",
sha256 = "b85f48fa105c4403326e9525ad2b2cc437babaa6e15a3fc0b1dbab0ab064bc7c",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/bazel-gazelle/releases/download/v0.22.2/bazel-gazelle-v0.22.2.tar.gz",
"https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.22.2/bazel-gazelle-v0.22.2.tar.gz",
],
)

load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies")

gazelle_dependencies(go_repository_default_config = "@//:WORKSPACE.bazel")

http_archive(
name = "com_github_bazelbuild_buildtools",
sha256 = "0d3ca4ed434958dda241fb129f77bd5ef0ce246250feed2d5a5470c6f29a77fa",
strip_prefix = "buildtools-4.0.0",
url = "https://github.com/bazelbuild/buildtools/archive/4.0.0.tar.gz",
)

# -- Load Rules Foreign CC -----------------------------------------------------
# Used for Z3.

Expand Down
21 changes: 20 additions & 1 deletion format.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
#!/bin/bash
#
# Formats source files according to Google's style guide. Requires clang-format.
#
# If you're on macOS with Apple silicon (M1, M2, M3, ...), call using
# ```
# PLATFORM=darown-arm64 ./format.sh
# ```
#
# If you're on macOS with Intel silicon, call using
# ```
# PLATFORM=darown-amd64 ./format.sh
# ```
# and similarly for other platforms.

# Possible values: {linux, darwin}_{amd64, arm64}.
PLATFORM=${PLATFORM:-linux-amd64}

# Only files with these extensions will be formatted by clang-format.
CLANG_FORMAT_EXTENSIONS="cc|h|proto"
Expand All @@ -10,4 +25,8 @@ find . -not -path "./third_party/**" \
| xargs clang-format --verbose -style=google -i

# Run buildifier (Bazel file formatter).
bazel run //:buildifier
BUILDIFIER="buildifier-$PLATFORM"
wget "https://github.com/bazelbuild/buildtools/releases/download/v7.3.1/$BUILDIFIER"
mv $BUILDIFIER buildifier
chmod +x buildifier
./buildifier --lint=fix -r .
10 changes: 5 additions & 5 deletions gutils/proto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ class StringErrorCollector : public google::protobuf::io::ErrorCollector {
StringErrorCollector(const StringErrorCollector &) = delete;
StringErrorCollector &operator=(const StringErrorCollector &) = delete;

// Implementation of protobuf::io::ErrorCollector::AddError.
void AddError(int line, int column, const std::string &message) override {
// Implementation of protobuf::io::ErrorCollector::RecordError.
void RecordError(int line, int column, std::string_view message) override {
if (error_text_ != nullptr) {
absl::SubstituteAndAppend(error_text_, "$0($1): $2\n", line, column,
message);
}
}

// Implementation of protobuf::io::ErrorCollector::AddWarning.
void AddWarning(int line, int column, const std::string &message) override {
AddError(line, column, message);
// Implementation of protobuf::io::ErrorCollector::RecordWarning.
void RecordWarning(int line, int column, std::string_view message) override {
RecordError(line, column, message);
}

private:
Expand Down
9 changes: 5 additions & 4 deletions gutils/protocol_buffer_matchers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <cstring>
#include <ostream>
#include <string>
#include <string_view>
#include <vector>

#include "absl/log/check.h"
Expand Down Expand Up @@ -52,14 +53,14 @@ class StringErrorCollector : public google::protobuf::io::ErrorCollector {
explicit StringErrorCollector(std::string* error_text)
: error_text_(error_text) {}

void AddError(int line, int column, const std::string& message) override {
void RecordError(int line, int column, std::string_view message) override {
absl::SubstituteAndAppend(error_text_, "$0($1): $2\n", line, column,
message.c_str());
message);
}

void AddWarning(int line, int column, const std::string& message) override {
void RecordWarning(int line, int column, std::string_view message) override {
absl::SubstituteAndAppend(error_text_, "$0($1): $2\n", line, column,
message.c_str());
message);
}

private:
Expand Down
Loading

0 comments on commit d8c34b2

Please sign in to comment.