aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChris Robinson <[email protected]>2020-04-04 08:46:18 -0700
committerChris Robinson <[email protected]>2020-04-04 08:46:18 -0700
commit431d01cc7f271c6bcf4aaf931a4df760bae2847e (patch)
treed5bda0bc2b617ecda212284cc939720247a5b381
parenta0b7638d635d4f583ecc1a48c193629868cf684a (diff)
Use a cmake script to convert a binary file to a header file
-rw-r--r--CMakeLists.txt39
-rw-r--r--cmake/bin2h.script.cmake13
2 files changed, 17 insertions, 35 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 5d3df4ed..2b81c24a 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1049,39 +1049,6 @@ ELSE()
ENDIF()
-SET(NATIVE_SRC_DIR "${OpenAL_SOURCE_DIR}/native-tools")
-
-SET(ALSOFT_NATIVE_TOOLS_PATH "" CACHE STRING "Path to prebuilt native tools (leave blank to auto-build)")
-IF(ALSOFT_NATIVE_TOOLS_PATH)
- find_program(BIN2H_NATIVE_COMMAND NAMES bin2h
- PATHS "${ALSOFT_NATIVE_TOOLS_PATH}"
- NO_DEFAULT_PATH)
- if(NOT BIN2H_NATIVE_COMMAND)
- message(FATAL_ERROR "Failed to find native tools in ${ALSOFT_NATIVE_TOOLS_PATH}.
-bin2h: ${BIN2H_NATIVE_COMMAND}")
- endif()
- SET(BIN2H_COMMAND ${BIN2H_NATIVE_COMMAND})
-ELSE()
- SET(NATIVE_BIN_DIR "${OpenAL_BINARY_DIR}/native-tools")
- FILE(MAKE_DIRECTORY "${NATIVE_BIN_DIR}")
-
- SET(BIN2H_COMMAND "${NATIVE_BIN_DIR}/bin2h")
- ADD_CUSTOM_COMMAND(OUTPUT "${BIN2H_COMMAND}"
- COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" "${NATIVE_SRC_DIR}"
- COMMAND ${CMAKE_COMMAND} -E remove "${BIN2H_COMMAND}"
- COMMAND ${CMAKE_COMMAND} --build . --config "Release"
- WORKING_DIRECTORY "${NATIVE_BIN_DIR}"
- DEPENDS "${NATIVE_SRC_DIR}/CMakeLists.txt"
- IMPLICIT_DEPENDS
- C "${NATIVE_SRC_DIR}/bin2h.c"
- VERBATIM
- )
-ENDIF()
-ADD_CUSTOM_TARGET(native-tools
- DEPENDS "${BIN2H_COMMAND}"
- VERBATIM
-)
-
option(ALSOFT_EMBED_HRTF_DATA "Embed the HRTF data files (increases library footprint)" ON)
if(ALSOFT_EMBED_HRTF_DATA)
MACRO(make_hrtf_header FILENAME VARNAME)
@@ -1089,8 +1056,10 @@ if(ALSOFT_EMBED_HRTF_DATA)
SET(outfile "${OpenAL_BINARY_DIR}/${VARNAME}.h")
ADD_CUSTOM_COMMAND(OUTPUT "${outfile}"
- COMMAND "${BIN2H_COMMAND}" "${infile}" "${outfile}" ${VARNAME}
- DEPENDS native-tools "${infile}"
+ COMMAND ${CMAKE_COMMAND} -D "INPUT_FILE=${infile}" -D "OUTPUT_FILE=${outfile}"
+ -D "VARIABLE_NAME=${VARNAME}" -P "${CMAKE_MODULE_PATH}/bin2h.script.cmake"
+ WORKING_DIRECTORY "${OpenAL_SOURCE_DIR}"
+ DEPENDS "${infile}" "${CMAKE_MODULE_PATH}/bin2h.script.cmake"
VERBATIM
)
SET(ALC_OBJS ${ALC_OBJS} "${outfile}")
diff --git a/cmake/bin2h.script.cmake b/cmake/bin2h.script.cmake
new file mode 100644
index 00000000..1438fde2
--- /dev/null
+++ b/cmake/bin2h.script.cmake
@@ -0,0 +1,13 @@
+# Read the input file into 'indata', converting each byte to a pair of hex
+# characters
+file(READ "${INPUT_FILE}" indata HEX)
+
+# For each pair of characters, indent them and prepend the 0x prefix, and
+# append a comma separateor.
+# TODO: Prettify this. Should group a number of bytes per line instead of one
+# per line.
+string(REGEX REPLACE "(..)" " 0x\\1,\n" output "${indata}")
+
+# Write the list of hex chars to the output file in a const byte array
+file(WRITE "${OUTPUT_FILE}"
+ "const unsigned char ${VARIABLE_NAME}[] = {\n${output}};\n")