MinGW.cmake 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. # Copyright (c) 2008-2022 the Urho3D project
  2. # License: MIT
  3. # https://cmake.org/cmake/help/book/mastering-cmake/chapter/Cross%20Compiling%20With%20CMake.html
  4. # Workaround try_compile() limitation where it cannot yet see cache variables during initial configuration
  5. get_property (IN_TRY_COMPILE GLOBAL PROPERTY IN_TRY_COMPILE)
  6. if (IN_TRY_COMPILE)
  7. foreach (VAR $ENV{VARS})
  8. set (${VAR} $ENV{${VAR}})
  9. endforeach ()
  10. else ()
  11. # Prevent critical variables from changing after the initial configuration
  12. if (CMAKE_CROSSCOMPILING)
  13. set (SAVED_MINGW_SYSROOT ${MINGW_SYSROOT} CACHE INTERNAL "Initial value for MINGW_SYSROOT")
  14. set (SAVED_MINGW_PREFIX ${MINGW_PREFIX} CACHE INTERNAL "Initial value for MINGW_PREFIX")
  15. # Save the initial values of CC and CXX environment variables
  16. set (SAVED_CC $ENV{CC} CACHE INTERNAL "Initial value for CC")
  17. set (SAVED_CXX $ENV{CXX} CACHE INTERNAL "Initial value for CXX")
  18. return ()
  19. elseif ((SAVED_MINGW_SYSROOT AND NOT SAVED_MINGW_SYSROOT STREQUAL MINGW_SYSROOT) OR (SAVED_MINGW_PREFIX AND NOT SAVED_MINGW_PREFIX STREQUAL MINGW_PREFIX))
  20. 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)
  21. set (MINGW_PREFIX ${SAVED_MINGW_PREFIX} CACHE STRING "Prefix path to MinGW cross-compiler tools (MinGW cross-compiling build only)" FORCE)
  22. message (FATAL_ERROR "MINGW_SYSROOT and MINGW_PREFIX cannot be changed after the initial configuration/generation. "
  23. "If you wish to change that then the build tree would have to be regenerated from scratch. Auto reverting to its initial value.")
  24. endif ()
  25. endif ()
  26. # Reference toolchain variable to suppress "unused variable" warning
  27. if (CMAKE_TOOLCHAIN_FILE)
  28. mark_as_advanced (CMAKE_TOOLCHAIN_FILE)
  29. endif ()
  30. # This one is important
  31. set (CMAKE_SYSTEM_NAME Windows)
  32. # This one not so much
  33. set (CMAKE_SYSTEM_PROCESSOR x86)
  34. set (CMAKE_SYSTEM_VERSION 1)
  35. # Cross compiler tools
  36. if (NOT IN_TRY_COMPILE AND NOT SAVED_MINGW_PREFIX)
  37. if (NOT MINGW_PREFIX AND DEFINED ENV{MINGW_PREFIX})
  38. file (TO_CMAKE_PATH $ENV{MINGW_PREFIX} MINGW_PREFIX)
  39. endif ()
  40. set (MINGW_PREFIX ${MINGW_PREFIX} CACHE STRING "Prefix path to MinGW cross-compiler tools (MinGW cross-compiling build only)")
  41. if (NOT EXISTS ${MINGW_PREFIX}-gcc)
  42. message (FATAL_ERROR "Could not find MinGW cross compilation tool. "
  43. "Use MINGW_PREFIX environment variable or build option to specify the location of the toolchain.")
  44. endif ()
  45. endif ()
  46. if (NOT MINGW_COMPILER_PREFIX)
  47. set (MINGW_COMPILER_PREFIX ${MINGW_PREFIX})
  48. if ("$ENV{USE_CCACHE}")
  49. get_filename_component (PATH ${MINGW_PREFIX} PATH)
  50. get_filename_component (NAME ${MINGW_PREFIX} NAME)
  51. execute_process (COMMAND whereis -b ccache COMMAND grep -o \\S*lib\\S* OUTPUT_VARIABLE CCACHE_SYMLINK ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
  52. if (CCACHE_SYMLINK AND EXISTS ${CCACHE_SYMLINK}/${NAME}-gcc AND EXISTS ${CCACHE_SYMLINK}/${NAME}-g++)
  53. set (MINGW_COMPILER_PREFIX ${CCACHE_SYMLINK}/${NAME})
  54. else ()
  55. # Fallback to create the ccache symlink in the build tree itself
  56. execute_process (COMMAND which ccache RESULT_VARIABLE EXIT_CODE OUTPUT_VARIABLE CCACHE ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
  57. if (EXIT_CODE EQUAL 0 AND CCACHE)
  58. foreach (TOOL gcc g++)
  59. execute_process (COMMAND ${CMAKE_COMMAND} -E create_symlink ${CCACHE} ${CMAKE_BINARY_DIR}/${NAME}-${TOOL})
  60. endforeach ()
  61. set (MINGW_COMPILER_PREFIX ${CMAKE_BINARY_DIR}/${NAME})
  62. else ()
  63. message (WARNING "ccache may not have been installed on this host system. "
  64. "This is required to enable ccache support for MinGW compiler toolchain. "
  65. "CMake has been configured to use the actual compiler toolchain instead of ccache. "
  66. "In order to rectify this, the build tree must be regenerated after installing ccache.")
  67. endif ()
  68. endif ()
  69. if (NOT MINGW_COMPILER_PREFIX STREQUAL MINGW_PREFIX AND NOT $ENV{PATH} MATCHES ${PATH})
  70. message (FATAL_ERROR "The bin directory containing the compiler toolchain (${PATH}) has not been added in the PATH environment variable. "
  71. "This is required to enable ccache support for MinGW compiler toolchain.")
  72. endif ()
  73. endif ()
  74. 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")
  75. endif ()
  76. set (CMAKE_C_COMPILER ${MINGW_COMPILER_PREFIX}-gcc CACHE PATH "C compiler")
  77. set (CMAKE_CXX_COMPILER ${MINGW_COMPILER_PREFIX}-g++ CACHE PATH "C++ compiler")
  78. set (CMAKE_STRIP ${MINGW_PREFIX}-strip CACHE PATH "strip")
  79. set (CMAKE_AR ${MINGW_PREFIX}-ar CACHE PATH "archive")
  80. set (CMAKE_LINKER ${MINGW_PREFIX}-ld CACHE PATH "linker")
  81. set (CMAKE_NM ${MINGW_PREFIX}-nm CACHE PATH "nm")
  82. set (CMAKE_OBJCOPY ${MINGW_PREFIX}-objcopy CACHE PATH "objcopy")
  83. set (CMAKE_OBJDUMP ${MINGW_PREFIX}-objdump CACHE PATH "objdump")
  84. set (CMAKE_RANLIB ${MINGW_PREFIX}-ranlib CACHE PATH "ranlib")
  85. set (CMAKE_RC_COMPILER ${MINGW_PREFIX}-windres CACHE PATH "RC compiler")
  86. # System root
  87. if (NOT IN_TRY_COMPILE AND NOT SAVED_MINGW_SYSROOT)
  88. if (NOT MINGW_SYSROOT)
  89. if (DEFINED ENV{MINGW_SYSROOT})
  90. file (TO_CMAKE_PATH $ENV{MINGW_SYSROOT} MINGW_SYSROOT)
  91. else ()
  92. execute_process (COMMAND ${CMAKE_COMMAND} -E echo "#include <_mingw.h>" COMMAND ${CMAKE_C_COMPILER} -E -M - OUTPUT_FILE ${CMAKE_BINARY_DIR}/find_mingw_sysroot_output ERROR_QUIET)
  93. file (STRINGS ${CMAKE_BINARY_DIR}/find_mingw_sysroot_output MINGW_SYSROOT REGEX _mingw\\.h)
  94. string (REGEX REPLACE "^[^ ]* *(.*)/include.*$" \\1 MINGW_SYSROOT "${MINGW_SYSROOT}") # Stringify for string replacement
  95. string (REPLACE "\\ " " " MINGW_SYSROOT "${MINGW_SYSROOT}")
  96. execute_process (COMMAND ${CMAKE_COMMAND} -E remove find_mingw_sysroot_output)
  97. endif ()
  98. endif ()
  99. 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")
  100. if (NOT EXISTS ${MINGW_SYSROOT})
  101. message (FATAL_ERROR "Could not find MinGW system root. "
  102. "Use MINGW_SYSROOT environment variable or build option to specify the location of system root.")
  103. endif ()
  104. endif ()
  105. set (CMAKE_FIND_ROOT_PATH ${MINGW_SYSROOT}) # Intentionally do not use CMAKE_SYSROOT because it does not work for MinGW compiler toolchain
  106. # Only search libraries and headers in sysroot
  107. set (CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
  108. set (CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
  109. set (CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
  110. # Workaround try_compile() limitation where it cannot yet see cache variables during initial configuration
  111. if (NOT IN_TRY_COMPILE)
  112. get_cmake_property (CACHE_VARIABLES CACHE_VARIABLES)
  113. foreach (VAR ${CACHE_VARIABLES})
  114. if (VAR MATCHES ^MINGW_|CMAKE_CX*_COMPILER)
  115. set (ENV{${VAR}} ${${VAR}})
  116. list (APPEND VARS ${VAR})
  117. endif ()
  118. endforeach ()
  119. set (ENV{VARS} "${VARS}") # Stringify to keep the list together
  120. endif ()