/CMakeModules/AddAppIconMacro.cmake

http://github.com/tomahawk-player/tomahawk · CMake · 114 lines · 84 code · 11 blank · 19 comment · 17 complexity · fd230931f2b61a005967a0fbd6ba41c6 MD5 · raw file

  1. SET(WINDRES_EXECUTABLE ${CMAKE_RC_COMPILER})
  2. # This macro is taken from kdelibs/cmake/modules/KDE4Macros.cmake.
  3. #
  4. # Copyright (c) 2006-2009 Alexander Neundorf, <neundorf@kde.org>
  5. # Copyright (c) 2006, 2007, Laurent Montel, <montel@kde.org>
  6. # Copyright (c) 2007 Matthias Kretz <kretz@kde.org>
  7. #
  8. # Redistribution and use is allowed according to the terms of the BSD license.
  9. # For details see the accompanying COPYING-CMAKE-SCRIPTS file [in KDE repositories].
  10. # adds application icon to target source list
  11. # for detailed documentation see the top of FindKDE4Internal.cmake
  12. macro (TOMAHAWK_ADD_APP_ICON appsources outfilename pattern)
  13. set (_outfilename ${CMAKE_CURRENT_BINARY_DIR}/${outfilename})
  14. if (WIN32)
  15. if(NOT WINCE)
  16. find_program(PNG2ICO_EXECUTABLE NAMES png2ico)
  17. else(NOT WINCE)
  18. find_program(PNG2ICO_EXECUTABLE NAMES png2ico PATHS ${HOST_BINDIR} NO_DEFAULT_PATH )
  19. endif(NOT WINCE)
  20. find_program(WINDRES_EXECUTABLE NAMES windres)
  21. if(MSVC)
  22. set(WINDRES_EXECUTABLE TRUE)
  23. endif(MSVC)
  24. if (PNG2ICO_EXECUTABLE AND WINDRES_EXECUTABLE)
  25. string(REPLACE "*" "(.*)" pattern_rx "${pattern}")
  26. file(GLOB files "${pattern}")
  27. foreach (it ${files})
  28. string(REGEX REPLACE "${pattern_rx}" "\\1" fn "${it}")
  29. if (fn MATCHES ".*16.*" )
  30. list (APPEND _icons ${it})
  31. endif (fn MATCHES ".*16.*")
  32. if (fn MATCHES ".*32.*" )
  33. list (APPEND _icons ${it})
  34. endif (fn MATCHES ".*32.*")
  35. if (fn MATCHES ".*48.*" )
  36. list (APPEND _icons ${it})
  37. endif (fn MATCHES ".*48.*")
  38. if (fn MATCHES ".*64.*" )
  39. list (APPEND _icons ${it})
  40. endif (fn MATCHES ".*64.*")
  41. if (fn MATCHES ".*128.*" )
  42. list (APPEND _icons ${it})
  43. endif (fn MATCHES ".*128.*")
  44. endforeach (it)
  45. if (_icons)
  46. add_custom_command(OUTPUT ${_outfilename}.ico ${_outfilename}.rc
  47. COMMAND ${PNG2ICO_EXECUTABLE} ARGS --rcfile ${_outfilename}.rc ${_outfilename}.ico ${_icons}
  48. DEPENDS ${PNG2ICO_EXECUTABLE} ${_icons}
  49. WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
  50. )
  51. if (MINGW)
  52. add_custom_command(OUTPUT ${_outfilename}_res.o
  53. COMMAND ${WINDRES_EXECUTABLE} ARGS -i ${_outfilename}.rc -o ${_outfilename}_res.o --include-dir=${CMAKE_CURRENT_SOURCE_DIR}
  54. DEPENDS ${WINDRES_EXECUTABLE} ${_outfilename}.rc
  55. WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
  56. )
  57. list(APPEND ${appsources} ${_outfilename}_res.o)
  58. else(MINGW)
  59. list(APPEND ${appsources} ${_outfilename}.rc)
  60. endif(MINGW)
  61. else(_icons)
  62. message(STATUS "Unable to find a related icon that matches pattern ${pattern} for variable ${appsources} - application will not have an application icon!")
  63. endif(_icons)
  64. else(PNG2ICO_EXECUTABLE AND WINDRES_EXECUTABLE)
  65. message(STATUS "Unable to find the png2ico or windres utilities - application will not have an application icon!")
  66. endif(PNG2ICO_EXECUTABLE AND WINDRES_EXECUTABLE)
  67. endif(WIN32)
  68. if (APPLE)
  69. # first convert image to a tiff using the Mac OS X "sips" utility,
  70. # then use tiff2icns to convert to an icon
  71. find_program(SIPS_EXECUTABLE NAMES sips)
  72. find_program(TIFF2ICNS_EXECUTABLE NAMES tiff2icns)
  73. if (SIPS_EXECUTABLE AND TIFF2ICNS_EXECUTABLE)
  74. file(GLOB_RECURSE files "${pattern}")
  75. # we can only test for the 128-icon like that - we don't use patterns anymore
  76. foreach (it ${files})
  77. if (it MATCHES ".*128.*" )
  78. set (_icon ${it})
  79. endif (it MATCHES ".*128.*")
  80. endforeach (it)
  81. if (_icon)
  82. # first, get the basename of our app icon
  83. add_custom_command(OUTPUT ${_outfilename}.icns ${_outfilename}.tiff
  84. COMMAND ${SIPS_EXECUTABLE} -s format tiff ${_icon} --out ${_outfilename}.tiff
  85. COMMAND ${TIFF2ICNS_EXECUTABLE} ${_outfilename}.tiff ${_outfilename}.icns
  86. DEPENDS ${_icon}
  87. )
  88. # This will register the icon into the bundle
  89. set(MACOSX_BUNDLE_ICON_FILE ${appsources}.icns)
  90. # Append the icns file to the sources list so it will be a dependency to the
  91. # main target
  92. list(APPEND ${appsources} ${_outfilename}.icns)
  93. # Install the icon into the Resources dir in the bundle
  94. set_source_files_properties(${_outfilename}.icns PROPERTIES MACOSX_PACKAGE_LOCATION Resources)
  95. else(_icon)
  96. # TODO - try to scale a non-128 icon...? Try to convert an SVG on the fly?
  97. message(STATUS "Unable to find an 128x128 icon that matches pattern ${pattern} for variable ${appsources} - application will not have an application icon!")
  98. endif(_icon)
  99. else(SIPS_EXECUTABLE AND TIFF2ICNS_EXECUTABLE)
  100. message(STATUS "Unable to find the sips and tiff2icns utilities - application will not have an application icon!")
  101. endif(SIPS_EXECUTABLE AND TIFF2ICNS_EXECUTABLE)
  102. endif(APPLE)
  103. endmacro (TOMAHAWK_ADD_APP_ICON)