CMakeFindBinUtils.cmake 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. # search for additional tools required for C/C++ (and other languages ?)
  4. #
  5. # If the internal cmake variable _CMAKE_TOOLCHAIN_PREFIX is set, this is used
  6. # as prefix for the tools (e.g. arm-elf-gcc etc.)
  7. # If the cmake variable _CMAKE_TOOLCHAIN_LOCATION is set, the compiler is
  8. # searched only there. The other tools are at first searched there, then
  9. # also in the default locations.
  10. #
  11. # Sets the following variables:
  12. # CMAKE_AR
  13. # CMAKE_RANLIB
  14. # CMAKE_LINKER
  15. # CMAKE_MT
  16. # CMAKE_STRIP
  17. # CMAKE_INSTALL_NAME_TOOL
  18. # on UNIX, cygwin and mingw
  19. # Resolve full path of CMAKE_TOOL from user-defined name and SEARCH_PATH.
  20. function(__resolve_tool_path CMAKE_TOOL SEARCH_PATH DOCSTRING)
  21. if(${CMAKE_TOOL})
  22. # We only get here if CMAKE_TOOL was
  23. # specified using -D or a pre-made CMakeCache.txt (e.g. via ctest)
  24. # or set in CMAKE_TOOLCHAIN_FILE.
  25. get_filename_component(_CMAKE_USER_TOOL_PATH "${${CMAKE_TOOL}}" DIRECTORY)
  26. # Is CMAKE_TOOL a user-defined name instead of a full path?
  27. if(NOT _CMAKE_USER_TOOL_PATH)
  28. # Find CMAKE_TOOL in the SEARCH_PATH directory by user-defined name.
  29. find_program(_CMAKE_TOOL_WITH_PATH NAMES ${${CMAKE_TOOL}} HINTS ${SEARCH_PATH})
  30. if(_CMAKE_TOOL_WITH_PATH)
  31. # Overwrite CMAKE_TOOL with full path found in SEARCH_PATH.
  32. set(${CMAKE_TOOL} ${_CMAKE_TOOL_WITH_PATH} PARENT_SCOPE)
  33. get_property(_CMAKE_TOOL_CACHED CACHE ${CMAKE_TOOL} PROPERTY TYPE)
  34. # If CMAKE_TOOL is present in the CMake Cache, then overwrit it as well.
  35. if(_CMAKE_TOOL_CACHED)
  36. set(${CMAKE_TOOL} "${_CMAKE_TOOL_WITH_PATH}" CACHE STRING ${DOCSTRING} FORCE)
  37. endif()
  38. endif()
  39. unset(_CMAKE_TOOL_WITH_PATH CACHE)
  40. endif()
  41. endif()
  42. endfunction()
  43. __resolve_tool_path(CMAKE_LINKER "${_CMAKE_TOOLCHAIN_LOCATION}" "Default Linker")
  44. __resolve_tool_path(CMAKE_MT "${_CMAKE_TOOLCHAIN_LOCATION}" "Default Manifest Tool")
  45. set(_CMAKE_TOOL_VARS "")
  46. # if it's the MS C/CXX compiler, search for link
  47. if(("x${CMAKE_${_CMAKE_PROCESSING_LANGUAGE}_SIMULATE_ID}" STREQUAL "xMSVC" AND
  48. ("x${CMAKE_${_CMAKE_PROCESSING_LANGUAGE}_COMPILER_FRONTEND_VARIANT}" STREQUAL "xMSVC"
  49. OR NOT "x${CMAKE_${_CMAKE_PROCESSING_LANGUAGE}_COMPILER_ID}" STREQUAL "xClang"))
  50. OR "x${CMAKE_${_CMAKE_PROCESSING_LANGUAGE}_COMPILER_ID}" STREQUAL "xMSVC"
  51. OR (CMAKE_HOST_WIN32 AND "x${CMAKE_${_CMAKE_PROCESSING_LANGUAGE}_COMPILER_ID}" STREQUAL "xPGI")
  52. OR (CMAKE_HOST_WIN32 AND "x${CMAKE_${_CMAKE_PROCESSING_LANGUAGE}_COMPILER_ID}" STREQUAL "xNVIDIA")
  53. OR (CMAKE_GENERATOR MATCHES "Visual Studio"
  54. AND NOT CMAKE_VS_PLATFORM_NAME STREQUAL "Tegra-Android"))
  55. if("x${CMAKE_${_CMAKE_PROCESSING_LANGUAGE}_COMPILER_ID}" STREQUAL "xClang")
  56. find_program(CMAKE_NM NAMES ${_CMAKE_TOOLCHAIN_PREFIX}nm llvm-nm HINTS ${_CMAKE_TOOLCHAIN_LOCATION})
  57. set(_CMAKE_ADDITIONAL_LINKER_NAMES "lld-link")
  58. endif()
  59. find_program(CMAKE_LINKER NAMES ${_CMAKE_ADDITIONAL_LINKER_NAMES} link HINTS ${_CMAKE_TOOLCHAIN_LOCATION})
  60. find_program(CMAKE_MT NAMES mt HINTS ${_CMAKE_TOOLCHAIN_LOCATION})
  61. list(APPEND _CMAKE_TOOL_VARS LINKER MT)
  62. # in all other cases search for ar, ranlib, etc.
  63. else()
  64. if(CMAKE_C_COMPILER_EXTERNAL_TOOLCHAIN)
  65. set(_CMAKE_TOOLCHAIN_LOCATION ${_CMAKE_TOOLCHAIN_LOCATION} ${CMAKE_C_COMPILER_EXTERNAL_TOOLCHAIN}/bin)
  66. endif()
  67. if(CMAKE_CXX_COMPILER_EXTERNAL_TOOLCHAIN)
  68. set(_CMAKE_TOOLCHAIN_LOCATION ${_CMAKE_TOOLCHAIN_LOCATION} ${CMAKE_CXX_COMPILER_EXTERNAL_TOOLCHAIN}/bin)
  69. endif()
  70. if("${CMAKE_${_CMAKE_PROCESSING_LANGUAGE}_COMPILER_ID}" STREQUAL Clang)
  71. set(_CMAKE_ADDITIONAL_AR_NAMES "llvm-ar")
  72. set(_CMAKE_ADDITIONAL_RANLIB_NAMES "llvm-ranlib")
  73. set(_CMAKE_ADDITIONAL_STRIP_NAMES "llvm-strip")
  74. set(_CMAKE_ADDITIONAL_LINKER_NAMES "ld.lld")
  75. set(_CMAKE_ADDITIONAL_NM_NAMES "llvm-nm")
  76. set(_CMAKE_ADDITIONAL_OBJDUMP_NAMES "llvm-objdump")
  77. set(_CMAKE_ADDITIONAL_OBJCOPY_NAMES "llvm-objcopy")
  78. set(_CMAKE_ADDITIONAL_READELF_NAMES "llvm-readelf")
  79. set(_CMAKE_ADDITIONAL_DLLTOOL_NAMES "llvm-dlltool")
  80. set(_CMAKE_ADDITIONAL_ADDR2LINE_NAMES "llvm-addr2line")
  81. endif()
  82. find_program(CMAKE_AR NAMES ${_CMAKE_TOOLCHAIN_PREFIX}ar${_CMAKE_TOOLCHAIN_SUFFIX} ${_CMAKE_ADDITIONAL_AR_NAMES} HINTS ${_CMAKE_TOOLCHAIN_LOCATION})
  83. find_program(CMAKE_RANLIB NAMES ${_CMAKE_TOOLCHAIN_PREFIX}ranlib ${_CMAKE_ADDITIONAL_RANLIB_NAMES} HINTS ${_CMAKE_TOOLCHAIN_LOCATION})
  84. if(NOT CMAKE_RANLIB)
  85. set(CMAKE_RANLIB : CACHE INTERNAL "noop for ranlib")
  86. endif()
  87. find_program(CMAKE_STRIP NAMES ${_CMAKE_TOOLCHAIN_PREFIX}strip${_CMAKE_TOOLCHAIN_SUFFIX} ${_CMAKE_ADDITIONAL_STRIP_NAMES} HINTS ${_CMAKE_TOOLCHAIN_LOCATION})
  88. find_program(CMAKE_LINKER NAMES ${_CMAKE_TOOLCHAIN_PREFIX}ld ${_CMAKE_ADDITIONAL_LINKER_NAMES} HINTS ${_CMAKE_TOOLCHAIN_LOCATION})
  89. find_program(CMAKE_NM NAMES ${_CMAKE_TOOLCHAIN_PREFIX}nm ${_CMAKE_ADDITIONAL_NM_NAMES} HINTS ${_CMAKE_TOOLCHAIN_LOCATION})
  90. find_program(CMAKE_OBJDUMP NAMES ${_CMAKE_TOOLCHAIN_PREFIX}objdump ${_CMAKE_ADDITIONAL_OBJDUMP_NAMES} HINTS ${_CMAKE_TOOLCHAIN_LOCATION})
  91. find_program(CMAKE_OBJCOPY NAMES ${_CMAKE_TOOLCHAIN_PREFIX}objcopy ${_CMAKE_ADDITIONAL_OBJCOPY_NAMES} HINTS ${_CMAKE_TOOLCHAIN_LOCATION})
  92. find_program(CMAKE_READELF NAMES ${_CMAKE_TOOLCHAIN_PREFIX}readelf ${_CMAKE_ADDITIONAL_READELF_NAMES} HINTS ${_CMAKE_TOOLCHAIN_LOCATION})
  93. find_program(CMAKE_DLLTOOL NAMES ${_CMAKE_TOOLCHAIN_PREFIX}dlltool ${_CMAKE_ADDITIONAL_DLLTOOL_NAMES} HINTS ${_CMAKE_TOOLCHAIN_LOCATION})
  94. find_program(CMAKE_ADDR2LINE NAMES ${_CMAKE_TOOLCHAIN_PREFIX}addr2line ${_CMAKE_ADDITIONAL_ADDR2LINE_NAMES} HINTS ${_CMAKE_TOOLCHAIN_LOCATION})
  95. list(APPEND _CMAKE_TOOL_VARS AR RANLIB STRIP LINKER NM OBJDUMP OBJCOPY READELF DLLTOOL ADDR2LINE)
  96. endif()
  97. if(CMAKE_PLATFORM_HAS_INSTALLNAME)
  98. find_program(CMAKE_INSTALL_NAME_TOOL NAMES ${_CMAKE_TOOLCHAIN_PREFIX}install_name_tool HINTS ${_CMAKE_TOOLCHAIN_LOCATION})
  99. if(NOT CMAKE_INSTALL_NAME_TOOL)
  100. message(FATAL_ERROR "Could not find install_name_tool, please check your installation.")
  101. endif()
  102. list(APPEND _CMAKE_TOOL_VARS INSTALL_NAME_TOOL)
  103. endif()
  104. # Mark any tool cache entries as advanced.
  105. foreach(var IN LISTS _CMAKE_TOOL_VARS)
  106. get_property(_CMAKE_TOOL_CACHED CACHE CMAKE_${var} PROPERTY TYPE)
  107. if(_CMAKE_TOOL_CACHED)
  108. mark_as_advanced(CMAKE_${var})
  109. endif()
  110. unset(_CMAKE_ADDITIONAL_${var}_NAMES)
  111. endforeach()
  112. unset(_CMAKE_TOOL_VARS)
  113. unset(_CMAKE_TOOL_CACHED)