diff options
author | Jaakko Keränen <jaakko.keranen@iki.fi> | 2020-07-21 15:06:52 +0300 |
---|---|---|
committer | Jaakko Keränen <jaakko.keranen@iki.fi> | 2020-07-21 15:07:38 +0300 |
commit | d773b499e595a43b9b1ae449262dcf13cabf2d02 (patch) | |
tree | b1baeb12025a04f8316636b5d0ab18e30ceedb2c /Embed.cmake |
Initial commit
Borrowing the app skeleton from Bitwise Harmony.
Diffstat (limited to 'Embed.cmake')
-rw-r--r-- | Embed.cmake | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/Embed.cmake b/Embed.cmake new file mode 100644 index 00000000..9b506905 --- /dev/null +++ b/Embed.cmake | |||
@@ -0,0 +1,66 @@ | |||
1 | function (embed_getname output fn) | ||
2 | get_filename_component (name ${fn} NAME_WE) | ||
3 | string (REPLACE "-" "" name ${name}) | ||
4 | string (SUBSTRING ${name} 0 1 first) | ||
5 | string (TOUPPER ${first} first) | ||
6 | string (SUBSTRING ${name} 1 -1 remainder) | ||
7 | set (name "${first}${remainder}") | ||
8 | get_filename_component (ext ${fn} EXT) | ||
9 | if (ext STREQUAL .ttf) | ||
10 | set (resName "font") | ||
11 | elseif (ext STREQUAL .png) | ||
12 | set (resName "image") | ||
13 | else () | ||
14 | set (resName "blob") | ||
15 | endif () | ||
16 | set (resName "${resName}${name}_Embedded" PARENT_SCOPE) | ||
17 | endfunction (embed_getname) | ||
18 | |||
19 | function (embed_write path name fnSource fnHeader) | ||
20 | message (STATUS "${path}") | ||
21 | file (READ ${path} fileData HEX) | ||
22 | string (REGEX REPLACE "([0-9a-f][0-9a-f])" "0x\\1," cList ${fileData}) | ||
23 | string (REGEX REPLACE | ||
24 | "(0x[0-9a-f][0-9a-f],0x[0-9a-f][0-9a-f],0x[0-9a-f][0-9a-f],0x[0-9a-f][0-9a-f],0x[0-9a-f][0-9a-f],0x[0-9a-f][0-9a-f],0x[0-9a-f][0-9a-f],0x[0-9a-f][0-9a-f],0x[0-9a-f][0-9a-f],0x[0-9a-f][0-9a-f],0x[0-9a-f][0-9a-f],0x[0-9a-f][0-9a-f],0x[0-9a-f][0-9a-f],0x[0-9a-f][0-9a-f],0x[0-9a-f][0-9a-f],0x[0-9a-f][0-9a-f],)" | ||
25 | "\\1\n " cList ${cList}) | ||
26 | string (LENGTH ${fileData} len) | ||
27 | math (EXPR alen "${len} / 2") | ||
28 | set (src "static const uint8_t bytes_${name}_[] = {\n") | ||
29 | string (APPEND src " ${cList}\n") | ||
30 | string (APPEND src "}; | ||
31 | static iBlockData data_${name}_ = { | ||
32 | .refCount = 2, .data = iConstCast(char *, bytes_${name}_), .size = ${alen}, .allocSize = ${alen} | ||
33 | }; | ||
34 | const iBlock ${name} = { &data_${name}_ }; | ||
35 | ") | ||
36 | set (header "extern const iBlock ${name};\n") | ||
37 | # Output the results. | ||
38 | file (APPEND ${fnSource} "${src}") | ||
39 | file (APPEND ${fnHeader} "${header}") | ||
40 | endfunction (embed_write) | ||
41 | |||
42 | function (embed_make) | ||
43 | set (EMB_H ${CMAKE_CURRENT_BINARY_DIR}/embedded.h) | ||
44 | set (EMB_C ${CMAKE_CURRENT_BINARY_DIR}/embedded.c) | ||
45 | set (needGen NO) | ||
46 | if (NOT EXISTS ${EMB_H} OR NOT EXISTS ${EMB_C}) | ||
47 | set (needGen YES) | ||
48 | else () | ||
49 | file (TIMESTAMP ${EMB_H} genTime %s) | ||
50 | foreach (resPath ${ARGV}) | ||
51 | set (fn "${CMAKE_CURRENT_LIST_DIR}/${resPath}") | ||
52 | file (TIMESTAMP ${fn} resTime %s) | ||
53 | if (${resTime} GREATER ${genTime}) | ||
54 | set (needGen YES) | ||
55 | endif () | ||
56 | endforeach (resPath) | ||
57 | endif () | ||
58 | if (needGen) | ||
59 | file (WRITE ${EMB_H} "#include <the_Foundation/block.h>\n") | ||
60 | file (WRITE ${EMB_C} "#include \"embedded.h\"\n") | ||
61 | foreach (fn ${ARGV}) | ||
62 | embed_getname (resName ${fn}) | ||
63 | embed_write (${fn} ${resName} ${EMB_C} ${EMB_H}) | ||
64 | endforeach (fn) | ||
65 | endif () | ||
66 | endfunction (embed_make) | ||