Add CMake function GenerateBinaryHeader to replace xxd steps

This commit is contained in:
Richard Berger 2020-04-02 14:22:04 -04:00
parent 19a52dc506
commit 1ec91bc214
1 changed files with 16 additions and 0 deletions

View File

@ -69,3 +69,19 @@ macro(pkg_depends PKG1 PKG2)
message(FATAL_ERROR "${PKG1} package needs LAMMPS to be build with ${PKG2}")
endif()
endmacro()
# CMake-only replacement for bin2c and xxd
function(GenerateBinaryHeader varname outfile files)
message("Creating ${outfile}...")
file(WRITE ${outfile} "// CMake generated file\n")
math(EXPR ARG_END "${ARGC}-1")
foreach(IDX RANGE 2 ${ARG_END})
list(GET ARGV ${IDX} filename)
file(READ ${filename} content HEX)
string(REGEX REPLACE "([0-9a-f][0-9a-f])" "0x\\1," content "${content}")
string(REGEX REPLACE ",$" "" content "${content}")
file(APPEND ${outfile} "const unsigned char ${varname}[] = { ${content} };\n")
file(APPEND ${outfile} "const unsigned int ${varname}_size = sizeof(${varname});\n")
endforeach()
endfunction(GenerateBinaryHeader)