From 05c7ee4c1a187d5a0c2077e39ff499d53c393560 Mon Sep 17 00:00:00 2001
From: Marko Lindqvist <cazfi74@gmail.com>
Date: Sat, 23 Nov 2024 03:36:24 +0200
Subject: [PATCH 27/45] Meson: Add mariadb fcdb support

- Add 'fcdb' configure option
- Support value 'mariadb' for it

See RM #1152

Signed-off-by: Marko Lindqvist <cazfi74@gmail.com>
---
 doc/INSTALL.meson                |  5 +++
 meson.build                      | 62 +++++++++++++++++++++++---------
 meson_options.txt                |  6 ++++
 platforms/emscripten/emsbuild.sh |  1 +
 4 files changed, 58 insertions(+), 16 deletions(-)

diff --git a/doc/INSTALL.meson b/doc/INSTALL.meson
index d5a1084c4a..551c930121 100644
--- a/doc/INSTALL.meson
+++ b/doc/INSTALL.meson
@@ -200,6 +200,11 @@ svgflags (boolean)
   Add support for svg flags features on clients where this is supported,
   currently only Qt.
 
+fcdb (array):
+  fcdb (player authentication) backends to build.
+  Possible backends are sqlite3 and mariadb.
+  The default is sqlite3.
+
 
 Project definition file
 -----------------------
diff --git a/meson.build b/meson.build
index ac7010ae8f..fd40f34df9 100644
--- a/meson.build
+++ b/meson.build
@@ -653,8 +653,6 @@ if emscripten
      '-s', 'TOTAL_MEMORY=64MB',
      language : [ 'c', 'cpp'])
 else
-  priv_conf_data.set('HAVE_FCDB', 1)
-  priv_conf_data.set('HAVE_FCDB_SQLITE3', 1)
   zlib_dep = c_compiler.find_library('z', dirs: cross_lib_path)
   if not c_compiler.has_header('zlib.h', args: header_arg)
     error('Mandatory header zlib.h not found!')
@@ -967,16 +965,9 @@ else
 endif
 
 if emscripten
-  sqlite3_dep = []
-  sqlite3_src = []
   curl_dep = []
   m_dep = []
 else
-  sqlite3_dep = c_compiler.find_library('sqlite3', dirs: cross_lib_path)
-  sqlite3_src = [
-    'dependencies/luasql/src/luasql.c',
-    'dependencies/luasql/src/ls_sqlite3.c',
-  ]
   curl_dep = c_compiler.find_library('curl', dirs: cross_lib_path)
   m_dep = c_compiler.find_library('m', dirs: cross_lib_path)
 endif
