Skip to content

Commit 55122c1

Browse files
committed
Prevent binary data regeneration at compile time for faster builds
1 parent ad91ec9 commit 55122c1

File tree

5 files changed

+127
-29
lines changed

5 files changed

+127
-29
lines changed

CMakeLists.txt

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,6 @@ file(GLOB StandaloneBinarySources
158158
${CMAKE_CURRENT_SOURCE_DIR}/Resources/Extra/GeneralUser_GS.sf3
159159
)
160160

161-
juce_add_binary_data(PlugDataBinaryData SOURCES ${plugdata_resources})
162-
set_target_properties(PlugDataBinaryData PROPERTIES POSITION_INDEPENDENT_CODE ON)
163-
164161
file(GLOB plugdata_global_sources
165162
${CMAKE_CACHEFILE_DIR}/plugdata_artefacts/JuceLibraryCode/JuceHeader.h)
166163

@@ -298,9 +295,13 @@ else()
298295
add_compile_definitions(NANOVG_GL3_IMPLEMENTATION=1)
299296
endif()
300297

298+
file(GLOB BINARY_DATA_FILES ${CMAKE_CURRENT_BINARY_DIR}/BinaryData/*.cpp)
299+
add_library(BinaryData STATIC ${BINARY_DATA_FILES})
300+
target_include_directories(BinaryData INTERFACE ${CMAKE_CURRENT_BINARY_DIR}/BinaryData)
301+
301302
set(libs
302303
juce
303-
PlugDataBinaryData
304+
BinaryData
304305
nanovg
305306
)
306307

@@ -592,10 +593,10 @@ elseif(APPLE)
592593
if(NOT "${CMAKE_SYSTEM_NAME}" MATCHES "iOS")
593594
set(LINK_CARBON "-framework Carbon")
594595
endif()
595-
target_link_libraries(plugdata_standalone PRIVATE plugdata_core pd-src externals ${LINK_CARBON} ${MACOS_COMPAT_LINKER_FLAGS})
596-
target_link_libraries(plugdata_midi PRIVATE plugdata_core pd-src-multi externals-multi ${LINK_CARBON} ${MACOS_COMPAT_LINKER_FLAGS})
597-
target_link_libraries(plugdata PRIVATE plugdata_core pd-src-multi externals-multi ${LINK_CARBON} ${MACOS_COMPAT_LINKER_FLAGS})
598-
target_link_libraries(plugdata_fx PRIVATE plugdata_core pd-src-multi externals-multi ${LINK_CARBON} ${MACOS_COMPAT_LINKER_FLAGS})
596+
target_link_libraries(plugdata_standalone PRIVATE plugdata_core pd-src externals ${LINK_CARBON} $<$<NOT:$<CONFIG:Debug>>:${MACOS_COMPAT_LINKER_FLAGS}>)
597+
target_link_libraries(plugdata_midi PRIVATE plugdata_core pd-src-multi externals-multi ${LINK_CARBON} $<$<NOT:$<CONFIG:Debug>>:${MACOS_COMPAT_LINKER_FLAGS}>)
598+
target_link_libraries(plugdata PRIVATE plugdata_core pd-src-multi externals-multi ${LINK_CARBON} $<$<NOT:$<CONFIG:Debug>>:${MACOS_COMPAT_LINKER_FLAGS}>)
599+
target_link_libraries(plugdata_fx PRIVATE plugdata_core pd-src-multi externals-multi ${LINK_CARBON} $<$<NOT:$<CONFIG:Debug>>:${MACOS_COMPAT_LINKER_FLAGS}>)
599600
else()
600601
target_link_libraries(plugdata PRIVATE plugdata_core pd-multi)
601602
target_link_libraries(plugdata_fx PRIVATE plugdata_core pd-multi)

Resources/Fonts/IBMPlexMono.ttf

-133 KB
Binary file not shown.

Resources/Scripts/package_resources.py

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import sys
66
import platform
77
import zipfile
8+
import binascii
89

910
#Parse arguments
1011
value_mappings = {
@@ -93,6 +94,101 @@ def replaceTextInFolder(folder_path, old_string, new_string):
9394
for dir_name in dirs:
9495
replaceTextInFolder(os.path.join(root, dir_name), old_string, new_string)
9596

97+
def expand_glob_list(file_patterns):
98+
expanded_files = []
99+
for pattern in file_patterns:
100+
expanded_files.extend(glob.glob(pattern))
101+
return expanded_files
102+
103+
def hash_resource_name(resource_name):
104+
# Hashes a resource name using the same algorithm as in the C++ code.
105+
hash_val = 0
106+
for char in resource_name:
107+
hash_val = 31 * hash_val + ord(char)
108+
return hash_val & 0xFFFFFFFF # Limit to 32-bit hash
109+
110+
def generate_binary_data(output_dir, file_list):
111+
os.makedirs(output_dir, exist_ok=True) # Ensure output directory exists
112+
file_data = [] # To store metadata for each file
113+
114+
# Gather file information and binary content
115+
for index, filepath in enumerate(file_list):
116+
filename = os.path.basename(filepath)
117+
if os.path.isfile(filepath):
118+
with open(filepath, "rb") as file:
119+
binary_content = file.read()
120+
121+
var_name = filename.replace('.', '_').replace('-', '_')
122+
temp_name = f"temp_binary_data_{index}"
123+
data = {
124+
"filename": filename,
125+
"var_name": var_name,
126+
"temp_name": temp_name,
127+
"size": len(binary_content),
128+
"binary": list(binary_content),
129+
"index": str(index),
130+
"hash": hash_resource_name(var_name)
131+
}
132+
file_data.append(data)
133+
134+
# Generate header file
135+
with open(output_dir + "/BinaryData.h", "w") as header:
136+
header.write("namespace BinaryData\n{\n")
137+
for data in file_data:
138+
header.write(f" extern const char* {data['var_name']};\n")
139+
header.write(f" const int {data['var_name']}Size = {data['size']};\n")
140+
141+
header.write("\n const int namedResourceListSize = {};\n".format(len(file_data)))
142+
header.write(" extern const char* namedResourceList[];\n")
143+
header.write(" extern const char* originalFilenames[];\n")
144+
header.write(" const char* getNamedResource (const char* resourceNameUTF8, int& dataSizeInBytes);\n")
145+
header.write("}\n")
146+
147+
with open(output_dir + "/BinaryData.cpp", "w") as source:
148+
source.write("#include \"BinaryData.h\"\n\n")
149+
source.write("namespace BinaryData\n{\n\n")
150+
151+
source.write("\n const char* namedResourceList[] = {\n")
152+
for data in file_data:
153+
source.write(f" \"{data['var_name']}\",\n")
154+
source.write(" };\n\n")
155+
156+
source.write("const char* getNamedResource (const char* resourceNameUTF8, int& numBytes)\n")
157+
source.write("{\n")
158+
source.write(" unsigned int hash = 0;\n")
159+
source.write(" if (resourceNameUTF8 != nullptr)\n")
160+
source.write(" while (*resourceNameUTF8 != 0)\n")
161+
source.write(" hash = 31 * hash + (unsigned int) *resourceNameUTF8++;\n\n")
162+
163+
source.write(" switch (hash)\n {\n")
164+
for data in file_data:
165+
source.write(f" case 0x{data['hash']:08x}: numBytes = {data['size']}; return {data['var_name']};\n")
166+
source.write(" default: break;\n")
167+
source.write(" }\n\n")
168+
169+
source.write(" numBytes = 0;\n")
170+
source.write(" return nullptr;\n")
171+
source.write("}\n")
172+
source.write("}\n")
173+
174+
# Generate .cpp files for each binary resource
175+
for data in file_data:
176+
cpp_filename = output_dir + "/BinaryData_" + data["index"] + ".cpp"
177+
with open(cpp_filename, "w") as cpp_file:
178+
cpp_file.write("namespace BinaryData\n{\n")
179+
cpp_file.write(f"//================== {data['filename']} ==================\n")
180+
cpp_file.write(f"static const unsigned char {data['temp_name']}[] =\n{{\n")
181+
182+
# Write binary data in readable hex format
183+
binary_chunks = [data['binary'][i:i+12] for i in range(0, len(data['binary']), 12)]
184+
for chunk in binary_chunks:
185+
hex_values = ', '.join(f"0x{binascii.hexlify(byte.to_bytes(1, 'big')).decode('utf-8').upper()}" for byte in chunk)
186+
cpp_file.write(f" {hex_values},\n")
187+
188+
cpp_file.write("};\n\n")
189+
cpp_file.write(f"const char* {data['var_name']} = (const char*) {data['temp_name']};\n")
190+
cpp_file.write("}\n")
191+
96192

97193
if existsAsFile("../Filesystem.zip"):
98194
removeFile("../Filesystem.zip")
@@ -204,3 +300,19 @@ def replaceTextInFolder(folder_path, old_string, new_string):
204300

205301
splitFile("./Filesystem.zip", output_dir + "/Filesystem_%i.zip", 12)
206302
removeFile("./Filesystem.zip")
303+
304+
generate_binary_data("../BinaryData", expand_glob_list({
305+
project_root + "/Resources/Fonts/IconFont.ttf",
306+
project_root + "/Resources/Fonts/InterTabular.ttf",
307+
project_root + "/Resources/Fonts/InterBold.ttf",
308+
project_root + "/Resources/Fonts/InterSemiBold.ttf",
309+
project_root + "/Resources/Fonts/InterThin.ttf",
310+
project_root + "/Resources/Fonts/InterVariable.ttf",
311+
project_root + "/Resources/Fonts/InterRegular.ttf",
312+
project_root + "/Resources/Fonts/RobotoMono-Regular.ttf",
313+
project_root + "/Resources/Icons/plugdata_large_logo.png",
314+
project_root + "/Resources/Icons/plugdata_logo.png",
315+
"Documentation.bin",
316+
"InterUnicode_*.ttf",
317+
"Filesystem_*.zip"
318+
}))

Source/Utility/Fonts.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ struct Fonts {
4444
boldTypeface = Typeface::createSystemTypefaceFor(BinaryData::InterBold_ttf, BinaryData::InterBold_ttfSize);
4545
semiBoldTypeface = Typeface::createSystemTypefaceFor(BinaryData::InterSemiBold_ttf, BinaryData::InterSemiBold_ttfSize);
4646
iconTypeface = Typeface::createSystemTypefaceFor(BinaryData::IconFont_ttf, BinaryData::IconFont_ttfSize);
47-
monoTypeface = Typeface::createSystemTypefaceFor(BinaryData::RobotoMonoRegular_ttf, BinaryData::RobotoMonoRegular_ttfSize);
47+
monoTypeface = Typeface::createSystemTypefaceFor(BinaryData::RobotoMono_Regular_ttf, BinaryData::RobotoMono_Regular_ttfSize);
4848
variableTypeface = Typeface::createSystemTypefaceFor(BinaryData::InterVariable_ttf, BinaryData::InterVariable_ttfSize);
4949
tabularTypeface = Typeface::createSystemTypefaceFor(BinaryData::InterTabular_ttf, BinaryData::InterTabular_ttfSize);
5050

Source/Utility/OSUtils.cpp

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,14 @@
1414
# include <raw_keyboard_input/raw_keyboard_input.cpp>
1515
#endif
1616

17-
#if defined(__APPLE__)
18-
# define HAS_STD_FILESYSTEM 0
19-
#elif defined(__unix__)
20-
# if defined(__cpp_lib_filesystem) || defined(__cpp_lib_experimental_filesystem)
21-
# define HAS_STD_FILESYSTEM 1
22-
# else
23-
# define HAS_STD_FILESYSTEM 0
24-
# endif
25-
#elif defined(_WIN32) || defined(_WIN64)
26-
# define HAS_STD_FILESYSTEM 1
27-
#endif
28-
29-
#if HAS_STD_FILESYSTEM
30-
# if defined(__cpp_lib_filesystem)
31-
# include <filesystem>
17+
#if defined(__cpp_lib_filesystem)
18+
#include <filesystem>
3219
namespace fs = std::filesystem;
33-
# elif defined(__cpp_lib_experimental_filesystem)
34-
# include <experimental/filesystem>
20+
#elif defined(__cpp_lib_experimental_filesystem)
21+
#include <experimental/filesystem>
3522
namespace fs = std::experimental::filesystem;
36-
# endif
3723
#else
38-
39-
# include <ghc_filesystem/include/ghc/filesystem.hpp>
24+
#include <ghc_filesystem/include/ghc/filesystem.hpp>
4025
namespace fs = ghc::filesystem;
4126
#endif
4227

0 commit comments

Comments
 (0)