generate_product_version.cmake 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. # The MIT License (MIT)
  2. # Copyright (c) 2015, by [halex2005](mailto:akharlov@gmail.com)
  3. # Permission is hereby granted, free of charge, to any person obtaining a copy
  4. # of this software and associated documentation files (the "Software"), to deal
  5. # in the Software without restriction, including without limitation the rights
  6. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. # copies of the Software, and to permit persons to whom the Software is
  8. # furnished to do so, subject to the following conditions:
  9. # The above copyright notice and this permission notice shall be included in all
  10. # copies or substantial portions of the Software.
  11. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  17. # SOFTWARE.
  18. include (CMakeParseArguments)
  19. set (GenerateProductVersionCurrentDir ${CMAKE_CURRENT_LIST_DIR})
  20. # generate_product_version() function
  21. #
  22. # This function uses VersionInfo.in template file and VersionResource.rc file
  23. # to generate WIN32 resource with version information and general resource strings.
  24. #
  25. # Usage:
  26. # generate_product_version(
  27. # SomeOutputResourceVariable
  28. # NAME MyGreatProject
  29. # ICON ${PATH_TO_APP_ICON}
  30. # VERSION_MAJOR 2
  31. # VERSION_MINOR 3
  32. # VERSION_PATH ${BUILD_COUNTER}
  33. # VERSION_REVISION ${BUILD_REVISION}
  34. # )
  35. # where BUILD_COUNTER and BUILD_REVISION could be values from your CI server.
  36. #
  37. # You can use generated resource for your executable targets:
  38. # add_executable(target-name ${target-files} ${SomeOutputResourceVariable})
  39. #
  40. # You can specify resource strings in arguments:
  41. # NAME - name of executable (no defaults, ex: Microsoft Word)
  42. # BUNDLE - bundle (${NAME} is default, ex: Microsoft Office)
  43. # USE_ICON - flag that shows whether icon is used or not
  44. # ICON - path to application icon (${CMAKE_SOURCE_DIR}/product.ico by default)
  45. # VERSION_MAJOR - 1 is default
  46. # VERSION_MINOR - 0 is default
  47. # VERSION_PATCH - 0 is default
  48. # VERSION_REVISION - 0 is default
  49. # COMPANY_NAME - your company name (no defaults)
  50. # COMPANY_COPYRIGHT - ${COMPANY_NAME} (C) Copyright ${CURRENT_YEAR} is default
  51. # COMMENTS - ${NAME} v${VERSION_MAJOR}.${VERSION_MINOR} is default
  52. # ORIGINAL_FILENAME - ${NAME} is default
  53. # INTERNAL_NAME - ${NAME} is default
  54. # FILE_DESCRIPTION - ${NAME} is default
  55. function(generate_product_version outfiles)
  56. set (options)
  57. set (oneValueArgs
  58. NAME
  59. BUNDLE
  60. USE_ICON
  61. ICON
  62. VERSION_MAJOR
  63. VERSION_MINOR
  64. VERSION_PATCH
  65. VERSION_REVISION
  66. COMPANY_NAME
  67. COMPANY_COPYRIGHT
  68. COMMENTS
  69. ORIGINAL_FILENAME
  70. INTERNAL_NAME
  71. FILE_DESCRIPTION)
  72. set (multiValueArgs)
  73. cmake_parse_arguments(PRODUCT "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  74. if (NOT PRODUCT_BUNDLE OR "${PRODUCT_BUNDLE}" STREQUAL "")
  75. set(PRODUCT_BUNDLE "${PRODUCT_NAME}")
  76. endif()
  77. if (USE_ICON)
  78. if (NOT PRODUCT_ICON OR "${PRODUCT_ICON}" STREQUAL "")
  79. set(PRODUCT_ICON "${CMAKE_SOURCE_DIR}/product.ico")
  80. endif()
  81. else ()
  82. set (USE_ICON 0)
  83. endif ()
  84. if (NOT PRODUCT_VERSION_MAJOR EQUAL 0 AND (NOT PRODUCT_VERSION_MAJOR OR "${PRODUCT_VERSION_MAJOR}" STREQUAL ""))
  85. set(PRODUCT_VERSION_MAJOR 1)
  86. endif()
  87. if (NOT PRODUCT_VERSION_MINOR EQUAL 0 AND (NOT PRODUCT_VERSION_MINOR OR "${PRODUCT_VERSION_MINOR}" STREQUAL ""))
  88. set(PRODUCT_VERSION_MINOR 0)
  89. endif()
  90. if (NOT PRODUCT_VERSION_PATCH EQUAL 0 AND (NOT PRODUCT_VERSION_PATCH OR "${PRODUCT_VERSION_PATCH}" STREQUAL ""))
  91. set(PRODUCT_VERSION_PATCH 0)
  92. endif()
  93. if (NOT PRODUCT_VERSION_REVISION EQUAL 0 AND (NOT PRODUCT_VERSION_REVISION OR "${PRODUCT_VERSION_REVISION}" STREQUAL ""))
  94. set(PRODUCT_VERSION_REVISION 0)
  95. endif()
  96. if (NOT PRODUCT_COMPANY_COPYRIGHT OR "${PRODUCT_COMPANY_COPYRIGHT}" STREQUAL "")
  97. string(TIMESTAMP PRODUCT_CURRENT_YEAR "%Y")
  98. set(PRODUCT_COMPANY_COPYRIGHT "${PRODUCT_COMPANY_NAME} (C) Copyright ${PRODUCT_CURRENT_YEAR}")
  99. endif()
  100. if (NOT PRODUCT_COMMENTS OR "${PRODUCT_COMMENTS}" STREQUAL "")
  101. set(PRODUCT_COMMENTS "${PRODUCT_NAME} v${PRODUCT_VERSION_MAJOR}.${PRODUCT_VERSION_MINOR}")
  102. endif()
  103. if (NOT PRODUCT_ORIGINAL_FILENAME OR "${PRODUCT_ORIGINAL_FILENAME}" STREQUAL "")
  104. set(PRODUCT_ORIGINAL_FILENAME "${PRODUCT_NAME}")
  105. endif()
  106. if (NOT PRODUCT_INTERNAL_NAME OR "${PRODUCT_INTERNAL_NAME}" STREQUAL "")
  107. set(PRODUCT_INTERNAL_NAME "${PRODUCT_NAME}")
  108. endif()
  109. if (NOT PRODUCT_FILE_DESCRIPTION OR "${PRODUCT_FILE_DESCRIPTION}" STREQUAL "")
  110. set(PRODUCT_FILE_DESCRIPTION "${PRODUCT_NAME}")
  111. endif()
  112. set (_VersionInfoFile ${CMAKE_CURRENT_BINARY_DIR}/VersionInfo.h)
  113. set (_VersionResourceFile ${CMAKE_CURRENT_BINARY_DIR}/VersionResource.rc)
  114. configure_file(
  115. ${GenerateProductVersionCurrentDir}/VersionInfo.in
  116. ${_VersionInfoFile}
  117. @ONLY)
  118. configure_file(
  119. ${GenerateProductVersionCurrentDir}/VersionResource.rc
  120. ${_VersionResourceFile}
  121. COPYONLY)
  122. list(APPEND ${outfiles} ${_VersionInfoFile} ${_VersionResourceFile})
  123. set (${outfiles} ${${outfiles}} PARENT_SCOPE)
  124. endfunction()