From 01db6d8624e41391343d501c431dd1a74741f5dc Mon Sep 17 00:00:00 2001 From: Henner Zeller Date: Fri, 24 May 2024 21:07:31 -0700 Subject: [PATCH] Add flag to build a static binary for easy use in various environments. Use CI to build a static built artifact. --- .github/workflows/ci.yml | 46 +++++++++++++++++++++++++++++++++++++++- bant/BUILD | 22 +++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ab6ba48..ef7ec44 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -9,7 +9,7 @@ on: - main jobs: - Build: + Test: runs-on: ubuntu-24.04 steps: - name: Checkout code @@ -180,3 +180,47 @@ jobs: run: | clang-tidy-18 --version CLANG_TIDY=clang-tidy-18 scripts/run-clang-tidy-cached.cc + + BuildStaticBinary: + runs-on: ubuntu-24.04 + steps: + - name: Checkout code + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Create Cache Timestamp + id: cache_timestamp + uses: nanzm/get-time-action@v2.0 + with: + format: 'YYYY-MM-DD-HH-mm-ss' + + - name: Mount bazel cache + uses: actions/cache@v4 + with: + path: "~/.cache/bazel" + key: bazelcache_release_${{ steps.cache_timestamp.outputs.time }} + restore-keys: bazelcache_release_ + + - name: Build + run: | + export CC=gcc-13 + export CXX=g++-13 + bazel build -c opt --//bant:create_static_linked_executables bant:bant + + VERSION=$(git log --date=short --pretty=format:'%cd_%h' -n1) + OUT=bant-${VERSION}-x86 + echo "OUT=${OUT}" >> $GITHUB_ENV + + mkdir -p "${OUT}/bin" + install bazel-bin/bant/bant "${OUT}/bin" + strip "${OUT}/bin/bant" + # We need to pack it to retain proper unix permissions. + # Unfortunately, github packs the whole result in a *.zip archive. + tar cvzf "${OUT}.tar.gz" ${OUT} + + - name: Upload artifact + uses: actions/upload-artifact@v4 + with: + name: ${{ env.OUT }} + path: ${{ env.OUT }}.tar.gz diff --git a/bant/BUILD b/bant/BUILD index a4af8e8..508dddf 100644 --- a/bant/BUILD +++ b/bant/BUILD @@ -1,3 +1,4 @@ +load("@bazel_skylib//rules:common_settings.bzl", "bool_flag") load("@com_github_google_rules_install//installer:def.bzl", "installer") package( @@ -5,6 +6,16 @@ package( default_visibility = ["//visibility:public"], ) +bool_flag( + name = "create_static_linked_executables", + build_setting_default = False, +) + +config_setting( + name = "static_linked_executables", + flag_values = {":create_static_linked_executables": "true"}, +) + installer( name = "install", data = [ @@ -17,6 +28,17 @@ cc_binary( srcs = [ "bant.cc", ], + features = select({ + "//bant:static_linked_executables": [ + "fully_static_link", + "-supports_start_end_lib", + ], + "//conditions:default": [], + }), + linkopts = select({ + "//bant:static_linked_executables": ["-fuse-ld=bfd"], + "//conditions:default": [], + }), deps = [ ":session", ":types",