LLVM-Config.cmake 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. function(get_system_libs return_var)
  2. message(AUTHOR_WARNING "get_system_libs no longer needed")
  3. set(${return_var} "" PARENT_SCOPE)
  4. endfunction()
  5. function(link_system_libs target)
  6. message(AUTHOR_WARNING "link_system_libs no longer needed")
  7. endfunction()
  8. function(is_llvm_target_library library return_var)
  9. # Sets variable `return_var' to ON if `library' corresponds to a
  10. # LLVM supported target. To OFF if it doesn't.
  11. set(${return_var} OFF PARENT_SCOPE)
  12. string(TOUPPER "${library}" capitalized_lib)
  13. string(TOUPPER "${LLVM_ALL_TARGETS}" targets)
  14. foreach(t ${targets})
  15. if( capitalized_lib STREQUAL t OR
  16. capitalized_lib STREQUAL "LLVM${t}" OR
  17. capitalized_lib STREQUAL "LLVM${t}CODEGEN" OR
  18. capitalized_lib STREQUAL "LLVM${t}ASMPARSER" OR
  19. capitalized_lib STREQUAL "LLVM${t}ASMPRINTER" OR
  20. capitalized_lib STREQUAL "LLVM${t}DISASSEMBLER" OR
  21. capitalized_lib STREQUAL "LLVM${t}INFO" )
  22. set(${return_var} ON PARENT_SCOPE)
  23. break()
  24. endif()
  25. endforeach()
  26. endfunction(is_llvm_target_library)
  27. macro(llvm_config executable)
  28. explicit_llvm_config(${executable} ${ARGN})
  29. endmacro(llvm_config)
  30. function(explicit_llvm_config executable)
  31. set( link_components ${ARGN} )
  32. llvm_map_components_to_libnames(LIBRARIES ${link_components})
  33. get_target_property(t ${executable} TYPE)
  34. if("x${t}" STREQUAL "xSTATIC_LIBRARY")
  35. target_link_libraries(${executable} INTERFACE ${LIBRARIES})
  36. elseif("x${t}" STREQUAL "xSHARED_LIBRARY" OR "x${t}" STREQUAL "xMODULE_LIBRARY")
  37. target_link_libraries(${executable} PRIVATE ${LIBRARIES})
  38. else()
  39. # Use plain form for legacy user.
  40. target_link_libraries(${executable} ${LIBRARIES})
  41. endif()
  42. endfunction(explicit_llvm_config)
  43. # This is Deprecated
  44. function(llvm_map_components_to_libraries OUT_VAR)
  45. message(AUTHOR_WARNING "Using llvm_map_components_to_libraries() is deprecated. Use llvm_map_components_to_libnames() instead")
  46. explicit_map_components_to_libraries(result ${ARGN})
  47. set( ${OUT_VAR} ${result} ${sys_result} PARENT_SCOPE )
  48. endfunction(llvm_map_components_to_libraries)
  49. # This is a variant intended for the final user:
  50. # Map LINK_COMPONENTS to actual libnames.
  51. function(llvm_map_components_to_libnames out_libs)
  52. set( link_components ${ARGN} )
  53. if(NOT LLVM_AVAILABLE_LIBS)
  54. # Inside LLVM itself available libs are in a global property.
  55. get_property(LLVM_AVAILABLE_LIBS GLOBAL PROPERTY LLVM_LIBS)
  56. endif()
  57. string(TOUPPER "${LLVM_AVAILABLE_LIBS}" capitalized_libs)
  58. # Expand some keywords:
  59. list(FIND LLVM_TARGETS_TO_BUILD "${LLVM_NATIVE_ARCH}" have_native_backend)
  60. list(FIND link_components "engine" engine_required)
  61. if( NOT engine_required EQUAL -1 )
  62. list(FIND LLVM_TARGETS_WITH_JIT "${LLVM_NATIVE_ARCH}" have_jit)
  63. if( NOT have_native_backend EQUAL -1 AND NOT have_jit EQUAL -1 )
  64. list(APPEND link_components "jit")
  65. list(APPEND link_components "native")
  66. else()
  67. list(APPEND link_components "interpreter")
  68. endif()
  69. endif()
  70. list(FIND link_components "native" native_required)
  71. if( NOT native_required EQUAL -1 )
  72. if( NOT have_native_backend EQUAL -1 )
  73. list(APPEND link_components ${LLVM_NATIVE_ARCH})
  74. endif()
  75. endif()
  76. # Translate symbolic component names to real libraries:
  77. foreach(c ${link_components})
  78. # add codegen, asmprinter, asmparser, disassembler
  79. list(FIND LLVM_TARGETS_TO_BUILD ${c} idx)
  80. if( NOT idx LESS 0 )
  81. if( TARGET LLVM${c}CodeGen )
  82. list(APPEND expanded_components "LLVM${c}CodeGen")
  83. else()
  84. if( TARGET LLVM${c} )
  85. list(APPEND expanded_components "LLVM${c}")
  86. elseif( NOT c STREQUAL "None" ) # HLSL Change
  87. message(FATAL_ERROR "Target ${c} is not in the set of libraries.")
  88. endif()
  89. endif()
  90. if( TARGET LLVM${c}AsmPrinter )
  91. list(APPEND expanded_components "LLVM${c}AsmPrinter")
  92. endif()
  93. if( TARGET LLVM${c}AsmParser )
  94. list(APPEND expanded_components "LLVM${c}AsmParser")
  95. endif()
  96. if( TARGET LLVM${c}Desc )
  97. list(APPEND expanded_components "LLVM${c}Desc")
  98. endif()
  99. if( TARGET LLVM${c}Info )
  100. list(APPEND expanded_components "LLVM${c}Info")
  101. endif()
  102. if( TARGET LLVM${c}Disassembler )
  103. list(APPEND expanded_components "LLVM${c}Disassembler")
  104. endif()
  105. elseif( c STREQUAL "native" )
  106. # already processed
  107. elseif( c STREQUAL "nativecodegen" )
  108. list(APPEND expanded_components "LLVM${LLVM_NATIVE_ARCH}CodeGen")
  109. if( TARGET LLVM${LLVM_NATIVE_ARCH}Desc )
  110. list(APPEND expanded_components "LLVM${LLVM_NATIVE_ARCH}Desc")
  111. endif()
  112. if( TARGET LLVM${LLVM_NATIVE_ARCH}Info )
  113. list(APPEND expanded_components "LLVM${LLVM_NATIVE_ARCH}Info")
  114. endif()
  115. elseif( c STREQUAL "backend" )
  116. # same case as in `native'.
  117. elseif( c STREQUAL "engine" )
  118. # already processed
  119. elseif( c STREQUAL "all" )
  120. list(APPEND expanded_components ${LLVM_AVAILABLE_LIBS})
  121. elseif( c STREQUAL "AllTargetsAsmPrinters" )
  122. # Link all the asm printers from all the targets
  123. foreach(t ${LLVM_TARGETS_TO_BUILD})
  124. if( TARGET LLVM${t}AsmPrinter )
  125. list(APPEND expanded_components "LLVM${t}AsmPrinter")
  126. endif()
  127. endforeach(t)
  128. elseif( c STREQUAL "AllTargetsAsmParsers" )
  129. # Link all the asm parsers from all the targets
  130. foreach(t ${LLVM_TARGETS_TO_BUILD})
  131. if( TARGET LLVM${t}AsmParser )
  132. list(APPEND expanded_components "LLVM${t}AsmParser")
  133. endif()
  134. endforeach(t)
  135. elseif( c STREQUAL "AllTargetsDescs" )
  136. # Link all the descs from all the targets
  137. foreach(t ${LLVM_TARGETS_TO_BUILD})
  138. if( TARGET LLVM${t}Desc )
  139. list(APPEND expanded_components "LLVM${t}Desc")
  140. endif()
  141. endforeach(t)
  142. elseif( c STREQUAL "AllTargetsDisassemblers" )
  143. # Link all the disassemblers from all the targets
  144. foreach(t ${LLVM_TARGETS_TO_BUILD})
  145. if( TARGET LLVM${t}Disassembler )
  146. list(APPEND expanded_components "LLVM${t}Disassembler")
  147. endif()
  148. endforeach(t)
  149. elseif( c STREQUAL "AllTargetsInfos" )
  150. # Link all the infos from all the targets
  151. foreach(t ${LLVM_TARGETS_TO_BUILD})
  152. if( TARGET LLVM${t}Info )
  153. list(APPEND expanded_components "LLVM${t}Info")
  154. endif()
  155. endforeach(t)
  156. else( NOT idx LESS 0 )
  157. # Canonize the component name:
  158. string(TOUPPER "${c}" capitalized)
  159. list(FIND capitalized_libs LLVM${capitalized} lib_idx)
  160. if( lib_idx LESS 0 )
  161. # The component is unknown. Maybe is an omitted target?
  162. is_llvm_target_library(${c} iltl_result)
  163. if( NOT iltl_result )
  164. message(FATAL_ERROR "Library `${c}' not found in list of llvm libraries.")
  165. endif()
  166. else( lib_idx LESS 0 )
  167. list(GET LLVM_AVAILABLE_LIBS ${lib_idx} canonical_lib)
  168. list(APPEND expanded_components ${canonical_lib})
  169. endif( lib_idx LESS 0 )
  170. endif( NOT idx LESS 0 )
  171. endforeach(c)
  172. set(${out_libs} ${expanded_components} PARENT_SCOPE)
  173. endfunction()
  174. # Perform a post-order traversal of the dependency graph.
  175. # This duplicates the algorithm used by llvm-config, originally
  176. # in tools/llvm-config/llvm-config.cpp, function ComputeLibsForComponents.
  177. function(expand_topologically name required_libs visited_libs)
  178. list(FIND visited_libs ${name} found)
  179. if( found LESS 0 )
  180. list(APPEND visited_libs ${name})
  181. set(visited_libs ${visited_libs} PARENT_SCOPE)
  182. get_property(lib_deps GLOBAL PROPERTY LLVMBUILD_LIB_DEPS_${name})
  183. foreach( lib_dep ${lib_deps} )
  184. expand_topologically(${lib_dep} "${required_libs}" "${visited_libs}")
  185. set(required_libs ${required_libs} PARENT_SCOPE)
  186. set(visited_libs ${visited_libs} PARENT_SCOPE)
  187. endforeach()
  188. list(APPEND required_libs ${name})
  189. set(required_libs ${required_libs} PARENT_SCOPE)
  190. endif()
  191. endfunction()
  192. # Expand dependencies while topologically sorting the list of libraries:
  193. function(llvm_expand_dependencies out_libs)
  194. set(expanded_components ${ARGN})
  195. set(required_libs)
  196. set(visited_libs)
  197. foreach( lib ${expanded_components} )
  198. expand_topologically(${lib} "${required_libs}" "${visited_libs}")
  199. endforeach()
  200. list(REVERSE required_libs)
  201. set(${out_libs} ${required_libs} PARENT_SCOPE)
  202. endfunction()
  203. function(explicit_map_components_to_libraries out_libs)
  204. llvm_map_components_to_libnames(link_libs ${ARGN})
  205. llvm_expand_dependencies(expanded_components ${link_libs})
  206. # Return just the libraries included in this build:
  207. set(result)
  208. foreach(c ${expanded_components})
  209. if( TARGET ${c} )
  210. set(result ${result} ${c})
  211. endif()
  212. endforeach(c)
  213. set(${out_libs} ${result} PARENT_SCOPE)
  214. endfunction(explicit_map_components_to_libraries)