Skip to content

Commit

Permalink
bazel: Stamping the product.h with build time and commit hash. Stampi…
Browse files Browse the repository at this point in the history
…ng the elf binary with a build ID.
  • Loading branch information
ivucica committed May 3, 2024
1 parent a38c20b commit cf531ae
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 12 deletions.
130 changes: 122 additions & 8 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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": [],
}),
)

Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -1007,6 +1033,10 @@ cc_library(
],
":linux_deps_bin": [], # added via deps
}),
linkstamp = select({
":stamped": "stamp.cpp",
"//conditions:default": None,
}),
deps = [
":confighandler",
":defines",
Expand Down Expand Up @@ -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 <string>" > $@
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 <string>" > $@
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,
)
2 changes: 1 addition & 1 deletion gm_mainmenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 19 additions & 1 deletion product.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
2 changes: 1 addition & 1 deletion tools/bazel.rc
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
15 changes: 15 additions & 0 deletions volatile_env.sh
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit cf531ae

Please sign in to comment.