-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathFileEmbed.cmake
More file actions
89 lines (67 loc) · 2.32 KB
/
FileEmbed.cmake
File metadata and controls
89 lines (67 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
function(FileEmbedSetup)
if (NOT EXISTS ${CMAKE_BINARY_DIR}/file_embed)
file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}file_embed)
endif ()
if (NOT EXISTS ${CMAKE_BINARY_DIR}/file_embed/file_embed_empty.c)
file(WRITE ${CMAKE_BINARY_DIR}/file_embed/file_embed_empty.c "")
endif ()
add_library(file_embed ${CMAKE_BINARY_DIR}/file_embed/file_embed_empty.c)
target_include_directories(file_embed PUBLIC ${CMAKE_BINARY_DIR}/file_embed)
endfunction()
function(FileEmbedAdd file)
FileEmbedGenerate(${file} var)
target_sources(file_embed PUBLIC ${var})
add_custom_command(
OUTPUT ${var}
COMMAND ${CMAKE_COMMAND}
-DRUN_FILE_EMBED_GENERATE=1
-DFILE_EMBED_GENERATE_PATH=${file}
-P ${CMAKE_SOURCE_DIR}/cmake/FileEmbed.cmake
MAIN_DEPENDENCY ${file}
)
endfunction()
function(FileEmbedGenerate file generated_c)
get_filename_component(base_filename ${file} NAME)
set(output_filename "${base_filename}.c")
string(MAKE_C_IDENTIFIER ${base_filename} c_name)
file(READ ${file} content HEX)
message(${content})
# Separate into individual bytes.
string(REGEX MATCHALL "([A-Fa-f0-9][A-Fa-f0-9])" SEPARATED_HEX ${content})
set(output_c "")
set(counter 0)
foreach (hex IN LISTS SEPARATED_HEX)
string(APPEND output_c "0x${hex},")
MATH(EXPR counter "${counter}+1")
if (counter GREATER 16)
string(APPEND output_c "\n ")
set(counter 0)
endif ()
endforeach ()
set(output_c "
#include \"${c_name}.h\"
uint8_t ${c_name}_data[] = {
${output_c}
}\;
unsigned ${c_name}_size = sizeof(${c_name}_data)\;
")
set(output_h "
#ifndef ${c_name}_H
#define ${c_name}_H
#include \"stdint.h\"
extern uint8_t ${c_name}_data[]\;
extern unsigned ${c_name}_size\;
#endif // ${c_name}_H
")
if (NOT EXISTS ${CMAKE_BINARY_DIR}/file_embed)
file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}file_embed)
endif ()
file(WRITE ${CMAKE_BINARY_DIR}/file_embed/${c_name}.c
${output_c})
file(WRITE ${CMAKE_BINARY_DIR}/file_embed/${c_name}.h
${output_h})
set(${generated_c} ${CMAKE_BINARY_DIR}/file_embed/${c_name}.c PARENT_SCOPE)
endfunction()
if (RUN_FILE_EMBED_GENERATE)
FileEmbedGenerate(${FILE_EMBED_GENERATE_PATH} var)
endif ()