Commit 1ec91bc2 authored by Richard Berger's avatar Richard Berger
Browse files

Add CMake function GenerateBinaryHeader to replace xxd steps

parent 19a52dc5
Loading
Loading
Loading
Loading
+16 −0
Original line number Diff line number Diff line
@@ -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)