CompilerSettings_linux.cmake 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #
  2. # Copyright (c) Contributors to the Open 3D Engine Project.
  3. # For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. #
  5. # SPDX-License-Identifier: Apache-2.0 OR MIT
  6. #
  7. #
  8. if(NOT CMAKE_C_COMPILER AND NOT CMAKE_CXX_COMPILER AND NOT "$ENV{CC}" AND NOT "$ENV{CXX}")
  9. set(path_search
  10. /bin
  11. /usr/bin
  12. /usr/local/bin
  13. /sbin
  14. /usr/sbin
  15. /usr/local/sbin
  16. )
  17. list(TRANSFORM path_search APPEND "/clang-[0-9]*" OUTPUT_VARIABLE path_with_version_search)
  18. file(GLOB clang_versions ${path_with_version_search})
  19. unset(compiler_found)
  20. # First search for clang with a version value
  21. if(clang_versions)
  22. # Find and pick the highest installed version
  23. list(SORT clang_versions COMPARE NATURAL ORDER DESCENDING)
  24. list(GET clang_versions 0 clang_higher_version_path)
  25. string(REGEX MATCH "(.*clang)-([0-9.]*)" clang_higher_version ${clang_higher_version_path})
  26. if(CMAKE_MATCH_2 AND EXISTS "${clang_higher_version_path}")
  27. set(clang_path_prefix ${CMAKE_MATCH_1})
  28. set(clang_version_suffix ${CMAKE_MATCH_2})
  29. if(EXISTS "${clang_path_prefix}++-${clang_version_suffix}")
  30. set(CMAKE_C_COMPILER ${clang_higher_version_path})
  31. set(CMAKE_CXX_COMPILER ${clang_path_prefix}++-${clang_version_suffix})
  32. set(compiler_found TRUE)
  33. elseif(EXISTS "${clang_path_prefix}++")
  34. set(CMAKE_C_COMPILER ${clang_higher_version_path})
  35. set(CMAKE_CXX_COMPILER ${clang_path_prefix}++)
  36. set(compiler_found TRUE)
  37. endif()
  38. endif()
  39. endif()
  40. # If clang-<version> number could not be found, try seaching for a clang executable without the version suffix
  41. if(NOT compiler_found)
  42. # Look for clang without a version idenfiier
  43. list(TRANSFORM path_search APPEND "/clang" OUTPUT_VARIABLE path_no_version_search)
  44. file(GLOB clang_executables ${path_no_version_search})
  45. if(clang_executables)
  46. list(GET clang_executables 0 clang_executable_front)
  47. cmake_path(GET clang_executable_front PARENT_PATH clang_cplusplus_executable)
  48. cmake_path(APPEND clang_cplusplus_executable "clang++")
  49. if(EXISTS "${clang_executable_front}" AND EXISTS "${clang_cplusplus_executable}")
  50. set(CMAKE_C_COMPILER ${clang_executable_front})
  51. set(CMAKE_CXX_COMPILER ${clang_cplusplus_executable})
  52. set(compiler_found TRUE)
  53. endif()
  54. endif()
  55. endif()
  56. if(NOT compiler_found)
  57. # Look for gcc without a version idenfiier
  58. list(TRANSFORM path_search APPEND "/gcc" OUTPUT_VARIABLE path_no_version_search)
  59. file(GLOB gcc_executables ${path_no_version_search})
  60. message(STATUS ${gcc_executables})
  61. if(gcc_executables)
  62. # Grab the path to the first gcc executable found
  63. list(GET gcc_executables 0 gcc_executable_front)
  64. # Make sure that the GNU g++ frontend exist as well
  65. cmake_path(GET gcc_executable_front PARENT_PATH gnu_cplusplus_executable)
  66. cmake_path(APPEND gnu_cplusplus_executable "g++")
  67. if(EXISTS "${gcc_executable_front}" AND EXISTS "${gnu_cplusplus_executable}")
  68. set(CMAKE_C_COMPILER ${gcc_executable_front})
  69. set(CMAKE_CXX_COMPILER ${gnu_cplusplus_executable})
  70. set(compiler_found TRUE)
  71. endif()
  72. endif()
  73. endif()
  74. if(NOT compiler_found)
  75. message(FATAL_ERROR "Neither Clang nor GCC has been found, please install clang or gcc or specify"
  76. " the compilers to use in CMAKE_C_COMPILER and CMAKE_CXX_COMPILER")
  77. endif()
  78. endif()