shithub: puzzles

Download patch

ref: 90e2c7539be4fce1b6dad397109f3c4927db1594
parent: 9d7c2b8c83506c1f239c840e372058fac603b255
author: Simon Tatham <anakin@pobox.com>
date: Wed Feb 22 07:46:32 EST 2023

Normalise pathnames in assert statements where possible.

After commit 1470c9530b1cff3 enabled assertions, I found that my build
scripts were complaining that the Windows binaries built from the same
source twice were not generating the same output, and it turned out to
be because the use of __FILE__ in every assert was baking in a
pathname from my build setup containing a mkstemp()-randomised path
component.

I've found the '-fmacro-prefix-map' option, available in both gcc and
clang (except the archaic gcc used by my NestedVM build, alas), which
lets you remap pathnames for purpose of what __FILE__ expands to. So
now our assertion statements should look as if the puzzle source just
lived in /puzzles, and (just in case a pathname in a generated header
ever becomes relevant) the cmake build directory is /build.

--- a/cmake/setup.cmake
+++ b/cmake/setup.cmake
@@ -48,6 +48,35 @@
   add_compile_definitions(NO_STDINT_H)
 endif()
 
+# Try to normalise source file pathnames as seen in __FILE__ (e.g.
+# assertion failure messages). Partly to avoid bloating the binaries
+# with file prefixes like /home/simon/stuff/things/tmp-7x6c5d54/, but
+# also to make the builds more deterministic - building from the same
+# source should give the same binary even if you do it in a
+# differently named temp directory.
+function(map_pathname src dst)
+  if(CMAKE_SYSTEM_NAME MATCHES "NestedVM")
+    # Do nothing: the NestedVM gcc is unfortunately too old to support
+    # this option.
+  elseif(CMAKE_C_COMPILER_ID MATCHES "Clang" AND
+      CMAKE_C_COMPILER_FRONTEND_VARIANT MATCHES "MSVC")
+    # -fmacro-prefix-map isn't available as a clang-cl option, so we
+    # prefix it with -Xclang to pass it straight through to the
+    # underlying clang -cc1 invocation, which spells the option the
+    # same way.
+    set(CMAKE_C_FLAGS
+      "${CMAKE_C_FLAGS} -Xclang -fmacro-prefix-map=${src}=${dst}"
+      PARENT_SCOPE)
+  elseif(CMAKE_C_COMPILER_ID MATCHES "GNU" OR
+      CMAKE_C_COMPILER_ID MATCHES "Clang")
+    set(CMAKE_C_FLAGS
+      "${CMAKE_C_FLAGS} -fmacro-prefix-map=${src}=${dst}"
+      PARENT_SCOPE)
+  endif()
+endfunction()
+map_pathname(${CMAKE_SOURCE_DIR} /puzzles)
+map_pathname(${CMAKE_BINARY_DIR} /build)
+
 include(icons/icons.cmake)
 
 # The main function called from the top-level CMakeLists.txt to define