From 1dc497a0d8e77df7e3b2b52aaede94d63c920b68 Mon Sep 17 00:00:00 2001
From: Marko Lindqvist <cazfi74@gmail.com>
Date: Sat, 7 Dec 2024 22:52:34 +0200
Subject: [PATCH 49/49] Autotools: Check if var arg can be const

See RM #1178

Signed-off-by: Marko Lindqvist <cazfi74@gmail.com>
---
 common/featured_text.c          |  2 +-
 common/featured_text.h          |  2 +-
 configure.ac                    |  3 ++-
 gen_headers/freeciv_config.h.in |  3 +++
 m4/tcc.m4                       | 18 ++++++++++++++++++
 utility/support.h               |  6 ++++++
 6 files changed, 31 insertions(+), 3 deletions(-)

diff --git a/common/featured_text.c b/common/featured_text.c
index b60d6ebcce..57f352d33c 100644
--- a/common/featured_text.c
+++ b/common/featured_text.c
@@ -78,7 +78,7 @@ const struct ft_color ftc_server        = FT_COLOR("#8B0000",   nullptr);
 const struct ft_color ftc_client        = FT_COLOR("#EF7F00",   nullptr);
 const struct ft_color ftc_editor        = FT_COLOR("#0000FF",   nullptr);
 const struct ft_color ftc_command       = FT_COLOR("#006400",   nullptr);
-const struct ft_color ftc_changed       = FT_COLOR("#FF0000",   nullptr);
+VAR_ARG_CONST struct ft_color ftc_changed = FT_COLOR("#FF0000", nullptr);
 const struct ft_color ftc_server_prompt = FT_COLOR("#FF0000",   "#BEBEBE");
 const struct ft_color ftc_player_lost   = FT_COLOR("#FFFFFF",   "#000000");
 const struct ft_color ftc_game_start    = FT_COLOR("#00FF00",   "#115511");
diff --git a/common/featured_text.h b/common/featured_text.h
index 4d6e9df469..6e0f0e2431 100644
--- a/common/featured_text.h
+++ b/common/featured_text.h
@@ -172,7 +172,7 @@ extern const struct ft_color ftc_server;
 extern const struct ft_color ftc_client;
 extern const struct ft_color ftc_editor;
 extern const struct ft_color ftc_command;
-extern const struct ft_color ftc_changed;
+extern VAR_ARG_CONST struct ft_color ftc_changed;
 extern const struct ft_color ftc_server_prompt;
 extern const struct ft_color ftc_player_lost;
 extern const struct ft_color ftc_game_start;
diff --git a/configure.ac b/configure.ac
index 1a6b9fe441..f4a52a8e8e 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1828,8 +1828,9 @@ CFLAGS="$EXTRA_DEBUG_CFLAGS $CFLAGS"
 CXXFLAGS="$EXTRA_DEBUG_CXXFLAGS $CXXFLAGS"
 LDFLAGS="$EXTRA_DEBUG_LDFLAGS $LDFLAGS"
 
-dnl Check this when -Werror is already set, if we're going to set it
+dnl Check these when -Werror is already set, if we're going to set it
 FC_VALUE_AFTER_EXIT
+FC_CONST_VAR_ARG
 
 dnl Rebuild 'configure' whenever fc_version changes, if maintainer mode enabled.
 AC_SUBST([CONFIGURE_DEPENDENCIES], ["$CONFIGURE_DEPENDENCIES \$(top_srcdir)/fc_version"])
diff --git a/gen_headers/freeciv_config.h.in b/gen_headers/freeciv_config.h.in
index 9c7e9e7743..288d7fd075 100644
--- a/gen_headers/freeciv_config.h.in
+++ b/gen_headers/freeciv_config.h.in
@@ -71,6 +71,9 @@
 /* Returning a value is expected even after exit() call */
 #undef FREECIV_RETURN_VALUE_AFTER_EXIT
 
+/* Var arg cannot be const */
+#undef FREECIV_NO_CONST_VAR_ARG
+
 /* locale.h available */
 #undef FREECIV_HAVE_LOCALE_H
 
diff --git a/m4/tcc.m4 b/m4/tcc.m4
index 085c2d7d14..da0f6199b6 100644
--- a/m4/tcc.m4
+++ b/m4/tcc.m4
@@ -60,3 +60,21 @@ struct outer
     AC_DEFINE([INIT_BRACE_END], [], [End of C99 structure initializer])
   fi
 ])
+
+# Are const var arg parameters supported?
+#
+AC_DEFUN([FC_CONST_VAR_ARG],
+[
+AC_CACHE_CHECK([can var arg be a const],
+  [ac_cv_const_var_arg],
+  [AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <stddef.h>
+void var_arg_func(...);]],
+  [[const char *param = NULL;
+var_arg_func(param);
+return 0;
+]])],
+  [ac_cv_const_var_arg=yes], [ac_cv_const_var_arg=no])])
+  if test "x${ac_cv_const_var_arg}" != "xyes" ; then
+    AC_DEFINE([FREECIV_NO_CONST_VAR_ARG], [1], [[Var arg cannot be const]])
+  fi
+])
diff --git a/utility/support.h b/utility/support.h
index 9524761ebb..16c2be1737 100644
--- a/utility/support.h
+++ b/utility/support.h
@@ -146,6 +146,12 @@ typedef int fc_errno;
 #define RETURN_VALUE_AFTER_EXIT(_val_)
 #endif
 
+#ifdef FREECIV_NO_CONST_VAR_ARG
+#define VAR_ARG_CONST
+#else
+#define VAR_ARG_CONST const
+#endif
+
 int fc_strcasecmp(const char *str0, const char *str1);
 int fc_strncasecmp(const char *str0, const char *str1, size_t n);
 int fc_strncasequotecmp(const char *str0, const char *str1, size_t n);
-- 
2.45.2

