mingw.toolchain.cmake 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #
  2. # Copyright (c) 2008-2016 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. if (DEFINED CMAKE_CROSSCOMPILING)
  23. return ()
  24. endif ()
  25. if (CMAKE_TOOLCHAIN_FILE)
  26. # Reference toolchain variable to suppress "unused variable" warning
  27. mark_as_advanced (CMAKE_TOOLCHAIN_FILE)
  28. endif ()
  29. # this one is important
  30. set (CMAKE_SYSTEM_NAME Windows)
  31. # this one not so much
  32. set (CMAKE_SYSTEM_PROCESSOR x86)
  33. # specify the cross compiler
  34. if (NOT MINGW_PREFIX AND DEFINED ENV{MINGW_PREFIX})
  35. file (TO_CMAKE_PATH $ENV{MINGW_PREFIX} MINGW_PREFIX)
  36. endif ()
  37. if (NOT EXISTS ${MINGW_PREFIX}-gcc)
  38. message (FATAL_ERROR "Could not find MinGW cross compilation tool. "
  39. "Use MINGW_PREFIX environment variable or build option to specify the location of the toolchain.")
  40. endif ()
  41. set (COMPILER_PREFIX ${MINGW_PREFIX})
  42. if (NOT CMAKE_C_COMPILER AND "$ENV{USE_CCACHE}")
  43. get_filename_component (NAME ${MINGW_PREFIX} NAME)
  44. execute_process (COMMAND whereis -b ccache COMMAND grep -o \\S*lib\\S* RESULT_VARIABLE EXIT_CODE OUTPUT_VARIABLE CCACHE_SYMLINK ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
  45. if (EXIT_CODE EQUAL 0 AND EXISTS ${CCACHE_SYMLINK}/${NAME}-gcc AND EXISTS ${CCACHE_SYMLINK}/${NAME}-g++)
  46. set (COMPILER_PREFIX ${CCACHE_SYMLINK}/${NAME})
  47. else ()
  48. # Most probably this is a custom compiler toolchain not provided by the distro's own repository
  49. get_filename_component (PATH ${MINGW_PREFIX} PATH)
  50. if (NOT $ENV{PATH} MATCHES ${PATH})
  51. message (FATAL_ERROR "The bin directory containing the compiler toolchain (${PATH}) has not been added in the PATH environment variable. "
  52. "This is required to enable ccache support for MinGW compiler toolchain.")
  53. endif ()
  54. execute_process (COMMAND which ccache RESULT_VARIABLE EXIT_CODE OUTPUT_VARIABLE CCACHE ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
  55. if (EXIT_CODE EQUAL 0)
  56. foreach (suffix gcc g++)
  57. execute_process (COMMAND ${CMAKE_COMMAND} -E create_symlink ${CCACHE} ${CMAKE_BINARY_DIR}/${NAME}-${suffix})
  58. endforeach ()
  59. set (COMPILER_PREFIX ${CMAKE_BINARY_DIR}/${NAME})
  60. else ()
  61. message (WARNING "ccache may not have been installed on this host system. "
  62. "This is required to enable ccache support for MinGW compiler toolchain. CMake has been configured to use the actual compiler toolchain instead of ccache. "
  63. "In order to rectify this, the build tree must be regenerated after installing ccache.")
  64. endif ()
  65. endif ()
  66. endif ()
  67. set (CMAKE_C_COMPILER ${COMPILER_PREFIX}-gcc CACHE PATH "C compiler")
  68. set (CMAKE_CXX_COMPILER ${COMPILER_PREFIX}-g++ CACHE PATH "C++ compiler")
  69. set (CMAKE_STRIP ${MINGW_PREFIX}-strip CACHE PATH "strip")
  70. set (CMAKE_AR ${MINGW_PREFIX}-ar CACHE PATH "archive")
  71. set (CMAKE_LINKER ${MINGW_PREFIX}-ld CACHE PATH "linker")
  72. set (CMAKE_NM ${MINGW_PREFIX}-nm CACHE PATH "nm")
  73. set (CMAKE_OBJCOPY ${MINGW_PREFIX}-objcopy CACHE PATH "objcopy")
  74. set (CMAKE_OBJDUMP ${MINGW_PREFIX}-objdump CACHE PATH "objdump")
  75. set (CMAKE_RANLIB ${MINGW_PREFIX}-ranlib CACHE PATH "ranlib")
  76. set (CMAKE_RC_COMPILER ${MINGW_PREFIX}-windres CACHE PATH "RC compiler")
  77. # specify the system root
  78. if (NOT MINGW_SYSROOT)
  79. if (DEFINED ENV{MINGW_SYSROOT})
  80. file (TO_CMAKE_PATH $ENV{MINGW_SYSROOT} MINGW_SYSROOT)
  81. else ()
  82. execute_process (COMMAND ${CMAKE_COMMAND} -E echo "#include <_mingw.h>" COMMAND ${CMAKE_C_COMPILER} -E -M - OUTPUT_FILE find_mingw_sysroot_output ERROR_QUIET)
  83. file (STRINGS ${CMAKE_BINARY_DIR}/find_mingw_sysroot_output MINGW_SYSROOT REGEX _mingw\\.h)
  84. string (REGEX REPLACE "^[^ ]* *(.*)/include.*$" \\1 MINGW_SYSROOT "${MINGW_SYSROOT}") # Stringify for string replacement
  85. string (REPLACE "\\ " " " MINGW_SYSROOT "${MINGW_SYSROOT}")
  86. execute_process (COMMAND ${CMAKE_COMMAND} -E remove find_mingw_sysroot_output)
  87. endif ()
  88. if (NOT EXISTS ${MINGW_SYSROOT})
  89. message (FATAL_ERROR "Could not find MinGW system root. "
  90. "Use MINGW_SYSROOT environment variable or build option to specify the location of system root.")
  91. endif ()
  92. set (MINGW_PREFIX ${MINGW_PREFIX} CACHE STRING "Prefix path to MinGW cross-compiler tools (MinGW cross-compiling build only)" FORCE)
  93. set (MINGW_SYSROOT ${MINGW_SYSROOT} CACHE PATH "Path to MinGW system root (MinGW build only); should only be used when the system root could not be auto-detected" FORCE)
  94. endif ()
  95. set (CMAKE_FIND_ROOT_PATH ${MINGW_SYSROOT})
  96. # only search libraries and headers in the target directories
  97. set (CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
  98. set (CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
  99. set (CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)