macros.cmake 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. macro(add_to_alloptions _NEWNAME)
  2. list(APPEND ALLOPTIONS ${_NEWNAME})
  3. string(LENGTH ${_NEWNAME} _SLEN)
  4. if(${LONGESTOPTIONNAME} LESS ${_SLEN})
  5. set(LONGESTOPTIONNAME ${_SLEN})
  6. endif()
  7. endmacro()
  8. macro(set_option _NAME _DESC)
  9. add_to_alloptions(${_NAME})
  10. if(${ARGC} EQUAL 3)
  11. set(_DEFLT ${ARGV2})
  12. else()
  13. set(_DEFLT OFF)
  14. endif()
  15. option(${_NAME} ${_DESC} ${_DEFLT})
  16. endmacro()
  17. macro(dep_option _NAME _DESC _DEFLT _DEPTEST _FAILDFLT)
  18. add_to_alloptions("${_NAME}")
  19. cmake_dependent_option("${_NAME}" "${_DESC}" "${_DEFLT}" "${_DEPTEST}" "${_FAILDFLT}")
  20. endmacro()
  21. macro(option_string _NAME _DESC _VALUE)
  22. add_to_alloptions(${_NAME})
  23. set(${_NAME} ${_VALUE} CACHE STRING "${_DESC}")
  24. set(HAVE_${_NAME} ${_VALUE})
  25. ENDMACRO()
  26. # Message Output
  27. macro(message_warn _TEXT)
  28. message(WARNING "${_TEXT}")
  29. endmacro()
  30. macro(message_error _TEXT)
  31. message(FATAL_ERROR "*** ERROR: ${_TEXT}")
  32. endmacro()
  33. macro(message_bool_option _NAME _VALUE)
  34. set(_PAD "\t")
  35. if(${ARGC} EQUAL 3)
  36. set(_PAD ${ARGV2})
  37. endif()
  38. if(${_VALUE})
  39. message(STATUS " ${_NAME}:${_PAD}ON")
  40. else()
  41. message(STATUS " ${_NAME}:${_PAD}OFF")
  42. endif()
  43. endmacro()
  44. macro(message_tested_option _NAME)
  45. set(_REQVALUE ${${_NAME}})
  46. set(_PAD " ")
  47. if(${ARGC} EQUAL 2)
  48. set(_PAD ${ARGV1})
  49. endif()
  50. string(SUBSTRING "${_NAME}" 0 4 _NAMESTART)
  51. if(_NAMESTART STREQUAL "SDL_")
  52. string(SUBSTRING "${_NAME}" 4 -1 _STRIPPEDNAME)
  53. else()
  54. set(_STRIPPEDNAME "${_NAME}")
  55. endif()
  56. if(NOT HAVE_${_STRIPPEDNAME})
  57. set(HAVE_${_STRIPPEDNAME} OFF)
  58. elseif("${HAVE_${_STRIPPEDNAME}}" MATCHES "1|TRUE|YES|Y")
  59. set(HAVE_${_STRIPPEDNAME} ON)
  60. endif()
  61. message(STATUS " ${_NAME}${_PAD}(Wanted: ${_REQVALUE}): ${HAVE_${_STRIPPEDNAME}}")
  62. endmacro()
  63. function(listtostr LIST OUTPUT)
  64. if(${ARGC} EQUAL 3)
  65. # prefix for each element
  66. set(LPREFIX ${ARGV2})
  67. else()
  68. set(LPREFIX "")
  69. endif()
  70. # Do not use string(REPLACE ";" " ") here to avoid messing up list entries
  71. set(res)
  72. foreach(ITEM ${${LIST}})
  73. string(SUBSTRING "${ITEM}" 0 6 start)
  74. if(start STREQUAL "SHELL:")
  75. string(SUBSTRING "${ITEM}" 6 -1 ITEM)
  76. endif()
  77. if(ITEM)
  78. set(res "${res} ${LPREFIX}${ITEM}")
  79. endif()
  80. endforeach()
  81. string(STRIP "${res}" res)
  82. set(${OUTPUT} "${res}" PARENT_SCOPE)
  83. endfunction()
  84. function(listtostrrev _LIST _OUTPUT)
  85. if(${ARGC} EQUAL 3)
  86. # prefix for each element
  87. set(_LPREFIX ${ARGV2})
  88. else()
  89. set(_LPREFIX "")
  90. endif()
  91. # Do not use string(REPLACE ";" " ") here to avoid messing up list
  92. # entries
  93. set(res)
  94. foreach(_ITEM ${${_LIST}})
  95. string(SUBSTRING "${_ITEM}" 0 6 start)
  96. if(start STREQUAL "SHELL:")
  97. string(SUBSTRING "${_ITEM}" 6 -1 _ITEM)
  98. endif()
  99. set(res "${res} ${_LPREFIX}${_ITEM}")
  100. endforeach()
  101. string(STRIP "${res}" res)
  102. set($_OUTPUT} "${res}" PARENT_SCOPE)
  103. endfunction()
  104. function(find_stringlength_longest_item inList outLength)
  105. set(maxLength 0)
  106. foreach(item IN LISTS ${inList})
  107. string(LENGTH "${item}" slen)
  108. if(slen GREATER maxLength)
  109. set(maxLength ${slen})
  110. endif()
  111. endforeach()
  112. set("${outLength}" ${maxLength} PARENT_SCOPE)
  113. endfunction()
  114. function(message_dictlist inList)
  115. find_stringlength_longest_item(${inList} maxLength)
  116. foreach(name IN LISTS ${inList})
  117. # Get the padding
  118. string(LENGTH ${name} nameLength)
  119. math(EXPR padLength "(${maxLength} + 1) - ${nameLength}")
  120. string(RANDOM LENGTH ${padLength} ALPHABET " " padding)
  121. message_tested_option(${name} ${padding})
  122. endforeach()
  123. endfunction()
  124. if(CMAKE_VERSION VERSION_LESS 3.16.0 OR SDL3_SUBPROJECT)
  125. # - CMake versions <3.16 do not support the OBJC language
  126. # - When SDL is built as a subproject and when the main project does not enable OBJC,
  127. # CMake fails due to missing internal CMake variables (CMAKE_OBJC_COMPILE_OBJECT)
  128. # (reproduced with CMake 3.24.2)
  129. macro(CHECK_OBJC_SOURCE_COMPILES SOURCE VAR)
  130. set(PREV_REQUIRED_DEFS "${CMAKE_REQUIRED_DEFINITIONS}")
  131. set(CMAKE_REQUIRED_DEFINITIONS "-x objective-c ${PREV_REQUIRED_DEFS}")
  132. CHECK_C_SOURCE_COMPILES("${SOURCE}" ${VAR})
  133. set(CMAKE_REQUIRED_DEFINITIONS "${PREV_REQUIRED_DEFS}")
  134. endmacro()
  135. else()
  136. include(CheckOBJCSourceCompiles)
  137. if (APPLE)
  138. enable_language(OBJC)
  139. endif()
  140. endif()
  141. if(CMAKE_VERSION VERSION_LESS 3.18)
  142. function(check_linker_flag LANG FLAG VAR)
  143. cmake_push_check_state()
  144. list(APPEND CMAKE_REQUIRED_LINK_OPTIONS ${FLAG} )
  145. if(LANG STREQUAL "C")
  146. include(CheckCSourceCompiles)
  147. check_c_source_compiles("int main(int argc,char*argv[]){(void)argc;(void)argv;return 0;}" ${VAR} FAIL_REGEX "warning")
  148. elseif(LANG STREQUAL "CXX")
  149. include(CheckCXXSourceCompiles)
  150. check_cxx_source_compiles("int main(int argc,char*argv[]){(void)argc;(void)argv;return 0;}" ${VAR} FAIL_REGEX "warning")
  151. else()
  152. message(FATAL_ERROR "Unsupported language: ${LANG}")
  153. endif()
  154. cmake_pop_check_state()
  155. endfunction()
  156. else()
  157. cmake_policy(SET CMP0057 NEW) # Support new if() IN_LIST operator. (used inside check_linker_flag, used in CMake 3.18)
  158. include(CheckLinkerFlag)
  159. endif()
  160. if(APPLE)
  161. check_language(OBJC)
  162. if(NOT CMAKE_OBJC_COMPILER)
  163. message(WARNING "Cannot find working OBJC compiler.")
  164. endif()
  165. endif()
  166. if(CMAKE_VERSION VERSION_LESS 3.13.0)
  167. macro(target_link_directories _TARGET _SCOPE)
  168. link_directories(${ARGN})
  169. endmacro()
  170. endif()