cmake: fix C identifier generation from embedded file

This commit is contained in:
Renz Bagaporo
2020-06-05 12:55:28 +08:00
parent 4395be9697
commit 01a7db799f
9 changed files with 47 additions and 18 deletions

View File

@@ -38,13 +38,11 @@ string(REGEX REPLACE "[^\n]+$" ".byte \\0\n" data "${data}")
string(REGEX REPLACE "[0-9a-f][0-9a-f]" "0x\\0, " data "${data}") # hex formatted C bytes
string(REGEX REPLACE ", \n" "\n" data "${data}") # trim the last comma
## Come up with C-friendly variable name based on source file
# unless VARIABLE_BASENAME is set
# Use source file name as variable name unless VARIABLE_BASENAME is set
if(NOT VARIABLE_BASENAME)
get_filename_component(source_filename "${DATA_FILE}" NAME)
string(MAKE_C_IDENTIFIER "${source_filename}" varname)
get_filename_component(varname "${DATA_FILE}" NAME)
else()
string(MAKE_C_IDENTIFIER "${VARIABLE_BASENAME}" varname)
set(varname "${VARIABLE_BASENAME}")
endif()
function(append str)
@@ -55,13 +53,14 @@ function(append_line str)
append("${str}\n")
endfunction()
function(append_identifier symbol)
append_line("\n.global ${symbol}")
append("${symbol}:")
if(${ARGC} GREATER 1) # optional comment
append(" /* ${ARGV1} */")
endif()
append("\n")
function(make_and_append_identifier str)
string(MAKE_C_IDENTIFIER "${str}" symbol)
append_line("\n.global ${symbol}")
append("${symbol}:")
if(${ARGC} GREATER 1) # optional comment
append(" /* ${ARGV1} */")
endif()
append("\n")
endfunction()
file(WRITE "${SOURCE_FILE}" "/*")
@@ -73,16 +72,15 @@ append_line(" */")
append_line(".data")
append_line(".section .rodata.embedded")
append_identifier("${varname}")
append_identifier("_binary_${varname}_start" "for objcopy compatibility")
make_and_append_identifier("${varname}")
make_and_append_identifier("_binary_${varname}_start" "for objcopy compatibility")
append("${data}")
append_identifier("_binary_${varname}_end" "for objcopy compatibility")
make_and_append_identifier("_binary_${varname}_end" "for objcopy compatibility")
append_line("")
if(FILE_TYPE STREQUAL "TEXT")
append_identifier("${varname}_length" "not including null byte")
make_and_append_identifier("${varname}_length" "not including null byte")
else()
append_identifier("${varname}_length")
make_and_append_identifier("${varname}_length")
endif()
append_line(".word ${data_len}")