mingw.toolchain.cmake 5.4 KB

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