From cf531aefe34060560c184bbe7342b6b7674ea05a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20Vu=C4=8Dica?= Date: Fri, 3 May 2024 16:25:15 +0100 Subject: [PATCH] bazel: Stamping the product.h with build time and commit hash. Stamping the elf binary with a build ID. --- BUILD | 130 +++++++++++++++++++++++++++++++++++++++++++++--- gm_mainmenu.cpp | 2 +- main.cpp | 2 +- product.h | 20 +++++++- tools/bazel.rc | 2 +- volatile_env.sh | 15 ++++++ 6 files changed, 159 insertions(+), 12 deletions(-) create mode 100644 volatile_env.sh diff --git a/BUILD b/BUILD index a15f6525..eddbf010 100644 --- a/BUILD +++ b/BUILD @@ -37,6 +37,15 @@ config_setting( visibility = [":__subpackages__"], ) +config_setting( + name = "stamped", + values = { + # --config=release has -c opt (compilation_mode opt) plus explicitly STAMPED_FOR_RELEASE + "define": "STAMPED_FOR_RELEASE=1", + "compilation_mode": "opt", + }, +) + config_setting( name = "use_sdl2", values = { @@ -889,6 +898,13 @@ cc_binary( "//conditions:default": [ ":enginegl", ], + }) + select({ + ":stamped": ["//:gnu_build_id.ldscript"], + "//conditions:default": [], + }), + linkopts = select({ + ":stamped": ["-Wl,@$(location //:gnu_build_id.ldscript)"], + "//conditions:default": [], }), ) @@ -956,8 +972,13 @@ cc_library( "gamemode.h", "confighandler.cpp", "confighandler.h", + "stamp.cpp", + "stamp.h", ] + glob(["*_test.cpp"]), - ), + ) + select({ + "//conditions:default": [], + ":stamped": ["stamp.cpp", "stamp.h"], + }), data = select({ "//conditions:default": [ "//translations:es_ES/LC_MESSAGES/yatc.mo", @@ -973,28 +994,33 @@ cc_library( "@tibia854//:Tibia.spr", #":YATC.pic", ], - defines = select({ + defines = [ + "BAZEL_BUILD=1", + ] + select({ "//conditions:default": [ "HAVE_LIBINTL_H=1", - "BAZEL_BUILD=1", "USE_OPENGL=1", ], - ":darwin": ["BAZEL_BUILD=1"], + ":darwin": [ + ], ":windows": [ "WIN32=1", - "BAZEL_BUILD=1", ], ":windows_msys": [ "WIN32=1", - "BAZEL_BUILD=1", ], ":windows_msvc": [ "WIN32=1", - "BAZEL_BUILD=1", ], + }) + select({ + "//conditions:default": [], + ":stamped": ["STAMPED_FOR_RELEASE=1"], }), linkopts = select({ - "//conditions:default": ["-lGLU"], + "//conditions:default": [ + "-lGLU", + # from _envoy_stamped_linkopts + ], ":darwin": [], ":windows": [ "-DEFAULTLIB:ws2_32.lib", @@ -1007,6 +1033,10 @@ cc_library( ], ":linux_deps_bin": [], # added via deps }), + linkstamp = select({ + ":stamped": "stamp.cpp", + "//conditions:default": None, + }), deps = [ ":confighandler", ":defines", @@ -1064,4 +1094,88 @@ refresh_compile_commands( # Wildcard patterns, like //... for everything, *are* allowed here, just like a build. # As are additional targets (+) and subtractions (-), like in bazel query https://docs.bazel.build/versions/main/query.html#expressions # And if you're working on a header-only library, specify a test or binary target that compiles it. + + # Easy rebuild with some flags (e.g. doing a release, and doing a keep-going): + # ~/bazelisk-linux-amd64 run --config=release -k :refresh_compile_commands -- -k --config=release +) + + +# from envoy: +# =========== +# including: https://raw.githubusercontent.com/envoyproxy/envoy/808a436ebe9c682bac81181c3ad45efd9bb24b71/bazel/volatile_env.sh +# replacing: BUILD_SCM_REVISION with BUILD_SCM_VERSION +sh_library( + name = "volatile_env", + srcs = ["volatile_env.sh"], +) + +# Stamp derived from tree hash + dirty status if `BAZEL_VOLATILE_DIRTY` +# is set, otherwise the git commit +genrule( + name = "volatile-scm-hash", + outs = ["volatile-scm-hash.txt"], + cmd = """ + grep BUILD_SCM_HASH bazel-out/volatile-status.txt > $@ + """, + stamp = 1, + tags = ["no-remote-exec"], +) + +genrule( + name = "gnu_build_id", + outs = ["gnu_build_id.ldscript"], + cmd = """ + echo --build-id=0x$$( + grep -E "^BUILD_SCM_VERSION" bazel-out/volatile-status.txt \ + | sed 's/^BUILD_SCM_VERSION //') \ + > $@ + """, + # Undocumented attr to depend on workspace status files. + # https://github.com/bazelbuild/bazel/issues/4942 + stamp = 1, +) + +# For macOS, which doesn't have GNU ld's `--build-id` flag. +genrule( + name = "raw_build_id", + outs = ["raw_build_id.ldscript"], + cmd = """ + grep -E "^BUILD_SCM_VERSION" bazel-out/volatile-status.txt \ + | sed 's/^BUILD_SCM_VERSION //' \ + | tr -d '\\n' \\ + > $@ + """, + # Undocumented attr to depend on workspace status files. + # https://github.com/bazelbuild/bazel/issues/4942 + stamp = 1, +) + +# ============== +# end from envoy + +genrule( + name = "stamp_cpp", + outs = ["stamp.cpp"], + cmd = """ +echo "#include " > $@ +echo "#include \\\"stamp.h\\\"" >> $@ +awk '{ print "std::string __yatc_stamp_" $$1 " = __YATC_STAMP_" $$1 ";"; }' bazel-out/volatile-status.txt >> $@ +awk '{ print "std::string __yatc_stamp_" $$1 " = __YATC_STAMP_" $$1 ";"; }' bazel-out/stable-status.txt >> $@ + """, + stamp=1, +) +genrule( + name = "stamp_h", + outs = ["stamp.h"], + cmd = """ +echo "#include " > $@ +echo "#ifndef __YATC_STAMP_H" >> $@ +echo "#define __YATC_STAMP_H" >> $@ +awk '{ print "extern std::string __yatc_stamp_" $$1 ";"; }' bazel-out/volatile-status.txt >> $@ +awk '{ print "extern std::string __yatc_stamp_" $$1 ";"; }' bazel-out/stable-status.txt >> $@ +sed -E 's/^([^ ]*) (.*)$$/#define __YATC_STAMP_\\1 "\\2"/' bazel-out/volatile-status.txt >> $@ +sed -E 's/^([^ ]*) (.*)$$/#define __YATC_STAMP_\\1 "\\2"/' bazel-out/stable-status.txt >> $@ +echo "#endif" >> $@ + """, + stamp=1, ) diff --git a/gm_mainmenu.cpp b/gm_mainmenu.cpp index ba4e0b99..5a04fcef 100644 --- a/gm_mainmenu.cpp +++ b/gm_mainmenu.cpp @@ -364,7 +364,7 @@ void GM_MainMenu::pnlMainMenu_btnAbout_OnClick(glictPos* relmousepos, glictConta txt << PRODUCTLONG << "\n" << PRODUCTVERSION << "\n" - << gettext("Compiled on: ") << __DATE__ << " " << __TIME__ << "\n" + << gettext("Compiled on: ") << PRODUCTDATE << " " << PRODUCTTIME << "\n" << "\n" << c <<" 2007-2010 OpenTibia Team\n" << "\n" diff --git a/main.cpp b/main.cpp index 79ae9009..97be265c 100644 --- a/main.cpp +++ b/main.cpp @@ -408,7 +408,7 @@ int main(int argc, char *argv[]) DEBUGPRINT(DEBUGPRINT_LEVEL_OBLIGATORY, DEBUGPRINT_NORMAL, PRODUCTLONG "\n"); DEBUGPRINT(DEBUGPRINT_LEVEL_OBLIGATORY, DEBUGPRINT_NORMAL, "================================\n"); DEBUGPRINT(DEBUGPRINT_LEVEL_OBLIGATORY, DEBUGPRINT_NORMAL, "version " PRODUCTVERSION "\n"); - DEBUGPRINT(DEBUGPRINT_LEVEL_OBLIGATORY, DEBUGPRINT_NORMAL, "compiled on " __DATE__ " " __TIME__ "\n"); + DEBUGPRINT(DEBUGPRINT_LEVEL_OBLIGATORY, DEBUGPRINT_NORMAL, "compiled on " PRODUCTDATE " " PRODUCTTIME "\n"); #ifdef BUILDVERSION DEBUGPRINT(DEBUGPRINT_LEVEL_OBLIGATORY, DEBUGPRINT_NORMAL, "build %s\n", BUILDVERSION); #endif diff --git a/product.h b/product.h index c556b9fd..4b429558 100644 --- a/product.h +++ b/product.h @@ -19,7 +19,17 @@ ////////////////////////////////////////////////////////////////////// #ifndef PRODUCTNAME -#define PRODUCTVERSION "0.3.3SVN" + +#define PRODUCTVERSION_BASE "0.3.3SVN" + +#ifdef STAMPED_FOR_RELEASE +#include "stamp.h" +#define PRODUCTVERSION PRODUCTVERSION_BASE " " __YATC_STAMP_STABLE_VERSION +#else +#define PRODUCTVERSION PRODUCTVERSION_BASE +#endif + + #define PRODUCTSHORT "YATC" #define PRODUCTLONG "YATC - Yet Another Tibia Client" #define PRODUCTNAME PRODUCTSHORT " v" PRODUCTVERSION @@ -33,4 +43,12 @@ #define __TIME__ "" #endif +#if (defined(__YATC_STAMP_FORMATTED_DATE)) +#define PRODUCTDATE __YATC_STAMP_FORMATTED_DATE +#define PRODUCTTIME "" +#else +#define PRODUCTDATE __DATE__ +#define PRODUCTTIME __TIME__ +#endif + #endif diff --git a/tools/bazel.rc b/tools/bazel.rc index 8563b2c3..88ca31b0 100644 --- a/tools/bazel.rc +++ b/tools/bazel.rc @@ -1,5 +1,5 @@ # Cut a stamped release. -build:release -c opt --stamp --workspace_status_command="$PWD/stamp.sh" +build:release -c opt --define STAMPED_FOR_RELEASE=1 --stamp --workspace_status_command="$PWD/stamp.sh" # Use buildbuddy by passing --config=remote. # This should be fine for this open source project. diff --git a/volatile_env.sh b/volatile_env.sh new file mode 100644 index 00000000..5c3cc4af --- /dev/null +++ b/volatile_env.sh @@ -0,0 +1,15 @@ +#!/usr/bin/env bash + +set -e + +if [[ ! -f bazel-out/volatile-status.txt ]]; then + # shellcheck disable=SC2016 + echo 'No `bazel-out/volatile-status.txt`, did you forget to stamp your build target?' >&2 + exit 1 +fi + +VOLATILE=$(cat bazel-out/volatile-status.txt) + +while read -r line ; do + export "$(echo "${line}" | cut -d' ' -f 1)=$(echo "${line}" | cut -d' ' -f 2)" +done <<< "$VOLATILE"