From 0909fce2e39d331cacb44c805f5c9d829f41dcc1 Mon Sep 17 00:00:00 2001
From: Ronald Kinard <furyhunter600@gmail.com>
Date: Wed, 28 Jan 2015 18:03:41 -0600
Subject: [PATCH] cmake: Configure source via cmake

silently fixing a bad malloc here too shhhhhhhhh
---
 CMakeLists.txt                   |  9 +++++++++
 assets/CMakeLists.txt            | 13 +++++++------
 cmake/Modules/GitUtilities.cmake | 22 ++++++++++++++++++++++
 src/CMakeLists.txt               |  4 ++++
 src/comptime.c                   |  8 +++++++-
 src/config.h.in                  | 25 +++++++++++++++++++++++++
 src/d_main.c                     | 14 ++++++++++----
 src/f_wipe.c                     |  4 ++--
 8 files changed, 86 insertions(+), 13 deletions(-)
 create mode 100644 cmake/Modules/GitUtilities.cmake
 create mode 100644 src/config.h.in

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 7672655fef..2ba9611b24 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -69,10 +69,19 @@ endif()
 set(SRB2_SDL2_EXE_NAME srb2)
 set(SRB2_WIN_EXE_NAME srb2dd)
 
+include_directories(${CMAKE_CURRENT_BINARY_DIR}/src)
+
 add_subdirectory(src)
 add_subdirectory(assets)
 
 
+## config.h generation
+set(GIT_EXECUTABLE "git" CACHE FILEPATH "Path to git binary")
+include(GitUtilities)
+git_describe(SRB2_COMP_REVISION "${CMAKE_CURRENT_SOURCE_DIR}")
+configure_file(${CMAKE_CURRENT_SOURCE_DIR}/src/config.h.in ${CMAKE_CURRENT_BINARY_DIR}/src/config.h)
+
+
 
 ##### PACKAGE CONFIGURATION #####
 
diff --git a/assets/CMakeLists.txt b/assets/CMakeLists.txt
index cbcb6ba1cf..8d149bf3f9 100644
--- a/assets/CMakeLists.txt
+++ b/assets/CMakeLists.txt
@@ -11,15 +11,16 @@ set(SRB2_ASSET_ALL
 )
 
 set(SRB2_ASSET_HASHED
-	${CMAKE_CURRENT_SOURCE_DIR}/srb2.srb
-	${CMAKE_CURRENT_SOURCE_DIR}/player.dta
-	${CMAKE_CURRENT_SOURCE_DIR}/rings.dta
-	${CMAKE_CURRENT_SOURCE_DIR}/zones.dta
-	${CMAKE_CURRENT_SOURCE_DIR}/patch.dta
+	srb2.srb
+	player.dta
+	rings.dta
+	zones.dta
+	patch.dta
 )
 
 foreach(SRB2_ASSET ${SRB2_ASSET_HASHED})
-	file(MD5 ${SRB2_ASSET} "SRB2_ASSET_${SRB2_ASSET}_HASH")
+	file(MD5 ${CMAKE_CURRENT_SOURCE_DIR}/${SRB2_ASSET} "SRB2_ASSET_${SRB2_ASSET}_HASH")
+	set(SRB2_ASSET_${SRB2_ASSET}_HASH ${SRB2_ASSET_${SRB2_ASSET}_HASH} PARENT_SCOPE)
 endforeach()
 
 # Installation
