From 38eb79c27eada091b10466a5cb3eb8393a01693c Mon Sep 17 00:00:00 2001
From: Alfred Wingate <parona@protonmail.com>
Date: Thu, 26 Dec 2024 22:08:21 +0200
Subject: [PATCH 1/5] Add library specific version scripts that match enabled
 features

LLD is stricter with version scripts and will complain about nonexisting
symbols in version files.

Bump meson reguirement due to use of compiler_args in preprocess.

Signed-off-by: Alfred Wingate <parona@protonmail.com>
--- a/meson.build
+++ b/meson.build
@@ -2,7 +2,7 @@ project('MangoHud',
   ['c', 'cpp'],
   version : 'v0.8.1',
   license : 'MIT',
-  meson_version: '>=0.60.0',
+  meson_version: '>=1.3.2',
   default_options : ['buildtype=release', 'c_std=c99', 'cpp_std=c++17', 'warning_level=2']
 )
 
--- a/src/mangohud.version
+++ b/src/mangohud.version
@@ -3,10 +3,5 @@
   global:
     overlay_GetInstanceProcAddr;
     overlay_GetDeviceProcAddr;
-    glX*;
-    egl*;
-    dlsym;
-    mangohud_find_glx_ptr;
-    mangohud_find_egl_ptr;
   local: *;
 };
--- /dev/null
+++ b/src/mangohud_opengl.version
@@ -0,0 +1,10 @@
+{
+    global:
+        #if defined(HAVE_X11)
+        mangohud_find_glx_ptr;
+        #endif
+        #if defined(HAVE_WAYLAND)
+        mangohud_find_egl_ptr;
+        #endif
+    local: *;
+};
--- a/src/meson.build
+++ b/src/meson.build
@@ -177,8 +177,6 @@ if is_unixy
 endif
 
 link_args = cc.get_supported_link_arguments(['-Wl,-Bsymbolic-functions', '-Wl,-z,relro', '-Wl,--exclude-libs,ALL', '-lGL', '-static-libstdc++'])
