Browse Source

CMake: Fix the ConcatenateToCXX script leaving off a null byte

This also adds a _size symbol. Who knows, maybe something will
find that useful.
Sam Edwards 6 years ago
parent
commit
26e60fe106
1 changed files with 18 additions and 2 deletions
  1. 18 2
      cmake/scripts/ConcatenateToCXX.cmake

+ 18 - 2
cmake/scripts/ConcatenateToCXX.cmake

@@ -3,6 +3,12 @@
 # Description: When run, creates a single C++ file which includes a const char[]
 # Description: When run, creates a single C++ file which includes a const char[]
 #   containing the bytes from one or more files.
 #   containing the bytes from one or more files.
 #
 #
+#   There is a {SYMBOL_NAME}_size symbol defined as well, storing the total
+#   number of bytes in the concatenated input files.
+#
+#   A single null terminator byte is added for the benefit of programs that
+#   simply treat the data array as a string.
+#
 # Usage:
 # Usage:
 #   This script is invoked via add_custom_target, like this:
 #   This script is invoked via add_custom_target, like this:
 #   cmake -D OUTPUT_FILE="out.cxx" -D SYMBOL_NAME=data -D INPUT_FILES="a.bin b.bin" -P ConcatenateToCXX.cmake
 #   cmake -D OUTPUT_FILE="out.cxx" -D SYMBOL_NAME=data -D INPUT_FILES="a.bin b.bin" -P ConcatenateToCXX.cmake
@@ -22,10 +28,14 @@ if(NOT DEFINED SYMBOL_NAME)
   set(SYMBOL_NAME "data")
   set(SYMBOL_NAME "data")
 endif()
 endif()
 
 
-file(WRITE "${OUTPUT_FILE}" "/* Generated by CMake.  DO NOT EDIT. */\
+file(WRITE "${OUTPUT_FILE}" "/* Generated by CMake.  DO NOT EDIT. */
+
 extern const char ${SYMBOL_NAME}[];
 extern const char ${SYMBOL_NAME}[];
+extern const int ${SYMBOL_NAME}_size;
+
 const char ${SYMBOL_NAME}[] = {\n")
 const char ${SYMBOL_NAME}[] = {\n")
 
 
+set(byte_count 0)
 separate_arguments(INPUT_FILES)
 separate_arguments(INPUT_FILES)
 foreach(infile ${INPUT_FILES})
 foreach(infile ${INPUT_FILES})
   file(APPEND "${OUTPUT_FILE}" "  /* ${infile} */\n")
   file(APPEND "${OUTPUT_FILE}" "  /* ${infile} */\n")
@@ -41,6 +51,10 @@ foreach(infile ${INPUT_FILES})
       break()
       break()
     endif()
     endif()
 
 
+    # Count the bytes we're adding
+    string(LENGTH "${data}" strlen)
+    math(EXPR byte_count "${byte_count} + (${strlen} / 2)")
+
     # Format runs of up to 32 hex chars by indenting and giving a newline
     # Format runs of up to 32 hex chars by indenting and giving a newline
     string(REGEX REPLACE
     string(REGEX REPLACE
       "(...?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?)" " \\1\n"
       "(...?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?.?)" " \\1\n"
@@ -51,4 +65,6 @@ foreach(infile ${INPUT_FILES})
   endwhile()
   endwhile()
 endforeach(infile)
 endforeach(infile)
 
 
-file(APPEND "${OUTPUT_FILE}" "};\n")
+file(APPEND "${OUTPUT_FILE}" "  0\n};
+
+extern const int ${SYMBOL_NAME}_size = ${byte_count};\n")