ResourceCompiler.cmake 1.7 KB

12345678910111213141516171819202122232425262728293031323334
  1. set(compile_resource__internal_dir ${CMAKE_CURRENT_LIST_DIR} CACHE INTERNAL "")
  2. find_program(XXD_EXECUTABLE xxd)
  3. if(NOT XXD_EXECUTABLE)
  4. find_program(PERL_EXECUTABLE perl)
  5. if(NOT PERL_EXECUTABLE)
  6. message(FATAL_ERROR "Neither xxd nor perl found in PATH. xxd is usually contained in your distributions vim-common package!")
  7. else()
  8. message("-- xxd not found, using xxdi.pl instead")
  9. set(XXD_EXECUTABLE "${compile_resource__internal_dir}/xxdi.pl")
  10. set(XXD_PARAMS "")
  11. endif()
  12. else()
  13. set(XXD_PARAMS -i)
  14. endif()
  15. find_program(SED_EXECUTABLE sed)
  16. if(NOT SED_EXECUTABLE)
  17. message(FATAL_ERROR "sed not found in path!")
  18. endif()
  19. function(compile_resource INPUT_FILE OUTPUT_FILE)
  20. get_filename_component(INPUT_FILE_NAME ${PROJECT_SOURCE_DIR}/data/${INPUT_FILE} NAME)
  21. set(OUTPUT_FILE ${PROJECT_BINARY_DIR}/generated/${INPUT_FILE_NAME}.h PARENT_SCOPE)
  22. add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/generated/${INPUT_FILE_NAME}.h
  23. COMMAND mkdir -p ${PROJECT_BINARY_DIR}/generated
  24. COMMAND ${XXD_EXECUTABLE} ${XXD_PARAMS} ${INPUT_FILE} > ${PROJECT_BINARY_DIR}/generated/${INPUT_FILE_NAME}.h
  25. COMMAND ${SED_EXECUTABLE} 's!unsigned char!static const unsigned char!' < ${PROJECT_BINARY_DIR}/generated/${INPUT_FILE_NAME}.h > ${PROJECT_BINARY_DIR}/generated/${INPUT_FILE_NAME}.h.tmp
  26. COMMAND mv -f ${PROJECT_BINARY_DIR}/generated/${INPUT_FILE_NAME}.h.tmp ${PROJECT_BINARY_DIR}/generated/${INPUT_FILE_NAME}.h
  27. WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/data/
  28. DEPENDS ${PROJECT_SOURCE_DIR}/data/${INPUT_FILE})
  29. set_source_files_properties(${PROJECT_BINARY_DIR}/generated/${INPUT_FILE_NAME}.h PROPERTIES GENERATED TRUE)
  30. endfunction()