raspberrypi.toolchain.cmake 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 Linux)
  29. # this one not so much
  30. set (CMAKE_SYSTEM_VERSION 1)
  31. # specify the cross compiler
  32. if (NOT RPI_PREFIX AND DEFINED ENV{RPI_PREFIX})
  33. file (TO_CMAKE_PATH $ENV{RPI_PREFIX} RPI_PREFIX)
  34. endif ()
  35. if (NOT EXISTS ${RPI_PREFIX}-gcc)
  36. message (FATAL_ERROR "Could not find Raspberry Pi cross compilation tool. "
  37. "Use RPI_PREFIX environment variable or build option to specify the location of the toolchain.")
  38. endif ()
  39. set (COMPILER_PREFIX ${RPI_PREFIX})
  40. if (NOT CMAKE_C_COMPILER AND "$ENV{USE_CCACHE}")
  41. get_filename_component (NAME ${RPI_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 ${RPI_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 Raspberry-Pi 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 Raspberry-Pi 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 ${RPI_PREFIX}-strip CACHE PATH "strip")
  68. set (CMAKE_AR ${RPI_PREFIX}-ar CACHE PATH "archive")
  69. set (CMAKE_LINKER ${RPI_PREFIX}-ld CACHE PATH "linker")
  70. set (CMAKE_NM ${RPI_PREFIX}-nm CACHE PATH "nm")
  71. set (CMAKE_OBJCOPY ${RPI_PREFIX}-objcopy CACHE PATH "objcopy")
  72. set (CMAKE_OBJDUMP ${RPI_PREFIX}-objdump CACHE PATH "objdump")
  73. set (CMAKE_RANLIB ${RPI_PREFIX}-ranlib CACHE PATH "ranlib")
  74. # specify the system root
  75. if (NOT RPI_SYSROOT OR NOT BCM_VC_INCLUDE_DIRS OR NOT BCM_VC_LIBRARIES)
  76. if (DEFINED ENV{RPI_SYSROOT})
  77. file (TO_CMAKE_PATH $ENV{RPI_SYSROOT} RPI_SYSROOT)
  78. endif ()
  79. if (NOT EXISTS ${RPI_SYSROOT})
  80. message (FATAL_ERROR "Could not find Raspberry Pi system root. "
  81. "Use RPI_SYSROOT environment variable or build option to specify the location of system root.")
  82. endif ()
  83. set (RPI_PREFIX ${RPI_PREFIX} CACHE STRING "Prefix path to Raspberry Pi cross-compiler tools (RPI cross-compiling build only)" FORCE)
  84. set (RPI_SYSROOT ${RPI_SYSROOT} CACHE PATH "Path to Raspberry Pi system root (RPI cross-compiling build only)" FORCE)
  85. endif ()
  86. set (CMAKE_FIND_ROOT_PATH ${RPI_SYSROOT})
  87. # only search libraries, and headers in the target directories
  88. set (CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
  89. set (CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
  90. set (CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)