@@ -995,6 +986,42 @@ int main(void) { curl_mime *mime = curl_mime_init(NULL); }''',
   endif
 endif
 
+if not emscripten
+  sqlite3_dep = c_compiler.find_library('sqlite3', dirs: cross_lib_path)
+else
+  sqlite3_dep = []
+endif
+
+fcdb = false
+fcdb_src = []
+fcdb_dep = []
+
+if get_option('fcdb').contains('sqlite3')
+  fcdb = true
+  priv_conf_data.set('HAVE_FCDB_SQLITE3', 1)
+  fcdb_src = [ fcdb_src,
+    'dependencies/luasql/src/ls_sqlite3.c',
+  ]
+  fcdb_dep = [ fcdb_dep, sqlite3_dep ]
+endif
+
+if get_option('fcdb').contains('mariadb')
+  fcdb = true
+  priv_conf_data.set('HAVE_FCDB_MYSQL', 1)
+  fcdb_src = [ fcdb_src,
+    'dependencies/luasql/src/ls_mysql.c',
+  ]
+  fcdb_dep = [ fcdb_dep, dependency('mariadb') ]
+endif
+
+if fcdb
+  if emscripten
+    error('FCDB not supported on emscripten builds.')
+  endif
+  priv_conf_data.set('HAVE_FCDB', 1)
+  fcdb_src = [ 'dependencies/luasql/src/luasql.c', fcdb_src ]
+endif
+
 if get_option('gitrev')
   priv_conf_data.set('GITREV', 1)
 endif
@@ -1211,10 +1238,10 @@ fc_deps = static_library('fc_dependencies',
   'dependencies/tolua-5.2/src/lib/tolua_map.c',
   'dependencies/tolua-5.2/src/lib/tolua_push.c',
   'dependencies/tolua-5.2/src/lib/tolua_to.c',
-  sqlite3_src, tinycthr_files,
+  fcdb_src, tinycthr_files,
   sources: [verhdr],
   include_directories : common_inc,
-  dependencies: lua_dep
+  dependencies: [ lua_dep, fcdb_dep ]
   )
 
 if meson.is_cross_build() or get_option('sys-tolua-cmd')
@@ -1408,7 +1435,7 @@ common_lib = library('freeciv',
             tolua_com_a, tolua_com_z, tolua_game, tolua_signal],
   link_whole: fc_deps,
   dependencies: [zlib_dep,
-                 curl_dep, m_dep, sqlite3_dep, icu_dep,
+                 curl_dep, m_dep, icu_dep,
                  net_dep, jansson_dep, lua_dep, bz2_dep, lzma_dep, zstd_dep,
                  bcrypt_lib_dep, iconv_lib_dep,
                  gettext_dep, charset_dep, mw_dep,
@@ -1556,7 +1583,8 @@ if server_type != 'disabled'
     servericon,
     include_directories: server_inc,
     link_with: [server_lib, common_lib, ais],
-    dependencies: [m_dep, net_dep, readline_dep, gettext_dep, mw_extra_dep],
+    dependencies: [m_dep, net_dep, readline_dep, gettext_dep, fcdb_dep,
+                   mw_extra_dep],
     install: true,
     win_subsystem: 'console'
     )
@@ -4368,7 +4396,8 @@ executable('freeciv-ruleup',
   'tools/ruleup.c',
   link_with: [common_lib, server_lib, tool_lib, ais],
   include_directories: tool_inc,
-  dependencies: [m_dep, net_dep, readline_dep, gettext_dep, mw_extra_dep],
+  dependencies: [m_dep, net_dep, readline_dep, gettext_dep, fcdb_dep,
+                 mw_extra_dep],
   install: true,
   win_subsystem: 'console'
   )
@@ -4452,7 +4481,7 @@ executable('freeciv-ruledit',
   mocced_ruledit, rulediticon,
   include_directories: tool_inc,
   dependencies: [qt_dep, m_dep, net_dep, readline_dep, gettext_dep,
-                 mw_extra_dep],
+                 fcdb_dep, mw_extra_dep],
   link_with: [common_lib, server_lib, ais, tool_lib],
   override_options: qt_opts,
   install: true,
@@ -4498,7 +4527,8 @@ executable('freeciv-manual',
   link_with: [common_lib, server_lib, tool_lib, ais],
   include_directories: [tool_inc,
                         include_directories('client', 'client/include')],
-  dependencies: [m_dep, net_dep, readline_dep, gettext_dep, mw_extra_dep],
+  dependencies: [m_dep, net_dep, readline_dep, gettext_dep, fcdb_dep,
+                 mw_extra_dep],
   install: true,
   win_subsystem: 'console'
   )
diff --git a/meson_options.txt b/meson_options.txt
index 82d3f26ca0..80ab3b8aec 100644
--- a/meson_options.txt
+++ b/meson_options.txt
@@ -113,3 +113,9 @@ option('python',
        type: 'string',
        value: 'python3',
        description: 'Python interpreter to use')
+
+option('fcdb',
+       type: 'array',
+       choices: ['sqlite3','mariadb'],
+       value: ['sqlite3'],
+       description: 'fcdb (player authentication) backends to build')
diff --git a/platforms/emscripten/emsbuild.sh b/platforms/emscripten/emsbuild.sh
index 14a9eb4924..fef45a7621 100755
--- a/platforms/emscripten/emsbuild.sh
+++ b/platforms/emscripten/emsbuild.sh
@@ -54,6 +54,7 @@ if ! CC=emcc CXX=em++ AR=emar meson setup \
      -Dtools=[] \
      -Dclients=stub,sdl2 \
      -Dfcmp=[] \
+     -Dfcdb=[] \
      "${PLATFORM_ROOT}/../../"
 then
   echo "Setup with meson failed!" >&2
-- 
2.45.2

