diff options
author | Friedrich W. H. Kossebau <kossebau@kde.org> | 2021-05-14 20:20:45 +0200 |
---|---|---|
committer | Christophe Giboudeaux <christophe@krop.fr> | 2021-05-15 17:30:53 +0000 |
commit | c28008a109cdd23c3de4a3fdceb12629431b03f1 (patch) | |
tree | 61eee05645d9d2003c357f6d424d4bd669b6e2a5 | |
parent | 4d307aaf0735ce40b9631b8161001a8c224bd5bf (diff) | |
download | extra-cmake-modules-c28008a109cdd23c3de4a3fdceb12629431b03f1.tar.gz extra-cmake-modules-c28008a109cdd23c3de4a3fdceb12629431b03f1.tar.bz2 |
ECMAddAppIcon: support target as argument
-rw-r--r-- | modules/ECMAddAppIcon.cmake | 46 | ||||
-rw-r--r-- | tests/ECMAddAppIconTest/CMakeLists.txt | 83 | ||||
-rw-r--r-- | tests/ECMAddAppIconTest/main.cpp | 4 |
3 files changed, 119 insertions, 14 deletions
diff --git a/modules/ECMAddAppIcon.cmake b/modules/ECMAddAppIcon.cmake index 7a674d63..9f399ac8 100644 --- a/modules/ECMAddAppIcon.cmake +++ b/modules/ECMAddAppIcon.cmake @@ -14,7 +14,7 @@ Add icons to executable files and packages. :: - ecm_add_app_icon(<sources_var> + ecm_add_app_icon(<sources_var_name(|target (since 5.83))> ICONS <icon> [<icon> [...]] [SIDEBAR_ICONS <icon> [<icon> [...]] # Since 5.49 [OUTFILE_BASENAME <name>]) # Since 5.49 @@ -24,11 +24,17 @@ The given icons, whose names must match the pattern:: <size>-<other_text>.png -will be added to the executable target whose sources are specified by -``<sources_var>`` on platforms that support it (Windows and Mac OS X). +will be added as platform-specific application icons +to the variable named ``<sources_var_name>`` or, if the first argument +is a target (since 5.83), to the SOURCES property of ``<target>``. +Any target must be created with add_executable() and not be an alias. + Other icon files are ignored but on Mac SVG files can be supported and it is thus possible to mix those with png files in a single macro call. +The platforms currently supported are Windows and Mac OS X, on all others +the call has no effect and is ignored. + ``<size>`` is a numeric pixel size (typically 16, 32, 48, 64, 128 or 256). ``<other_text>`` can be any other text. See the platform notes below for any recommendations about icon sizes. @@ -40,7 +46,7 @@ application is dragged into Finder's sidebar. Since 5.49. ``OUTFILE_BASENAME`` will be used as the basename for the icon file. If you specify it, the icon file will be called ``<OUTFILE_BASENAME>.icns`` on Mac OS X and ``<OUTFILE_BASENAME>.ico`` on Windows. If you don't specify it, it defaults -to ``<sources_var>.<ext>``. Since 5.49. +to ``<sources_var_name>.<ext>``. Since 5.49. Windows notes @@ -77,7 +83,7 @@ Since 1.7.0. include(CMakeParseArguments) -function(ecm_add_app_icon appsources) +function(ecm_add_app_icon appsources_or_target) set(options) set(oneValueArgs OUTFILE_BASENAME) set(multiValueArgs ICONS SIDEBAR_ICONS) @@ -86,6 +92,16 @@ function(ecm_add_app_icon appsources) if(NOT ARG_ICONS) message(FATAL_ERROR "No ICONS argument given to ecm_add_app_icon") endif() + if (TARGET ${appsources_or_target}) + get_target_property(target_type ${appsources_or_target} TYPE) + if (NOT target_type STREQUAL "EXECUTABLE") + message(FATAL_ERROR "Target argument passed to ecm_add_app_icon is not an executable: ${appsources_or_target}") + endif() + get_target_property(aliased_target ${appsources_or_target} ALIASED_TARGET) + if(aliased_target) + message(FATAL_ERROR "Target argument passed to ecm_add_app_icon must not be an alias: ${appsources_or_target}") + endif() + endif() if(ARG_UNPARSED_ARGUMENTS) message(FATAL_ERROR "Unexpected arguments to ecm_add_app_icon: ${ARG_UNPARSED_ARGUMENTS}") endif() @@ -106,7 +122,11 @@ function(ecm_add_app_icon appsources) # install the icns file we just created get_filename_component(icon_name ${icon_full} NAME_WE) set(MACOSX_BUNDLE_ICON_FILE ${icon_name}.icns PARENT_SCOPE) - set(${appsources} "${${appsources}};${CMAKE_CURRENT_BINARY_DIR}/${icon_name}.icns" PARENT_SCOPE) + if (TARGET ${appsources_or_target}) + target_sources(${appsources_or_target} PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/${icon_name}.icns") + else() + set(${appsources_or_target} "${${appsources_or_target}};${CMAKE_CURRENT_BINARY_DIR}/${icon_name}.icns" PARENT_SCOPE) + endif() set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/${icon_name}.icns PROPERTIES MACOSX_PACKAGE_LOCATION Resources) # we're done now return() @@ -162,7 +182,7 @@ function(ecm_add_app_icon appsources) if (ARG_OUTFILE_BASENAME) set (_outfilebasename "${ARG_OUTFILE_BASENAME}") else() - set (_outfilebasename "${appsources}") + set (_outfilebasename "${appsources_or_target}") endif() set (_outfilename "${CMAKE_CURRENT_BINARY_DIR}/${_outfilebasename}") @@ -220,7 +240,11 @@ function(ecm_add_app_icon appsources) endforeach() create_windows_icon_and_rc(IcoTool::IcoTool "${icotool_args}" "${windows_icons_modern}") - set(${appsources} "${${appsources}};${_outfilename}.rc" PARENT_SCOPE) + if (TARGET ${appsources_or_target}) + target_sources(${appsources_or_target} PRIVATE "${_outfilename}.rc") + else() + set(${appsources_or_target} "${${appsources_or_target}};${_outfilename}.rc" PARENT_SCOPE) + endif() else() message(WARNING "Unable to find the icotool utilities or icons in matching sizes - application will not have an application icon!") endif() @@ -301,7 +325,11 @@ function(ecm_add_app_icon appsources) # Append the icns file to the sources list so it will be a dependency to the # main target - set(${appsources} "${${appsources}};${_outfilename}.icns" PARENT_SCOPE) + if (TARGET ${appsources_or_target}) + target_sources(${appsources_or_target} PRIVATE "${_outfilename}.icns") + else() + set(${appsources_or_target} "${${appsources_or_target}};${_outfilename}.icns" PARENT_SCOPE) + endif() # Install the icon into the Resources dir in the bundle set_source_files_properties("${_outfilename}.icns" PROPERTIES MACOSX_PACKAGE_LOCATION Resources) diff --git a/tests/ECMAddAppIconTest/CMakeLists.txt b/tests/ECMAddAppIconTest/CMakeLists.txt index 6782c63d..a8a9d1b1 100644 --- a/tests/ECMAddAppIconTest/CMakeLists.txt +++ b/tests/ECMAddAppIconTest/CMakeLists.txt @@ -14,33 +14,38 @@ elseif(APPLE) set(icon_extension_to_test "icns") endif() -#### Test 1: ecm_add_app_icon with only regular icons and no OUTFILE_BASENAME #### +#### Test 1: ecm_add_app_icon with only regular icons and no OUTFILE_BASENAME, for sources var #### set(ICONS_1 16-category-name.png 128-category-name.png) ecm_add_app_icon(OUT_1 ICONS ${ICONS_1}) +list(LENGTH OUT_1 out_count) + if(WIN32 OR APPLE) - list(LENGTH OUT_1 out_count) if(out_count EQUAL 0) message(FATAL_ERROR "ecm_add_app_icon() hasn't produced anything") endif() - if(NOT OUT_1 MATCHES "OUT_1.${icon_extension_to_test}") message(FATAL_ERROR "ecm_add_app_icon() did not fall back to target name for icon name: ${OUT_1}") endif() +else() + if(NOT out_count EQUAL 0) + message(FATAL_ERROR "ecm_add_app_icon() produced something unexpectedly.") + endif() endif() add_custom_target(t_1 ALL DEPENDS ${OUT_1}) list(APPEND OUT "${OUT_1}") -#### Test 2: ecm_add_app_icon with regular and sidebar icons and OUTFILE_BASENAME set #### +#### Test 2: ecm_add_app_icon with regular and sidebar icons and OUTFILE_BASENAME set, for sources var #### set(ICONS_2 16-category-name.png 128-category-name.png) set(SIDEBAR_ICONS_2 16-category-name-sidebar.png 128-category-name-sidebar.png) ecm_add_app_icon(OUT_2 ICONS ${ICONS_2} SIDEBAR_ICONS ${SIDEBAR_ICONS_2} OUTFILE_BASENAME "SuperBasename") +list(LENGTH OUT_2 out_count) + if(WIN32 OR APPLE) - list(LENGTH OUT_2 out_count) if(out_count EQUAL 0) message(FATAL_ERROR "ecm_add_app_icon() hasn't produced anything") endif() @@ -48,6 +53,10 @@ if(WIN32 OR APPLE) if(NOT OUT_2 MATCHES "SuperBasename.${icon_extension_to_test}") message(FATAL_ERROR "ecm_add_app_icon() did not respect OUTFILE_BASENAME: ${OUT_2}") endif() +else() + if(NOT out_count EQUAL 0) + message(FATAL_ERROR "ecm_add_app_icon() produced something unexpectedly.") + endif() endif() if(APPLE) @@ -60,5 +69,69 @@ endif() add_custom_target(t_2 ALL DEPENDS ${OUT_2}) list(APPEND OUT "${OUT_2}") +#### Test 3: ecm_add_app_icon with only regular icons and no OUTFILE_BASENAME, for target #### +set(ICONS_3 16-category-name.png 128-category-name.png) + +add_executable(app_3) +target_sources(app_3 PRIVATE main.cpp) + +ecm_add_app_icon(app_3 ICONS ${ICONS_1}) + +get_target_property(OUT_3 app_3 SOURCES) +list(REMOVE_ITEM OUT_3 main.cpp) +list(LENGTH OUT_3 out_count) + +if(WIN32 OR APPLE) + if(out_count EQUAL 0) + message(FATAL_ERROR "ecm_add_app_icon() hasn't produced anything") + endif() + + if(NOT OUT_3 MATCHES "app_3.${icon_extension_to_test}") + message(FATAL_ERROR "ecm_add_app_icon() did not fall back to target name for icon name: ${OUT_1}") + endif() +else() + if(NOT out_count EQUAL 0) + message(FATAL_ERROR "ecm_add_app_icon() produced something unexpectedly.") + endif() +endif() + +list(APPEND OUT "${OUT_3}") + +#### Test 4: ecm_add_app_icon with regular and sidebar icons and OUTFILE_BASENAME set, for target #### +set(ICONS_4 16-category-name.png 128-category-name.png) +set(SIDEBAR_ICONS_4 16-category-name-sidebar.png 128-category-name-sidebar.png) + +add_executable(app_4) +target_sources(app_4 PRIVATE main.cpp) + +ecm_add_app_icon(app_4 ICONS ${ICONS_4} SIDEBAR_ICONS ${SIDEBAR_ICONS_4} OUTFILE_BASENAME "SuperBasename4") + +get_target_property(OUT_4 app_4 SOURCES) +list(REMOVE_ITEM OUT_4 main.cpp) +list(LENGTH OUT_4 out_count) + +if(WIN32 OR APPLE) + if(out_count EQUAL 0) + message(FATAL_ERROR "ecm_add_app_icon() hasn't produced anything") + endif() + + if(NOT OUT_4 MATCHES "SuperBasename4.${icon_extension_to_test}") + message(FATAL_ERROR "ecm_add_app_icon() did not respect OUTFILE_BASENAME: ${OUT_4}") + endif() +else() + if(NOT out_count EQUAL 0) + message(FATAL_ERROR "ecm_add_app_icon() produced something unexpectedly.") + endif() +endif() + +if(APPLE) + list(APPEND expected_icons "icon_16x16.png" "icon_128x128.png" "sidebar_16x16.png" "sidebar_32x32.png") + foreach(expected_icon ${expected_icons}) + list(APPEND OUT_4 "${CMAKE_CURRENT_BINARY_DIR}/SuperBasename4.iconset/${expected_icon}") + endforeach() +endif() + +list(APPEND OUT "${OUT_4}") + #### this will be run by CTest #### configure_file(check_files.cmake.in "${CMAKE_CURRENT_BINARY_DIR}/check_files.cmake" @ONLY) diff --git a/tests/ECMAddAppIconTest/main.cpp b/tests/ECMAddAppIconTest/main.cpp new file mode 100644 index 00000000..0f45ea60 --- /dev/null +++ b/tests/ECMAddAppIconTest/main.cpp @@ -0,0 +1,4 @@ +int main(int, char**) +{ + return 0; +} |