| 1 |
cmake_minimum_required(VERSION 3.17.0 FATAL_ERROR) |
| 2 |
|
| 3 |
# generates and adds a Windows rc file for the application/library also attaches the application icon if ffmpeg is available |
| 4 |
# does nothing if not building with mingw-w64 |
| 5 |
|
| 6 |
if (NOT BASIC_PROJECT_CONFIG_DONE) |
| 7 |
message( |
| 8 |
FATAL_ERROR |
| 9 |
"Before including the WindowsResources module, the LibraryConfig/ApplicationConfig module must be included.") |
| 10 |
endif () |
| 11 |
|
| 12 |
option(WINDOWS_RESOURCES_ENABLED "controls whether Windows resources are enabled" ON) |
| 13 |
option(WINDOWS_ICON_ENABLED "controls whether Windows icon is enabled" ON) |
| 14 |
|
| 15 |
if (NOT WIN32 OR NOT WINDOWS_RESOURCES_ENABLED) |
| 16 |
return() |
| 17 |
endif () |
| 18 |
|
| 19 |
# find rc template, define path of output rc file |
| 20 |
include(TemplateFinder) |
| 21 |
find_template_file("windows.rc" CPP_UTILITIES RC_TEMPLATE_FILE) |
| 22 |
find_template_file("windows-cli-wrapper.rc" CPP_UTILITIES RC_CLI_TEMPLATE_FILE) |
| 23 |
set(WINDOWS_RC_FILE "${CMAKE_CURRENT_BINARY_DIR}/resources/windows") |
| 24 |
set(WINDOWS_CLI_RC_FILE "${CMAKE_CURRENT_BINARY_DIR}/resources/windows-cli-wrapper") |
| 25 |
|
| 26 |
# create Windows icon from png with ffmpeg if available |
| 27 |
unset(WINDOWS_ICON_RC_ENTRY) |
| 28 |
if (WINDOWS_ICON_ENABLED) |
| 29 |
if (NOT WINDOWS_ICON_PATH AND PNG_ICON_PATH) |
| 30 |
find_program(FFMPEG_BIN ffmpeg avconv) |
| 31 |
if (FFMPEG_BIN) |
| 32 |
set(WINDOWS_ICON_PATH "${CMAKE_CURRENT_BINARY_DIR}/resources/${META_PROJECT_NAME}.ico") |
| 33 |
add_custom_command( |
| 34 |
COMMENT "Generating icon for Windows executable" |
| 35 |
OUTPUT "${WINDOWS_ICON_PATH}" |
| 36 |
COMMAND ${FFMPEG_BIN} -y -i "${PNG_ICON_PATH}" "${WINDOWS_ICON_PATH}" |
| 37 |
DEPENDS "${PNG_ICON_PATH}") |
| 38 |
message(STATUS "Generating Windows icon from \"${PNG_ICON_PATH}\" via ${FFMPEG_BIN}.") |
| 39 |
else () |
| 40 |
message(STATUS "Unable to find ffmpeg, not creating a Windows icon") |
| 41 |
endif () |
| 42 |
endif () |
| 43 |
if (WINDOWS_ICON_PATH) |
| 44 |
set(WINDOWS_ICON_RC_ENTRY "IDI_ICON1 ICON DISCARDABLE \"${WINDOWS_ICON_PATH}\"") |
| 45 |
set_source_files_properties("${WINDOWS_RC_FILE}" PROPERTIES OBJECT_DEPENDS "${WINDOWS_ICON_PATH}") |
| 46 |
endif () |
| 47 |
endif () |
| 48 |
|
| 49 |
# create Windows rc file from template |
| 50 |
configure_file("${RC_TEMPLATE_FILE}" "${WINDOWS_RC_FILE}-configured.rc") |
| 51 |
file( |
| 52 |
GENERATE |
| 53 |
OUTPUT "${WINDOWS_RC_FILE}-$<CONFIG>.rc" |
| 54 |
INPUT "${WINDOWS_RC_FILE}-configured.rc") |
| 55 |
if (BUILD_CLI_WRAPPER AND META_PROJECT_TYPE STREQUAL "application") |
| 56 |
configure_file("${RC_CLI_TEMPLATE_FILE}" "${WINDOWS_CLI_RC_FILE}-configured.rc") |
| 57 |
file( |
| 58 |
GENERATE |
| 59 |
OUTPUT "${WINDOWS_CLI_RC_FILE}-$<CONFIG>.rc" |
| 60 |
INPUT "${WINDOWS_CLI_RC_FILE}-configured.rc") |
| 61 |
endif () |
| 62 |
|
| 63 |
# add resource file to sources |
| 64 |
list(APPEND RES_FILES "${WINDOWS_RC_FILE}-${CMAKE_BUILD_TYPE}.rc") |
| 65 |
set_property(SOURCE "${WINDOWS_RC_FILE}-${CMAKE_BUILD_TYPE}.rc" PROPERTY GENERATED ON) |
| 66 |
if (BUILD_CLI_WRAPPER AND META_PROJECT_TYPE STREQUAL "application") |
| 67 |
list(APPEND CLI_WRAPPER_RES_FILES "${WINDOWS_CLI_RC_FILE}-${CMAKE_BUILD_TYPE}.rc") |
| 68 |
set_property(SOURCE "${WINDOWS_CLI_RC_FILE}-${CMAKE_BUILD_TYPE}.rc" PROPERTY GENERATED ON) |
| 69 |
endif () |
| 70 |
|
| 71 |
# configure resource compiler; use windres when compiling with mingw-w64 |
| 72 |
if (MINGW) |
| 73 |
set(CMAKE_RC_COMPILER_INIT windres) |
| 74 |
set(CMAKE_RC_COMPILE_OBJECT "<CMAKE_RC_COMPILER> <FLAGS> -O coff <DEFINES> -i <SOURCE> -o <OBJECT>") |
| 75 |
endif () |
| 76 |
enable_language(RC) |