llvm-tools.cmake 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. if (REQUIRED_LLVM_TOOLING_VERSION)
  2. if (CMAKE_VERSION VERSION_LESS 3.18)
  3. message(FATAL_ERROR "Forcing LLVM tooling versions requires at least cmake 3.18")
  4. endif()
  5. find_program(CLANGPLUSPLUS clang++-${REQUIRED_LLVM_TOOLING_VERSION} REQUIRED)
  6. find_program(CLANGC clang-${REQUIRED_LLVM_TOOLING_VERSION} REQUIRED)
  7. find_program(LLVMLINK llvm-link-${REQUIRED_LLVM_TOOLING_VERSION} REQUIRED)
  8. find_program(LLVMOPT opt-${REQUIRED_LLVM_TOOLING_VERSION} REQUIRED)
  9. find_program(LLVMLLC llc-${REQUIRED_LLVM_TOOLING_VERSION} REQUIRED)
  10. find_program(LLVMAS llvm-as-${REQUIRED_LLVM_TOOLING_VERSION} REQUIRED)
  11. else()
  12. if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  13. set(CLANGPLUSPLUS ${CMAKE_CXX_COMPILER})
  14. message(STATUS "Using ${CLANGPLUSPLUS} for c++ to LLVM IR translation")
  15. else()
  16. find_program(CLANGPLUSPLUS NAMES clang++-12 clang++-14 clang++)
  17. if (CLANGPLUSPLUS MATCHES "CLANGPLUSPLUS-NOTFOUND")
  18. message(SEND_ERROR "clang++ not found")
  19. else()
  20. message(STATUS "Using ${CLANGPLUSPLUS} for c++ to LLVM IR translation")
  21. endif()
  22. endif()
  23. if (CMAKE_C_COMPILER_ID MATCHES "Clang")
  24. set(CLANGC ${CMAKE_C_COMPILER})
  25. message(STATUS "Using ${CLANGC} for c++ to LLVM IR translation")
  26. else()
  27. find_program(CLANGC NAMES clang-12 clang-14 clang)
  28. if (CLANGC MATCHES "CLANGC-NOTFOUND")
  29. message(SEND_ERROR "clang not found")
  30. else()
  31. message(STATUS "Using ${CLANGC} for c to LLVM IR translation")
  32. endif()
  33. endif()
  34. find_program(LLVMLINK NAMES llvm-link-12 llvm-link-14 llvm-link)
  35. if (LLVMLINK MATCHES "LLVMLINK-NOTFOUND")
  36. message(SEND_ERROR "llvm-link not found")
  37. else()
  38. message(STATUS "Using ${LLVMLINK} for LLVM IR linking")
  39. endif()
  40. find_program(LLVMOPT NAMES opt-12 opt-14 opt)
  41. if (LLVMOPT MATCHES "LLVMOPT-NOTFOUND")
  42. message(SEND_ERROR "llvm opt tool not found")
  43. else()
  44. message(STATUS "Using ${LLVMOPT} for LLVM IR optimization")
  45. endif()
  46. find_program(LLVMLLC NAMES llc-12 llc-14 llc)
  47. if (LLVMLLC MATCHES "LLVMLLC-NOTFOUND")
  48. message(SEND_ERROR "llvm llc tool not found")
  49. else()
  50. message(STATUS "Using ${LLVMLLC} for LLVM IR to binary code compilation")
  51. endif()
  52. find_program(LLVMAS NAMES llvm-as-12 llvm-as-14 llvm-as)
  53. if (LLVMAS MATCHES "LLVMAS-NOTFOUND")
  54. message(SEND_ERROR "llvm-as not found")
  55. else()
  56. message(STATUS "Using ${LLVMAS} for LLVM IR -> BC assembling")
  57. endif()
  58. endif()
  59. function(llvm_compile_cxx Tgt Inpt Out Tool UseC)
  60. list(APPEND TARGET_INCLUDES "-I$<JOIN:$<TARGET_PROPERTY:${Tgt},INCLUDE_DIRECTORIES>,$<SEMICOLON>-I>")
  61. list(APPEND TARGET_COMPILE_OPTIONS "'$<JOIN:$<TARGET_GENEX_EVAL:${Tgt},$<TARGET_PROPERTY:${Tgt},COMPILE_OPTIONS>>,'$<SEMICOLON>'>'")
  62. list(APPEND TARGET_COMPILE_DEFINITIONS "'-D$<JOIN:$<TARGET_GENEX_EVAL:${Tgt},$<TARGET_PROPERTY:${Tgt},COMPILE_DEFINITIONS>>,'$<SEMICOLON>'-D>'")
  63. if (${UseC})
  64. set(STD_FLAG "")
  65. separate_arguments(LANG_FLAGS NATIVE_COMMAND ${CMAKE_C_FLAGS})
  66. separate_arguments(FLAGSLIST_DEBUG NATIVE_COMMAND ${CMAKE_C_FLAGS_DEBUG})
  67. separate_arguments(FLAGSLIST_RELEASE NATIVE_COMMAND ${CMAKE_C_FLAGS_RELEASE})
  68. separate_arguments(FLAGSLIST_MINSIZEREL NATIVE_COMMAND ${CMAKE_C_FLAGS_MINSIZEREL})
  69. separate_arguments(FLAGSLIST_RELWITHDEBINFO NATIVE_COMMAND ${CMAKE_C_FLAGS_RELWITHDEBINFO})
  70. else()
  71. get_target_property(TARGET_STANDARD ${Tgt} CXX_STANDARD)
  72. set(STD_FLAG "-std=c++${TARGET_STANDARD}")
  73. separate_arguments(LANG_FLAGS NATIVE_COMMAND ${CMAKE_CXX_FLAGS})
  74. separate_arguments(FLAGSLIST_DEBUG NATIVE_COMMAND ${CMAKE_CXX_FLAGS_DEBUG})
  75. separate_arguments(FLAGSLIST_RELEASE NATIVE_COMMAND ${CMAKE_CXX_FLAGS_RELEASE})
  76. separate_arguments(FLAGSLIST_MINSIZEREL NATIVE_COMMAND ${CMAKE_CXX_FLAGS_MINSIZEREL})
  77. separate_arguments(FLAGSLIST_RELWITHDEBINFO NATIVE_COMMAND ${CMAKE_CXX_FLAGS_RELWITHDEBINFO})
  78. endif()
  79. add_custom_command(
  80. OUTPUT ${Out}
  81. COMMAND
  82. ${Tool}
  83. ${TARGET_INCLUDES}
  84. ${LANG_FLAGS}
  85. "$<$<CONFIG:DEBUG>:${FLAGSLIST_DEBUG}>"
  86. "$<$<CONFIG:RELEASE>:${FLAGSLIST_RELEASE}>"
  87. "$<$<CONFIG:MINSIZEREL>:${FLAGSLIST_MINSIZEREL}>"
  88. "$<$<CONFIG:RELWITHDEBINFO>:${FLAGSLIST_RELWITHDEBINFO}>"
  89. ${TARGET_COMPILE_DEFINITIONS}
  90. ${STD_FLAG}
  91. ${TARGET_COMPILE_OPTIONS}
  92. -Wno-unknown-warning-option
  93. -fno-lto
  94. -emit-llvm
  95. -c
  96. ${Inpt}
  97. -o
  98. ${Out}
  99. COMMAND_EXPAND_LISTS
  100. DEPENDS ${Inpt} ${Tool}
  101. )
  102. endfunction()
  103. function(llvm_compile_c Tgt Inpt Out Tool)
  104. llvm_compile_cxx(${Tgt} ${Inpt} ${Out} ${Tool} TRUE)
  105. endfunction()