11#[[
2- # Adds Windows version information to a target.
2+ # Adds Windows version information and an optional icon to a target.
33#
44# stdc_add_win_rc(<target>
55# COPYRIGHT <copyright>
66# [DESCRIPTION <description>]
77# [PRODUCT_NAME <name>]
88# [VERSION <version>]
9+ # [ICON <ico>]
910# )
11+ #
12+ # A relative ICON is resolved against the current source directory. A changed icon recompiles the
13+ # resource at the next build without a configure.
1014#]]
1115function (stdc_add_win_rc _target )
1216 if (NOT WIN32 )
@@ -18,7 +22,7 @@ function(stdc_add_win_rc _target)
1822 endif ()
1923
2024 set (options)
21- set (oneValueArgs DESCRIPTION COPYRIGHT PRODUCT_NAME VERSION)
25+ set (oneValueArgs DESCRIPTION COPYRIGHT PRODUCT_NAME VERSION ICON )
2226 set (multiValueArgs)
2327 cmake_parse_arguments (FUNC "${options} " "${oneValueArgs} " "${multiValueArgs} " ${ARGN} )
2428
@@ -69,12 +73,21 @@ function(stdc_add_win_rc _target)
6973 list (GET _version_parts 2 _version_patch)
7074 list (GET _version_parts 3 _version_tweak)
7175
76+ set (_icon_path)
77+ set (_icon_line)
78+
79+ if (FUNC_ICON)
80+ get_filename_component (_icon_path "${FUNC_ICON} " ABSOLUTE )
81+ set (_icon_line "IDI_ICON1 ICON \" ${_icon_path} \"\n " )
82+ endif ()
83+
7284 set (_rc_content "#include <windows.h>
7385
7486#ifndef VS_VERSION_INFO
7587#define VS_VERSION_INFO 1
7688#endif
7789
90+ ${_icon_line}
7891#define _STRINGIFY(x) #x
7992#define STRINGIFY(x) _STRINGIFY(x)
8093
@@ -100,7 +113,17 @@ VS_VERSION_INFO VERSIONINFO
100113 }
101114}" )
102115
116+ # No generator records the icon as a dependency of the script, and Visual Studio ignores
117+ # OBJECT_DEPENDS. The script is therefore made at build time by a command that depends on it.
103118 set (_rc_file "${CMAKE_CURRENT_BINARY_DIR } /${_target} _res.rc" )
104- file (WRITE "${_rc_file} " "${_rc_content} " )
119+ file (WRITE "${_rc_file} .in" "${_rc_content} " )
120+ configure_file ("${_rc_file} .in" "${_rc_file} .configured" COPYONLY )
121+
122+ add_custom_command (OUTPUT "${_rc_file} "
123+ COMMAND ${CMAKE_COMMAND } -E copy "${_rc_file} .configured" "${_rc_file} "
124+ COMMAND ${CMAKE_COMMAND } -E touch "${_rc_file} "
125+ DEPENDS "${_rc_file} .configured" ${_icon_path}
126+ VERBATIM
127+ )
105128 target_sources (${_target} PRIVATE "${_rc_file} " )
106129endfunction ()
0 commit comments