diff --git a/cmake/Modules/GitUtilities.cmake b/cmake/Modules/GitUtilities.cmake
new file mode 100644
index 0000000000..de4015b0d1
--- /dev/null
+++ b/cmake/Modules/GitUtilities.cmake
@@ -0,0 +1,22 @@
+# Git utilities
+
+if(__GitUtilities)
+	return()
+endif()
+
+set(__GitUtilities ON)
+
+function(git_describe variable path)
+	execute_process(COMMAND "${GIT_EXECUTABLE}" "describe"
+		WORKING_DIRECTORY "${path}"
+		RESULT_VARIABLE result
+		OUTPUT_VARIABLE output
+		ERROR_QUIET
+		OUTPUT_STRIP_TRAILING_WHITESPACE
+	)
+	#if(NOT result EQUAL 0)
+	#	set(${variable} "GITERROR-${result}-NOTFOUND" CACHE STRING "revision" FORCE)
+	#endif()
+
+	set(${variable} "${output}" PARENT_SCOPE)
+endfunction()
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 6915071e52..b3e01ccef8 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -160,6 +160,8 @@ set(SRB2_CORE_HEADERS
 prepend_sources(SRB2_CORE_SOURCES)
 prepend_sources(SRB2_CORE_HEADERS)
 
+set(SRB2_CORE_HEADERS ${SRB2_CORE_HEADERS} ${CMAKE_CURRENT_BINARY_DIR}/config.h)
+
 set(SRB2_HWRENDER_SOURCES
 	hardware/hw_bsp.c
 	hardware/hw_cache.c
@@ -358,4 +360,6 @@ if(NOT CLANG AND NOT MSVC)
 	add_compile_options(-mno-ms-bitfields)
 endif()
 
+add_definitions(-DCMAKECONFIG)
+
 add_subdirectory(sdl)
diff --git a/src/comptime.c b/src/comptime.c
index cd6bfd9bb2..a4dc5b0f96 100644
--- a/src/comptime.c
+++ b/src/comptime.c
@@ -7,10 +7,16 @@
  *
  */
 
-#ifdef COMPVERSION
+#if (defined(CMAKECONFIG))
+#include "config.h"
+const char *comprevision = SRB2_COMP_REVISION;
+
+#elif (defined(COMPVERSION))
 #include "comptime.h"
+
 #else
 const char *comprevision = "illegal";
+
 #endif
 
 const char *compdate = __DATE__;
diff --git a/src/config.h.in b/src/config.h.in
new file mode 100644
index 0000000000..9c98d36e7a
--- /dev/null
+++ b/src/config.h.in
@@ -0,0 +1,25 @@
+/** SRB2 CMake Configuration */
+
+#ifndef __CONFIG_H__
+#define __CONFIG_H__
+
+#ifdef CMAKECONFIG
+
+#define ASSET_HASH_SRB2_SRB   "${SRB2_ASSET_srb2.srb_HASH}"
+#define ASSET_HASH_PLAYER_DTA "${SRB2_ASSET_player.dta_HASH}"
+#define ASSET_HASH_RINGS_DTA  "${SRB2_ASSET_rings.dta_HASH}"
+#define ASSET_HASH_ZONES_DTA  "${SRB2_ASSET_zones.dta_HASH}"
+#define ASSET_HASH_PATCH_DTA  "${SRB2_ASSET_patch.dta_HASH}"
+
+#define SRB2_COMP_REVISION    "${SRB2_COMP_REVISION}"
+
+#else
+
+#define ASSET_HASH_SRB2_SRB   "c1b9577687f8a795104aef4600720ea7"
+#define ASSET_HASH_ZONES_DTA  "303838c6c534d9540288360fa49cca60"
+#define ASSET_HASH_PLAYER_DTA "cfca0f1c73023cbbd8f844f45480f799"
+#define ASSET_HASH_RINGS_DTA  "85901ad4bf94637e5753d2ac2c03ea26"
+
+#define 
+#endif
+#endif
diff --git a/src/d_main.c b/src/d_main.c
index 95808b274f..a959a86328 100644
--- a/src/d_main.c
+++ b/src/d_main.c
@@ -74,6 +74,12 @@ int	snprintf(char *str, size_t n, const char *fmt, ...);
 #include "m_cond.h" // condition initialization
 #include "fastcmp.h"
 
+#ifdef CMAKECONFIG
+#include "config.h"
+#else
+#include "config.h.in"
+#endif
+
 #ifdef _XBOX
 #include "sdl/SRB2XBOX/xboxhelp.h"
 #endif
@@ -1115,10 +1121,10 @@ void D_SRB2Main(void)
 #if 1 // md5s last updated 12/14/14
 
 	// Check MD5s of autoloaded files
-	W_VerifyFileMD5(0, "c1b9577687f8a795104aef4600720ea7"); // srb2.srb/srb2.wad
-	W_VerifyFileMD5(1, "303838c6c534d9540288360fa49cca60"); // zones.dta
-	W_VerifyFileMD5(2, "cfca0f1c73023cbbd8f844f45480f799"); // player.dta
-	W_VerifyFileMD5(3, "85901ad4bf94637e5753d2ac2c03ea26"); // rings.dta
+	W_VerifyFileMD5(0, ASSET_HASH_SRB2_SRB); // srb2.srb/srb2.wad
+	W_VerifyFileMD5(1, ASSET_HASH_ZONES_DTA); // zones.dta
+	W_VerifyFileMD5(2, ASSET_HASH_PLAYER_DTA); // player.dta
+	W_VerifyFileMD5(3, ASSET_HASH_RINGS_DTA); // rings.dta
 	//W_VerifyFileMD5(4, "0c66790502e648bfce90fdc5bb15722e"); // patch.dta
 	// don't check music.dta because people like to modify it, and it doesn't matter if they do
 	// ...except it does if they slip maps in there, and that's what W_VerifyNMUSlumps is for.
diff --git a/src/f_wipe.c b/src/f_wipe.c
index 69c9808804..8e7c622c43 100644
--- a/src/f_wipe.c
+++ b/src/f_wipe.c
@@ -210,8 +210,8 @@ static void F_DoWipe(fademask_t *fademask)
 		UINT32 draw_linestogo, draw_rowstogo;
 
 		// rectangle coordinates, etc.
-		UINT16* scrxpos = (UINT16*)malloc(fademask->width + 1); //[fademask->width  + 1];
-		UINT16* scrypos = (UINT16*)malloc(fademask->height + 1);// [fademask->height + 1];
+		UINT16* scrxpos = (UINT16*)malloc((fademask->width + 1)  * sizeof(UINT16));
+		UINT16* scrypos = (UINT16*)malloc((fademask->height + 1) * sizeof(UINT16));
 		UINT16 maskx, masky;
 		UINT32 relativepos;
 
-- 
GitLab