Arm.cmake 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #
  2. # Copyright (c) 2008-2017 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_ARM_SYSROOT ${ARM_SYSROOT} CACHE INTERNAL "Initial value for ARM_SYSROOT")
  32. set (SAVED_ARM_PREFIX ${ARM_PREFIX} CACHE INTERNAL "Initial value for ARM_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_ARM_SYSROOT AND NOT SAVED_ARM_SYSROOT STREQUAL ARM_SYSROOT) OR (SAVED_ARM_PREFIX AND NOT SAVED_ARM_PREFIX STREQUAL ARM_PREFIX))
  38. set (ARM_SYSROOT ${SAVED_ARM_SYSROOT} CACHE PATH "Path to ARM system root (ARM on Linux cross-compiling build only)" FORCE)
  39. set (ARM_PREFIX ${SAVED_ARM_PREFIX} CACHE STRING "Prefix path to ARM cross-compiler tools (ARM on Linux cross-compiling build only)" FORCE)
  40. message (FATAL_ERROR "ARM_SYSROOT and ARM_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 Linux)
  50. # This one not so much
  51. set (CMAKE_SYSTEM_PROCESSOR arm)
  52. set (CMAKE_SYSTEM_VERSION 1)
  53. # System root
  54. if (NOT IN_TRY_COMPILE AND NOT SAVED_ARM_SYSROOT)
  55. if (NOT ARM_SYSROOT AND DEFINED ENV{ARM_SYSROOT})
  56. file (TO_CMAKE_PATH $ENV{ARM_SYSROOT} ARM_SYSROOT)
  57. endif ()
  58. set (ARM_SYSROOT ${ARM_SYSROOT} CACHE PATH "Path to ARM system root (ARM on Linux cross-compiling build only)")
  59. if (NOT EXISTS ${ARM_SYSROOT})
  60. message (FATAL_ERROR "Could not find ARM system root. "
  61. "Use ARM_SYSROOT environment variable or build option to specify the location of system root.")
  62. endif ()
  63. endif ()
  64. set (CMAKE_SYSROOT ${ARM_SYSROOT})
  65. # Only search libraries and headers in sysroot
  66. set (CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
  67. set (CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
  68. set (CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
  69. # Cross compiler tools
  70. if (NOT IN_TRY_COMPILE AND NOT SAVED_ARM_PREFIX)
  71. if (NOT ARM_PREFIX AND DEFINED ENV{ARM_PREFIX})
  72. file (TO_CMAKE_PATH $ENV{ARM_PREFIX} ARM_PREFIX)
  73. endif ()
  74. set (ARM_PREFIX ${ARM_PREFIX} CACHE STRING "Prefix path to ARM cross-compiler tools (ARM on Linux cross-compiling build only)")
  75. if (NOT EXISTS ${ARM_PREFIX}-gcc)
  76. message (FATAL_ERROR "Could not find ARM cross compilation tool. "
  77. "Use ARM_PREFIX environment variable or build option to specify the location of the toolchain.")
  78. endif ()
  79. endif ()
  80. if (NOT ARM_COMPILER_PREFIX)
  81. set (ARM_COMPILER_PREFIX ${ARM_PREFIX})
  82. if ("$ENV{USE_CCACHE}")
  83. get_filename_component (PATH ${ARM_PREFIX} PATH)
  84. get_filename_component (NAME ${ARM_PREFIX} NAME)
  85. execute_process (COMMAND whereis -b ccache COMMAND grep -o \\S*lib\\S* OUTPUT_VARIABLE CCACHE_SYMLINK ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
  86. if (CCACHE_SYMLINK AND EXISTS ${CCACHE_SYMLINK}/${NAME}-gcc AND EXISTS ${CCACHE_SYMLINK}/${NAME}-g++)
  87. set (ARM_COMPILER_PREFIX ${CCACHE_SYMLINK}/${NAME})
  88. else ()
  89. # Fallback to create the ccache symlink in the build tree itself
  90. execute_process (COMMAND which ccache RESULT_VARIABLE EXIT_CODE OUTPUT_VARIABLE CCACHE ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
  91. if (EXIT_CODE EQUAL 0 AND CCACHE)
  92. foreach (TOOL gcc g++)
  93. execute_process (COMMAND ${CMAKE_COMMAND} -E create_symlink ${CCACHE} ${CMAKE_BINARY_DIR}/${NAME}-${TOOL})
  94. endforeach ()
  95. set (ARM_COMPILER_PREFIX ${CMAKE_BINARY_DIR}/${NAME})
  96. else ()
  97. message (WARNING "ccache may not have been installed on this host system. "
  98. "This is required to enable ccache support for ARM compiler toolchain. "
  99. "CMake has been configured to use the actual compiler toolchain instead of ccache. "
  100. "In order to rectify this, the build tree must be regenerated after installing ccache.")
  101. endif ()
  102. endif ()
  103. if (NOT ARM_COMPILER_PREFIX STREQUAL ARM_PREFIX AND NOT $ENV{PATH} MATCHES ${PATH})
  104. message (FATAL_ERROR "The bin directory containing the compiler toolchain (${PATH}) has not been added in the PATH environment variable. "
  105. "This is required to enable ccache support for ARM compiler toolchain.")
  106. endif ()
  107. endif ()
  108. set (ARM_COMPILER_PREFIX ${ARM_COMPILER_PREFIX} CACHE INTERNAL "Path to C/C++ compiler tool symlinks or to the actual tools if not using ccache")
  109. endif ()
  110. set (CMAKE_C_COMPILER ${ARM_COMPILER_PREFIX}-gcc CACHE PATH "C compiler")
  111. set (CMAKE_CXX_COMPILER ${ARM_COMPILER_PREFIX}-g++ CACHE PATH "C++ compiler")
  112. set (CMAKE_STRIP ${ARM_PREFIX}-strip CACHE PATH "strip")
  113. set (CMAKE_AR ${ARM_PREFIX}-ar CACHE PATH "archive")
  114. set (CMAKE_LINKER ${ARM_PREFIX}-ld CACHE PATH "linker")
  115. set (CMAKE_NM ${ARM_PREFIX}-nm CACHE PATH "nm")
  116. set (CMAKE_OBJCOPY ${ARM_PREFIX}-objcopy CACHE PATH "objcopy")
  117. set (CMAKE_OBJDUMP ${ARM_PREFIX}-objdump CACHE PATH "objdump")
  118. set (CMAKE_RANLIB ${ARM_PREFIX}-ranlib CACHE PATH "ranlib")
  119. # Workaround try_compile() limitation where it cannot yet see cache variables during initial configuration
  120. if (NOT IN_TRY_COMPILE)
  121. get_cmake_property (CACHE_VARIABLES CACHE_VARIABLES)
  122. foreach (VAR ${CACHE_VARIABLES})
  123. if (VAR MATCHES ^ARM_|CMAKE_CX*_COMPILER)
  124. set (ENV{${VAR}} ${${VAR}})
  125. list (APPEND VARS ${VAR})
  126. endif ()
  127. endforeach ()
  128. set (ENV{VARS} "${VARS}") # Stringify to keep the list together
  129. endif ()
  130. set (ARM 1)