Last active
February 1, 2024 14:17
-
-
Save amir-saniyan/de99cee82fa9d8d615bb69f3f53b6004 to your computer and use it in GitHub Desktop.
Pure CMake function to convert any file into C/C++ source code, implemented with only CMake commands.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#################################################################################################### | |
# This function converts any file into C/C++ source code. | |
# Example: | |
# - input file: data.dat | |
# - output file: data.h | |
# - variable name declared in output file: DATA | |
# - data length: sizeof(DATA) | |
# embed_resource("data.dat" "data.h" "DATA") | |
#################################################################################################### | |
function(embed_resource resource_file_name source_file_name variable_name) | |
if(EXISTS "${source_file_name}") | |
if("${source_file_name}" IS_NEWER_THAN "${resource_file_name}") | |
return() | |
endif() | |
endif() | |
file(READ "${resource_file_name}" hex_content HEX) | |
string(REPEAT "[0-9a-f]" 32 pattern) | |
string(REGEX REPLACE "(${pattern})" "\\1\n" content "${hex_content}") | |
string(REGEX REPLACE "([0-9a-f][0-9a-f])" "0x\\1, " content "${content}") | |
string(REGEX REPLACE ", $" "" content "${content}") | |
set(array_definition "static const unsigned char ${variable_name}[] =\n{\n${content}\n};") | |
set(source "// Auto generated file.\n${array_definition}\n") | |
file(WRITE "${source_file_name}" "${source}") | |
endfunction() |
There is an error in line 21 string does not recognize sub-command REPEAT
CMake 3.10on CMake 3.19 its ok
string(REPEAT <input string> <count> <output variable>)
added in CMake 3.15.
string(REPEAT "[0-9a-f]" 32 pattern)
could be replaced by
foreach(a RANGE 31)
string(CONCAT pattern "[0-9a-f]" pattern)
endforeach(a RANGE 31)
to support CMAke 3.10
How can I call this function? Does this function have a name?
Would please add some for attaching size?
static const unsigned char variable[] = {
};
static const size_t variable_size = N;
I add following lines and it seems work.
STRING(LENGTH "${hex_content}" hex_content_length)
set(size_definition "\nstatic const size_t ${variable_name}_length = ${hex_content_length}\n;")
file(APPEND "${source_file_name}" "${size_definition}")
which yields
// Auto generated file.
static const unsigned char variable[] =
{
...
};
static const size_t variable_length = N;
You can use sizeof
operator:
// Auto generated file.
static const unsigned char variable[] =
{
...
};
// Your code:
static const size_t variable_length = sizeof(variable);
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There is an error in line 21 string does not recognize sub-command REPEAT
CMake 3.10
on CMake 3.19 its ok