MinGW.cmake 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #
  2. # Copyright (c) 2008-2020 the Urho3D project.
  3. #
  4. # Permission is hereby granted, free of charge, to any person obtaining a copy
  5. # of this software and associated documentation files (the "Software"), to deal
  6. # in the Software without restriction, including without limitation the rights
  7. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. # copies of the Software, and to permit persons to whom the Software is
  9. # furnished to do so, subject to the following conditions:
  10. #
  11. # The above copyright notice and this permission notice shall be included in
  12. # all copies or substantial portions of the Software.
  13. #
  14. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. # THE SOFTWARE.
  21. #
  22. # Workaround try_compile() limitation where it cannot yet see cache variables during initial configuration
  23. get_property (IN_TRY_COMPILE GLOBAL PROPERTY IN_TRY_COMPILE)
  24. if (IN_TRY_COMPILE)
  25. foreach (VAR $ENV{VARS})
  26. set (${VAR} $ENV{${VAR}})
  27. endforeach ()
  28. else ()
  29. # Prevent critical variables from changing after the initial configuration
  30. if (CMAKE_CROSSCOMPILING)
  31. set (SAVED_MINGW_SYSROOT ${MINGW_SYSROOT} CACHE INTERNAL "Initial value for MINGW_SYSROOT")
  32. set (SAVED_MINGW_PREFIX ${MINGW_PREFIX} CACHE INTERNAL "Initial value for MINGW_PREFIX")
  33. # Save the initial values of CC and CXX environment variables
  34. set (SAVED_CC $ENV{CC} CACHE INTERNAL "Initial value for CC")
  35. set (SAVED_CXX $ENV{CXX} CACHE INTERNAL "Initial value for CXX")
  36. return ()
  37. elseif ((SAVED_MINGW_SYSROOT AND NOT SAVED_MINGW_SYSROOT STREQUAL MINGW_SYSROOT) OR (SAVED_MINGW_PREFIX AND NOT SAVED_MINGW_PREFIX STREQUAL MINGW_PREFIX))
  38. set (MINGW_SYSROOT ${SAVED_MINGW_SYSROOT} CACHE PATH "Path to MinGW system root (MinGW only); should only be used when the system root could not be auto-detected" FORCE)
  39. set (MINGW_PREFIX ${SAVED_MINGW_PREFIX} CACHE STRING "Prefix path to MinGW cross-compiler tools (MinGW cross-compiling build only)" FORCE)
  40. message (FATAL_ERROR "MINGW_SYSROOT and MINGW_PREFIX cannot be changed after the initial configuration/generation. "
  41. "If you wish to change that then the build tree would have to be regenerated from scratch. Auto reverting to its initial value.")
  42. endif ()
  43. endif ()
  44. # Reference toolchain variable to suppress "unused variable" warning
  45. if (CMAKE_TOOLCHAIN_FILE)
  46. mark_as_advanced (CMAKE_TOOLCHAIN_FILE)
  47. endif ()
  48. # This one is important
  49. set (CMAKE_SYSTEM_NAME Windows)
  50. # This one not so much
  51. set (CMAKE_SYSTEM_PROCESSOR x86)
  52. set (CMAKE_SYSTEM_VERSION 1)
  53. # Cross compiler tools
  54. if (NOT IN_TRY_COMPILE AND NOT SAVED_MINGW_PREFIX)
  55. if (NOT MINGW_PREFIX AND DEFINED ENV{MINGW_PREFIX})
  56. file (TO_CMAKE_PATH $ENV{MINGW_PREFIX} MINGW_PREFIX)
  57. endif ()
  58. set (MINGW_PREFIX ${MINGW_PREFIX} CACHE STRING "Prefix path to MinGW cross-compiler tools (MinGW cross-compiling build only)")
  59. if (NOT EXISTS ${MINGW_PREFIX}-gcc)
  60. message (FATAL_ERROR "Could not find MinGW cross compilation tool. "
  61. "Use MINGW_PREFIX environment variable or build option to specify the location of the toolchain.")
  62. endif ()
  63. endif ()
  64. if (NOT MINGW_COMPILER_PREFIX)
  65. set (MINGW_COMPILER_PREFIX ${MINGW_PREFIX})
  66. if ("$ENV{USE_CCACHE}")
  67. get_filename_component (PATH ${MINGW_PREFIX} PATH)
  68. get_filename_component (NAME ${MINGW_PREFIX} NAME)
  69. execute_process (COMMAND whereis -b ccache COMMAND grep -o \\S*lib\\S* OUTPUT_VARIABLE CCACHE_SYMLINK ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
  70. if (CCACHE_SYMLINK AND EXISTS ${CCACHE_SYMLINK}/${NAME}-gcc AND EXISTS ${CCACHE_SYMLINK}/${NAME}-g++)
  71. set (MINGW_COMPILER_PREFIX ${CCACHE_SYMLINK}/${NAME})
  72. else ()
  73. # Fallback to create the ccache symlink in the build tree itself
  74. execute_process (COMMAND which ccache RESULT_VARIABLE EXIT_CODE OUTPUT_VARIABLE CCACHE ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
  75. if (EXIT_CODE EQUAL 0 AND CCACHE)
  76. foreach (TOOL gcc g++)
  77. execute_process (COMMAND ${CMAKE_COMMAND} -E create_symlink ${CCACHE} ${CMAKE_BINARY_DIR}/${NAME}-${TOOL})
  78. endforeach ()
  79. set (MINGW_COMPILER_PREFIX ${CMAKE_BINARY_DIR}/${NAME})
  80. else ()
  81. message (WARNING "ccache may not have been installed on this host system. "
  82. "This is required to enable ccache support for MinGW compiler toolchain. "
  83. "CMake has been configured to use the actual compiler toolchain instead of ccache. "
  84. "In order to rectify this, the build tree must be regenerated after installing ccache.")
  85. endif ()
  86. endif ()
  87. if (NOT MINGW_COMPILER_PREFIX STREQUAL MINGW_PREFIX AND NOT $ENV{PATH} MATCHES ${PATH})
  88. message (FATAL_ERROR "The bin directory containing the compiler toolchain (${PATH}) has not been added in the PATH environment variable. "
  89. "This is required to enable ccache support for MinGW compiler toolchain.")
  90. endif ()
  91. endif ()
  92. set (MINGW_COMPILER_PREFIX ${MINGW_COMPILER_PREFIX} CACHE INTERNAL "Path to C/C++ compiler tool symlinks or to the actual tools if not using ccache")
  93. endif ()
  94. set (CMAKE_C_COMPILER ${MINGW_COMPILER_PREFIX}-gcc CACHE PATH "C compiler")
  95. set (CMAKE_CXX_COMPILER ${MINGW_COMPILER_PREFIX}-g++ CACHE PATH "C++ compiler")
  96. set (CMAKE_STRIP ${MINGW_PREFIX}-strip CACHE PATH "strip")
  97. set (CMAKE_AR ${MINGW_PREFIX}-ar CACHE PATH "archive")
  98. set (CMAKE_LINKER ${MINGW_PREFIX}-ld CACHE PATH "linker")
  99. set (CMAKE_NM ${MINGW_PREFIX}-nm CACHE PATH "nm")
  100. set (CMAKE_OBJCOPY ${MINGW_PREFIX}-objcopy CACHE PATH "objcopy")
  101. set (CMAKE_OBJDUMP ${MINGW_PREFIX}-objdump CACHE PATH "objdump")
  102. set (CMAKE_RANLIB ${MINGW_PREFIX}-ranlib CACHE PATH "ranlib")
  103. set (CMAKE_RC_COMPILER ${MINGW_PREFIX}-windres CACHE PATH "RC compiler")
  104. # System root
  105. if (NOT IN_TRY_COMPILE AND NOT SAVED_MINGW_SYSROOT)
  106. if (NOT MINGW_SYSROOT)
  107. if (DEFINED ENV{MINGW_SYSROOT})
  108. file (TO_CMAKE_PATH $ENV{MINGW_SYSROOT} MINGW_SYSROOT)
  109. else ()
  110. execute_process (COMMAND ${CMAKE_COMMAND} -E echo "#include <_mingw.h>" COMMAND ${CMAKE_C_COMPILER} -E -M - OUTPUT_FILE find_mingw_sysroot_output ERROR_QUIET)
  111. file (STRINGS ${CMAKE_BINARY_DIR}/find_mingw_sysroot_output MINGW_SYSROOT REGEX _mingw\\.h)
  112. string (REGEX REPLACE "^[^ ]* *(.*)/include.*$" \\1 MINGW_SYSROOT "${MINGW_SYSROOT}") # Stringify for string replacement
  113. string (REPLACE "\\ " " " MINGW_SYSROOT "${MINGW_SYSROOT}")
  114. execute_process (COMMAND ${CMAKE_COMMAND} -E remove find_mingw_sysroot_output)
  115. endif ()
  116. endif ()
  117. set (MINGW_SYSROOT ${MINGW_SYSROOT} CACHE PATH "Path to MinGW system root (MinGW only); should only be used when the system root could not be auto-detected")
  118. if (NOT EXISTS ${MINGW_SYSROOT})
  119. message (FATAL_ERROR "Could not find MinGW system root. "
  120. "Use MINGW_SYSROOT environment variable or build option to specify the location of system root.")
  121. endif ()
  122. endif ()
  123. set (CMAKE_FIND_ROOT_PATH ${MINGW_SYSROOT}) # Intentionally do not use CMAKE_SYSROOT because it does not work for MinGW compiler toolchain
  124. # Only search libraries and headers in sysroot
  125. set (CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
  126. set (CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
  127. set (CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
  128. # Workaround try_compile() limitation where it cannot yet see cache variables during initial configuration
  129. if (NOT IN_TRY_COMPILE)
  130. get_cmake_property (CACHE_VARIABLES CACHE_VARIABLES)
  131. foreach (VAR ${CACHE_VARIABLES})
  132. if (VAR MATCHES ^MINGW_|CMAKE_CX*_COMPILER)
  133. set (ENV{${VAR}} ${${VAR}})
  134. list (APPEND VARS ${VAR})
  135. endif ()
  136. endforeach ()
  137. set (ENV{VARS} "${VARS}") # Stringify to keep the list together
  138. endif ()