From 1ec91bc214977c7d00a2eb1f418106dc034436a9 Mon Sep 17 00:00:00 2001 From: Richard Berger Date: Thu, 2 Apr 2020 14:22:04 -0400 Subject: [PATCH] Add CMake function GenerateBinaryHeader to replace xxd steps --- cmake/Modules/LAMMPSUtils.cmake | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/cmake/Modules/LAMMPSUtils.cmake b/cmake/Modules/LAMMPSUtils.cmake index 3ea2b3cb7e..02de630c82 100644 --- a/cmake/Modules/LAMMPSUtils.cmake +++ b/cmake/Modules/LAMMPSUtils.cmake @@ -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)