diff --git a/.bazelrc b/.bazelrc index afffe76..242620f 100644 --- a/.bazelrc +++ b/.bazelrc @@ -1,4 +1,6 @@ common --enable_platform_specific_config +common:linux --host_platform=//:local_linux_platform +common:windows --host_platform=//:local_windows_platform # Determines whether C++ binaries will be linked dynamically, interacting with the linkstatic attribute on build rules. # off: Links all targets in mostly static mode. If -static is set in linkopts, targets will change to fully static. @@ -8,41 +10,12 @@ build --dynamic_mode=off build --experimental_platform_in_output_dir build --incompatible_strict_action_env build --compilation_mode=opt +build --features=thin_lto # On Linux (targetting linux and macos) use the zig toolchain from hermetic_cc_toolchain -build:linux --platforms @zig_sdk//libc_aware/platform:linux_amd64_musl -build:linux --repo_env=BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1 -build:linux --action_env=BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1 -build:linux --host_action_env=BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1 - -# Compilation Flags for zig-cc -build:linux --copt -ffunction-sections -build:linux --copt -fdata-sections -build:linux --strip=always - -build:linux --features=thin_lto -# ThinLTO feature doesn't exist in Zig toolchain, so we must set the flags manually. -# It also doesn't support LTO on darwin targets, so we can't set this globally. -# Instead, we configure copt/linkopt inside the toolchain itself (with a WORKSPACE patch). -#build --copt -flto=thin - -# On Windows, use the host toolchain -# NB: after https://github.com/uber/hermetic_cc_toolchain/pull/190 is included in a release, -# this flag should no longer be needed. -build:windows --noincompatible_enable_cc_toolchain_resolution -build:windows --copt=/W1 -build:windows --copt=/WX -build:windows --copt=/external:W1 -build:windows --copt=/diagnostics:column -build:windows --copt=/O2 -build:windows --copt=/Ob2 -build:windows --copt=/Oi -build:windows --copt=/D_MBCS -build:windows --copt=/DWIN32 -build:windows --copt=/D_WINDOWS -build:windows --copt=/MD -build:windows --copt=/GS -build:windows --copt=/fp:precise -build:windows --copt=/Gd -build:windows --copt=/TC +common --repo_env=BAZEL_MSVC_RUNTIME_VISUAL_STUDIO_EULA=1 +common --repo_env=BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1 +build --action_env=BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1 +build --host_action_env=BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1 +common --@llvm//config:experimental_stub_libgcc_s \ No newline at end of file diff --git a/.bazelversion b/.bazelversion index 815da58..e977f5e 100644 --- a/.bazelversion +++ b/.bazelversion @@ -1 +1 @@ -7.4.1 +9.1.0 \ No newline at end of file diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 97212fa..be4e1f5 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -7,11 +7,8 @@ on: pull_request: workflow_call: outputs: - windows: - description: "Windows binaries" - value: ${{jobs.build_windows.outputs.artifact}} - linux: - description: "Linux and MacOS binaries" + binaries: + description: "Linux, MacOS and Windows binaries" value: ${{jobs.build.outputs.artifact}} permissions: id-token: write @@ -19,7 +16,7 @@ permissions: attestations: write jobs: build: - name: darwin and linux + name: Build binaries runs-on: ubuntu-latest outputs: artifact: ${{steps.upload.outputs.artifact-url}} @@ -27,45 +24,24 @@ jobs: - uses: actions/checkout@v4 - run: | bazel build //:for_all_platforms - mv bazel-out/darwin_arm64-opt/bin/external/libarchive\~/tar/bsdtar tar_darwin_arm64 - mv bazel-out/darwin_amd64-opt/bin/external/libarchive\~/tar/bsdtar tar_darwin_amd64 - mv bazel-out/linux_arm64_musl-opt/bin/external/libarchive\~/tar/bsdtar tar_linux_arm64 - mv bazel-bin/external/libarchive\~/tar/bsdtar tar_linux_amd64 - - uses: actions/attest-build-provenance@v2 + mv bazel-out/macos_aarch64-opt/bin/external/libarchive+/tar/bsdtar tar_darwin_arm64 + mv bazel-out/macos_x86_64-opt/bin/external/libarchive+/tar/bsdtar tar_darwin_amd64 + mv bazel-out/linux_aarch64_musl-opt/bin/external/libarchive+/tar/bsdtar tar_linux_arm64 + mv bazel-out/linux_x86_64_musl-opt/bin/external/libarchive+/tar/bsdtar tar_linux_amd64 + mv bazel-out/windows_x86_64-opt/bin/external/libarchive+/tar/bsdtar.exe tar_windows_x86_64.exe + mv bazel-out/windows_aarch64-opt/bin/external/libarchive+/tar/bsdtar.exe tar_windows_arm64.exe + - uses: actions/attest-build-provenance@v4 with: subject-path: 'tar_*' - uses: actions/upload-artifact@v4 id: upload with: - name: linux + name: binaries retention-days: 1 path: | tar_darwin_arm64 tar_darwin_amd64 tar_linux_arm64 tar_linux_amd64 - build_windows: - name: windows - runs-on: windows-2022 - outputs: - artifact: ${{steps.upload.outputs.artifact-url}} - steps: - - uses: actions/checkout@v4 - - run: | - bazel build @libarchive//tar - mv bazel-bin/external/libarchive~/tar/bsdtar.exe tar_windows_x86_64.exe - bazel build --platforms @zig_sdk//platform:windows_arm64 @libarchive//tar - mv bazel-bin/external/libarchive~/tar/bsdtar.exe tar_windows_arm64.exe - - name: smoke test - run: ./tar_windows_x86_64.exe --help - - uses: actions/attest-build-provenance@v2 - with: - subject-path: 'tar_*' - - uses: actions/upload-artifact@v4 - id: upload - with: - name: windows - retention-days: 1 - path: | tar_windows_arm64.exe tar_windows_x86_64.exe diff --git a/BUILD.bazel b/BUILD.bazel index d6f6dc3..9e608f7 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -1,10 +1,12 @@ -load("@aspect_bazel_lib//lib:transitions.bzl", "platform_transition_filegroup") +load("@bazel_lib//lib:transitions.bzl", "platform_transition_filegroup") PLATFORMS = [ - "@zig_sdk//platform:darwin_amd64", - "@zig_sdk//platform:darwin_arm64", - "@zig_sdk//libc_aware/platform:linux_arm64_musl", - "@zig_sdk//libc_aware/platform:linux_amd64_musl" + "@llvm//platforms:macos_x86_64", + "@llvm//platforms:macos_aarch64", + "@llvm//platforms:linux_x86_64_musl", + "@llvm//platforms:linux_aarch64_musl", + "@llvm//platforms:windows_x86_64", + "@llvm//platforms:windows_aarch64", ] [ @@ -12,7 +14,6 @@ PLATFORMS = [ name = "for_" + platform.split(":")[1], srcs = ["@libarchive//tar"], target_platform = platform, - tags = ["manual"], ) for platform in PLATFORMS ] @@ -20,7 +21,20 @@ PLATFORMS = [ filegroup( name = "for_all_platforms", srcs = ["for_" + platform.split(":")[1] for platform in PLATFORMS], - target_compatible_with = [ - "@platforms//os:linux", +) + +platform( + name = "local_linux_platform", + constraint_values = [ + "@llvm//constraints/libc:gnu.2.28", + ], + parents = ["@platforms//host"], +) + +platform( + name = "local_windows_platform", + constraint_values = [ + "@llvm//constraints/windows/abi:gnullvm", ], + parents = ["@platforms//host"], ) diff --git a/MODULE.bazel b/MODULE.bazel index 5a151c3..62e3da0 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -1,24 +1,26 @@ -bazel_dep(name = "aspect_bazel_lib", version = "2.15.3") +bazel_dep(name = "bazel_lib", version = "3.2.2") +bazel_dep(name = "platforms", version = "1.0.0") bazel_dep(name = "libarchive", version = "3.8.1") -bazel_dep(name = "hermetic_cc_toolchain", version = "4.0.0") +bazel_dep(name = "llvm", version = "0.7.7") +bazel_dep(name = "xz", version = "5.4.5.bcr.8") # For compatibility with Bazel 9+ +bazel_dep(name = "bzip2", version = "1.0.8.bcr.4") # For compatibility with Bazel 9+ # libarchive_bsdtar_windows_crlf.patch # - tolerate mtree files with CRLF endings # - TODO: remove when https://github.com/libarchive/libarchive/pull/2717 is merged +# libarchive_windows_mingw.patch +# - avoid redefining mode_t before MinGW sys/types.h provides it +# - link bcrypt for Windows clang/MinGW builds using libarchive's BCrypt path single_version_override( module_name = "libarchive", + patch_strip = 1, patches = [ "//patches:libarchive_bsdtar_windows_crlf.patch", + "//patches:libarchive_windows_mingw.patch", ], - patch_strip = 1, ) -toolchains = use_extension("@hermetic_cc_toolchain//toolchain:ext.bzl", "toolchains") -use_repo(toolchains, "zig_sdk") +toolchains = use_extension("@llvm//extensions:toolchain.bzl", "toolchain") +use_repo(toolchains, "llvm_toolchains") -register_toolchains( - "@zig_sdk//libc_aware/toolchain:linux_arm64_musl", - "@zig_sdk//libc_aware/toolchain:linux_amd64_musl", - "@zig_sdk//toolchain:darwin_amd64", - "@zig_sdk//toolchain:darwin_arm64", -) +register_toolchains("@llvm_toolchains//:all") diff --git a/patches/libarchive_windows_mingw.patch b/patches/libarchive_windows_mingw.patch new file mode 100644 index 0000000..6d4c590 --- /dev/null +++ b/patches/libarchive_windows_mingw.patch @@ -0,0 +1,41 @@ +diff --git a/libarchive_bazel_windows_config.h b/libarchive_bazel_windows_config.h +index e6473eb3..2b063ac1 100644 +--- a/libarchive_bazel_windows_config.h ++++ b/libarchive_bazel_windows_config.h +@@ -118,7 +118,9 @@ + + #define gid_t short + #define id_t short ++#if !defined(__MINGW32__) + #define mode_t unsigned short ++#endif + #define pid_t int + #define ssize_t int64_t + #define uid_t short +diff --git a/libarchive/BUILD.bazel b/libarchive/BUILD.bazel +index 9dbef4e6..3689982d 100644 +--- a/libarchive/BUILD.bazel ++++ b/libarchive/BUILD.bazel +@@ -3,6 +3,12 @@ load("//bazel:config.bzl", "PLATFORM_LOCAL_DEFINES") + + package(default_visibility = ["//visibility:public"]) + ++config_setting( ++ name = "windows_clang", ++ constraint_values = ["@platforms//os:windows"], ++ flag_values = {"@rules_cc//cc/compiler:compiler": "clang"}, ++) ++ + cc_library( + name = "internal_hdrs", + hdrs = glob(["*.h"]), +@@ -168,6 +174,9 @@ cc_library( + "@rules_cc//cc/compiler:msvc-cl": [ + "/DEFAULTLIB:bcrypt", + ], ++ ":windows_clang": [ ++ "-lbcrypt", ++ ], + "//conditions:default": [], + }), + local_defines = PLATFORM_LOCAL_DEFINES,