ARMClang.cmake 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. if(_ARMClang_CMAKE_LOADED)
  2. return()
  3. endif()
  4. # This file requires CMAKE_LINKER and CMAKE_AR set by CMakeFindBinUtils.cmake.
  5. if(NOT (DEFINED CMAKE_LINKER AND DEFINED CMAKE_AR))
  6. return()
  7. endif()
  8. set(_ARMClang_CMAKE_LOADED TRUE)
  9. # Save the CMP0123 setting in a variable used both below and by try_compile.
  10. cmake_policy(GET CMP0123 CMAKE_ARMClang_CMP0123)
  11. set(CMAKE_EXECUTABLE_SUFFIX ".elf")
  12. if (CMAKE_LINKER MATCHES "armlink")
  13. set(__CMAKE_ARMClang_USING_armlink TRUE)
  14. set(CMAKE_LIBRARY_PATH_FLAG "--userlibpath=")
  15. else()
  16. set(__CMAKE_ARMClang_USING_armlink FALSE)
  17. endif()
  18. # get compiler supported cpu list
  19. function(__armclang_set_processor_list lang out_var)
  20. execute_process(COMMAND "${CMAKE_${lang}_COMPILER}" --target=${CMAKE_${lang}_COMPILER_TARGET} -mcpu=list
  21. OUTPUT_VARIABLE processor_list
  22. ERROR_VARIABLE processor_list)
  23. string(REGEX MATCHALL "-mcpu=([^ \n]*)" processor_list "${processor_list}")
  24. string(REGEX REPLACE "-mcpu=" "" processor_list "${processor_list}")
  25. set(${out_var} "${processor_list}" PARENT_SCOPE)
  26. endfunction()
  27. # check processor is in list
  28. function(__armclang_check_processor processor list out_var)
  29. string(TOLOWER "${processor}" processor)
  30. if(processor IN_LIST list)
  31. set(${out_var} TRUE PARENT_SCOPE)
  32. else()
  33. set(${out_var} FALSE PARENT_SCOPE)
  34. endif()
  35. endfunction()
  36. # get compiler supported arch list
  37. function(__armclang_set_arch_list lang out_var)
  38. execute_process(COMMAND "${CMAKE_${lang}_COMPILER}" --target=${CMAKE_${lang}_COMPILER_TARGET} -march=list
  39. OUTPUT_VARIABLE arch_list
  40. ERROR_VARIABLE arch_list)
  41. string(REGEX MATCHALL "-march=([^ \n]*)" arch_list "${arch_list}")
  42. string(REGEX REPLACE "-march=" "" arch_list "${arch_list}")
  43. set(${out_var} "${arch_list}" PARENT_SCOPE)
  44. endfunction()
  45. # get linker supported cpu list
  46. function(__armlink_set_cpu_list lang out_var)
  47. if(__CMAKE_ARMClang_USING_armlink)
  48. set(__linker_wrapper_flags "")
  49. else()
  50. set(__linker_wrapper_flags --target=${CMAKE_${lang}_COMPILER_TARGET} -Xlinker)
  51. endif()
  52. execute_process(COMMAND "${CMAKE_LINKER}" ${__linker_wrapper_flags} --cpu=list
  53. OUTPUT_VARIABLE cpu_list
  54. ERROR_VARIABLE cpu_list)
  55. string(REGEX MATCHALL "--cpu=([^ \n]*)" cpu_list "${cpu_list}")
  56. string(REGEX REPLACE "--cpu=" "" cpu_list "${cpu_list}")
  57. set(${out_var} "${cpu_list}" PARENT_SCOPE)
  58. endfunction()
  59. macro(__compiler_armclang lang)
  60. if(NOT CMAKE_${lang}_COMPILER_TARGET)
  61. set(CMAKE_${lang}_COMPILER_TARGET arm-arm-none-eabi)
  62. endif()
  63. if(NOT CMAKE_${lang}_COMPILER_PROCESSOR_LIST)
  64. __armclang_set_processor_list(${lang} CMAKE_${lang}_COMPILER_PROCESSOR_LIST)
  65. endif()
  66. if(NOT CMAKE_${lang}_COMPILER_ARCH_LIST)
  67. __armclang_set_arch_list(${lang} CMAKE_${lang}_COMPILER_ARCH_LIST)
  68. endif()
  69. # CMAKE_SYSTEM_PROCESSOR and CMAKE_SYSTEM_ARCH are not sufficient because they provide no
  70. # information of additional CPU features needed in `-mcpu=<name>[+[no]<feature>+...]`.
  71. # The automatic setting of compile and link options is deprecated and projects should specify their own.
  72. cmake_policy(GET CMP0123 policy_CMP0123)
  73. if(NOT "x${CMAKE_ARMClang_CMP0123}x" STREQUAL "xNEWx")
  74. if(NOT "x${CMAKE_ARMClang_CMP0123}x" STREQUAL "xOLDx")
  75. cmake_policy(GET_WARNING CMP0123 _cmp0123_warning)
  76. message(AUTHOR_WARNING
  77. "${_cmp0123_warning}\n"
  78. "For compatibility, CMake will automatically add cpu/arch flags based "
  79. "on the CMAKE_SYSTEM_PROCESSOR and/or CMAKE_SYSTEM_ARCH variables."
  80. )
  81. endif()
  82. if(NOT CMAKE_SYSTEM_PROCESSOR AND NOT CMAKE_SYSTEM_ARCH)
  83. message(FATAL_ERROR " CMAKE_SYSTEM_PROCESSOR or CMAKE_SYSTEM_ARCH must be set for ARMClang\n"
  84. " Supported processor: ${CMAKE_${lang}_COMPILER_PROCESSOR_LIST}\n"
  85. " Supported Architecture: ${CMAKE_${lang}_COMPILER_ARCH_LIST}")
  86. else()
  87. __armclang_check_processor("${CMAKE_SYSTEM_ARCH}" "${CMAKE_${lang}_COMPILER_ARCH_LIST}" _CMAKE_${lang}_CHECK_ARCH_RESULT)
  88. if( _CMAKE_${lang}_CHECK_ARCH_RESULT)
  89. string(APPEND CMAKE_${lang}_FLAGS_INIT " -march=${CMAKE_SYSTEM_ARCH}")
  90. set(__march_flag_set TRUE)
  91. endif()
  92. __armclang_check_processor("${CMAKE_SYSTEM_PROCESSOR}" "${CMAKE_${lang}_COMPILER_PROCESSOR_LIST}" _CMAKE_${lang}_CHECK_PROCESSOR_RESULT)
  93. if(_CMAKE_${lang}_CHECK_PROCESSOR_RESULT)
  94. string(APPEND CMAKE_${lang}_FLAGS_INIT " -mcpu=${CMAKE_SYSTEM_PROCESSOR}")
  95. set(__mcpu_flag_set TRUE)
  96. endif()
  97. if(NOT __march_flag_set AND NOT __mcpu_flag_set)
  98. message(FATAL_ERROR "At least one of the variables CMAKE_SYSTEM_PROCESSOR or CMAKE_SYSTEM_ARCH must be set for ARMClang\n"
  99. "Supported processor: ${CMAKE_${lang}_COMPILER_PROCESSOR_LIST}\n"
  100. " Supported Architecture: ${CMAKE_${lang}_COMPILER_ARCH_LIST}")
  101. endif()
  102. unset(_CMAKE_${lang}_CHECK_PROCESSOR_RESULT)
  103. unset(_CMAKE_${lang}_CHECK_ARCH_RESULT)
  104. endif()
  105. #check if CMAKE_SYSTEM_PROCESSOR belongs to supported cpu list for armlink
  106. __armlink_set_cpu_list( ${lang} CMAKE_LINKER_CPU_LIST)
  107. list(TRANSFORM CMAKE_LINKER_CPU_LIST TOLOWER)
  108. __armclang_check_processor("${CMAKE_SYSTEM_PROCESSOR}" "${CMAKE_LINKER_CPU_LIST}" _CMAKE_CHECK_LINK_CPU_RESULT)
  109. if(_CMAKE_CHECK_LINK_CPU_RESULT)
  110. string(APPEND CMAKE_${lang}_LINK_FLAGS " --cpu=${CMAKE_SYSTEM_PROCESSOR}")
  111. endif()
  112. endif()
  113. if(__CMAKE_ARMClang_USING_armlink)
  114. unset(CMAKE_${lang}_LINKER_WRAPPER_FLAG)
  115. set(__CMAKE_ARMClang_USING_armlink_WRAPPER "")
  116. else()
  117. set(__CMAKE_ARMClang_USING_armlink_WRAPPER "-Xlinker")
  118. endif()
  119. set(CMAKE_${lang}_LINK_EXECUTABLE "<CMAKE_LINKER> <CMAKE_${lang}_LINK_FLAGS> <LINK_FLAGS> <LINK_LIBRARIES> <OBJECTS> -o <TARGET> ${__CMAKE_ARMClang_USING_armlink_WRAPPER}")
  120. set(CMAKE_${lang}_CREATE_STATIC_LIBRARY "<CMAKE_AR> --create -cr <TARGET> <LINK_FLAGS> <OBJECTS>")
  121. set(CMAKE_${lang}_ARCHIVE_CREATE "<CMAKE_AR> --create -cr <TARGET> <LINK_FLAGS> <OBJECTS>")
  122. set(CMAKE_${lang}_RESPONSE_FILE_LINK_FLAG "${__CMAKE_ARMClang_USING_armlink_WRAPPER} --via=")
  123. set(CMAKE_${lang}_OUTPUT_EXTENSION ".o")
  124. set(CMAKE_${lang}_OUTPUT_EXTENSION_REPLACE 1)
  125. endmacro()