From 2406a2c5bc3c895c6c8656a1e271a4b473c2b585 Mon Sep 17 00:00:00 2001
From: Marko Lindqvist <cazfi74@gmail.com>
Date: Fri, 2 May 2025 05:18:02 +0300
Subject: [PATCH 37/37] Centralize exchndl.dll loading to executable.[ch]
 module.

See RM #1346

Signed-off-by: Marko Lindqvist <cazfi74@gmail.com>
---
 client/client_main.c      | 19 ++--------------
 meson.build               |  1 +
 server/srv_entrypoint.c   | 18 ++-------------
 tools/manual/fc_manual.c  | 16 ++------------
 tools/ruledit/ruledit.cpp | 18 ++-------------
 tools/ruleup.c            | 16 ++------------
 utility/Makefile.am       |  2 ++
 utility/executable.c      | 46 +++++++++++++++++++++++++++++++++++++++
 utility/executable.h      | 27 +++++++++++++++++++++++
 9 files changed, 86 insertions(+), 77 deletions(-)
 create mode 100644 utility/executable.c
 create mode 100644 utility/executable.h

diff --git a/client/client_main.c b/client/client_main.c
index 566df16860..833573107d 100644
--- a/client/client_main.c
+++ b/client/client_main.c
@@ -15,16 +15,9 @@
 #include <fc_config.h>
 #endif
 
-#include "fc_prehdrs.h"
-
-#ifdef FREECIV_MSWINDOWS
-#include <windows.h>	/* LoadLibrary() */
-#endif
-
 #include <math.h>
 #include <signal.h>
 #include <stdarg.h>
-#include <stdio.h>
 #include <stdlib.h>
 #include <time.h>
 
@@ -33,6 +26,7 @@
 #include "capstr.h"
 #include "dataio.h"
 #include "deprecations.h"
+#include "executable.h"
 #include "fcbacktrace.h"
 #include "fc_cmdline.h"
 #include "fciconv.h"
@@ -363,16 +357,7 @@ int client_main(int argc, char *argv[], bool postpone_tileset)
   int aii;
   int uret;
 
-  /* Load Windows post-crash debugger */
-#ifdef FREECIV_MSWINDOWS
-# ifndef FREECIV_NDEBUG
-  if (LoadLibrary("exchndl.dll") == NULL) {
-#  ifdef FREECIV_DEBUG
-    fprintf(stderr, "exchndl.dll could not be loaded, no crash debugger\n");
-#  endif /* FREECIV_DEBUG */
-  }
-# endif /* FREECIV_NDEBUG */
-#endif /* FREECIV_MSWINDOWS */
+  executable_init();
 
   /* fc_interface_init_client() includes low level support like
    * guaranteeing that fc_vsnprintf() will work after it,
diff --git a/meson.build b/meson.build
index db141656d5..8828bb6995 100644
--- a/meson.build
+++ b/meson.build
@@ -1363,6 +1363,7 @@ common_lib = library('freeciv',
   'utility/capability.c',
   'utility/deprecations.c',
   'utility/distribute.c',
+  'utility/executable.c',
   'utility/fcbacktrace.c',
   'utility/fc_cmdhelp.c',
   'utility/fc_cmdline.c',
diff --git a/server/srv_entrypoint.c b/server/srv_entrypoint.c
index 3ac5471e74..f944b0af5a 100644
--- a/server/srv_entrypoint.c
+++ b/server/srv_entrypoint.c
@@ -17,7 +17,6 @@
 
 #include "fc_prehdrs.h"
 
-#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 
@@ -29,12 +28,9 @@
 #include <signal.h>
 #endif
 
-#ifdef FREECIV_MSWINDOWS
-#include <windows.h>
-#endif
-
 /* utility */
 #include "deprecations.h"
+#include "executable.h"
 #include "fc_cmdline.h"
 #include "fciconv.h"
 #include "fcintl.h"
@@ -67,17 +63,7 @@ int main(int argc, char *argv[])
   bool showvers = FALSE;
   char *option = NULL;
 
-  /* Load Windows post-crash debugger */
-#ifdef FREECIV_MSWINDOWS
-# ifndef FREECIV_NDEBUG
-  if (LoadLibrary("exchndl.dll") == NULL) {
-#  ifdef FREECIV_DEBUG
-    fprintf(stderr, "exchndl.dll could not be loaded, no crash debugger\n");
-#  endif /* FREECIV_DEBUG */
-  }
-# endif /* FREECIV_NDEBUG */
-#endif /* FREECIV_MSWINDOWS */
-
+  executable_init();
   setup_interrupt_handlers();
 
   /* Initialize server */
diff --git a/tools/manual/fc_manual.c b/tools/manual/fc_manual.c
index 30ae166049..a35bb063eb 100644
--- a/tools/manual/fc_manual.c
+++ b/tools/manual/fc_manual.c
@@ -23,12 +23,9 @@
 #include <signal.h>
 #endif
 
-#ifdef FREECIV_MSWINDOWS
-#include <windows.h>
-#endif
-
 /* utility */
 #include "capability.h"
+#include "executable.h"
 #include "fc_cmdline.h"
 #include "fciconv.h"
 #include "fcintl.h"
@@ -366,16 +363,7 @@ int main(int argc, char **argv)
   int retval = EXIT_SUCCESS;
   struct tag_types *tag_info = &html_tags;
 
