shithub: cstory

Download patch

ref: 5b89a3197603e9d16d3b198288dbb6622b7ecc94
parent: 43958d27717ce27042b87462dd954abc58e6a7d7
author: Clownacy <[email protected]>
date: Thu May 23 15:41:37 EDT 2019

Split bin2h to its own CMake file

Also added warnings to its part of the Makefile

--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -263,17 +263,7 @@
 endif()
 
 # Magic to convert resources to header files
-add_executable(bin2h "src/misc/bin2h.c")
-
-set_target_properties(bin2h PROPERTIES
-	C_STANDARD 90
-	C_STANDARD_REQUIRED ON
-	C_EXTENSIONS OFF
-)
-
-if(MSVC)
-	target_compile_definitions(bin2h PRIVATE _CRT_SECURE_NO_WARNINGS)	# Shut up those stupid warnings
-endif()
+add_subdirectory("bin2h")
 foreach(FILENAME IN LISTS RESOURCES)
 	set(IN_DIR "${CMAKE_CURRENT_SOURCE_DIR}/res")
 	set(OUT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/src/Resource")
--- a/Makefile
+++ b/Makefile
@@ -226,10 +226,10 @@
 	@echo Converting $<
 	@obj/bin2h $< $@
 
-obj/bin2h: src/misc/bin2h.c
+obj/bin2h: bin2h/bin2h.c
 	@mkdir -p $(@D)
 	@echo Compiling $^
-	@$(CC) -O3 -s -std=c90 $^ -o $@
+	@$(CC) -O3 -s -std=c90 -Wall -Wextra -pedantic $^ -o $@
 
 include $(wildcard $(DEPENDENCIES))
 
--- /dev/null
+++ b/bin2h/CMakeLists.txt
@@ -1,0 +1,31 @@
+cmake_minimum_required(VERSION 3.7.2)
+
+if((${CMAKE_VERSION} VERSION_EQUAL 3.9) OR (${CMAKE_VERSION} VERSION_GREATER 3.9))
+	cmake_policy(SET CMP0069 NEW)
+endif()
+
+project(bin2h LANGUAGES C)
+
+add_executable(bin2h "bin2h.c")
+
+set_target_properties(bin2h PROPERTIES
+	C_STANDARD 90
+	C_STANDARD_REQUIRED ON
+	C_EXTENSIONS OFF
+)
+
+# MSVC tweak
+if(MSVC)
+	target_compile_definitions(bin2h PRIVATE _CRT_SECURE_NO_WARNINGS)	# Shut up those stupid warnings
+endif()
+
+# Enable link-time optimisation if available
+if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
+	if((${CMAKE_VERSION} VERSION_EQUAL 3.9) OR (${CMAKE_VERSION} VERSION_GREATER 3.9))
+		include(CheckIPOSupported)
+		check_ipo_supported(RESULT result)
+		if(result)
+			set_target_properties(bin2h PROPERTIES INTERPROCEDURAL_OPTIMIZATION TRUE)
+		endif()
+	endif()
+endif()
--- /dev/null
+++ b/bin2h/bin2h.c
@@ -1,0 +1,92 @@
+/*Bin2h by -C-u-c-k-y- Clownypants*/
+/*Converts files to the .h's expected by Cave Story Engine for resources.*/
+
+#include <stddef.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+int main(int argc, char *argv[])
+{
+	int result = 0;
+
+	if (argc > 2)
+	{
+		char *last_forward_slash;
+		char *last_back_slash;
+		char *last_path_seperator;
+		char *filename_pointer;
+		char *dot;
+		size_t filename_length;
+		char *filename;
+		FILE *in_file;
+		FILE *out_file;
+
+		last_forward_slash = strrchr(argv[1], '/');
+		last_back_slash = strrchr(argv[1], '\\');
+
+		last_path_seperator = last_forward_slash > last_back_slash ? last_forward_slash : last_back_slash;
+
+		filename_pointer = (last_path_seperator == NULL) ? argv[1] : last_path_seperator + 1;
+		dot = strchr(filename_pointer, '.');
+		filename_length = (dot == NULL) ? strlen(filename_pointer) : dot - filename_pointer;
+
+		filename = malloc(filename_length + 1);
+		memcpy(filename, filename_pointer, filename_length);
+		filename[filename_length] = '\0';
+
+		in_file = fopen(argv[1], "rb");
+		out_file = fopen(argv[2], "w");
+
+		if (in_file == NULL)
+		{
+			printf("Couldn't open '%s'\n", argv[1]);
+			result = 1;
+		}
+		else if (out_file == NULL)
+		{
+			printf("Couldn't open '%s'\n", argv[2]);
+			result = 1;
+		}
+		else
+		{
+			long in_file_size;
+			unsigned char *in_file_buffer;
+			unsigned char *in_file_pointer;
+			long i;
+
+			fseek(in_file, 0, SEEK_END);
+			in_file_size = ftell(in_file);
+			rewind(in_file);
+			in_file_buffer = malloc(in_file_size);
+			fread(in_file_buffer, 1, in_file_size, in_file);
+			fclose(in_file);
+			in_file_pointer = in_file_buffer;
+
+			setvbuf(out_file, NULL, _IOFBF, 0x10000);
+
+			fprintf(out_file, "#pragma once\n\nstatic const unsigned char r%s[0x%lX] = {\n\t", filename, in_file_size);
+
+			for (i = 0; i < in_file_size - 1; ++i)
+			{
+				if (i % 32 == 32-1)
+					fprintf(out_file, "%d,\n\t", *in_file_pointer++);
+				else
+					fprintf(out_file, "%d,", *in_file_pointer++);
+			}
+
+			fprintf(out_file, "%d\n};\n", *in_file_pointer++);
+
+			fclose(out_file);
+			free(in_file_buffer);
+		}
+
+		free(filename);
+	}
+	else
+	{
+		result = 1;
+	}
+
+	return result;
+}
--- a/src/misc/bin2h.c
+++ /dev/null
@@ -1,92 +1,0 @@
-/*Bin2h by -C-u-c-k-y- Clownypants*/
-/*Converts files to the .h's expected by Cave Story Engine for resources.*/
-
-#include <stddef.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-int main(int argc, char *argv[])
-{
-	int result = 0;
-
-	if (argc > 2)
-	{
-		char *last_forward_slash;
-		char *last_back_slash;
-		char *last_path_seperator;
-		char *filename_pointer;
-		char *dot;
-		size_t filename_length;
-		char *filename;
-		FILE *in_file;
-		FILE *out_file;
-
-		last_forward_slash = strrchr(argv[1], '/');
-		last_back_slash = strrchr(argv[1], '\\');
-
-		last_path_seperator = last_forward_slash > last_back_slash ? last_forward_slash : last_back_slash;
-
-		filename_pointer = (last_path_seperator == NULL) ? argv[1] : last_path_seperator + 1;
-		dot = strchr(filename_pointer, '.');
-		filename_length = (dot == NULL) ? strlen(filename_pointer) : dot - filename_pointer;
-
-		filename = malloc(filename_length + 1);
-		memcpy(filename, filename_pointer, filename_length);
-		filename[filename_length] = '\0';
-
-		in_file = fopen(argv[1], "rb");
-		out_file = fopen(argv[2], "w");
-
-		if (in_file == NULL)
-		{
-			printf("Couldn't open '%s'\n", argv[1]);
-			result = 1;
-		}
-		else if (out_file == NULL)
-		{
-			printf("Couldn't open '%s'\n", argv[2]);
-			result = 1;
-		}
-		else
-		{
-			long in_file_size;
-			unsigned char *in_file_buffer;
-			unsigned char *in_file_pointer;
-			long i;
-
-			fseek(in_file, 0, SEEK_END);
-			in_file_size = ftell(in_file);
-			rewind(in_file);
-			in_file_buffer = malloc(in_file_size);
-			fread(in_file_buffer, 1, in_file_size, in_file);
-			fclose(in_file);
-			in_file_pointer = in_file_buffer;
-
-			setvbuf(out_file, NULL, _IOFBF, 0x10000);
-
-			fprintf(out_file, "#pragma once\n\nstatic const unsigned char r%s[0x%lX] = {\n\t", filename, in_file_size);
-
-			for (i = 0; i < in_file_size - 1; ++i)
-			{
-				if (i % 32 == 32-1)
-					fprintf(out_file, "%d,\n\t", *in_file_pointer++);
-				else
-					fprintf(out_file, "%d,", *in_file_pointer++);
-			}
-
-			fprintf(out_file, "%d\n};\n", *in_file_pointer++);
-
-			fclose(out_file);
-			free(in_file_buffer);
-		}
-
-		free(filename);
-	}
-	else
-	{
-		result = 1;
-	}
-
-	return result;
-}