CMakeCXXInformation.cmake 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. # This file sets the basic flags for the C++ language in CMake.
  4. # It also loads the available platform file for the system-compiler
  5. # if it exists.
  6. # It also loads a system - compiler - processor (or target hardware)
  7. # specific file, which is mainly useful for crosscompiling and embedded systems.
  8. include(CMakeLanguageInformation)
  9. # some compilers use different extensions (e.g. sdcc uses .rel)
  10. # so set the extension here first so it can be overridden by the compiler specific file
  11. if(UNIX)
  12. set(CMAKE_CXX_OUTPUT_EXTENSION .o)
  13. else()
  14. set(CMAKE_CXX_OUTPUT_EXTENSION .obj)
  15. endif()
  16. set(_INCLUDED_FILE 0)
  17. # Load compiler-specific information.
  18. if(CMAKE_CXX_COMPILER_ID)
  19. include(Compiler/${CMAKE_CXX_COMPILER_ID}-CXX OPTIONAL)
  20. endif()
  21. set(CMAKE_BASE_NAME)
  22. get_filename_component(CMAKE_BASE_NAME "${CMAKE_CXX_COMPILER}" NAME_WE)
  23. # since the gnu compiler has several names force g++
  24. if(CMAKE_COMPILER_IS_GNUCXX)
  25. set(CMAKE_BASE_NAME g++)
  26. endif()
  27. # load a hardware specific file, mostly useful for embedded compilers
  28. if(CMAKE_SYSTEM_PROCESSOR)
  29. if(CMAKE_CXX_COMPILER_ID)
  30. include(Platform/${CMAKE_EFFECTIVE_SYSTEM_NAME}-${CMAKE_CXX_COMPILER_ID}-CXX-${CMAKE_SYSTEM_PROCESSOR} OPTIONAL RESULT_VARIABLE _INCLUDED_FILE)
  31. endif()
  32. if (NOT _INCLUDED_FILE)
  33. include(Platform/${CMAKE_EFFECTIVE_SYSTEM_NAME}-${CMAKE_BASE_NAME}-${CMAKE_SYSTEM_PROCESSOR} OPTIONAL)
  34. endif ()
  35. endif()
  36. # load the system- and compiler specific files
  37. if(CMAKE_CXX_COMPILER_ID)
  38. include(Platform/${CMAKE_EFFECTIVE_SYSTEM_NAME}-${CMAKE_CXX_COMPILER_ID}-CXX OPTIONAL RESULT_VARIABLE _INCLUDED_FILE)
  39. endif()
  40. if (NOT _INCLUDED_FILE)
  41. include(Platform/${CMAKE_EFFECTIVE_SYSTEM_NAME}-${CMAKE_BASE_NAME} OPTIONAL
  42. RESULT_VARIABLE _INCLUDED_FILE)
  43. endif ()
  44. # load any compiler-wrapper specific information
  45. if (CMAKE_CXX_COMPILER_WRAPPER)
  46. __cmake_include_compiler_wrapper(CXX)
  47. endif ()
  48. # We specify the compiler information in the system file for some
  49. # platforms, but this language may not have been enabled when the file
  50. # was first included. Include it again to get the language info.
  51. # Remove this when all compiler info is removed from system files.
  52. if (NOT _INCLUDED_FILE)
  53. include(Platform/${CMAKE_SYSTEM_NAME} OPTIONAL)
  54. endif ()
  55. if(CMAKE_CXX_SIZEOF_DATA_PTR)
  56. foreach(f ${CMAKE_CXX_ABI_FILES})
  57. include(${f})
  58. endforeach()
  59. unset(CMAKE_CXX_ABI_FILES)
  60. endif()
  61. # This should be included before the _INIT variables are
  62. # used to initialize the cache. Since the rule variables
  63. # have if blocks on them, users can still define them here.
  64. # But, it should still be after the platform file so changes can
  65. # be made to those values.
  66. if(CMAKE_USER_MAKE_RULES_OVERRIDE)
  67. # Save the full path of the file so try_compile can use it.
  68. include(${CMAKE_USER_MAKE_RULES_OVERRIDE} RESULT_VARIABLE _override)
  69. set(CMAKE_USER_MAKE_RULES_OVERRIDE "${_override}")
  70. endif()
  71. if(CMAKE_USER_MAKE_RULES_OVERRIDE_CXX)
  72. # Save the full path of the file so try_compile can use it.
  73. include(${CMAKE_USER_MAKE_RULES_OVERRIDE_CXX} RESULT_VARIABLE _override)
  74. set(CMAKE_USER_MAKE_RULES_OVERRIDE_CXX "${_override}")
  75. endif()
  76. # Create a set of shared library variable specific to C++
  77. # For 90% of the systems, these are the same flags as the C versions
  78. # so if these are not set just copy the flags from the c version
  79. if(NOT CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS)
  80. set(CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS ${CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS})
  81. endif()
  82. if(NOT CMAKE_CXX_COMPILE_OPTIONS_PIC)
  83. set(CMAKE_CXX_COMPILE_OPTIONS_PIC ${CMAKE_C_COMPILE_OPTIONS_PIC})
  84. endif()
  85. if(NOT CMAKE_CXX_COMPILE_OPTIONS_PIE)
  86. set(CMAKE_CXX_COMPILE_OPTIONS_PIE ${CMAKE_C_COMPILE_OPTIONS_PIE})
  87. endif()
  88. if(NOT CMAKE_CXX_LINK_OPTIONS_PIE)
  89. set(CMAKE_CXX_LINK_OPTIONS_PIE ${CMAKE_C_LINK_OPTIONS_PIE})
  90. endif()
  91. if(NOT CMAKE_CXX_LINK_OPTIONS_NO_PIE)
  92. set(CMAKE_CXX_LINK_OPTIONS_NO_PIE ${CMAKE_C_LINK_OPTIONS_NO_PIE})
  93. endif()
  94. if(NOT CMAKE_CXX_COMPILE_OPTIONS_DLL)
  95. set(CMAKE_CXX_COMPILE_OPTIONS_DLL ${CMAKE_C_COMPILE_OPTIONS_DLL})
  96. endif()
  97. if(NOT CMAKE_SHARED_LIBRARY_CXX_FLAGS)
  98. set(CMAKE_SHARED_LIBRARY_CXX_FLAGS ${CMAKE_SHARED_LIBRARY_C_FLAGS})
  99. endif()
  100. if(NOT DEFINED CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS)
  101. set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS ${CMAKE_SHARED_LIBRARY_LINK_C_FLAGS})
  102. endif()
  103. if(NOT CMAKE_SHARED_LIBRARY_RUNTIME_CXX_FLAG)
  104. set(CMAKE_SHARED_LIBRARY_RUNTIME_CXX_FLAG ${CMAKE_SHARED_LIBRARY_RUNTIME_C_FLAG})
  105. endif()
  106. if(NOT CMAKE_SHARED_LIBRARY_RUNTIME_CXX_FLAG_SEP)
  107. set(CMAKE_SHARED_LIBRARY_RUNTIME_CXX_FLAG_SEP ${CMAKE_SHARED_LIBRARY_RUNTIME_C_FLAG_SEP})
  108. endif()
  109. if(NOT CMAKE_SHARED_LIBRARY_RPATH_LINK_CXX_FLAG)
  110. set(CMAKE_SHARED_LIBRARY_RPATH_LINK_CXX_FLAG ${CMAKE_SHARED_LIBRARY_RPATH_LINK_C_FLAG})
  111. endif()
  112. if(NOT DEFINED CMAKE_EXE_EXPORTS_CXX_FLAG)
  113. set(CMAKE_EXE_EXPORTS_CXX_FLAG ${CMAKE_EXE_EXPORTS_C_FLAG})
  114. endif()
  115. if(NOT DEFINED CMAKE_SHARED_LIBRARY_SONAME_CXX_FLAG)
  116. set(CMAKE_SHARED_LIBRARY_SONAME_CXX_FLAG ${CMAKE_SHARED_LIBRARY_SONAME_C_FLAG})
  117. endif()
  118. if(NOT CMAKE_EXECUTABLE_RUNTIME_CXX_FLAG)
  119. set(CMAKE_EXECUTABLE_RUNTIME_CXX_FLAG ${CMAKE_SHARED_LIBRARY_RUNTIME_CXX_FLAG})
  120. endif()
  121. if(NOT CMAKE_EXECUTABLE_RUNTIME_CXX_FLAG_SEP)
  122. set(CMAKE_EXECUTABLE_RUNTIME_CXX_FLAG_SEP ${CMAKE_SHARED_LIBRARY_RUNTIME_CXX_FLAG_SEP})
  123. endif()
  124. if(NOT CMAKE_EXECUTABLE_RPATH_LINK_CXX_FLAG)
  125. set(CMAKE_EXECUTABLE_RPATH_LINK_CXX_FLAG ${CMAKE_SHARED_LIBRARY_RPATH_LINK_CXX_FLAG})
  126. endif()
  127. if(NOT DEFINED CMAKE_SHARED_LIBRARY_LINK_CXX_WITH_RUNTIME_PATH)
  128. set(CMAKE_SHARED_LIBRARY_LINK_CXX_WITH_RUNTIME_PATH ${CMAKE_SHARED_LIBRARY_LINK_C_WITH_RUNTIME_PATH})
  129. endif()
  130. if(NOT CMAKE_INCLUDE_FLAG_CXX)
  131. set(CMAKE_INCLUDE_FLAG_CXX ${CMAKE_INCLUDE_FLAG_C})
  132. endif()
  133. # for most systems a module is the same as a shared library
  134. # so unless the variable CMAKE_MODULE_EXISTS is set just
  135. # copy the values from the LIBRARY variables
  136. if(NOT CMAKE_MODULE_EXISTS)
  137. set(CMAKE_SHARED_MODULE_CXX_FLAGS ${CMAKE_SHARED_LIBRARY_CXX_FLAGS})
  138. set(CMAKE_SHARED_MODULE_CREATE_CXX_FLAGS ${CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS})
  139. endif()
  140. # repeat for modules
  141. if(NOT CMAKE_SHARED_MODULE_CREATE_CXX_FLAGS)
  142. set(CMAKE_SHARED_MODULE_CREATE_CXX_FLAGS ${CMAKE_SHARED_MODULE_CREATE_C_FLAGS})
  143. endif()
  144. if(NOT CMAKE_SHARED_MODULE_CXX_FLAGS)
  145. set(CMAKE_SHARED_MODULE_CXX_FLAGS ${CMAKE_SHARED_MODULE_C_FLAGS})
  146. endif()
  147. # Initialize CXX link type selection flags from C versions.
  148. foreach(type SHARED_LIBRARY SHARED_MODULE EXE)
  149. if(NOT CMAKE_${type}_LINK_STATIC_CXX_FLAGS)
  150. set(CMAKE_${type}_LINK_STATIC_CXX_FLAGS
  151. ${CMAKE_${type}_LINK_STATIC_C_FLAGS})
  152. endif()
  153. if(NOT CMAKE_${type}_LINK_DYNAMIC_CXX_FLAGS)
  154. set(CMAKE_${type}_LINK_DYNAMIC_CXX_FLAGS
  155. ${CMAKE_${type}_LINK_DYNAMIC_C_FLAGS})
  156. endif()
  157. endforeach()
  158. # add the flags to the cache based
  159. # on the initial values computed in the platform/*.cmake files
  160. # use _INIT variables so that this only happens the first time
  161. # and you can set these flags in the cmake cache
  162. set(CMAKE_CXX_FLAGS_INIT "$ENV{CXXFLAGS} ${CMAKE_CXX_FLAGS_INIT}")
  163. cmake_initialize_per_config_variable(CMAKE_CXX_FLAGS "Flags used by the CXX compiler")
  164. if(CMAKE_CXX_STANDARD_LIBRARIES_INIT)
  165. set(CMAKE_CXX_STANDARD_LIBRARIES "${CMAKE_CXX_STANDARD_LIBRARIES_INIT}"
  166. CACHE STRING "Libraries linked by default with all C++ applications.")
  167. mark_as_advanced(CMAKE_CXX_STANDARD_LIBRARIES)
  168. endif()
  169. include(CMakeCommonLanguageInclude)
  170. # now define the following rules:
  171. # CMAKE_CXX_CREATE_SHARED_LIBRARY
  172. # CMAKE_CXX_CREATE_SHARED_MODULE
  173. # CMAKE_CXX_COMPILE_OBJECT
  174. # CMAKE_CXX_LINK_EXECUTABLE
  175. # variables supplied by the generator at use time
  176. # <TARGET>
  177. # <TARGET_BASE> the target without the suffix
  178. # <OBJECTS>
  179. # <OBJECT>
  180. # <LINK_LIBRARIES>
  181. # <FLAGS>
  182. # <LINK_FLAGS>
  183. # CXX compiler information
  184. # <CMAKE_CXX_COMPILER>
  185. # <CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS>
  186. # <CMAKE_CXX_SHARED_MODULE_CREATE_FLAGS>
  187. # <CMAKE_CXX_LINK_FLAGS>
  188. # Static library tools
  189. # <CMAKE_AR>
  190. # <CMAKE_RANLIB>
  191. # create a shared C++ library
  192. if(NOT CMAKE_CXX_CREATE_SHARED_LIBRARY)
  193. set(CMAKE_CXX_CREATE_SHARED_LIBRARY
  194. "<CMAKE_CXX_COMPILER> <CMAKE_SHARED_LIBRARY_CXX_FLAGS> <LANGUAGE_COMPILE_FLAGS> <LINK_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS> <SONAME_FLAG><TARGET_SONAME> -o <TARGET> <OBJECTS> <LINK_LIBRARIES>")
  195. endif()
  196. # create a c++ shared module copy the shared library rule by default
  197. if(NOT CMAKE_CXX_CREATE_SHARED_MODULE)
  198. set(CMAKE_CXX_CREATE_SHARED_MODULE ${CMAKE_CXX_CREATE_SHARED_LIBRARY})
  199. endif()
  200. # Create a static archive incrementally for large object file counts.
  201. # If CMAKE_CXX_CREATE_STATIC_LIBRARY is set it will override these.
  202. if(NOT DEFINED CMAKE_CXX_ARCHIVE_CREATE)
  203. set(CMAKE_CXX_ARCHIVE_CREATE "<CMAKE_AR> qc <TARGET> <LINK_FLAGS> <OBJECTS>")
  204. endif()
  205. if(NOT DEFINED CMAKE_CXX_ARCHIVE_APPEND)
  206. set(CMAKE_CXX_ARCHIVE_APPEND "<CMAKE_AR> q <TARGET> <LINK_FLAGS> <OBJECTS>")
  207. endif()
  208. if(NOT DEFINED CMAKE_CXX_ARCHIVE_FINISH)
  209. set(CMAKE_CXX_ARCHIVE_FINISH "<CMAKE_RANLIB> <TARGET>")
  210. endif()
  211. # compile a C++ file into an object file
  212. if(NOT CMAKE_CXX_COMPILE_OBJECT)
  213. set(CMAKE_CXX_COMPILE_OBJECT
  214. "<CMAKE_CXX_COMPILER> <DEFINES> <INCLUDES> <FLAGS> -o <OBJECT> -c <SOURCE>")
  215. endif()
  216. if(NOT CMAKE_CXX_LINK_EXECUTABLE)
  217. set(CMAKE_CXX_LINK_EXECUTABLE
  218. "<CMAKE_CXX_COMPILER> <FLAGS> <CMAKE_CXX_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>")
  219. endif()
  220. mark_as_advanced(
  221. CMAKE_VERBOSE_MAKEFILE
  222. )
  223. set(CMAKE_CXX_INFORMATION_LOADED 1)