-# meson fails to check version-script so just force add
-link_args += '-Wl,--version-script,@0@'.format(join_paths(meson.current_source_dir(), 'mangohud.version'))
 
 mangohud_static_lib = static_library(
   'MangoHud',
@@ -219,11 +217,17 @@ mangohud_shared_lib = shared_library(
   'MangoHud',
   objects: mangohud_static_lib.extract_all_objects(),
   link_with: mangohud_static_lib,
-  link_args : link_args,
+  link_args : [link_args, '-Wl,--version-script,@0@'.format(
+    join_paths(meson.current_source_dir(), 'mangohud.version'))],
   install_dir : libdir_mangohud,
   install: true
 )
 
+mangohud_opengl_versionscript = cpp.preprocess(
+  'mangohud_opengl.version',
+  compile_args: pre_args,
+)
+
 mangohud_opengl_shared_lib = shared_library(
   'MangoHud_opengl',
   mangohud_version,
@@ -251,8 +255,9 @@ mangohud_opengl_shared_lib = shared_library(
     windows_deps,
     implot_dep],
   include_directories : [inc_common],
-  link_args : link_args,
+  link_args : [link_args, '-Wl,--version-script,@0@'.format(mangohud_opengl_versionscript[0].full_path())],
   link_with: mangohud_static_lib,
+  link_depends: mangohud_opengl_versionscript,
   install_dir : libdir_mangohud,
   install: true
 )
-- 
2.51.2

From 313d8d61522c94f60b6bd5878599818ecb0dda6b Mon Sep 17 00:00:00 2001
From: Alfred Wingate <parona@protonmail.com>
Date: Thu, 26 Dec 2024 23:01:54 +0200
Subject: [PATCH 2/5] Do not append -static-libc++ to linker_args when libcxx
 is used

Clang has a long standing bug where it will try to mix c++ libraries
instead of erroring out early or transparently linking against libcxx
statically instead.

Signed-off-by: Alfred Wingate <parona@protonmail.com>
--- a/src/meson.build
+++ b/src/meson.build
@@ -176,7 +176,23 @@ if is_unixy
   endif
 endif
 
-link_args = cc.get_supported_link_arguments(['-Wl,-Bsymbolic-functions', '-Wl,-z,relro', '-Wl,--exclude-libs,ALL', '-lGL', '-static-libstdc++'])
+link_args = cpp.get_supported_link_arguments(['-Wl,-Bsymbolic-functions', '-Wl,-z,relro', '-Wl,--exclude-libs,ALL', '-lGL'])
+
+# Approach for libc++ detection taken from https://github.com/mesonbuild/meson/commit/675b47b0692131fae974298829ba807d730ab098
+# Rationale for the approach is explained in this SO answer https://stackoverflow.com/a/31658120
+if cpp.has_header('<version>')
+  header = 'version'
+else
+  header = 'ciso646'
+endif
+is_libcxx = cpp.has_header_symbol(header, '_LIBCPP_VERSION')
+
+# https://github.com/llvm/llvm-project/issues/38622
+if is_libcxx
+  message('Detected libcxx, not appending -static-libstc++ to linker arguments')
+else
+  link_args+=['-static-libstdc++']
+endif
 
 mangohud_static_lib = static_library(
   'MangoHud',
-- 
2.51.2

From 9521f9c90254903b52616dd585f66c65d0d34fd9 Mon Sep 17 00:00:00 2001
From: Alfred Wingate <parona@protonmail.com>
Date: Thu, 26 Dec 2024 23:33:17 +0200
Subject: [PATCH 3/5] Inherit cmocka after c++ system headers due to symbol
 conflicts

LLVM alternative C++ standard library is happier if it can hide its own
symbols first without preprocessor macros complicating things.

FAILED: amdgpu.p/tests_test_amdgpu.cpp.o
clang++ -Iamdgpu.p -I. -I.. -I../include -I../subprojects/spdlog-1.14.1/include -Isubprojects/implot-0.16 -I../subprojects/implot-0.16 -Isubprojects/imgui-1.89.9 -I../subprojects/imgui-1.89.9 -I../subprojects/imgui-1.89.9/backends -fdiagnostics-color=always -D_FILE_OFFSET_BITS=64 -Wall -Winvalid-pch -Wextra -std=c++14 -O3 -Werror=missing-declarations -Werror=return-type -Wno-unused-parameter -Qunused-arguments -fno-math-errno -fno-trapping-math -Wno-non-virtual-dtor -Wno-missing-field-initializers -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS '-DPACKAGE_VERSION="v0.7.2"' -DSPDLOG_ACTIVE_LEVEL=SPDLOG_LEVEL_DEBUG -D_GNU_SOURCE -DHAVE_PTHREAD -DUSE_GCC_ATOMIC_BUILTINS -DHAVE_TIMESPEC_GET -DHAVE___BUILTIN_BSWAP32 -DHAVE___BUILTIN_BSWAP64 -DHAVE___BUILTIN_CLZ -DHAVE___BUILTIN_CLZLL -DHAVE___BUILTIN_CTZ -DHAVE___BUILTIN_EXPECT -DHAVE___BUILTIN_FFS -DHAVE___BUILTIN_FFSLL -DHAVE___BUILTIN_POPCOUNT -DHAVE___BUILTIN_POPCOUNTLL -DHAVE___BUILTIN_UNREACHABLE -O2 -pipe '-DIMGUI_API=__attribute__((visibility("default")))' -pthread -DSPDLOG_COMPILED_LIB -DTEST_ONLY -MD -MQ amdgpu.p/tests_test_amdgpu.cpp.o -MF amdgpu.p/tests_test_amdgpu.cpp.o.d -o amdgpu.p/tests_test_amdgpu.cpp.o -c ../tests/test_amdgpu.cpp
In file included from ../tests/test_amdgpu.cpp:9:
In file included from ../tests/../src/amdgpu.h:6:
In file included from ../tests/../src/overlay_params.h:6:
In file included from /usr/include/c++/v1/vector:3023:
In file included from /usr/include/c++/v1/locale:212:
/usr/include/c++/v1/ios:343:30: error: expected parameter declarator
  343 |   _LIBCPP_HIDE_FROM_ABI bool fail() const;
      |                              ^
/usr/include/cmocka.h:1616:22: note: expanded from macro 'fail'
 1616 | #define fail() _fail(__FILE__, __LINE__)
      |                      ^
<scratch space>:2:1: note: expanded from here
    2 | "/usr/include/c++/v1/ios"

Signed-off-by: Alfred Wingate <parona@protonmail.com>
--- a/tests/test_amdgpu.cpp
+++ b/tests/test_amdgpu.cpp
@@ -2,11 +2,13 @@
 #include <stddef.h>
 #include <setjmp.h>
 #include <stdint.h>
+#include "stdio.h"
+#include "../src/amdgpu.h"
+// cmocka and libc++ have clashing symbols which causes issues if the symbols
+// are defined before libc++ can hide its own with _LIBCPP_HIDE_FROM_ABI.
 extern "C" {
 #include <cmocka.h>
 }
-#include "stdio.h"
-#include "../src/amdgpu.h"
 
 #define UNUSED(x) (void)(x)
 
-- 
2.51.2

From 49c837de04d50c16d82d76d015329f12af52f27f Mon Sep 17 00:00:00 2001
From: Alfred Wingate <parona@protonmail.com>
Date: Fri, 27 Dec 2024 14:15:04 +0200
Subject: [PATCH 4/5] Build mangohud_opengl only in unixy environments

Signed-off-by: Alfred Wingate <parona@protonmail.com>
--- a/src/meson.build
+++ b/src/meson.build
@@ -239,44 +239,46 @@ mangohud_shared_lib = shared_library(
   install: true
 )
 
-mangohud_opengl_versionscript = cpp.preprocess(
-  'mangohud_opengl.version',
-  compile_args: pre_args,
-)
+if is_unixy
+  mangohud_opengl_versionscript = cpp.preprocess(
+    'mangohud_opengl.version',
+    compile_args: pre_args,
+  )
 
-mangohud_opengl_shared_lib = shared_library(
-  'MangoHud_opengl',
-  mangohud_version,
-  opengl_files,
-  vklayer_files,
-  util_files,
-  c_args : [
-    pre_args,
-    vulkan_wsi_args
-    ],
-  cpp_args : [
-    pre_args,
-    vulkan_wsi_args
-    ],
-  dependencies : [
-    mangohud_version_dep,
-    vulkan_wsi_deps,
-    dearimgui_dep,
-    spdlog_dep,
-    dbus_dep,
-    dep_dl,
-    dep_rt,
-    dep_pthread,
-    dep_vulkan,
-    windows_deps,
-    implot_dep],
-  include_directories : [inc_common],
-  link_args : [link_args, '-Wl,--version-script,@0@'.format(mangohud_opengl_versionscript[0].full_path())],
-  link_with: mangohud_static_lib,
-  link_depends: mangohud_opengl_versionscript,
-  install_dir : libdir_mangohud,
-  install: true
-)
+  mangohud_opengl_shared_lib = shared_library(
+    'MangoHud_opengl',
+    mangohud_version,
+    opengl_files,
+    vklayer_files,
+    util_files,
+    c_args : [
+      pre_args,
+      vulkan_wsi_args
+      ],
+    cpp_args : [
+      pre_args,
+      vulkan_wsi_args
+      ],
+    dependencies : [
+      mangohud_version_dep,
+      vulkan_wsi_deps,
+      dearimgui_dep,
+      spdlog_dep,
+      dbus_dep,
+      dep_dl,
+      dep_rt,
+      dep_pthread,
+      dep_vulkan,
+      windows_deps,
+      implot_dep],
+    include_directories : [inc_common],
+    link_args : [link_args, '-Wl,--version-script,@0@'.format(mangohud_opengl_versionscript[0].full_path())],
+    link_with: mangohud_static_lib,
+    link_depends: mangohud_opengl_versionscript,
+    install_dir : libdir_mangohud,
+    install: true
+  )
+endif
 
 if get_option('mangoapp')
   if not get_option('with_x11').enabled()
-- 
2.51.2

From 98b3de25e7cb18475cf0bc053945c2d731efb188 Mon Sep 17 00:00:00 2001
From: Alfred Wingate <parona@protonmail.com>
Date: Thu, 13 Feb 2025 05:38:27 +0200
Subject: [PATCH 5/5] Use project arguments for HAVE_X11 and HAVE_WAYLAND

This means you don't have to use explicit compiler_args for
cpp.preprocess which then means the meson requirement can be lowered.

Signed-off-by: Alfred Wingate <parona@protonmail.com>
--- a/meson.build
+++ b/meson.build
@@ -2,7 +2,7 @@ project('MangoHud',
   ['c', 'cpp'],
   version : 'v0.8.1',
   license : 'MIT',
-  meson_version: '>=1.3.2',
+  meson_version: '>=0.64.0',
   default_options : ['buildtype=release', 'c_std=c99', 'cpp_std=c++17', 'warning_level=2']
 )
 
--- a/src/meson.build
+++ b/src/meson.build
@@ -146,7 +146,7 @@ if is_unixy
   endif
 
   if get_option('with_x11').enabled()
-    pre_args += '-DHAVE_X11'
+    add_project_arguments('-DHAVE_X11', language : ['c', 'cpp'])
 
     vklayer_files += files(
       'loaders/loader_x11.cpp',
@@ -160,7 +160,7 @@ if is_unixy
   endif
 
   if get_option('with_wayland').enabled()
-    pre_args += '-DHAVE_WAYLAND'
+    add_project_arguments('-DHAVE_WAYLAND', language : ['c', 'cpp'])
 
     vklayer_files += files(
       'wayland_keybinds.cpp'
@@ -240,10 +240,7 @@ mangohud_shared_lib = shared_library(
 )
 
 if is_unixy
-  mangohud_opengl_versionscript = cpp.preprocess(
-    'mangohud_opengl.version',
-    compile_args: pre_args,
-  )
+  mangohud_opengl_versionscript = cpp.preprocess('mangohud_opengl.version')
 
   mangohud_opengl_shared_lib = shared_library(
     'MangoHud_opengl',
-- 
2.51.2

