panda3d/cmake/scripts/ConcatenateToCXX.cmake
Sam Edwards 873c6b48d6 CMake: Generate interrogate_preamble_python_native.cxx at build time
This also makes sure the file is only rebuilt if the inputs
change, saving on unnecessary rebuilds.
2019-04-16 19:44:03 -06:00

55 lines
1.7 KiB
CMake

# Filename: ConcatenateToCXX.cmake
#
# Description: When run, creates a single C++ file which includes a const char[]
# containing the bytes from one or more files.
#
# Usage:
# This script is invoked via add_custom_target, like this:
# cmake -D OUTPUT_FILE="out.cxx" -D SYMBOL_NAME=data -D INPUT_FILES="a.bin b.bin" -P ConcatenateToCXX.cmake
#
if(NOT CMAKE_SCRIPT_MODE_FILE)
message(FATAL_ERROR "ConcatenateToCXX.cmake should not be included but run in script mode.")
return()
endif()
if(NOT DEFINED OUTPUT_FILE)
message(FATAL_ERROR "OUTPUT_FILE should be defined when running ConcatenateToCXX.cmake!")
return()
endif()
if(NOT DEFINED SYMBOL_NAME)
set(SYMBOL_NAME "data")
endif()
file(WRITE "${OUTPUT_FILE}" "/* Generated by CMake. DO NOT EDIT. */\
extern const char ${SYMBOL_NAME}[];
const char ${SYMBOL_NAME}[] = {\n")
separate_arguments(INPUT_FILES)
foreach(infile ${INPUT_FILES})
file(APPEND "${OUTPUT_FILE}" " /* ${infile} */\n")
set(offset 0)
while(1)
# Read up to 1024 bytes from the input file
file(READ "${infile}" data LIMIT 1024 OFFSET ${offset} HEX)
math(EXPR offset "${offset} + 1024")
# If 'data' is empty, we're done
if(NOT data)
break()
endif()
# Format runs of up to 32 hex chars by indenting and giving a newline
string(REGEX REPLACE
"(...?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?)" " \\1\n"
data "${data}")
# Format each byte (2 hex chars) in each line with 0x prefix and comma suffix
string(REGEX REPLACE "([0-9a-fA-F][0-9a-fA-F])" " 0x\\1," data "${data}")
file(APPEND "${OUTPUT_FILE}" "${data}")
endwhile()
endforeach(infile)
file(APPEND "${OUTPUT_FILE}" "};\n")