-
Notifications
You must be signed in to change notification settings - Fork 103
/
CMakeXxdImpl.txt
49 lines (44 loc) · 1.96 KB
/
CMakeXxdImpl.txt
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
# CMAKE_XXD
# expected defines:
# INPUT_FILE_NAME - file name
# FILE_TYPE - "TEXT" or "BIN"
if("${TMP_DIR}" STREQUAL "")
set(XXD_TMP_DIR ".")
else()
set(XXD_TMP_DIR "${TMP_DIR}")
endif()
get_filename_component(INPUT_SRC ${INPUT_FILE_NAME} ABSOLUTE)
get_filename_component(INPUT_DIR ${INPUT_SRC} DIRECTORY)
get_filename_component(INPUT_NAME ${INPUT_FILE_NAME} NAME)
string(REPLACE "." "_" CPP_STR_CONST_NAME ${INPUT_NAME})
file(READ ${INPUT_DIR}/${INPUT_NAME} HEX_CONTENTS HEX)
string(REGEX MATCHALL "([A-Za-z0-9][A-Za-z0-9])" SEPARATED_HEX "${HEX_CONTENTS}")
list(JOIN SEPARATED_HEX ",0x" FORMATTED_HEX)
set(FORMATTED_HEX "0x${FORMATTED_HEX}")
if(${FILE_TYPE} MATCHES "TEXT")
string(REPLACE "0x0D," "" FORMATTED_HEX ${FORMATTED_HEX})
string(REPLACE "0x0d," "" FORMATTED_HEX ${FORMATTED_HEX})
string(REPLACE "0x0A," "0x0A,," FORMATTED_HEX ${FORMATTED_HEX})
string(REPLACE "0x0a," "0x0a,," FORMATTED_HEX ${FORMATTED_HEX})
elseif(${FILE_TYPE} MATCHES "BIN")
else()
message(FATAL_ERROR "CMAKE_XXD: expected \"TEXT\" or \"BIN\" in FILE_TYPE")
endif()
string(REGEX MATCHALL "([^,]+,)([^,]+,)?([^,]+,)?([^,]+,)?([^,]+,)?([^,]+,)?([^,]+,)?([^,]+,)?"
SEPARATED_LINES "${FORMATTED_HEX}, ")
list(JOIN SEPARATED_LINES "\n" FORMATTED_HEX)
file(WRITE ${XXD_TMP_DIR}/${INPUT_NAME}.inc.temp "static unsigned char ${CPP_STR_CONST_NAME}[] = {\n${FORMATTED_HEX}\n};\n")
file(READ ${XXD_TMP_DIR}/${INPUT_NAME}.inc.temp TEMP_CONTENT HEX)
if(EXISTS ${INPUT_DIR}/${INPUT_NAME}.inc)
file(READ ${INPUT_DIR}/${INPUT_NAME}.inc DEST_CONTENT HEX)
else()
set(DEST_CONTENT "")
endif()
if(NOT "${TEMP_CONTENT}" STREQUAL "${DEST_CONTENT}")
file(REMOVE ${INPUT_DIR}/${INPUT_NAME}.inc)
file(RENAME ${XXD_TMP_DIR}/${INPUT_NAME}.inc.temp ${INPUT_DIR}/${INPUT_NAME}.inc)
message(STATUS "File ${INPUT_FILE_NAME} converted to C-array ${INPUT_DIR}/${INPUT_NAME}.inc")
else()
file(REMOVE ${XXD_TMP_DIR}/${INPUT_NAME}.inc.temp)
message(STATUS "XXD: ${INPUT_FILE_NAME} has not been modified. C-array generation is skipped.")
endif()