macros.cmake 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. macro(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
  71. # entries
  72. foreach(_ITEM ${${_LIST}})
  73. set(${_OUTPUT} "${${_OUTPUT}} ${_LPREFIX}${_ITEM}")
  74. endforeach()
  75. endmacro()
  76. function(listtostrrev _LIST _OUTPUT)
  77. if(${ARGC} EQUAL 3)
  78. # prefix for each element
  79. set(_LPREFIX ${ARGV2})
  80. else()
  81. set(_LPREFIX "")
  82. endif()
  83. # Do not use string(REPLACE ";" " ") here to avoid messing up list
  84. # entries
  85. set(res)
  86. foreach(_ITEM ${${_LIST}})
  87. set(res "${res} ${_LPREFIX}${_ITEM}")
  88. endforeach()
  89. set($_OUTPUT} "${res}" PARENT_SCOPE)
  90. endfunction()
  91. function(find_stringlength_longest_item inList outLength)
  92. set(maxLength 0)
  93. foreach(item IN LISTS ${inList})
  94. string(LENGTH "${item}" slen)
  95. if(slen GREATER maxLength)
  96. set(maxLength ${slen})
  97. endif()
  98. endforeach()
  99. set("${outLength}" ${maxLength} PARENT_SCOPE)
  100. endfunction()
  101. function(message_dictlist inList)
  102. find_stringlength_longest_item(${inList} maxLength)
  103. foreach(name IN LISTS ${inList})
  104. # Get the padding
  105. string(LENGTH ${name} nameLength)
  106. math(EXPR padLength "(${maxLength} + 1) - ${nameLength}")
  107. string(RANDOM LENGTH ${padLength} ALPHABET " " padding)
  108. message_tested_option(${name} ${padding})
  109. endforeach()
  110. endfunction()
  111. if(CMAKE_VERSION VERSION_LESS 3.16.0 OR SDL3_SUBPROJECT)
  112. # - CMake versions <3.16 do not support the OBJC language
  113. # - When SDL is built as a subproject and when the main project does not enable OBJC,
  114. # CMake fails due to missing internal CMake variables (CMAKE_OBJC_COMPILE_OBJECT)
  115. # (reproduced with CMake 3.24.2)
  116. macro(CHECK_OBJC_SOURCE_COMPILES SOURCE VAR)
  117. set(PREV_REQUIRED_DEFS "${CMAKE_REQUIRED_DEFINITIONS}")
  118. set(CMAKE_REQUIRED_DEFINITIONS "-x objective-c ${PREV_REQUIRED_DEFS}")
  119. CHECK_C_SOURCE_COMPILES("${SOURCE}" ${VAR})
  120. set(CMAKE_REQUIRED_DEFINITIONS "${PREV_REQUIRED_DEFS}")
  121. endmacro()
  122. else()
  123. include(CheckOBJCSourceCompiles)
  124. if (APPLE)
  125. enable_language(OBJC)
  126. endif()
  127. endif()
  128. if(CMAKE_VERSION VERSION_LESS 3.18)
  129. function(check_linker_flag LANG FLAG VAR)
  130. cmake_push_check_state()
  131. list(APPEND CMAKE_REQUIRED_LINK_OPTIONS ${FLAG} )
  132. if(LANG STREQUAL "C")
  133. include(CheckCSourceCompiles)
  134. check_c_source_compiles("int main(int argc,char*argv[]){(void)argc;(void)argv;return 0;}" ${VAR} FAIL_REGEX "warning")
  135. elseif(LANG STREQUAL "CXX")
  136. include(CheckCXXSourceCompiles)
  137. check_cxx_source_compiles("int main(int argc,char*argv[]){(void)argc;(void)argv;return 0;}" ${VAR} FAIL_REGEX "warning")
  138. else()
  139. message(FATAL_ERROR "Unsupported language: ${LANG}")
  140. endif()
  141. cmake_pop_check_state()
  142. endfunction()
  143. else()
  144. cmake_policy(SET CMP0057 NEW) # Support new if() IN_LIST operator. (used inside check_linker_flag, used in CMake 3.18)
  145. include(CheckLinkerFlag)
  146. endif()
  147. if(APPLE)
  148. check_language(OBJC)
  149. if(NOT CMAKE_OBJC_COMPILER)
  150. message(WARNING "Cannot find working OBJC compiler.")
  151. endif()
  152. endif()
  153. if(CMAKE_VERSION VERSION_LESS 3.13.0)
  154. macro(target_link_directories _TARGET _SCOPE)
  155. link_directories(${ARGN})
  156. endmacro()
  157. endif()