libsuperderpy/cmake/ImgToWebp.cmake

47 lines
1.8 KiB
CMake
Raw Normal View History

set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}")
include(AssetCache)
find_program(CONVERT NAMES convert NO_CMAKE_FIND_ROOT_PATH)
if(CONVERT AND DATADIR)
file(GLOB_RECURSE IMAGE_FILES RELATIVE ${DATADIR} ${DATADIR}/*.png ${DATADIR}/*.jpg ${DATADIR}/*.JPG ${DATADIR}/*.webp)
message(STATUS "ImgToWebp engaging... (using ${CONVERT})")
foreach(file IN LISTS IMAGE_FILES)
if (NOT file MATCHES "^icons/")
message(STATUS ${file})
string(REGEX REPLACE "^(.+)(\\.[^.]+)$" "\\1" filepath ${file})
if (RESIZE)
set(resizecmd -resize ${RESIZE})
else()
set(resizecmd "")
endif()
if (LOSSLESS)
set(losslesscmd -define webp:lossless=true)
else()
set(losslesscmd "")
endif()
GetFromAssetCache(${CACHE} ${DATADIR}/${file} "${LOSSLESS}-${QUALITY}-${RESIZE}-${PARAMS}" CACHED HASH)
if (CACHED)
file(REMOVE ${DATADIR}/${file})
configure_file(${CACHED} ${DATADIR}/${filepath}.wbp COPYONLY)
unset(CACHED)
else()
separate_arguments(PARAMS)
set(CONVERT_CMD ${CONVERT} ${file} -quality ${QUALITY} ${PARAMS} ${resizecmd} ${losslesscmd} webp:${filepath}.wbp)
execute_process(COMMAND ${CONVERT_CMD} WORKING_DIRECTORY ${DATADIR} RESULT_VARIABLE CONVERT_RESULT)
if(CONVERT_RESULT)
message(WARNING "ERROR: ${CONVERT_RESULT}")
else()
file(REMOVE ${DATADIR}/${file})
AddToAssetCache(${CACHE} ${HASH} "${LOSSLESS}-${QUALITY}-${RESIZE}-${PARAMS}" "${DATADIR}/${filepath}.wbp")
endif()
endif()
endif()
endforeach(file)
else(CONVERT AND DATADIR)
if(NOT CONVERT)
message(WARNING "ImgToWebp: can't find convert!")
else()
message(WARNING "ImgToWebp: no DATADIR specified!")
endif()
endif(CONVERT AND DATADIR)