diff --git a/.bazelrc b/.bazelrc index 575c0ef..f596a3b 100644 --- a/.bazelrc +++ b/.bazelrc @@ -1,16 +1,24 @@ try-import %workspace%/.bazelrc.ci -test --sandbox_default_allow_network=false -test --test_output=errors +common --enable_platform_specific_config build --incompatible_disallow_empty_glob build --verbose_failures build --worker_sandboxing -# TODO: turn this back on when Zig can build CGo (https://github.com/uber/hermetic_cc_toolchain/issues/10) -#build --action_env BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1 -build --sandbox_add_mount_pair=/tmp -build --sandbox_add_mount_pair=/var/tmp - build --experimental_output_directory_naming_scheme=diff_against_dynamic_baseline -build:darwin_toolchains --extra_toolchains @zig_sdk//toolchain:darwin_amd64,@zig_sdk//toolchain:darwin_arm64 + +build:linux --sandbox_add_mount_pair=/tmp +build:macos --sandbox_add_mount_pair=/var/tmp +build:windows --sandbox_add_mount_pair=C:\Temp + +build:macos_toolchains --extra_toolchains @zig_sdk//toolchain:darwin_amd64,@zig_sdk//toolchain:darwin_arm64 +build:macos_toolchains --action_env BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1 +build:macos_toolchains --build_tag_filters= + +build:linux --config=macos_toolchains +build:windows --config=macos_toolchains +build:macos --build_tag_filters=-darwin_c + +test --sandbox_default_allow_network=false +test --test_output=errors diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 9b6d6ae..dc9bfce 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -50,37 +50,28 @@ jobs: /tmp/zig-cache ~/.cache/bazelisk + - run: echo "common --announce_rc" >> .bazelrc.ci + - run: brew install bash if: runner.os == 'macOS' - - run: echo "common --announce_rc" >> .bazelrc.ci - - run: echo "build --sandbox_add_mount_pair=C:\\Temp" >> .bazelrc.ci - if: runner.os == 'Windows' - # Linux, macOS and Windows + - run: tools/bazel test //... - run: ci/list_toolchains_platforms - run: ci/zig-utils - # Linux and macOS - - run: ci/release - if: runner.os == 'Linux' || runner.os == 'macOS' + # Test the Zig toolchain for Darwin on Darwin. + - run: tools/bazel build --config=macos_toolchains --@rules_go//go/config:static --@rules_go//go/config:pure //test/c/... + if: runner.os == 'macOS' + + # Windows problems: + # protoc does not compile due to `-lpthread`. - run: ci/lint - if: runner.os == 'Linux' || runner.os == 'macOS' - # TODO: releaser is broken on windows + if: runner.os != 'Windows' + # releaser fails to write `README.md` for some reason. + - run: ci/release + if: runner.os != 'Windows' - run: ci/prepare_git && ci/test_example rules_cc override_repository - if: runner.os == 'Linux' || runner.os == 'macOS' - - # Linux and Windows - - run: tools/bazel build --config=darwin_toolchains //... - if: runner.os == 'Linux' || runner.os == 'Windows' - - # Linux - - run: ci/test --config=darwin_toolchains - if: runner.os == 'Linux' - # TODO windows: broken releaser. MacOS: cgo. + if: runner.os != 'Windows' - run: ci/prepare_git && ci/test_example bzlmod override_module - if: runner.os == 'Linux' - - # macOS - - run: tools/bazel build --config=darwin_toolchains --build_tag_filters=darwin_c //... - if: runner.os == 'macOS' + if: runner.os != 'Windows' diff --git a/README.md b/README.md index 9f473bb..249c362 100644 --- a/README.md +++ b/README.md @@ -61,14 +61,10 @@ zig_toolchains() And this to `.bazelrc` on a Unix-y systems: ``` -build --sandbox_add_mount_pair=/tmp -build --sandbox_add_mount_pair=/var/tmp -``` - -Windows: - -``` -build --sandbox_add_mount_pair=C:\Temp +common --enable_platform_specific_config +build:linux --sandbox_add_mount_pair=/tmp +build:macos --sandbox_add_mount_pair=/var/tmp +build:windows --sandbox_add_mount_pair=C:\Temp ``` The directories can be narrowed down to `/tmp/zig-cache` (Linux), @@ -375,9 +371,9 @@ A standalone Docker environment to play with `hermetic_cc_toolchain`: $ docker run -e CC=/usr/bin/false -ti --rm -v "$PWD:/x" -w /x debian:bookworm-slim # apt update && apt install --no-install-recommends -y shellcheck ca-certificates python3 git # git config --global --add safe.directory /x +# tools/bazel test ... # ./ci/lint # ./ci/release -# ./ci/test # ./ci/zig-wrapper ``` ## Communication diff --git a/ci/release b/ci/release index 72121cb..5d97e92 100755 --- a/ci/release +++ b/ci/release @@ -8,7 +8,7 @@ set -xeuo pipefail cd "$(git rev-parse --show-toplevel)" >&2 echo "--- releaser :flag-lt:" -TAG=$(git -c 'versionsort.suffix=-rc' tag --sort=v:refname | grep -vE '2\.2\.(2|3)' | tail -1) +TAG=$(git -c 'versionsort.suffix=-rc' tag --sort=v:refname | tail -1) tools/bazel run //tools/releaser -- -tag "$TAG" -skipBranchCheck >&2 echo "--- git diff :git:" diff --git a/ci/test b/ci/test deleted file mode 100755 index 7bef54a..0000000 --- a/ci/test +++ /dev/null @@ -1,44 +0,0 @@ -#!/usr/bin/env bash - -# Copyright 2023 Uber Technologies, Inc. -# Licensed under the MIT License - -set -xeuo pipefail - -case "$(uname)" in - Linux) default_cache_prefix='/tmp/zig-cache' ;; - Darwin) default_cache_prefix='/var/tmp/zig-cache' ;; - *) default_cache_prefix='C:\Temp\zig-cache' ;; -esac - -cache_prefix="${HERMETIC_CC_TOOLCHAIN_CACHE_PREFIX:-$default_cache_prefix}" - -# check a very hermetic setup with a single target. Re-building all of -# them takes a long time, so using only one. If we ever decide to build all -# targets, we will need to exclude Go, since go dynamically links to glibc on -# linux. - -echo "--- build a single target with very hermetic sandbox" -tools/bazel build "$@" \ - --experimental_use_hermetic_linux_sandbox \ - --sandbox_writable_path="$cache_prefix" \ - --sandbox_add_mount_pair=/proc \ - //test/c:which_libc - -# then test everything else with the standard sandbox -echo "--- bazel test $* ..." -tools/bazel test "$@" ... - -echo "--- ensure github.com/ziglang/zig/issues/13050 does not regress" -find "$cache_prefix" \ - -name mutex_destructor.o -execdir file '{}' \; \ - | sort \ - | uniq -c \ - | sort -rn > /tmp/got_cache - -diff -u ci/testdata/want_cache /tmp/got_cache || { - >&2 echo "ERROR: unexpected artifacts. This is TODO." - # TODO: Go 1.20 regressed this. Find a way to re-enable. See README. - #exit 1 - exit 0 -} diff --git a/examples/bzlmod/.bazelrc b/examples/bzlmod/.bazelrc index 5eb4254..1ce1e00 100644 --- a/examples/bzlmod/.bazelrc +++ b/examples/bzlmod/.bazelrc @@ -1,15 +1,17 @@ # Copyright 2023 Uber Technologies, Inc. # Licensed under the MIT License -test --sandbox_default_allow_network=false -test --test_output=errors +try-import %workspace%/../../.bazelrc.ci + +common --enable_platform_specific_config build --incompatible_disallow_empty_glob build --verbose_failures build --worker_sandboxing +build --experimental_output_directory_naming_scheme=diff_against_dynamic_baseline -build --action_env BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1 - -build --sandbox_add_mount_pair=/tmp -build --sandbox_add_mount_pair=/var/tmp +build:linux --sandbox_add_mount_pair=/tmp +build:macos --sandbox_add_mount_pair=/var/tmp +build:windows --sandbox_add_mount_pair=C:\Temp -build --experimental_output_directory_naming_scheme=diff_against_dynamic_baseline +test --sandbox_default_allow_network=false +test --test_output=errors diff --git a/examples/rules_cc/.bazelrc b/examples/rules_cc/.bazelrc index aa3f2f7..3379c49 100644 --- a/examples/rules_cc/.bazelrc +++ b/examples/rules_cc/.bazelrc @@ -1,14 +1,17 @@ # Copyright 2023 Uber Technologies, Inc. # Licensed under the MIT License -test --sandbox_default_allow_network=false -test --test_output=errors +try-import %workspace%/../../.bazelrc.ci + +common --enable_platform_specific_config build --incompatible_disallow_empty_glob build --verbose_failures build --worker_sandboxing - +build --experimental_output_directory_naming_scheme=diff_against_dynamic_baseline build --action_env BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1 +build:linux --sandbox_add_mount_pair=/tmp +build:macos --sandbox_add_mount_pair=/var/tmp +build:windows --sandbox_add_mount_pair=C:\Temp -build --sandbox_add_mount_pair=/tmp -build --sandbox_add_mount_pair=/var/tmp -build --experimental_output_directory_naming_scheme=diff_against_dynamic_baseline +test --sandbox_default_allow_network=false +test --test_output=errors diff --git a/test/c/BUILD b/test/c/BUILD index de8c7b9..5333f8e 100644 --- a/test/c/BUILD +++ b/test/c/BUILD @@ -1,55 +1,101 @@ load("@hermetic_cc_toolchain//rules:platform.bzl", "platform_binary") +load("@rules_go//go:def.bzl", "go_library", "go_test") cc_binary( name = "which_libc", srcs = ["main.c"], ) +_LINUX_AMD64 = [ + "@platforms//os:linux", + "@platforms//cpu:x86_64", +] + +_DARWIN_AMD64 = [ + "@platforms//os:macos", + "@platforms//cpu:x86_64", +] + +_DARWIN_ARM64 = [ + "@platforms//os:macos", + "@platforms//cpu:aarch64", +] + +_WINDOWS_AMD64 = [ + "@platforms//os:windows", + "@platforms//cpu:x86_64", +] + [ ( platform_binary( name = "which_libc_{}".format(name), src = "which_libc", platform = platform, + tags = tags, ), - sh_test( + go_test( name = "test_libc_{}".format(name), - srcs = ["test.sh"], - data = ["which_libc_{}".format(name)], + data = [":which_libc_{}".format(name)], + embed = [":c_test"], env = { "WANT": want, - "BINARY": "$(location which_libc_{})".format(name), + "BINARY": "$(rlocationpath which_libc_{})".format(name), }, + target_compatible_with = compatible_with, ), ) - for name, platform, want in [ - ("linux_amd64_musl", "//libc_aware/platform:linux_amd64_musl", "linux non-glibc"), - ("linux_amd64_gnu.2.28", "//libc_aware/platform:linux_amd64_gnu.2.28", "linux glibc_2.28"), - ("linux_amd64", "//platform:linux_amd64", "linux glibc_2.28"), - ] -] - -[ - ( - platform_binary( - name = "which_libc_{}".format(name), - src = "which_libc", - platform = platform, - tags = tags, + for name, platform, compatible_with, want, tags in [ + ( + "linux_amd64_musl", + "//libc_aware/platform:linux_amd64_musl", + _LINUX_AMD64, + "^linux non-glibc", + [], + ), + ( + "linux_amd64_gnu.2.28", + "//libc_aware/platform:linux_amd64_gnu.2.28", + _LINUX_AMD64, + "^linux glibc_2.28", + [], + ), + ( + "linux_amd64", + "//platform:linux_amd64", + _LINUX_AMD64, + "^linux glibc_2.28", + [], + ), + ( + "windows_amd64", + "//platform:windows_amd64", + _WINDOWS_AMD64, + "^windows ", + [], ), - ) - for name, platform, tags in [ ( "darwin_amd64", "//platform:darwin_amd64", + _DARWIN_AMD64, + "^macos non-glibc", ["darwin_c"], ), ( "darwin_arm64", "//platform:darwin_arm64", + _DARWIN_ARM64, + "^macos non-glibc", ["darwin_c"], ), - ("windows_amd64", "//platform:windows_amd64", []), - ("windows_arm64", "//platform:windows_arm64", []), ] ] + +go_library( + name = "c_test", + srcs = ["c_test.go"], + deps = [ + "@com_github_stretchr_testify//assert", + "@rules_go//go/runfiles", + ], +) diff --git a/test/c/c_test.go b/test/c/c_test.go new file mode 100644 index 0000000..2c4e426 --- /dev/null +++ b/test/c/c_test.go @@ -0,0 +1,26 @@ +package c_test + +import ( + "os" + "os/exec" + "testing" + + "github.com/bazelbuild/rules_go/go/runfiles" + "github.com/stretchr/testify/assert" +) + +func TestYadda(t *testing.T) { + want := os.Getenv("WANT") + + binary, err := runfiles.Rlocation(os.Getenv("BINARY")) + if err != nil { + t.Fatalf("unable to locate guest binary: %v", err) + } + + got, err := exec.Command(binary).CombinedOutput() + if err != nil { + t.Fatalf("run %q: %v", binary, err) + } + + assert.Regexp(t, string(want), string(got)) +} diff --git a/test/c/test.sh b/test/c/test.sh deleted file mode 100755 index 02dbb58..0000000 --- a/test/c/test.sh +++ /dev/null @@ -1,20 +0,0 @@ -#!/bin/sh - -# Copyright 2023 Uber Technologies, Inc. -# Licensed under the MIT License - -set -eu - -# shellcheck disable=SC2153 -want=$WANT -# shellcheck disable=SC2153 -binary=$BINARY -got=$($binary) - -if [ "$got" != "$want" ]; then - echo wanted: - echo \ \ "$want" - echo got: - echo \ \ "$got" - exit 1 -fi diff --git a/test/cgo/BUILD b/test/cgo/BUILD index b6ac6bc..024e1cd 100644 --- a/test/cgo/BUILD +++ b/test/cgo/BUILD @@ -21,42 +21,43 @@ go_test( go_binary( name = "cgo", embed = [":cgo_lib"], - # darwin targets are disabled, see below - #gc_linkopts = select({ - # "@platforms//os:macos": [ - # "-w", # https://github.com/ziglang/zig/issues/15439 - # "-buildmode=pie", # https://github.com/ziglang/zig/issues/15438 - # ], - # "//conditions:default": [], - #}), visibility = ["//visibility:public"], ) -[ - platform_binary( - name = "cgo_{}".format(name), - src = "cgo", - platform = platform, - ) - for name, platform in [ - ("linux_amd64_musl", "//libc_aware/platform:linux_amd64_musl"), - ("linux_amd64_gnu.2.28", "//libc_aware/platform:linux_amd64_gnu.2.28"), - ("linux_arm64_musl", "//libc_aware/platform:linux_arm64_musl"), - ("linux_arm64_gnu.2.28", "//libc_aware/platform:linux_arm64_gnu.2.28"), - # https://github.com/uber/hermetic_cc_toolchain/issues/10 - # ("darwin_amd64", "//platform:darwin_amd64"), - # ("darwin_arm64", "//platform:darwin_arm64"), - ] +_LINUX_AMD64 = [ + "@platforms//os:linux", + "@platforms//cpu:x86_64", +] + +_WINDOWS_AMD64 = [ + "@platforms//os:windows", + "@platforms//cpu:x86_64", ] [ - platform_test( - name = "cgo_test_{}".format(name), - src = "cgo_test", - platform = platform, + ( + platform_binary( + name = "cgo_{}".format(name), + src = "cgo", + platform = platform, + ), ) - for name, platform, is_arm64 in [ - ("linux_amd64_musl", "//libc_aware/platform:linux_amd64_musl", False), - ("linux_amd64_gnu.2.28", "//libc_aware/platform:linux_amd64_gnu.2.28", False), + for name, platform in [ + ( + "linux_amd64_musl", + "//libc_aware/platform:linux_amd64_musl", + ), + ( + "linux_amd64_gnu.2.28", + "//libc_aware/platform:linux_amd64_gnu.2.28", + ), + ( + "windows_amd64", + "//platform:windows_amd64", + ), + ( + "windows_arm64", + "//platform:windows_arm64", + ), ] ] diff --git a/tools/releaser/main.go b/tools/releaser/main.go index 1a0dcb3..b432449 100644 --- a/tools/releaser/main.go +++ b/tools/releaser/main.go @@ -45,6 +45,7 @@ var ( "v2.1.1": "86ace5cd211d0ae49a729a11afb344843698b64464f2095a776c57ebbdf06698", "v2.1.3": "a5caccbf6d86d4f60afd45b541a05ca4cc3f5f523aec7d3f7711e584600fb075", "v2.2.1": "3b8107de0d017fe32e6434086a9568f97c60a111b49dc34fc7001e139c30fdea", + "v3.0.0": "fe00bd126e57a4c3fec4efa620bf074e3d1f1fbd70b75113ca56a010d7a70d93", } _boilerplateFiles = []string{