[Bf-blender-cvs] [9762a0992b3] master: CMake: configue_file() to pass strings for build-info

Campbell Barton noreply at git.blender.org
Fri Nov 6 07:26:44 CET 2020


Commit: 9762a0992b3b15f63c7c45f752ae9eafd1b17daa
Author: Campbell Barton
Date:   Fri Nov 6 17:11:27 2020 +1100
Branches: master
https://developer.blender.org/rB9762a0992b3b15f63c7c45f752ae9eafd1b17daa

CMake: configue_file() to pass strings for build-info

Using configue_file(..) would have avoided the breakage from
1daa3c3f0a1cfd, caused by buildinfo not properly escaping quotes.

Rely on CMake to escaping strings instead using configure_file().

===================================================================

M	build_files/cmake/buildinfo.cmake
A	build_files/cmake/buildinfo_static.h.in
M	source/creator/CMakeLists.txt

===================================================================

diff --git a/build_files/cmake/buildinfo.cmake b/build_files/cmake/buildinfo.cmake
index babc52df748..a349ffc1b56 100644
--- a/build_files/cmake/buildinfo.cmake
+++ b/build_files/cmake/buildinfo.cmake
@@ -161,6 +161,7 @@ file(WRITE buildinfo.h.txt
   "#define BUILD_BRANCH \"${MY_WC_BRANCH}\"\n"
   "#define BUILD_DATE \"${BUILD_DATE}\"\n"
   "#define BUILD_TIME \"${BUILD_TIME}\"\n"
+  "#include \"buildinfo_static.h\"\n"
 )
 
 # cleanup
diff --git a/build_files/cmake/buildinfo_static.h.in b/build_files/cmake/buildinfo_static.h.in
new file mode 100644
index 00000000000..9dd45b95b8a
--- /dev/null
+++ b/build_files/cmake/buildinfo_static.h.in
@@ -0,0 +1,8 @@
+/* CMake expanded values that won't change between CMake execution (unlike date/time).
+ * This is included by `buildinfo.h` generated by `buildinfo.cmake`. */
+#define BUILD_PLATFORM "@BUILD_PLATFORM@"
+#define BUILD_TYPE "@BUILD_TYPE@"
+#define BUILD_CFLAGS "@BUILD_CFLAGS@"
+#define BUILD_CXXFLAGS "@BUILD_CXXFLAGS@"
+#define BUILD_LINKFLAGS "@BUILD_LINKFLAGS@"
+#define BUILD_SYSTEM "@BUILD_SYSTEM@"
diff --git a/source/creator/CMakeLists.txt b/source/creator/CMakeLists.txt
index 95af663d53e..7f4f8d47688 100644
--- a/source/creator/CMakeLists.txt
+++ b/source/creator/CMakeLists.txt
@@ -153,45 +153,46 @@ if(WITH_BUILDINFO)
   # --------------------------------------------------------------------------
   # These defines could all be moved into the header below
 
-  set(BUILDINFO_CFLAGS "${CMAKE_C_FLAGS}")
-  set(BUILDINFO_CXXFLAGS "${CMAKE_CXX_FLAGS}")
-  set(BUILDINFO_LINKFLAGS "${PLATFORM_LINKFLAGS}")
+  # Write strings into a separate header since we can escape C-strings
+  # in a way that's not practical when passing defines.
+  set(BUILD_PLATFORM "${CMAKE_SYSTEM_NAME}")
+  set(BUILD_TYPE "${CMAKE_BUILD_TYPE}")
+  set(BUILD_CFLAGS "${CMAKE_C_FLAGS}")
+  set(BUILD_CXXFLAGS "${CMAKE_CXX_FLAGS}")
+  set(BUILD_LINKFLAGS "${PLATFORM_LINKFLAGS}")
+  set(BUILD_SYSTEM "CMake")
 
   if(WITH_COMPILER_SHORT_FILE_MACRO)
-    # Needed because currently including quotes isn't supported.
-    # Besides this it's not necessary to include path information
+    # It's not necessary to include path information
     # about the system building Blender in the executable.
-    string(REPLACE "${PLATFORM_CFLAGS_FMACRO_PREFIX_MAP}" " " BUILDINFO_CFLAGS "${BUILDINFO_CFLAGS}")
-    string(REPLACE "${PLATFORM_CFLAGS_FMACRO_PREFIX_MAP}" " " BUILDINFO_CXXFLAGS "${BUILDINFO_CXXFLAGS}")
+    string(REPLACE "${PLATFORM_CFLAGS_FMACRO_PREFIX_MAP}" " " BUILD_CFLAGS "${BUILD_CFLAGS}")
+    string(REPLACE "${PLATFORM_CFLAGS_FMACRO_PREFIX_MAP}" " " BUILD_CXXFLAGS "${BUILD_CXXFLAGS}")
   endif()
 
-  string(REPLACE " " "\ " BUILDINFO_CFLAGS "${BUILDINFO_CFLAGS}")
-  string(REPLACE " " "\ " BUILDINFO_CXXFLAGS "${BUILDINFO_CXXFLAGS}")
-  string(REPLACE " " "\ " BUILDINFO_LINKFLAGS "${BUILDINFO_LINKFLAGS}")
-
-  add_definitions(
-    # # define in header now, else these get out of date on rebuilds.
-    # -DBUILD_DATE="${BUILD_DATE}"
-    # -DBUILD_TIME="${BUILD_TIME}"
-    # -DBUILD_COMMIT_TIMESTAMP="${BUILD_COMMIT_TIMESTAMP}"
-    # -DBUILD_COMMIT_TIME="${BUILD_COMMIT_TIME}"
-    # -DBUILD_COMMIT_DATE="${BUILD_COMMIT_DATE}"
-    # -DBUILD_HASH="${BUILD_HASH}"
-    # -DBUILD_BRANCH="${BUILD_BRANCH}"
-    -DWITH_BUILDINFO_HEADER # alternative to lines above
-    -DBUILD_PLATFORM="${CMAKE_SYSTEM_NAME}"
-    -DBUILD_TYPE="${CMAKE_BUILD_TYPE}"
-    -DBUILD_CFLAGS="${BUILDINFO_CFLAGS}"
-    -DBUILD_CXXFLAGS="${BUILDINFO_CXXFLAGS}"
-    -DBUILD_LINKFLAGS="${BUILDINFO_LINKFLAGS}"
-    -DBUILD_SYSTEM="CMake"
+  # Use `configure_file` instead of definitions since properly
+  # escaping the multiple command line arguments which themselves
+  # contain strings and spaces becomes overly error-prone & complicated.
+  configure_file(
+    "${CMAKE_SOURCE_DIR}/build_files/cmake/buildinfo_static.h.in"
+    "${CMAKE_CURRENT_BINARY_DIR}/buildinfo_static.h"
+    ESCAPE_QUOTES
+    @ONLY
   )
 
+  unset(BUILD_PLATFORM)
+  unset(BUILD_TYPE)
+  unset(BUILD_CFLAGS)
+  unset(BUILD_CXXFLAGS)
+  unset(BUILD_LINKFLAGS)
+  unset(BUILD_SYSTEM)
+
   # --------------------------------------------------------------------------
   # write header for values that change each build
   # note, generaed file is in build dir's source/creator
   #       except when used as an include path.
 
+  add_definitions(-DWITH_BUILDINFO_HEADER)
+
   # include the output directory, where the buildinfo.h file is generated
   include_directories(${CMAKE_CURRENT_BINARY_DIR})



More information about the Bf-blender-cvs mailing list