singe/cmake/embed.cmake

61 lines
1.8 KiB
CMake

# Embeds a file as a C array. Invoked in script mode by CMakeLists.txt:
# cmake -DINPUT=<file> -DOUTPUT=<header> -DGUARD=<macro> -DSYMBOL=<name> -P embed.cmake
# The header defines the array when EMBED_HERE is defined and declares it extern otherwise.
file(READ "${INPUT}" content HEX)
string(LENGTH "${content}" hexLength)
math(EXPR byteLength "${hexLength} / 2")
# 0x00, 0x01, ... twelve per line.
string(REGEX REPLACE "([0-9a-f][0-9a-f])" "0x\\1, " bytes "${content}")
string(REGEX REPLACE "((0x[0-9a-f][0-9a-f], ){12})" "\\1\n\t" bytes "${bytes}")
string(REGEX REPLACE ", $" "" bytes "${bytes}")
string(REGEX REPLACE ",\n\t$" "" bytes "${bytes}")
file(WRITE "${OUTPUT}" "/*
*
* Singe 2
* Copyright (C) 2006-${COPYRIGHT_END_YEAR} Scott Duensing <scott@kangaroopunch.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 3
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*
*/
#ifndef ${GUARD}
#define ${GUARD}
// ===== THIS FILE IS AUTOMATICALLY GENERATED - DO NOT EDIT =====
#ifdef EMBED_HERE
const unsigned char ${SYMBOL}[] = {
${bytes}
};
const unsigned int ${SYMBOL}_len = ${byteLength};
#else // EMBED_HERE
extern const unsigned char ${SYMBOL}[];
extern const unsigned int ${SYMBOL}_len;
#endif // EMBED_HERE
#endif // ${GUARD}
")