Version.cmake 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #
  2. # Copyright (c) Contributors to the Open 3D Engine Project.
  3. # For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. #
  5. # SPDX-License-Identifier: Apache-2.0 OR MIT
  6. #
  7. #
  8. include_guard()
  9. string(TIMESTAMP current_year "%Y")
  10. set(O3DE_COPYRIGHT_YEAR ${current_year} CACHE STRING "Open 3D Engine's copyright year")
  11. # avoid reading engine.json multiple times
  12. ly_file_read("${LY_ROOT_FOLDER}/engine.json" tmp_json_data)
  13. set_property(GLOBAL PROPERTY O3DE_ENGINE_JSON_DATA ${tmp_json_data})
  14. unset(tmp_json_data)
  15. #! o3de_get_name_and_version_specifier: Parse the dependency name, and optional version
  16. # operator and version from the supplied input string.
  17. # \arg:input - the input string e.g. o3de==1.0.0
  18. # \arg:dependency_name - the name to the left of any optional version specifier
  19. # \arg:operator - the version specifier operator e.g. ==
  20. # \arg:version - the version specifier version e.g. 1.0.0
  21. function(o3de_get_name_and_version_specifier input dependency_name operator version)
  22. if("${input}" MATCHES "^(.*)(~=|==|!=|<=|>=|<|>|===)(.*)$")
  23. if(${CMAKE_MATCH_COUNT} GREATER_EQUAL 1)
  24. string(STRIP ${CMAKE_MATCH_1} _dependency_name)
  25. set(${dependency_name} ${_dependency_name} PARENT_SCOPE)
  26. endif()
  27. if(${CMAKE_MATCH_COUNT} GREATER_EQUAL 2)
  28. set(${operator} ${CMAKE_MATCH_2} PARENT_SCOPE)
  29. endif()
  30. if(${CMAKE_MATCH_COUNT} GREATER_EQUAL 3)
  31. string(STRIP ${CMAKE_MATCH_3} _version)
  32. set(${version} ${_version} PARENT_SCOPE)
  33. endif()
  34. else()
  35. # format unknown, assume it's just the dependency name
  36. set(${dependency_name} ${input} PARENT_SCOPE)
  37. endif()
  38. endfunction()
  39. #! o3de_get_version_compatible: Check if the input version is compatible based on
  40. # the operator and specifier version provided
  41. # \arg:version - input version to check e.g. 1.0.0
  42. # \arg:op - the version specifier operator e.g. ==
  43. # \arg:specifier_version - the version part of the version specifier e.g. 1.2.0
  44. # \arg:is_compatible - TRUE if version is compatible otherwise FALSE
  45. function(o3de_get_version_compatible version op specifier_version is_compatible)
  46. set(contains_version FALSE)
  47. if(op STREQUAL "==" AND version VERSION_EQUAL specifier_version)
  48. set(contains_version TRUE)
  49. elseif(op STREQUAL "!=" AND NOT version VERSION_EQUAL specifier_version)
  50. set(contains_version TRUE)
  51. elseif(op STREQUAL "<=" AND version VERSION_LESS_EQUAL specifier_version)
  52. set(contains_version TRUE)
  53. elseif(op STREQUAL ">=" AND version VERSION_GREATER_EQUAL specifier_version)
  54. set(contains_version TRUE)
  55. elseif(op STREQUAL "<" AND version VERSION_LESS specifier_version)
  56. set(contains_version TRUE)
  57. elseif(op STREQUAL ">" AND version VERSION_GREATER specifier_version)
  58. set(contains_version TRUE)
  59. elseif(op STREQUAL "===" AND version STREQUAL specifier_version)
  60. set(contains_version TRUE)
  61. elseif(op STREQUAL "~=")
  62. # compatible versions have an equivalent combination of >= and ==
  63. # e.g. ~=2.2 is equivalent to >=2.2,==2.*
  64. if(version VERSION_GREATER_EQUAL specifier_version)
  65. string(REPLACE "." ";" specifier_version_part_list ${specifier_version})
  66. list(LENGTH specifier_version_part_list list_length)
  67. if(list_length LESS 2)
  68. # truncating would leave nothing to compare
  69. set(contains_version TRUE)
  70. else()
  71. # trim the last version part because CMake doesn't support '*'
  72. math(EXPR truncated_length "${list_length} - 1")
  73. list(SUBLIST specifier_version_part_list 0 ${truncated_length} specifier_version)
  74. string(REPLACE ";" "." specifier_version "${specifier_version}")
  75. string(REPLACE "." ";" version_part_list ${version})
  76. list(SUBLIST version_part_list 0 ${truncated_length} version)
  77. string(REPLACE ";" "." version "${version}")
  78. # compare the truncated versions
  79. if(version VERSION_EQUAL specifier_version)
  80. set(contains_version TRUE)
  81. endif()
  82. endif()
  83. endif()
  84. endif()
  85. set(${is_compatible} ${contains_version} PARENT_SCOPE)
  86. endfunction()
  87. #! o3de_read_engine_default: Read a field from engine.json or use the default if not found
  88. # \arg:output_value - name of output variable to set
  89. # \arg:key - name of field in engine.json
  90. # \arg:default_value - value to use if neither environment var or engine.json value found
  91. macro(o3de_read_engine_default output_value key default_value)
  92. get_property(engine_json_data GLOBAL PROPERTY O3DE_ENGINE_JSON_DATA)
  93. string(JSON tmp_value ERROR_VARIABLE manifest_json_error GET ${engine_json_data} ${key})
  94. # unset engine_json_data because we're in a macro
  95. unset(engine_json_data)
  96. if(manifest_json_error)
  97. message(WARNING "Failed to read ${key} from \"${LY_ROOT_FOLDER}/engine.json\" : ${manifest_json_error}")
  98. set(tmp_value ${default_value})
  99. endif()
  100. ly_set(${output_value} ${tmp_value})
  101. set_property(GLOBAL PROPERTY ${output_value} ${tmp_value})
  102. # unset tmp_value because we're in a macro
  103. unset(tmp_value)
  104. endmacro()
  105. #! o3de_set_major_minor_patch_with_prefix: Parses a SemVer string and sets
  106. # separate major, minor and patch global properties.
  107. # e.g. given "1.2.3" and prefix VER the following properties are set:
  108. # VER_MAJOR 1
  109. # VER_MINOR 2
  110. # VER_PATCH 3
  111. #
  112. # \arg:prefix - prefix for cmake variable names
  113. # \arg:version_string - input string in SemVer format e.g. "1.2.3"
  114. function(o3de_set_major_minor_patch_with_prefix prefix version_string)
  115. string(REPLACE "." ";" version_list ${version_string})
  116. list(GET version_list 0 major)
  117. list(GET version_list 1 minor)
  118. list(GET version_list 2 patch)
  119. set_property(GLOBAL PROPERTY ${prefix}_MAJOR ${major})
  120. set_property(GLOBAL PROPERTY ${prefix}_MINOR ${minor})
  121. set_property(GLOBAL PROPERTY ${prefix}_PATCH ${patch})
  122. endfunction()
  123. #! o3de_get_major_minor_patch_with_prefix: Get the major, minor and patch
  124. # for global properties with the specified prefix and store the output
  125. # in the provided variables.
  126. #
  127. # Example storing O3DE_VERSION_MAJOR in major, O3DE_VERSION_MINOR in minor
  128. # and O3DE_VERSION_PATCH in patch and print the SemVer:
  129. #
  130. # o3de_get_major_minor_patch_with_prefix(O3DE_VERSION major minor patch)
  131. # message(INFO "O3DE version is ${major}.${minor}.${patch}")
  132. #
  133. # \arg:prefix - global properties prefix
  134. # \arg:output_major - output for <prefix>_MAJOR property
  135. # \arg:output_minor - output for <prefix>_MINOR property
  136. # \arg:output_patch - output for <prefix>_PATCH property
  137. macro(o3de_get_major_minor_patch_with_prefix prefix output_major output_minor output_patch)
  138. get_property(${output_major} GLOBAL PROPERTY ${prefix}_MAJOR )
  139. get_property(${output_minor} GLOBAL PROPERTY ${prefix}_MINOR )
  140. get_property(${output_patch} GLOBAL PROPERTY ${prefix}_PATCH )
  141. endmacro()
  142. # set engine.json variables and global properties
  143. # these are not cached variables and cannot be configured by the user
  144. o3de_read_engine_default(O3DE_VERSION_STRING "version" "0.0.0")
  145. o3de_read_engine_default(O3DE_DISPLAY_VERSION_STRING "display_version" "00.00")
  146. o3de_read_engine_default(O3DE_BUILD_VERSION "build" 0)
  147. o3de_read_engine_default(O3DE_ENGINE_NAME "engine_name" "o3de")
  148. # set O3DE_VERSION_MAJOR/MINOR/PATCH global properties
  149. o3de_set_major_minor_patch_with_prefix(O3DE_VERSION ${O3DE_VERSION_STRING})
  150. # set variables for INSTALL targets
  151. # these ARE cached variables and can be configured by the user
  152. o3de_set_from_env_with_default(O3DE_INSTALL_VERSION_STRING O3DE_INSTALL_VERSION "@O3DE_VERSION_STRING@" CACHE STRING "Open 3D Engine's version for the INSTALL target")
  153. o3de_set_from_env_with_default(O3DE_INSTALL_DISPLAY_VERSION_STRING O3DE_INSTALL_DISPLAY_VERSION "@O3DE_DISPLAY_VERSION_STRING@" CACHE STRING "Open 3D Engine's display version for the INSTALL target")
  154. o3de_set_from_env_with_default(O3DE_INSTALL_BUILD_VERSION O3DE_INSTALL_BUILD_VERSION "@O3DE_BUILD_VERSION@" CACHE STRING "Open 3D Engine's build number for the INSTALL target")
  155. o3de_set_from_env_with_default(O3DE_INSTALL_ENGINE_NAME O3DE_INSTALL_ENGINE_NAME "@O3DE_ENGINE_NAME@" CACHE STRING "Open 3D Engine's engine name for the INSTALL target")