summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CMakeLists.txt3
-rw-r--r--res/CMakeLists.txt4
-rw-r--r--res/Embed.cmake (renamed from Embed.cmake)33
-rw-r--r--res/bincat.c48
4 files changed, 71 insertions, 17 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index f4b0376c..cb97185c 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -14,7 +14,6 @@
14# https://libsdl.org/). To make configuration easier, consider writing 14# https://libsdl.org/). To make configuration easier, consider writing
15# for your personal use a pkg-config sdl2.pc file that uses the Windows 15# for your personal use a pkg-config sdl2.pc file that uses the Windows
16# version of the library. 16# version of the library.
17# - `cat` is relied upon for merging all the resource files together.
18 17
19cmake_minimum_required (VERSION 3.9) 18cmake_minimum_required (VERSION 3.9)
20 19
@@ -33,7 +32,7 @@ option (ENABLE_RESOURCE_EMBED "Embed resources inside the executable" OFF)
33option (ENABLE_WINDOWPOS_FIX "Set position after showing window (workaround for SDL bug)" OFF) 32option (ENABLE_WINDOWPOS_FIX "Set position after showing window (workaround for SDL bug)" OFF)
34 33
35include (BuildType.cmake) 34include (BuildType.cmake)
36include (Embed.cmake) 35include (res/Embed.cmake)
37if (NOT EXISTS ${CMAKE_SOURCE_DIR}/lib/the_Foundation/CMakeLists.txt) 36if (NOT EXISTS ${CMAKE_SOURCE_DIR}/lib/the_Foundation/CMakeLists.txt)
38 set (INSTALL_THE_FOUNDATION YES) 37 set (INSTALL_THE_FOUNDATION YES)
39 find_package (the_Foundation REQUIRED) 38 find_package (the_Foundation REQUIRED)
diff --git a/res/CMakeLists.txt b/res/CMakeLists.txt
new file mode 100644
index 00000000..0a8c6260
--- /dev/null
+++ b/res/CMakeLists.txt
@@ -0,0 +1,4 @@
1# CMakeLists for bincat
2cmake_minimum_required (VERSION 3.0)
3project (BINCAT VERSION 1.0 LANGUAGES C)
4add_executable (bincat bincat.c)
diff --git a/Embed.cmake b/res/Embed.cmake
index 75f3e06f..8da9e1d6 100644
--- a/Embed.cmake
+++ b/res/Embed.cmake
@@ -3,8 +3,16 @@
3# License: BSD 2-Clause 3# License: BSD 2-Clause
4 4
5option (ENABLE_RESOURCE_EMBED "Embed resources inside the executable" OFF) 5option (ENABLE_RESOURCE_EMBED "Embed resources inside the executable" OFF)
6# Note: If disabled, the Unix "cat" tool is required for concatenating 6
7# the resources into a single "resources.binary" file. 7# Build "bincat" for concatenating files.
8if (NOT ENABLE_RESOURCE_EMBED)
9 message (STATUS "Compiling bincat for merging resource files...")
10 set (_catDir ${CMAKE_BINARY_DIR}/res)
11 execute_process (COMMAND ${CMAKE_COMMAND} -E make_directory ${_catDir})
12 execute_process (COMMAND ${CMAKE_COMMAND} ${CMAKE_SOURCE_DIR}/res WORKING_DIRECTORY ${_catDir})
13 execute_process (COMMAND ${CMAKE_COMMAND} --build . WORKING_DIRECTORY ${_catDir})
14 set (BINCAT_COMMAND ${_catDir}/bincat)
15endif ()
8 16
9function (embed_getname output fn) 17function (embed_getname output fn)
10 get_filename_component (name ${fn} NAME_WE) 18 get_filename_component (name ${fn} NAME_WE)
@@ -48,13 +56,6 @@ const iBlock ${name} = { &data_${name}_ };
48 file (APPEND ${fnHeader} "${header}") 56 file (APPEND ${fnHeader} "${header}")
49endfunction (embed_write) 57endfunction (embed_write)
50 58
51function (embed_filesize path sizeVar)
52 file (READ ${path} data HEX)
53 string (LENGTH ${data} fsize)
54 math (EXPR fsize "${fsize}/2")
55 set (${sizeVar} ${fsize} PARENT_SCOPE)
56endfunction (embed_filesize)
57
58function (embed_make) 59function (embed_make)
59 set (EMB_H ${CMAKE_CURRENT_BINARY_DIR}/embedded.h) 60 set (EMB_H ${CMAKE_CURRENT_BINARY_DIR}/embedded.h)
60 set (EMB_C ${CMAKE_CURRENT_BINARY_DIR}/embedded.c) 61 set (EMB_C ${CMAKE_CURRENT_BINARY_DIR}/embedded.c)
@@ -88,15 +89,17 @@ function (embed_make)
88 # Collect resources in a single binary file. 89 # Collect resources in a single binary file.
89 set (EMB_BIN ${CMAKE_CURRENT_BINARY_DIR}/resources.binary) 90 set (EMB_BIN ${CMAKE_CURRENT_BINARY_DIR}/resources.binary)
90 file (REMOVE ${EMB_BIN}) 91 file (REMOVE ${EMB_BIN})
91 execute_process (COMMAND cat ${ARGV} OUTPUT_FILE ${EMB_BIN} 92 list (LENGTH ARGV fileCount)
92 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}) 93 execute_process (COMMAND ${BINCAT_COMMAND} ${EMB_BIN} ${ARGV}
94 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
95 OUTPUT_VARIABLE fileSizes
96 )
93 set (offsets) 97 set (offsets)
94 set (fpos 0) 98 set (fpos 0)
95 foreach (fn ${ARGV}) 99 foreach (fileSize ${fileSizes})
96 embed_filesize (${CMAKE_SOURCE_DIR}/${fn} fileSize)
97 list (APPEND offsets "${fpos}") 100 list (APPEND offsets "${fpos}")
98 math (EXPR fpos "${fpos} + ${fileSize}") 101 math (EXPR fpos "${fpos} + ${fileSize}")
99 endforeach (fn) 102 endforeach (fileSize)
100 file (WRITE ${EMB_H} "#include <the_Foundation/block.h>\n 103 file (WRITE ${EMB_H} "#include <the_Foundation/block.h>\n
101#define iHaveLoadEmbed 1 104#define iHaveLoadEmbed 1
102iBool load_Embed(const char *path);\n\n") 105iBool load_Embed(const char *path);\n\n")
@@ -116,7 +119,7 @@ static const iEmbedChunk chunks_Embed_[] = {
116]]) 119]])
117 set (index 0) 120 set (index 0)
118 foreach (fn ${ARGV}) 121 foreach (fn ${ARGV})
119 embed_filesize (${CMAKE_SOURCE_DIR}/${fn} fileSize) 122 list (GET fileSizes ${index} fileSize)
120 list (GET offsets ${index} fpos) 123 list (GET offsets ${index} fpos)
121 file (APPEND ${EMB_C} " { ${fpos}, ${fileSize} },\n") 124 file (APPEND ${EMB_C} " { ${fpos}, ${fileSize} },\n")
122 math (EXPR index "${index} + 1") 125 math (EXPR index "${index} + 1")
diff --git a/res/bincat.c b/res/bincat.c
new file mode 100644
index 00000000..8fc9d831
--- /dev/null
+++ b/res/bincat.c
@@ -0,0 +1,48 @@
1/* Copyright 2020 Jaakko Keränen <jaakko.keranen@iki.fi>
2
3Redistribution and use in source and binary forms, with or without
4modification, are permitted provided that the following conditions are met:
5
61. Redistributions of source code must retain the above copyright notice, this
7 list of conditions and the following disclaimer.
82. Redistributions in binary form must reproduce the above copyright notice,
9 this list of conditions and the following disclaimer in the documentation
10 and/or other materials provided with the distribution.
11
12THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
13ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
14WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
15DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
16ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
17(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
18LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
19ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
20(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
21SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
22
23/* bincat.c: Tiny tool for concatenating binary files */
24
25#include <stdio.h>
26#include <stdlib.h>
27
28int main(int argc, char *argv[]) {
29 const size_t bufSize = 1024 * 256;
30 char *buf = malloc(bufSize);
31 FILE *out = fopen(argv[1], "wb");
32 int i;
33 for (i = 2; i < argc; ++i) {
34 FILE *f = fopen(argv[i], "rb");
35 size_t fileSize = 0;
36 for (;;) {
37 size_t num = fread(buf, 1, bufSize, f);
38 if (num <= 0) break;
39 fileSize += num;
40 fwrite(buf, 1, num, out);
41 }
42 fclose(f);
43 printf("%zu;", fileSize);
44 }
45 fclose(out);
46 free(buf);
47 return 0;
48}