Warning, /frameworks/extra-cmake-modules/modules/ECMInstallIcons.cmake is written in an unsupported language. File is not indexed.

0001 # SPDX-FileCopyrightText: 2014 Alex Merry <alex.merry@kde.org>
0002 # SPDX-FileCopyrightText: 2013 David Edmundson <kde@davidedmundson.co.uk>
0003 # SPDX-FileCopyrightText: 2008 Chusslove Illich <caslav.ilic@gmx.net>
0004 # SPDX-FileCopyrightText: 2006 Alex Neundorf <neundorf@kde.org>
0005 #
0006 # SPDX-License-Identifier: BSD-3-Clause
0007 
0008 #[=======================================================================[.rst:
0009 ECMInstallIcons
0010 ---------------
0011 
0012 Installs icons, sorting them into the correct directories according to the
0013 FreeDesktop.org icon naming specification.
0014 
0015 ::
0016 
0017   ecm_install_icons(ICONS <icon> [<icon> [...]]
0018                     DESTINATION <icon_install_dir>
0019                     [LANG <l10n_code>]
0020                     [THEME <theme>])
0021 
0022 The given icons, whose names must match the pattern::
0023 
0024   <size>-<group>-<name>.<ext>
0025 
0026 will be installed to the appropriate subdirectory of ``DESTINATION`` according to
0027 the FreeDesktop.org icon naming scheme. By default, they are installed to the
0028 "hicolor" theme, but this can be changed using the ``THEME`` argument.  If the
0029 icons are localized, the LANG argument can be used to install them in a
0030 locale-specific directory.
0031 
0032 ``<size>`` is a numeric pixel size (typically 16, 22, 32, 48, 64, 128 or 256)
0033 or ``sc`` for scalable (SVG) files, ``<group>`` is one of the standard
0034 FreeDesktop.org icon groups (actions, animations, apps, categories, devices,
0035 emblems, emotes, intl, mimetypes, places, status) and ``<ext>`` is one of
0036 ``.png``, ``.mng`` or ``.svgz``.
0037 
0038 The typical installation directory is ``share/icons``.
0039 
0040 .. code-block:: cmake
0041 
0042   ecm_install_icons(ICONS 22-actions-menu_new.png
0043                     DESTINATION share/icons)
0044 
0045 The above code will install the file ``22-actions-menu_new.png`` as
0046 ``${CMAKE_INSTALL_PREFIX}/share/icons/<theme>/22x22/actions/menu_new.png``
0047 
0048 Users of the :kde-module:`KDEInstallDirs` module would normally use
0049 ``${KDE_INSTALL_ICONDIR}`` as the DESTINATION, while users of the GNUInstallDirs
0050 module should use ``${CMAKE_INSTALL_DATAROOTDIR}/icons``.
0051 
0052 An old form of arguments will also be accepted::
0053 
0054   ecm_install_icons(<icon_install_dir> [<l10n_code>])
0055 
0056 This matches files named like::
0057 
0058   <theme><size>-<group>-<name>.<ext>
0059 
0060 where ``<theme>`` is one of
0061 
0062 * ``hi`` for hicolor
0063 * ``lo`` for locolor
0064 * ``cr`` for the Crystal icon theme
0065 * ``ox`` for the Oxygen icon theme
0066 * ``br`` for the Breeze icon theme
0067 
0068 With this syntax, the file ``hi22-actions-menu_new.png`` would be installed
0069 into ``<icon_install_dir>/hicolor/22x22/actions/menu_new.png``
0070 
0071 Since pre-1.0.0.
0072 #]=======================================================================]
0073 
0074 # A "map" of short type names to the directories.
0075 # Unknown names produce a warning.
0076 set(_ECM_ICON_GROUP_mimetypes  "mimetypes")
0077 set(_ECM_ICON_GROUP_places     "places")
0078 set(_ECM_ICON_GROUP_devices    "devices")
0079 set(_ECM_ICON_GROUP_apps       "apps")
0080 set(_ECM_ICON_GROUP_actions    "actions")
0081 set(_ECM_ICON_GROUP_categories "categories")
0082 set(_ECM_ICON_GROUP_status     "status")
0083 set(_ECM_ICON_GROUP_emblems    "emblems")
0084 set(_ECM_ICON_GROUP_emotes     "emotes")
0085 set(_ECM_ICON_GROUP_animations "animations")
0086 set(_ECM_ICON_GROUP_intl       "intl")
0087 
0088 # For the "compatibility" syntax: a "map" of short theme names to the theme
0089 # directory
0090 set(_ECM_ICON_THEME_br "breeze")
0091 set(_ECM_ICON_THEME_ox "oxygen")
0092 set(_ECM_ICON_THEME_cr "crystalsvg")
0093 set(_ECM_ICON_THEME_lo "locolor")
0094 set(_ECM_ICON_THEME_hi "hicolor")
0095 
0096 macro(_ecm_install_icons_v1 _defaultpath)
0097    # the l10n-subdir if language given as second argument (localized icon)
0098    set(_lang ${ARGV1})
0099    if(_lang)
0100       set(_l10n_SUBDIR l10n/${_lang})
0101    else()
0102       set(_l10n_SUBDIR ".")
0103    endif()
0104 
0105    set(_themes)
0106 
0107    # first the png icons
0108    file(GLOB _icons *.png)
0109    foreach (_current_ICON ${_icons} )
0110       # since CMake 2.6 regex matches are stored in special variables CMAKE_MATCH_x, if it didn't match, they are empty
0111       string(REGEX MATCH "^.*/([a-zA-Z]+)([0-9]+)\\-([a-z]+)\\-(.+\\.png)$" _dummy  "${_current_ICON}")
0112       set(_type  "${CMAKE_MATCH_1}")
0113       set(_size  "${CMAKE_MATCH_2}")
0114       set(_group "${CMAKE_MATCH_3}")
0115       set(_name  "${CMAKE_MATCH_4}")
0116 
0117       set(_theme_GROUP ${_ECM_ICON_THEME_${_type}})
0118       if( _theme_GROUP)
0119          list(APPEND _themes "${_theme_GROUP}")
0120          _ECM_ADD_ICON_INSTALL_RULE(${CMAKE_CURRENT_BINARY_DIR}/install_icons.cmake
0121                     ${_defaultpath}/${_theme_GROUP}/${_size}x${_size}
0122                     ${_group} ${_current_ICON} ${_name} ${_l10n_SUBDIR})
0123       endif()
0124    endforeach (_current_ICON)
0125 
0126    # mng icons
0127    file(GLOB _icons *.mng)
0128    foreach (_current_ICON ${_icons} )
0129       # since CMake 2.6 regex matches are stored in special variables CMAKE_MATCH_x, if it didn't match, they are empty
0130       string(REGEX MATCH "^.*/([a-zA-Z]+)([0-9]+)\\-([a-z]+)\\-(.+\\.mng)$" _dummy  "${_current_ICON}")
0131       set(_type  "${CMAKE_MATCH_1}")
0132       set(_size  "${CMAKE_MATCH_2}")
0133       set(_group "${CMAKE_MATCH_3}")
0134       set(_name  "${CMAKE_MATCH_4}")
0135 
0136       set(_theme_GROUP ${_ECM_ICON_THEME_${_type}})
0137       if( _theme_GROUP)
0138          list(APPEND _themes "${_theme_GROUP}")
0139          _ECM_ADD_ICON_INSTALL_RULE(${CMAKE_CURRENT_BINARY_DIR}/install_icons.cmake
0140                 ${_defaultpath}/${_theme_GROUP}/${_size}x${_size}
0141                 ${_group} ${_current_ICON} ${_name} ${_l10n_SUBDIR})
0142       endif()
0143    endforeach (_current_ICON)
0144 
0145    # and now the svg icons
0146    file(GLOB _icons *.svgz)
0147    foreach (_current_ICON ${_icons} )
0148       # since CMake 2.6 regex matches are stored in special variables CMAKE_MATCH_x, if it didn't match, they are empty
0149       string(REGEX MATCH "^.*/([a-zA-Z]+)sc\\-([a-z]+)\\-(.+\\.svgz)$" _dummy "${_current_ICON}")
0150       set(_type  "${CMAKE_MATCH_1}")
0151       set(_group "${CMAKE_MATCH_2}")
0152       set(_name  "${CMAKE_MATCH_3}")
0153 
0154       set(_theme_GROUP ${_ECM_ICON_THEME_${_type}})
0155       if( _theme_GROUP)
0156          list(APPEND _themes "${_theme_GROUP}")
0157           _ECM_ADD_ICON_INSTALL_RULE(${CMAKE_CURRENT_BINARY_DIR}/install_icons.cmake
0158                             ${_defaultpath}/${_theme_GROUP}/scalable
0159                             ${_group} ${_current_ICON} ${_name} ${_l10n_SUBDIR})
0160       endif()
0161    endforeach (_current_ICON)
0162 
0163    if (_themes)
0164        list(REMOVE_DUPLICATES _themes)
0165        foreach(_theme ${_themes})
0166            _ecm_update_iconcache("${_defaultpath}" "${_theme}")
0167        endforeach()
0168    else()
0169        message(AUTHOR_WARNING "No suitably-named icons found")
0170    endif()
0171 
0172 endmacro()
0173 
0174 # only used internally by _ecm_install_icons_v1
0175 macro(_ecm_add_icon_install_rule _install_SCRIPT _install_PATH _group _orig_NAME _install_NAME _l10n_SUBDIR)
0176 
0177    # if the string doesn't match the pattern, the result is the full string, so all three have the same content
0178    if (NOT ${_group} STREQUAL ${_install_NAME} )
0179       set(_icon_GROUP  ${_ECM_ICON_GROUP_${_group}})
0180       if(NOT _icon_GROUP)
0181          message(WARNING "Icon ${_install_NAME} uses invalid category ${_group}, setting to 'actions'")
0182          set(_icon_GROUP "actions")
0183       endif()
0184 #      message(STATUS "icon: ${_current_ICON} size: ${_size} group: ${_group} name: ${_name} l10n: ${_l10n_SUBDIR}")
0185       install(FILES ${_orig_NAME} DESTINATION ${_install_PATH}/${_icon_GROUP}/${_l10n_SUBDIR}/ RENAME ${_install_NAME} )
0186    endif (NOT ${_group} STREQUAL ${_install_NAME} )
0187 
0188 endmacro()
0189 
0190 # Updates the mtime of the icon theme directory, so caches that
0191 # watch for changes to the directory will know to update.
0192 # If present, this also runs gtk-update-icon-cache (which despite the name is also used by Qt).
0193 function(_ecm_update_iconcache installdir theme)
0194     find_program(GTK_UPDATE_ICON_CACHE_EXECUTABLE NAMES gtk-update-icon-cache)
0195     # We don't always have touch command (e.g. on Windows), so instead
0196     # create and delete a temporary file in the theme dir.
0197     install(CODE "
0198     set(DESTDIR_VALUE \"\$ENV{DESTDIR}\")
0199     if (NOT DESTDIR_VALUE)
0200         execute_process(COMMAND \"${CMAKE_COMMAND}\" -E touch \"${CMAKE_INSTALL_PREFIX}/${installdir}/${theme}\")
0201         set(HAVE_GTK_UPDATE_ICON_CACHE_EXEC ${GTK_UPDATE_ICON_CACHE_EXECUTABLE})
0202         if (HAVE_GTK_UPDATE_ICON_CACHE_EXEC)
0203             execute_process(COMMAND ${GTK_UPDATE_ICON_CACHE_EXECUTABLE} -q -t -i . WORKING_DIRECTORY \"${CMAKE_INSTALL_PREFIX}/${installdir}/${theme}\")
0204         endif ()
0205     endif (NOT DESTDIR_VALUE)
0206     ")
0207 endfunction()
0208 
0209 function(ecm_install_icons)
0210     set(options)
0211     set(oneValueArgs DESTINATION LANG THEME)
0212     set(multiValueArgs ICONS)
0213     cmake_parse_arguments(ARG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
0214 
0215     if(NOT ARG_ICONS AND NOT ARG_DESTINATION)
0216         message(AUTHOR_WARNING "ecm_install_icons() with no ICONS argument is deprecated")
0217         _ecm_install_icons_v1(${ARGN})
0218         return()
0219     endif()
0220     if(ARG_UNPARSED_ARGUMENTS)
0221         message(FATAL_ERROR "Unexpected arguments to ecm_install_icons: ${ARG_UNPARSED_ARGUMENTS}")
0222     endif()
0223     if(NOT ARG_DESTINATION)
0224         message(FATAL_ERROR "No DESTINATION argument given to ecm_install_icons")
0225     endif()
0226     if(NOT ARG_THEME)
0227         set(ARG_THEME "hicolor")
0228     endif()
0229     if(ARG_LANG)
0230         set(l10n_subdir "l10n/${ARG_LANG}/")
0231     endif()
0232 
0233     foreach(icon ${ARG_ICONS})
0234         get_filename_component(filename "${icon}" NAME)
0235         string(REGEX MATCH "([0-9sc]+)\\-([a-z]+)\\-([^/]+)\\.([a-z]+)$"
0236                            complete_match "${filename}")
0237         set(size  "${CMAKE_MATCH_1}")
0238         set(group "${CMAKE_MATCH_2}")
0239         set(name  "${CMAKE_MATCH_3}")
0240         set(ext   "${CMAKE_MATCH_4}")
0241         if(NOT size OR NOT group OR NOT name OR NOT ext)
0242             message(WARNING "${icon} is not named correctly for ecm_install_icons - ignoring")
0243         elseif(NOT size STREQUAL "sc" AND NOT size GREATER 0)
0244             message(WARNING "${icon} size (${size}) is invalid - ignoring")
0245         else()
0246             if (NOT complete_match STREQUAL filename)
0247                 # We can't stop accepting filenames with leading characters,
0248                 # because that would break existing projects, so just warn
0249                 # about them instead.
0250                 message(AUTHOR_WARNING "\"${icon}\" has characters before the size; it should be renamed to \"${size}-${group}-${name}.${ext}\"")
0251             endif()
0252             if(NOT _ECM_ICON_GROUP_${group})
0253                 message(WARNING "${icon} group (${group}) is not recognized")
0254             endif()
0255             if(size STREQUAL "sc")
0256                 if(NOT ext STREQUAL "svg" AND NOT ext STREQUAL "svgz")
0257                     message(WARNING "Scalable icon ${icon} is not SVG or SVGZ")
0258                 endif()
0259                 set(size_dir "scalable")
0260             else()
0261                 if(NOT ext STREQUAL "png" AND NOT ext STREQUAL "mng" AND NOT ext STREQUAL "svg" AND NOT ext STREQUAL "svgz")
0262                     message(WARNING "Fixed-size icon ${icon} is not PNG/MNG/SVG/SVGZ")
0263                 endif()
0264                 set(size_dir "${size}x${size}")
0265             endif()
0266             install(
0267                 FILES "${icon}"
0268                 DESTINATION "${ARG_DESTINATION}/${ARG_THEME}/${size_dir}/${group}/${l10n_subdir}"
0269                 RENAME "${name}.${ext}"
0270             )
0271         endif()
0272     endforeach()
0273     _ecm_update_iconcache("${ARG_DESTINATION}" "${ARG_THEME}")
0274 endfunction()