-  /* Load Windows post-crash debugger */
-#ifdef FREECIV_MSWINDOWS
-# ifndef FREECIV_NDEBUG
-  if (LoadLibrary("exchndl.dll") == NULL) {
-#  ifdef FREECIV_DEBUG
-    fprintf(stderr, "exchndl.dll could not be loaded, no crash debugger\n");
-#  endif /* FREECIV_DEBUG */
-  }
-# endif /* FREECIV_NDEBUG */
-#endif /* FREECIV_MSWINDOWS */
+  executable_init();
 
   /* Initialize the fc_interface functions needed to generate the help
    * text.
diff --git a/tools/ruledit/ruledit.cpp b/tools/ruledit/ruledit.cpp
index bced574a78..fdb8c03daf 100644
--- a/tools/ruledit/ruledit.cpp
+++ b/tools/ruledit/ruledit.cpp
@@ -15,18 +15,13 @@
 #include <fc_config.h>
 #endif
 
-#include "fc_prehdrs.h"
-
 // ANSI
 #include <stdlib.h>
 
 #include <signal.h>
 
-#ifdef FREECIV_MSWINDOWS
-#include <windows.h>
-#endif
-
 // utility
+#include "executable.h"
 #include "fc_cmdline.h"
 #include "fciconv.h"
 #include "fcintl.h"
@@ -65,16 +60,7 @@ int main(int argc, char **argv)
   int ui_options;
   int exit_status = EXIT_SUCCESS;
 
-  // Load Windows post-crash debugger
-#ifdef FREECIV_MSWINDOWS
-# ifndef FREECIV_NDEBUG
-  if (LoadLibrary("exchndl.dll") == nullptr) {
-#  ifdef FREECIV_DEBUG
-    fprintf(stderr, "exchndl.dll could not be loaded, no crash debugger\n");
-#  endif // FREECIV_DEBUG
-  }
-# endif // FREECIV_NDEBUG
-#endif // FREECIV_MSWINDOWS
+  executable_init();
 
   /* Initialize the fc_interface functions needed to understand rules.
    * fc_interface_init_tool() includes low level support like
diff --git a/tools/ruleup.c b/tools/ruleup.c
index d51ab8a032..644b6ef9b7 100644
--- a/tools/ruleup.c
+++ b/tools/ruleup.c
@@ -17,11 +17,8 @@
 
 #include <signal.h>
 
-#ifdef FREECIV_MSWINDOWS
-#include <windows.h>
-#endif
-
 /* utility */
+#include "executable.h"
 #include "fc_cmdline.h"
 #include "fciconv.h"
 #include "registry.h"
@@ -143,16 +140,7 @@ int main(int argc, char **argv)
   enum log_level loglevel = LOG_NORMAL;
   int exit_status = EXIT_SUCCESS;
 
-  /* Load Windows post-crash debugger */
-#ifdef FREECIV_MSWINDOWS
-# ifndef FREECIV_NDEBUG
-  if (LoadLibrary("exchndl.dll") == NULL) {
-#  ifdef FREECIV_DEBUG
-    fprintf(stderr, "exchndl.dll could not be loaded, no crash debugger\n");
-#  endif /* FREECIV_DEBUG */
-  }
-# endif /* FREECIV_NDEBUG */
-#endif /* FREECIV_MSWINDOWS */
+  executable_init();
 
   /* Initialize the fc_interface functions needed to understand rules.
    * fc_interface_init_tool() includes low level support like
diff --git a/utility/Makefile.am b/utility/Makefile.am
index 6cb4d47c5d..ab5f29593a 100644
--- a/utility/Makefile.am
+++ b/utility/Makefile.am
@@ -19,6 +19,8 @@ libcivutility_la_SOURCES = \
 		deprecations.h	\
 		distribute.c	\
 		distribute.h	\
+		executable.c	\
+		executable.h	\
 		fc_cmdhelp.c	\
 		fc_cmdhelp.h	\
 		fc_cmdline.c	\
diff --git a/utility/executable.c b/utility/executable.c
new file mode 100644
index 0000000000..fe034e17d1
--- /dev/null
+++ b/utility/executable.c
@@ -0,0 +1,46 @@
+/***********************************************************************
+ Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2, or (at your option)
+   any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+***********************************************************************/
+
+#ifdef HAVE_CONFIG_H
+#include <fc_config.h>
+#endif /* HAVE_CONFIG_H */
+
+#include "fc_prehdrs.h"
+
+#ifdef FREECIV_MSWINDOWS
+#include <windows.h>
+#endif
+
+#include <stdio.h>
+
+/* utility */
+#include "support.h"
+
+#include "executable.h"
+
+/********************************************************************//**
+  Common part of executable initialization
+************************************************************************/
+void executable_init(void)
+{
+  /* Load Windows post-crash debugger */
+#ifdef FREECIV_MSWINDOWS
+# ifndef FREECIV_NDEBUG
+  if (LoadLibrary("exchndl.dll") == nullptr) {
+#  ifdef FREECIV_DEBUG
+    fprintf(stderr, "exchndl.dll could not be loaded, no crash debugger\n");
+#  endif /* FREECIV_DEBUG */
+  }
+# endif /* FREECIV_NDEBUG */
+#endif /* FREECIV_MSWINDOWS */
+}
diff --git a/utility/executable.h b/utility/executable.h
new file mode 100644
index 0000000000..fb5f688eae
--- /dev/null
+++ b/utility/executable.h
@@ -0,0 +1,27 @@
+/***********************************************************************
+ Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2, or (at your option)
+   any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+***********************************************************************/
+
+#ifndef FC__EXECUTABLE_H
+#define FC__EXECUTABLE_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
+void executable_init(void);
+
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+
+#endif /* FC__EXECUTABLE_H */
-- 
2.47.2

