Windows-GNU.cmake 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 module is shared by multiple languages; use include blocker.
  4. if(__WINDOWS_GNU)
  5. return()
  6. endif()
  7. set(__WINDOWS_GNU 1)
  8. set(CMAKE_IMPORT_LIBRARY_PREFIX "lib")
  9. set(CMAKE_SHARED_LIBRARY_PREFIX "lib")
  10. set(CMAKE_SHARED_MODULE_PREFIX "lib")
  11. set(CMAKE_STATIC_LIBRARY_PREFIX "lib")
  12. set(CMAKE_EXECUTABLE_SUFFIX ".exe")
  13. set(CMAKE_IMPORT_LIBRARY_SUFFIX ".dll.a")
  14. set(CMAKE_SHARED_LIBRARY_SUFFIX ".dll")
  15. set(CMAKE_SHARED_MODULE_SUFFIX ".dll")
  16. set(CMAKE_STATIC_LIBRARY_SUFFIX ".a")
  17. if(MSYS OR MINGW)
  18. set(CMAKE_EXTRA_LINK_EXTENSIONS ".lib") # MinGW can also link to a MS .lib
  19. endif()
  20. if(MINGW)
  21. set(CMAKE_FIND_LIBRARY_PREFIXES "lib" "")
  22. set(CMAKE_FIND_LIBRARY_SUFFIXES ".dll" ".dll.a" ".a" ".lib")
  23. set(CMAKE_C_STANDARD_LIBRARIES_INIT "-lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32")
  24. set(CMAKE_CXX_STANDARD_LIBRARIES_INIT "${CMAKE_C_STANDARD_LIBRARIES_INIT}")
  25. endif()
  26. set(CMAKE_DL_LIBS "")
  27. set(CMAKE_LIBRARY_PATH_FLAG "-L")
  28. set(CMAKE_LINK_LIBRARY_FLAG "-l")
  29. set(CMAKE_LINK_DEF_FILE_FLAG "") # Empty string: passing the file is enough
  30. set(CMAKE_LINK_LIBRARY_SUFFIX "")
  31. set(CMAKE_CREATE_WIN32_EXE "-mwindows")
  32. set(CMAKE_GNULD_IMAGE_VERSION
  33. "-Wl,--major-image-version,<TARGET_VERSION_MAJOR>,--minor-image-version,<TARGET_VERSION_MINOR>")
  34. # Check if GNU ld is too old to support @FILE syntax.
  35. set(__WINDOWS_GNU_LD_RESPONSE 1)
  36. execute_process(COMMAND ld -v OUTPUT_VARIABLE _help ERROR_VARIABLE _help)
  37. if("${_help}" MATCHES "GNU ld .* 2\\.1[1-6]")
  38. set(__WINDOWS_GNU_LD_RESPONSE 0)
  39. endif()
  40. if(NOT CMAKE_GENERATOR_RC AND CMAKE_GENERATOR MATCHES "Unix Makefiles")
  41. set(CMAKE_GENERATOR_RC windres)
  42. endif()
  43. macro(__windows_compiler_gnu lang)
  44. if(MSYS OR MINGW)
  45. # Create archiving rules to support large object file lists for static libraries.
  46. set(CMAKE_${lang}_ARCHIVE_CREATE "<CMAKE_AR> qc <TARGET> <LINK_FLAGS> <OBJECTS>")
  47. set(CMAKE_${lang}_ARCHIVE_APPEND "<CMAKE_AR> q <TARGET> <LINK_FLAGS> <OBJECTS>")
  48. set(CMAKE_${lang}_ARCHIVE_FINISH "<CMAKE_RANLIB> <TARGET>")
  49. # Initialize C link type selection flags. These flags are used when
  50. # building a shared library, shared module, or executable that links
  51. # to other libraries to select whether to use the static or shared
  52. # versions of the libraries.
  53. foreach(type SHARED_LIBRARY SHARED_MODULE EXE)
  54. set(CMAKE_${type}_LINK_STATIC_${lang}_FLAGS "-Wl,-Bstatic")
  55. set(CMAKE_${type}_LINK_DYNAMIC_${lang}_FLAGS "-Wl,-Bdynamic")
  56. endforeach()
  57. endif()
  58. # No -fPIC on Windows
  59. set(CMAKE_${lang}_COMPILE_OPTIONS_PIC "")
  60. set(CMAKE_${lang}_COMPILE_OPTIONS_PIE "")
  61. set(_CMAKE_${lang}_PIE_MAY_BE_SUPPORTED_BY_LINKER NO)
  62. set(CMAKE_${lang}_LINK_OPTIONS_PIE "")
  63. set(CMAKE_${lang}_LINK_OPTIONS_NO_PIE "")
  64. set(CMAKE_SHARED_LIBRARY_${lang}_FLAGS "")
  65. set(CMAKE_${lang}_USE_RESPONSE_FILE_FOR_OBJECTS ${__WINDOWS_GNU_LD_RESPONSE})
  66. set(CMAKE_${lang}_USE_RESPONSE_FILE_FOR_LIBRARIES ${__WINDOWS_GNU_LD_RESPONSE})
  67. set(CMAKE_${lang}_USE_RESPONSE_FILE_FOR_INCLUDES 1)
  68. # We prefer "@" for response files but it is not supported by gcc 3.
  69. execute_process(COMMAND ${CMAKE_${lang}_COMPILER} --version OUTPUT_VARIABLE _ver ERROR_VARIABLE _ver)
  70. if("${_ver}" MATCHES "\\(GCC\\) 3\\.")
  71. if("${lang}" STREQUAL "Fortran")
  72. # The GNU Fortran compiler reports an error:
  73. # no input files; unwilling to write output files
  74. # when the response file is passed with "-Wl,@".
  75. set(CMAKE_Fortran_USE_RESPONSE_FILE_FOR_OBJECTS 0)
  76. else()
  77. # Use "-Wl,@" to pass the response file to the linker.
  78. set(CMAKE_${lang}_RESPONSE_FILE_LINK_FLAG "-Wl,@")
  79. endif()
  80. # The GNU 3.x compilers do not support response files (only linkers).
  81. set(CMAKE_${lang}_USE_RESPONSE_FILE_FOR_INCLUDES 0)
  82. # Link libraries are generated only for the front-end.
  83. set(CMAKE_${lang}_USE_RESPONSE_FILE_FOR_LIBRARIES 0)
  84. else()
  85. # Use "@" to pass the response file to the front-end.
  86. set(CMAKE_${lang}_RESPONSE_FILE_LINK_FLAG "@")
  87. endif()
  88. # Binary link rules.
  89. set(CMAKE_${lang}_CREATE_SHARED_MODULE
  90. "<CMAKE_${lang}_COMPILER> <CMAKE_SHARED_MODULE_${lang}_FLAGS> <LANGUAGE_COMPILE_FLAGS> <LINK_FLAGS> <CMAKE_SHARED_MODULE_CREATE_${lang}_FLAGS> -o <TARGET> ${CMAKE_GNULD_IMAGE_VERSION} <OBJECTS> <LINK_LIBRARIES>")
  91. set(CMAKE_${lang}_CREATE_SHARED_LIBRARY
  92. "<CMAKE_${lang}_COMPILER> <CMAKE_SHARED_LIBRARY_${lang}_FLAGS> <LANGUAGE_COMPILE_FLAGS> <LINK_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_${lang}_FLAGS> -o <TARGET> -Wl,--out-implib,<TARGET_IMPLIB> ${CMAKE_GNULD_IMAGE_VERSION} <OBJECTS> <LINK_LIBRARIES>")
  93. set(CMAKE_${lang}_LINK_EXECUTABLE
  94. "<CMAKE_${lang}_COMPILER> <FLAGS> <CMAKE_${lang}_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> -Wl,--out-implib,<TARGET_IMPLIB> ${CMAKE_GNULD_IMAGE_VERSION} <LINK_LIBRARIES>")
  95. list(APPEND CMAKE_${lang}_ABI_FILES "Platform/Windows-GNU-${lang}-ABI")
  96. # Support very long lists of object files.
  97. # TODO: check for which gcc versions this is still needed, not needed for gcc >= 4.4.
  98. # Ninja generator doesn't support this work around.
  99. if("${CMAKE_${lang}_RESPONSE_FILE_LINK_FLAG}" STREQUAL "@" AND NOT CMAKE_GENERATOR MATCHES "Ninja")
  100. foreach(rule CREATE_SHARED_MODULE CREATE_SHARED_LIBRARY LINK_EXECUTABLE)
  101. # The gcc/collect2/ld toolchain does not use response files
  102. # internally so we cannot pass long object lists. Instead pass
  103. # the object file list in a response file to the archiver to put
  104. # them in a temporary archive. Hand the archive to the linker.
  105. string(REPLACE "<OBJECTS>" "-Wl,--whole-archive <OBJECT_DIR>/objects.a -Wl,--no-whole-archive"
  106. CMAKE_${lang}_${rule} "${CMAKE_${lang}_${rule}}")
  107. set(CMAKE_${lang}_${rule}
  108. "<CMAKE_COMMAND> -E remove -f <OBJECT_DIR>/objects.a"
  109. "<CMAKE_AR> cr <OBJECT_DIR>/objects.a <OBJECTS>"
  110. "${CMAKE_${lang}_${rule}}"
  111. )
  112. endforeach()
  113. endif()
  114. if(NOT CMAKE_RC_COMPILER_INIT AND NOT CMAKE_GENERATOR_RC)
  115. set(CMAKE_RC_COMPILER_INIT windres)
  116. endif()
  117. enable_language(RC)
  118. endmacro()
  119. macro(__windows_compiler_gnu_abi lang)
  120. if(CMAKE_NO_GNUtoMS)
  121. set(CMAKE_GNUtoMS 0)
  122. else()
  123. option(CMAKE_GNUtoMS "Convert GNU import libraries to MS format (requires Visual Studio)" OFF)
  124. endif()
  125. if(CMAKE_GNUtoMS AND NOT CMAKE_GNUtoMS_LIB)
  126. # Find MS development environment setup script for this architecture.
  127. # We need to use the MS Librarian tool (lib.exe).
  128. # Find the most recent version available.
  129. # Query the VS Installer tool for locations of VS 2017 and above.
  130. set(_vs_installer_paths "")
  131. foreach(vs RANGE 16 15 -1) # change the first number to the largest supported version
  132. cmake_host_system_information(RESULT _vs_dir QUERY VS_${vs}_DIR)
  133. if(_vs_dir)
  134. list(APPEND _vs_installer_paths "${_vs_dir}/VC/Auxiliary/Build")
  135. endif()
  136. endforeach(vs)
  137. if("${CMAKE_SIZEOF_VOID_P}" EQUAL 4)
  138. find_program(CMAKE_GNUtoMS_VCVARS NAMES vcvars32.bat
  139. DOC "Visual Studio vcvars32.bat"
  140. PATHS
  141. ${_vs_installer_paths}
  142. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\14.0\\Setup\\VC;ProductDir]/bin"
  143. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\12.0\\Setup\\VC;ProductDir]/bin"
  144. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\11.0\\Setup\\VC;ProductDir]/bin"
  145. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\10.0\\Setup\\VC;ProductDir]/bin"
  146. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\9.0\\Setup\\VC;ProductDir]/bin"
  147. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\8.0\\Setup\\VC;ProductDir]/bin"
  148. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\7.1\\Setup\\VC;ProductDir]/bin"
  149. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\6.0\\Setup\\Microsoft Visual C++;ProductDir]/bin"
  150. )
  151. set(CMAKE_GNUtoMS_ARCH x86)
  152. elseif("${CMAKE_SIZEOF_VOID_P}" EQUAL 8)
  153. find_program(CMAKE_GNUtoMS_VCVARS NAMES vcvars64.bat vcvarsamd64.bat
  154. DOC "Visual Studio vcvarsamd64.bat"
  155. PATHS
  156. ${_vs_installer_paths}
  157. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\14.0\\Setup\\VC;ProductDir]/bin/amd64"
  158. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\12.0\\Setup\\VC;ProductDir]/bin/amd64"
  159. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\11.0\\Setup\\VC;ProductDir]/bin/amd64"
  160. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\10.0\\Setup\\VC;ProductDir]/bin/amd64"
  161. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\9.0\\Setup\\VC;ProductDir]/bin/amd64"
  162. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\8.0\\Setup\\VC;ProductDir]/bin/amd64"
  163. )
  164. set(CMAKE_GNUtoMS_ARCH amd64)
  165. endif()
  166. unset(_vs_installer_paths)
  167. set_property(CACHE CMAKE_GNUtoMS_VCVARS PROPERTY ADVANCED 1)
  168. if(CMAKE_GNUtoMS_VCVARS)
  169. # Create helper script to run lib.exe from MS environment.
  170. string(REPLACE "/" "\\" CMAKE_GNUtoMS_BAT "${CMAKE_GNUtoMS_VCVARS}")
  171. set(CMAKE_GNUtoMS_LIB ${CMAKE_BINARY_DIR}/CMakeFiles/CMakeGNUtoMS_lib.bat)
  172. configure_file(${CMAKE_ROOT}/Modules/Platform/GNUtoMS_lib.bat.in ${CMAKE_GNUtoMS_LIB})
  173. else()
  174. message(WARNING "Disabling CMAKE_GNUtoMS option because CMAKE_GNUtoMS_VCVARS is not set.")
  175. set(CMAKE_GNUtoMS 0)
  176. endif()
  177. endif()
  178. if(CMAKE_GNUtoMS)
  179. # Teach CMake how to create a MS import library at link time.
  180. set(CMAKE_${lang}_GNUtoMS_RULE " -Wl,--output-def,<TARGET_NAME>.def"
  181. "<CMAKE_COMMAND> -Dlib=\"${CMAKE_GNUtoMS_LIB}\" -Ddef=<TARGET_NAME>.def -Ddll=<TARGET> -Dimp=<TARGET_IMPLIB> -P \"${CMAKE_ROOT}/Modules/Platform/GNUtoMS_lib.cmake\""
  182. )
  183. endif()
  184. endmacro()