aboutsummaryrefslogtreecommitdiffstats
path: root/cmake/llvm-tools.cmake
blob: 83276f3dde230f59b40fc67a2b25ec03f207bbe2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
if (REQUIRED_LLVM_TOOLING_VERSION)
  if (CMAKE_VERSION VERSION_LESS 3.18)
    message(FATAL_ERROR "Forcing LLVM tooling versions requires at least cmake 3.18")
  endif()
  find_program(CLANGPLUSPLUS clang++-${REQUIRED_LLVM_TOOLING_VERSION} REQUIRED)
  find_program(CLANGC clang-${REQUIRED_LLVM_TOOLING_VERSION} REQUIRED)
  find_program(LLVMLINK llvm-link-${REQUIRED_LLVM_TOOLING_VERSION} REQUIRED)
  find_program(LLVMOPT opt-${REQUIRED_LLVM_TOOLING_VERSION} REQUIRED)
  find_program(LLVMLLC llc-${REQUIRED_LLVM_TOOLING_VERSION} REQUIRED)
else()
  if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
    set(CLANGPLUSPLUS ${CMAKE_CXX_COMPILER})
    message(STATUS "Using ${CLANGPLUSPLUS} for c++ to LLVM IR translation")
  else()
    find_program(CLANGPLUSPLUS NAMES clang++-12 clang++-14 clang++)
    if (CLANGPLUSPLUS MATCHES "CLANGPLUSPLUS-NOTFOUND")
      message(SEND_ERROR "clang++ not found")
    else()
      message(STATUS "Using ${CLANGPLUSPLUS} for c++ to LLVM IR translation")
    endif()
  endif()

  if (CMAKE_C_COMPILER_ID MATCHES "Clang")
    set(CLANGC ${CMAKE_C_COMPILER})
    message(STATUS "Using ${CLANGC} for c++ to LLVM IR translation")
  else()
    find_program(CLANGC NAMES clang-12 clang-14 clang)
    if (CLANGC MATCHES "CLANGC-NOTFOUND")
      message(SEND_ERROR "clang not found")
    else()
      message(STATUS "Using ${CLANGC} for c to LLVM IR translation")
    endif()
  endif()

  find_program(LLVMLINK NAMES llvm-link-12 llvm-link-14 llvm-link)
  if (LLVMLINK MATCHES "LLVMLINK-NOTFOUND")
    message(SEND_ERROR "llvm-link not found")
  else()
    message(STATUS "Using ${LLVMLINK} for LLVM IR linking")
  endif()
  find_program(LLVMOPT NAMES opt-12 opt-14 opt)
  if (LLVMOPT MATCHES "LLVMOPT-NOTFOUND")
    message(SEND_ERROR "llvm opt tool not found")
  else()
    message(STATUS "Using ${LLVMOPT} for LLVM IR optimization")
  endif()

  find_program(LLVMLLC NAMES llc-12 llc-14 llc)
  if (LLVMLLC MATCHES "LLVMLLC-NOTFOUND")
    message(SEND_ERROR "llvm llc tool not found")
  else()
    message(STATUS "Using ${LLVMLLC} for LLVM IR to binary code compilation")
  endif()
endif()

function(llvm_compile_cxx Tgt Inpt Out Tool UseC)
  list(APPEND TARGET_INCLUDES "-I$<JOIN:$<TARGET_PROPERTY:${Tgt},INCLUDE_DIRECTORIES>,$<SEMICOLON>-I>")
  list(APPEND TARGET_COMPILE_OPTIONS "'$<JOIN:$<TARGET_GENEX_EVAL:${Tgt},$<TARGET_PROPERTY:${Tgt},COMPILE_OPTIONS>>,'$<SEMICOLON>'>'")
  list(APPEND TARGET_COMPILE_DEFINITIONS "'-D$<JOIN:$<TARGET_GENEX_EVAL:${Tgt},$<TARGET_PROPERTY:${Tgt},COMPILE_DEFINITIONS>>,'$<SEMICOLON>'-D>'")
  if (${UseC})
    set(STD_FLAG "")
    separate_arguments(LANG_FLAGS NATIVE_COMMAND ${CMAKE_C_FLAGS})
    separate_arguments(FLAGSLIST_DEBUG NATIVE_COMMAND ${CMAKE_C_FLAGS_DEBUG})
    separate_arguments(FLAGSLIST_RELEASE NATIVE_COMMAND ${CMAKE_C_FLAGS_RELEASE})
    separate_arguments(FLAGSLIST_MINSIZEREL NATIVE_COMMAND ${CMAKE_C_FLAGS_MINSIZEREL})
    separate_arguments(FLAGSLIST_RELWITHDEBINFO NATIVE_COMMAND ${CMAKE_C_FLAGS_RELWITHDEBINFO})
  else()
    get_target_property(TARGET_STANDARD ${Tgt} CXX_STANDARD)
    set(STD_FLAG "-std=c++${TARGET_STANDARD}")
    separate_arguments(LANG_FLAGS NATIVE_COMMAND ${CMAKE_CXX_FLAGS})
    separate_arguments(FLAGSLIST_DEBUG NATIVE_COMMAND ${CMAKE_CXX_FLAGS_DEBUG})
    separate_arguments(FLAGSLIST_RELEASE NATIVE_COMMAND ${CMAKE_CXX_FLAGS_RELEASE})
    separate_arguments(FLAGSLIST_MINSIZEREL NATIVE_COMMAND ${CMAKE_CXX_FLAGS_MINSIZEREL})
    separate_arguments(FLAGSLIST_RELWITHDEBINFO NATIVE_COMMAND ${CMAKE_CXX_FLAGS_RELWITHDEBINFO})
  endif()

  add_custom_command(
    OUTPUT ${Out}
    COMMAND
    ${Tool}
    ${TARGET_INCLUDES}
    ${LANG_FLAGS}
    "$<$<CONFIG:DEBUG>:${FLAGSLIST_DEBUG}>"
    "$<$<CONFIG:RELEASE>:${FLAGSLIST_RELEASE}>"
    "$<$<CONFIG:MINSIZEREL>:${FLAGSLIST_MINSIZEREL}>"
    "$<$<CONFIG:RELWITHDEBINFO>:${FLAGSLIST_RELWITHDEBINFO}>"
    ${TARGET_COMPILE_DEFINITIONS}
    ${STD_FLAG}
    ${TARGET_COMPILE_OPTIONS}
    -Wno-unknown-warning-option
    -fno-lto
    -emit-llvm
    -c
    ${Inpt}
    -o
    ${Out}
    COMMAND_EXPAND_LISTS
    DEPENDS ${Inpt} ${Tool}
  )
endfunction()

function(llvm_compile_c Tgt Inpt Out Tool)
  llvm_compile_cxx(${Tgt} ${Inpt} ${Out} ${Tool} TRUE)
endfunction()