RaspberryPi.cmake 6.3 KB

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