-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenerateEmbeddedSources.cmake
More file actions
78 lines (66 loc) · 2.75 KB
/
GenerateEmbeddedSources.cmake
File metadata and controls
78 lines (66 loc) · 2.75 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
if(NOT DEFINED MANIFEST)
message(FATAL_ERROR "GenerateEmbeddedSources.cmake requires MANIFEST")
endif()
if(NOT DEFINED OUTPUT)
message(FATAL_ERROR "GenerateEmbeddedSources.cmake requires OUTPUT")
endif()
string(REGEX REPLACE "^\"(.*)\"$" "\\1" MANIFEST "${MANIFEST}")
string(REGEX REPLACE "^\"(.*)\"$" "\\1" OUTPUT "${OUTPUT}")
file(READ "${MANIFEST}" _manifest)
string(REPLACE "\r\n" "\n" _manifest "${_manifest}")
string(REPLACE "\r" "\n" _manifest "${_manifest}")
string(REPLACE "\n" ";" _entries "${_manifest}")
set(_content "// Auto-generated by CMake -- do not edit\n")
string(APPEND _content "#include \"runtime/embedded_modules.h\"\n\n")
# Read each script file and emit it as concatenated raw string chunks.
# MSVC has a 16,380-char limit per string literal (C2026), so split into
# smaller chunks joined by implicit string literal concatenation.
set(_CHUNK_LIMIT 14000)
set(_idx 0)
foreach(_entry ${_entries})
if("${_entry}" STREQUAL "")
continue()
endif()
string(FIND "${_entry}" "|" _sep)
if(_sep LESS 0)
message(FATAL_ERROR "Invalid embedded source manifest entry: ${_entry}")
endif()
math(EXPR _src_start "${_sep} + 1")
string(SUBSTRING "${_entry}" 0 ${_sep} _path)
string(SUBSTRING "${_entry}" ${_src_start} -1 _src_file)
file(READ "${_src_file}" _file_contents)
string(LENGTH "${_file_contents}" _total_len)
if(_total_len LESS_EQUAL ${_CHUNK_LIMIT})
string(APPEND _content "static const char s_src_${_idx}[] = R\"SRCDELIM(${_file_contents})SRCDELIM\";\n\n")
else()
string(APPEND _content "static const char s_src_${_idx}[] =\n")
set(_offset 0)
while(_offset LESS _total_len)
math(EXPR _remaining "${_total_len} - ${_offset}")
if(_remaining LESS_EQUAL ${_CHUNK_LIMIT})
set(_chunk_len ${_remaining})
else()
set(_chunk_len ${_CHUNK_LIMIT})
endif()
string(SUBSTRING "${_file_contents}" ${_offset} ${_chunk_len} _chunk)
string(APPEND _content " R\"SRCDELIM(${_chunk})SRCDELIM\"\n")
math(EXPR _offset "${_offset} + ${_chunk_len}")
endwhile()
string(APPEND _content ";\n\n")
endif()
math(EXPR _idx "${_idx} + 1")
endforeach()
string(APPEND _content "extern const EmbeddedScriptModule g_embedded_script_modules[] = {\n")
set(_idx 0)
foreach(_entry ${_entries})
if("${_entry}" STREQUAL "")
continue()
endif()
string(FIND "${_entry}" "|" _sep)
string(SUBSTRING "${_entry}" 0 ${_sep} _path)
string(APPEND _content " { \"${_path}\", s_src_${_idx} },\n")
math(EXPR _idx "${_idx} + 1")
endforeach()
string(APPEND _content " { nullptr, nullptr },\n")
string(APPEND _content "};\n")
file(WRITE "${OUTPUT}" "${_content}")