From b066c0affb091ecd70e7e7f12695159479d74c8a Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Mon, 20 Jan 2025 17:49:55 +0100 Subject: [PATCH] REORG: version: move the remaining BUILD_* stuff from haproxy.c to version.c version.c tries to centralize all variables conveying version information, but there's still an issue with the BUILD_* variables which are only passed to haproxy.o and are only updated when that one is rebuilt. This is not very logical given that we can end up with values there which contradict info from version.c. Better move all of these to version.c which is systematically rebuilt. Most of these variables only end up as string concatenation at the moment. Some of them are even duplicated. In version.c we now have one variable (or constant) for each of them and haproxy.c references them in messages. This is much more logical and easier to maintain in a consistent state. The patch looks a bit large but it really only moves the ifdefed string assignment from one file to another, placing them into variables. --- Makefile | 2 +- include/haproxy/global.h | 1 - include/haproxy/version.h | 4 +++ src/cfgcond.c | 2 +- src/haproxy.c | 61 +++++---------------------------------- src/version.c | 49 +++++++++++++++++++++++++++++++ 6 files changed, 62 insertions(+), 57 deletions(-) diff --git a/Makefile b/Makefile index 9bf63a280..d128251e1 100644 --- a/Makefile +++ b/Makefile @@ -1080,7 +1080,7 @@ dev/udp/udp-perturb: dev/udp/udp-perturb.o src/calltrace.o: src/calltrace.c $(DEP) $(cmd_CC) $(TRACE_COPTS) -c -o $@ $< -src/haproxy.o: src/haproxy.c $(DEP) +src/version.o: src/version.c $(DEP) $(cmd_CC) $(COPTS) \ -DBUILD_TARGET='"$(strip $(TARGET))"' \ -DBUILD_CC='"$(strip $(CC))"' \ diff --git a/include/haproxy/global.h b/include/haproxy/global.h index ba0523cbf..26cba4ac4 100644 --- a/include/haproxy/global.h +++ b/include/haproxy/global.h @@ -25,7 +25,6 @@ #include #include -extern char *build_features; extern struct global global; extern int pid; /* current process id */ extern int actconn; /* # of active sessions */ diff --git a/include/haproxy/version.h b/include/haproxy/version.h index cca4fc159..59c62735e 100644 --- a/include/haproxy/version.h +++ b/include/haproxy/version.h @@ -81,6 +81,10 @@ extern char haproxy_version[]; extern char haproxy_date[]; extern char stats_version_string[]; +extern char build_opts_string[]; +extern const char pm_target_opts[]; +extern const char pm_toolchain_opts[]; +extern char *build_features; #endif /* _HAPROXY_VERSION_H */ diff --git a/src/cfgcond.c b/src/cfgcond.c index 117cf6c28..f01638df4 100644 --- a/src/cfgcond.c +++ b/src/cfgcond.c @@ -13,9 +13,9 @@ #include #include #include -#include #include #include +#include /* supported condition predicates */ const struct cond_pred_kw cond_predicates[] = { diff --git a/src/haproxy.c b/src/haproxy.c index 52d58e067..7ee654b07 100644 --- a/src/haproxy.c +++ b/src/haproxy.c @@ -142,12 +142,6 @@ DECLARE_INIT_STAGES; */ empty_t __read_mostly_align HA_SECTION("read_mostly") ALIGNED(64); -#ifdef BUILD_FEATURES -char *build_features = BUILD_FEATURES; -#else -char *build_features = ""; -#endif - /* list of config files */ static struct list cfg_cfgfiles = LIST_HEAD_INIT(cfg_cfgfiles); int pid; /* current process id */ @@ -561,26 +555,12 @@ static void display_build_opts() { const char **opt; - printf("Build options :" -#ifdef BUILD_TARGET - "\n TARGET = " BUILD_TARGET -#endif -#ifdef BUILD_CC - "\n CC = " BUILD_CC -#endif -#ifdef BUILD_CFLAGS - "\n CFLAGS = " BUILD_CFLAGS -#endif -#ifdef BUILD_OPTIONS - "\n OPTIONS = " BUILD_OPTIONS -#endif -#ifdef BUILD_DEBUG - "\n DEBUG = " BUILD_DEBUG -#endif + printf("Build options : %s" "\n\nFeature list : %s" "\n\nDefault settings :" "\n bufsize = %d, maxrewrite = %d, maxpollevents = %d" "\n\n", + build_opts_string, build_features, BUFSIZE, MAXREWRITE, MAX_POLL_EVENTS); for (opt = NULL; (opt = hap_get_next_build_opt(opt)); puts(*opt)) @@ -2287,23 +2267,11 @@ static void step_init_2(int argc, char** argv) #endif /* toolchain opts */ cflags = chunk_newstr(&trash); -#ifdef BUILD_CC - chunk_appendf(&trash, "%s", BUILD_CC); -#endif -#ifdef BUILD_CFLAGS - chunk_appendf(&trash, " %s", BUILD_CFLAGS); -#endif -#ifdef BUILD_DEBUG - chunk_appendf(&trash, " %s", BUILD_DEBUG); -#endif + chunk_appendf(&trash, "%s", pm_toolchain_opts); + /* settings */ opts = chunk_newstr(&trash); -#ifdef BUILD_TARGET - chunk_appendf(&trash, "TARGET='%s'", BUILD_TARGET); -#endif -#ifdef BUILD_OPTIONS - chunk_appendf(&trash, " %s", BUILD_OPTIONS); -#endif + chunk_appendf(&trash, "TARGET='%s'", pm_target_opts); post_mortem_add_component("haproxy", haproxy_version, cc, cflags, opts, argv[0]); } @@ -3081,23 +3049,8 @@ int main(int argc, char **argv) fprintf(stderr, "FATAL ERROR: invalid code detected -- cannot go further, please recompile!\n" "%s" - "\nBuild options :" -#ifdef BUILD_TARGET - "\n TARGET = " BUILD_TARGET -#endif -#ifdef BUILD_CC - "\n CC = " BUILD_CC -#endif -#ifdef BUILD_CFLAGS - "\n CFLAGS = " BUILD_CFLAGS -#endif -#ifdef BUILD_OPTIONS - "\n OPTIONS = " BUILD_OPTIONS -#endif -#ifdef BUILD_DEBUG - "\n DEBUG = " BUILD_DEBUG -#endif - "\n\n", msg); + "\nBuild options :%s" + "\n\n", msg, build_opts_string); return 1; } diff --git a/src/version.c b/src/version.c index e7bb748f8..03f26e825 100644 --- a/src/version.c +++ b/src/version.c @@ -15,6 +15,55 @@ char haproxy_version[] = HAPROXY_VERSION; char haproxy_date[] = HAPROXY_DATE; char stats_version_string[] = STATS_VERSION_STRING; +/* the build options string depending on known settings */ +char build_opts_string[] = "" +#ifdef BUILD_TARGET + "\n TARGET = " BUILD_TARGET +#endif +#ifdef BUILD_CC + "\n CC = " BUILD_CC +#endif +#ifdef BUILD_CFLAGS + "\n CFLAGS = " BUILD_CFLAGS +#endif +#ifdef BUILD_OPTIONS + "\n OPTIONS = " BUILD_OPTIONS +#endif +#ifdef BUILD_DEBUG + "\n DEBUG = " BUILD_DEBUG +#endif + ""; + +/* compact string of toolchain options for post-mortem */ +const char pm_toolchain_opts[] = "" +#ifdef BUILD_CC + BUILD_CC +#endif +#ifdef BUILD_CFLAGS + " " BUILD_CFLAGS +#endif +#ifdef BUILD_DEBUG + " " BUILD_DEBUG +#endif + ""; + +/* compact string of target options for post-mortem */ +const char pm_target_opts[] = "" +#ifdef BUILD_TARGET + "TARGET='" BUILD_TARGET "'" +#endif +#ifdef BUILD_OPTIONS + " " BUILD_OPTIONS +#endif + ""; + +/* Build features may be passed by the makefile */ +#ifdef BUILD_FEATURES +char *build_features = BUILD_FEATURES; +#else +char *build_features = ""; +#endif + #if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__) #define SANITIZE_STRING " with address sanitizer" #else