-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary_resource.cmake
67 lines (59 loc) · 2.03 KB
/
binary_resource.cmake
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
if (NOT TARGET bin2c)
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/bin2c ${CMAKE_BINARY_DIR}/bin2c)
endif()
function(make_binary_resource)
set(opts GZIP)
set(val_args INPUT ARRAY_NAME OUTPUT EXTENSION)
set(mval_args PREFIX)
cmake_parse_arguments(BINRES "${opts}" "${val_args}" "${mval_args}" ${ARGN})
if (NOT BINRES_EXTENSION)
set(BINRES_EXTENSION ".c")
endif()
if (NOT BINRES_INPUT)
message(FATAL_ERROR "make_binary_resource: no input specified")
endif()
if (NOT BINRES_OUTPUT AND NOT BINRES_ARRAY_NAME)
get_filename_component(in_basename "${BINRES_INPUT}" NAME_WE)
set(BINRES_OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${in_basename}${BINRES_EXTENSION}")
endif()
if (BINRES_OUTPUT AND NOT BINRES_ARRAY_NAME)
get_filename_component(filename "${BINRES_OUTPUT}" NAME_WE)
string(MAKE_C_IDENTIFIER ${filename} BINRES_ARRAY_NAME)
elseif(NOT BINRES_OUTPUT AND BINRES_ARRAY_NAME)
set(BINRES_OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${BINRES_ARRAY_NAME}${BINRES_EXTENSION}")
elseif(NOT BINRES_OUTPUT AND NOT BINRES_ARRAY_NAME)
message(FATAL_ERROR "make_binary_resource: Unable to deduce output path and array name")
endif()
set_directory_properties(PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES "${BINRES_OUTPUT}")
if (BINRES_PREFIX)
set(prefix_option -p "\"${BINRES_PREFIX}\"")
else()
set(prefix_option "")
endif()
if (BINRES_GZIP)
add_custom_command(
OUTPUT "${BINRES_OUTPUT}"
COMMAND gzip "${BINRES_INPUT}" --stdout | $<TARGET_FILE:bin2c>
-a ${BINRES_ARRAY_NAME}
-l 15
-o ${BINRES_OUTPUT}
${prefix_option}
DEPENDS bin2c ${BINRES_INPUT}
COMMENT "Making gzipped binary resource for ${BINRES_ARRAY_NAME}"
WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
)
else()
add_custom_command(
OUTPUT "${BINRES_OUTPUT}"
COMMAND $<TARGET_FILE:bin2c>
-i "${BINRES_INPUT}"
-a ${BINRES_ARRAY_NAME}
-l 15
-o ${BINRES_OUTPUT}
${prefix_option}
DEPENDS bin2c ${BINRES_INPUT}
COMMENT "Making binary resource for ${BINRES_ARRAY_NAME}"
WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
)
endif()
endfunction()