forked from OSGeo/PROJ
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make EMBED_RESOURCE_FILES work with non-C23 compilers
- Loading branch information
Showing
5 changed files
with
140 additions
and
17 deletions.
There are no files selected for viewing
This file contains 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 file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
# Derived from https://gitlab.com/jhamberg/cmake-examples/-/blob/master/cmake/FileEmbed.cmake | ||
# MIT licensed | ||
# Copyright (c) 2022 Jonathan Hamberg | ||
|
||
function(FileEmbedSetup) | ||
|
||
if (NOT EXISTS ${CMAKE_CURRENT_BINARY_DIR}/file_embed) | ||
file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}file_embed) | ||
endif () | ||
|
||
if (NOT EXISTS ${CMAKE_CURRENT_BINARY_DIR}/file_embed/file_embed_empty.c) | ||
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/file_embed/file_embed_empty.c "") | ||
endif () | ||
|
||
add_library(file_embed ${CMAKE_CURRENT_BINARY_DIR}/file_embed/file_embed_empty.c) | ||
target_include_directories(file_embed PUBLIC ${CMAKE_CURRENT_BINARY_DIR}/file_embed) | ||
|
||
endfunction() | ||
|
||
function(FileEmbedAdd file) | ||
FileEmbedGenerate(${file} var) | ||
target_sources(file_embed PUBLIC ${var}) | ||
|
||
add_custom_command( | ||
OUTPUT ${var} | ||
COMMAND ${CMAKE_COMMAND} | ||
-DRUN_FILE_EMBED_GENERATE=1 | ||
"-DFILE_EMBED_GENERATE_PATH=${file}" | ||
-P ${PROJECT_SOURCE_DIR}/cmake/FileEmbed.cmake | ||
MAIN_DEPENDENCY ${file} | ||
) | ||
endfunction() | ||
|
||
function(FileEmbedGenerate file generated_c) | ||
|
||
get_filename_component(base_filename ${file} NAME) | ||
set(output_filename "${base_filename}.c") | ||
string(MAKE_C_IDENTIFIER ${base_filename} c_name) | ||
file(READ ${file} content HEX) | ||
|
||
string(LENGTH "${content}" size) | ||
math(EXPR size_mult_16 "${size} - (${size} % 16)") | ||
string(SUBSTRING "${content}" 0 ${size_mult_16} content_mult_16) | ||
|
||
string(REGEX REPLACE | ||
"([A-Fa-f0-9][A-Fa-f0-9])\ | ||
([A-Fa-f0-9][A-Fa-f0-9])\ | ||
([A-Fa-f0-9][A-Fa-f0-9])\ | ||
([A-Fa-f0-9][A-Fa-f0-9])\ | ||
([A-Fa-f0-9][A-Fa-f0-9])\ | ||
([A-Fa-f0-9][A-Fa-f0-9])\ | ||
([A-Fa-f0-9][A-Fa-f0-9])\ | ||
([A-Fa-f0-9][A-Fa-f0-9])" | ||
"0x\\1,0x\\2,0x\\3,0x\\4,0x\\5,0x\\6,0x\\7,0x\\8,\n" SEPARATED_HEX "${content_mult_16}") | ||
|
||
set(SEPARATED_HEX_REMAINDER "") | ||
if (NOT ${size_mult_16} EQUAL ${size}) | ||
string(SUBSTRING "${content}" ${size_mult_16} 16 content_remainder) | ||
string(REGEX REPLACE "([A-Fa-f0-9][A-Fa-f0-9])" "0x\\1," SEPARATED_HEX_REMAINDER "${content_remainder}") | ||
endif() | ||
|
||
set(output_c "${SEPARATED_HEX}${SEPARATED_HEX_REMAINDER}") | ||
|
||
set(output_c " | ||
#include \"${c_name}.h\" | ||
const uint8_t ${c_name}_data[] = { | ||
${output_c} | ||
}\; | ||
unsigned ${c_name}_size = sizeof(${c_name}_data)\; | ||
") | ||
|
||
set(output_h " | ||
#ifndef ${c_name}_H | ||
#define ${c_name}_H | ||
#include \"stdint.h\" | ||
extern const uint8_t ${c_name}_data[]\; | ||
extern unsigned ${c_name}_size\; | ||
#endif // ${c_name}_H | ||
") | ||
|
||
|
||
if (NOT EXISTS ${CMAKE_CURRENT_BINARY_DIR}/file_embed) | ||
file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}file_embed) | ||
endif () | ||
|
||
|
||
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/file_embed/${c_name}.c | ||
${output_c}) | ||
|
||
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/file_embed/${c_name}.h | ||
${output_h}) | ||
|
||
set(${generated_c} ${CMAKE_CURRENT_BINARY_DIR}/file_embed/${c_name}.c PARENT_SCOPE) | ||
|
||
endfunction() | ||
|
||
if (RUN_FILE_EMBED_GENERATE) | ||
FileEmbedGenerate(${FILE_EMBED_GENERATE_PATH} var) | ||
endif () |
This file contains 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 file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,17 @@ | ||
#include "embedded_resources.h" | ||
|
||
#if USE_SHARP_EMBED | ||
const unsigned char *pj_get_embedded_proj_db(unsigned int *pnSize) { | ||
static const unsigned char proj_db[] = { | ||
#embed PROJ_DB | ||
}; | ||
*pnSize = (unsigned int)sizeof(proj_db); | ||
return proj_db; | ||
} | ||
#else | ||
#include "file_embed/proj_db.h" | ||
const unsigned char *pj_get_embedded_proj_db(unsigned int *pnSize) { | ||
*pnSize = proj_db_size; | ||
return proj_db_data; | ||
} | ||
#endif |
This file contains 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