LLVM-Config.cmake 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. # is_llvm_target_library(
  9. # library
  10. # Name of the LLVM library to check
  11. # return_var
  12. # Output variable name
  13. # ALL_TARGETS;INCLUDED_TARGETS;OMITTED_TARGETS
  14. # ALL_TARGETS - default looks at the full list of known targets
  15. # INCLUDED_TARGETS - looks only at targets being configured
  16. # OMITTED_TARGETS - looks only at targets that are not being configured
  17. # )
  18. function(is_llvm_target_library library return_var)
  19. cmake_parse_arguments(ARG "ALL_TARGETS;INCLUDED_TARGETS;OMITTED_TARGETS" "" "" ${ARGN})
  20. # Sets variable `return_var' to ON if `library' corresponds to a
  21. # LLVM supported target. To OFF if it doesn't.
  22. set(${return_var} OFF PARENT_SCOPE)
  23. string(TOUPPER "${library}" capitalized_lib)
  24. if(ARG_INCLUDED_TARGETS)
  25. string(TOUPPER "${LLVM_TARGETS_TO_BUILD}" targets)
  26. elseif(ARG_OMITTED_TARGETS)
  27. set(omitted_targets ${LLVM_ALL_TARGETS})
  28. list(REMOVE_ITEM omitted_targets ${LLVM_TARGETS_TO_BUILD})
  29. string(TOUPPER "${omitted_targets}" targets)
  30. else()
  31. string(TOUPPER "${LLVM_ALL_TARGETS}" targets)
  32. endif()
  33. foreach(t ${targets})
  34. if( capitalized_lib STREQUAL t OR
  35. capitalized_lib STREQUAL "${t}" OR
  36. capitalized_lib STREQUAL "${t}DESC" OR
  37. capitalized_lib STREQUAL "${t}CODEGEN" OR
  38. capitalized_lib STREQUAL "${t}ASMPARSER" OR
  39. capitalized_lib STREQUAL "${t}ASMPRINTER" OR
  40. capitalized_lib STREQUAL "${t}DISASSEMBLER" OR
  41. capitalized_lib STREQUAL "${t}INFO" OR
  42. capitalized_lib STREQUAL "${t}UTILS" )
  43. set(${return_var} ON PARENT_SCOPE)
  44. break()
  45. endif()
  46. endforeach()
  47. endfunction(is_llvm_target_library)
  48. function(is_llvm_target_specifier library return_var)
  49. is_llvm_target_library(${library} ${return_var} ${ARGN})
  50. string(TOUPPER "${library}" capitalized_lib)
  51. if(NOT ${return_var})
  52. if( capitalized_lib STREQUAL "ALLTARGETSASMPARSERS" OR
  53. capitalized_lib STREQUAL "ALLTARGETSDESCS" OR
  54. capitalized_lib STREQUAL "ALLTARGETSDISASSEMBLERS" OR
  55. capitalized_lib STREQUAL "ALLTARGETSINFOS" OR
  56. capitalized_lib STREQUAL "NATIVE" OR
  57. capitalized_lib STREQUAL "NATIVECODEGEN" )
  58. set(${return_var} ON PARENT_SCOPE)
  59. endif()
  60. endif()
  61. endfunction()
  62. macro(llvm_config executable)
  63. cmake_parse_arguments(ARG "USE_SHARED" "" "" ${ARGN})
  64. set(link_components ${ARG_UNPARSED_ARGUMENTS})
  65. if(ARG_USE_SHARED)
  66. # If USE_SHARED is specified, then we link against libLLVM,
  67. # but also against the component libraries below. This is
  68. # done in case libLLVM does not contain all of the components
  69. # the target requires.
  70. #
  71. # Strip LLVM_DYLIB_COMPONENTS out of link_components.
  72. # To do this, we need special handling for "all", since that
  73. # may imply linking to libraries that are not included in
  74. # libLLVM.
  75. if (DEFINED link_components AND DEFINED LLVM_DYLIB_COMPONENTS)
  76. if("${LLVM_DYLIB_COMPONENTS}" STREQUAL "all")
  77. set(link_components "")
  78. else()
  79. list(REMOVE_ITEM link_components ${LLVM_DYLIB_COMPONENTS})
  80. endif()
  81. endif()
  82. target_link_libraries(${executable} PRIVATE LLVM)
  83. endif()
  84. explicit_llvm_config(${executable} ${link_components})
  85. endmacro(llvm_config)
  86. function(explicit_llvm_config executable)
  87. set( link_components ${ARGN} )
  88. llvm_map_components_to_libnames(LIBRARIES ${link_components})
  89. get_target_property(t ${executable} TYPE)
  90. if(t STREQUAL "STATIC_LIBRARY")
  91. target_link_libraries(${executable} INTERFACE ${LIBRARIES})
  92. elseif(t STREQUAL "EXECUTABLE" OR t STREQUAL "SHARED_LIBRARY" OR t STREQUAL "MODULE_LIBRARY")
  93. target_link_libraries(${executable} PRIVATE ${LIBRARIES})
  94. else()
  95. # Use plain form for legacy user.
  96. target_link_libraries(${executable} ${LIBRARIES})
  97. endif()
  98. endfunction(explicit_llvm_config)
  99. # This is Deprecated
  100. function(llvm_map_components_to_libraries OUT_VAR)
  101. message(AUTHOR_WARNING "Using llvm_map_components_to_libraries() is deprecated. Use llvm_map_components_to_libnames() instead")
  102. explicit_map_components_to_libraries(result ${ARGN})
  103. set( ${OUT_VAR} ${result} ${sys_result} PARENT_SCOPE )
  104. endfunction(llvm_map_components_to_libraries)
  105. # Expand pseudo-components into real components.
  106. # Does not cover 'native', 'backend', or 'engine' as these require special
  107. # handling. Also does not cover 'all' as we only have a list of the libnames
  108. # available and not a list of the components.
  109. function(llvm_expand_pseudo_components out_components)
  110. set( link_components ${ARGN} )
  111. foreach(c ${link_components})
  112. # add codegen, asmprinter, asmparser, disassembler
  113. list(FIND LLVM_TARGETS_TO_BUILD ${c} idx)
  114. if( NOT idx LESS 0 )
  115. if( TARGET LLVM${c}CodeGen )
  116. list(APPEND expanded_components "${c}CodeGen")
  117. else()
  118. if( TARGET LLVM${c} )
  119. list(APPEND expanded_components "${c}")
  120. else()
  121. message(FATAL_ERROR "Target ${c} is not in the set of libraries.")
  122. endif()
  123. endif()
  124. if( TARGET LLVM${c}AsmPrinter )
  125. list(APPEND expanded_components "${c}AsmPrinter")
  126. endif()
  127. if( TARGET LLVM${c}AsmParser )
  128. list(APPEND expanded_components "${c}AsmParser")
  129. endif()
  130. if( TARGET LLVM${c}Desc )
  131. list(APPEND expanded_components "${c}Desc")
  132. endif()
  133. if( TARGET LLVM${c}Disassembler )
  134. list(APPEND expanded_components "${c}Disassembler")
  135. endif()
  136. if( TARGET LLVM${c}Info )
  137. list(APPEND expanded_components "${c}Info")
  138. endif()
  139. if( TARGET LLVM${c}Utils )
  140. list(APPEND expanded_components "${c}Utils")
  141. endif()
  142. elseif( c STREQUAL "nativecodegen" )
  143. if( TARGET LLVM${LLVM_NATIVE_ARCH}CodeGen )
  144. list(APPEND expanded_components "${LLVM_NATIVE_ARCH}CodeGen")
  145. endif()
  146. if( TARGET LLVM${LLVM_NATIVE_ARCH}Desc )
  147. list(APPEND expanded_components "${LLVM_NATIVE_ARCH}Desc")
  148. endif()
  149. if( TARGET LLVM${LLVM_NATIVE_ARCH}Info )
  150. list(APPEND expanded_components "${LLVM_NATIVE_ARCH}Info")
  151. endif()
  152. elseif( c STREQUAL "AllTargetsCodeGens" )
  153. # Link all the codegens from all the targets
  154. foreach(t ${LLVM_TARGETS_TO_BUILD})
  155. if( TARGET LLVM${t}CodeGen)
  156. list(APPEND expanded_components "${t}CodeGen")
  157. endif()
  158. endforeach(t)
  159. elseif( c STREQUAL "AllTargetsAsmPrinters" )
  160. # Link all the asm printers from all the targets
  161. foreach(t ${LLVM_TARGETS_TO_BUILD})
  162. if( TARGET LLVM${t}AsmPrinter )
  163. list(APPEND expanded_components "${t}AsmPrinter")
  164. endif()
  165. endforeach(t)
  166. elseif( c STREQUAL "AllTargetsAsmParsers" )
  167. # Link all the asm parsers from all the targets
  168. foreach(t ${LLVM_TARGETS_TO_BUILD})
  169. if( TARGET LLVM${t}AsmParser )
  170. list(APPEND expanded_components "${t}AsmParser")
  171. endif()
  172. endforeach(t)
  173. elseif( c STREQUAL "AllTargetsDescs" )
  174. # Link all the descs from all the targets
  175. foreach(t ${LLVM_TARGETS_TO_BUILD})
  176. if( TARGET LLVM${t}Desc )
  177. list(APPEND expanded_components "${t}Desc")
  178. endif()
  179. endforeach(t)
  180. elseif( c STREQUAL "AllTargetsDisassemblers" )
  181. # Link all the disassemblers from all the targets
  182. foreach(t ${LLVM_TARGETS_TO_BUILD})
  183. if( TARGET LLVM${t}Disassembler )
  184. list(APPEND expanded_components "${t}Disassembler")
  185. endif()
  186. endforeach(t)
  187. elseif( c STREQUAL "AllTargetsInfos" )
  188. # Link all the infos from all the targets
  189. foreach(t ${LLVM_TARGETS_TO_BUILD})
  190. if( TARGET LLVM${t}Info )
  191. list(APPEND expanded_components "${t}Info")
  192. endif()
  193. endforeach(t)
  194. else()
  195. list(APPEND expanded_components "${c}")
  196. endif()
  197. endforeach()
  198. set(${out_components} ${expanded_components} PARENT_SCOPE)
  199. endfunction(llvm_expand_pseudo_components out_components)
  200. # This is a variant intended for the final user:
  201. # Map LINK_COMPONENTS to actual libnames.
  202. function(llvm_map_components_to_libnames out_libs)
  203. set( link_components ${ARGN} )
  204. if(NOT LLVM_AVAILABLE_LIBS)
  205. # Inside LLVM itself available libs are in a global property.
  206. get_property(LLVM_AVAILABLE_LIBS GLOBAL PROPERTY LLVM_LIBS)
  207. endif()
  208. string(TOUPPER "${LLVM_AVAILABLE_LIBS}" capitalized_libs)
  209. get_property(LLVM_TARGETS_CONFIGURED GLOBAL PROPERTY LLVM_TARGETS_CONFIGURED)
  210. # Generally in our build system we avoid order-dependence. Unfortunately since
  211. # not all targets create the same set of libraries we actually need to ensure
  212. # that all build targets associated with a target are added before we can
  213. # process target dependencies.
  214. if(NOT LLVM_TARGETS_CONFIGURED)
  215. foreach(c ${link_components})
  216. is_llvm_target_specifier(${c} iltl_result ALL_TARGETS)
  217. if(iltl_result)
  218. message(FATAL_ERROR "Specified target library before target registration is complete.")
  219. endif()
  220. endforeach()
  221. endif()
  222. # Expand some keywords:
  223. list(FIND LLVM_TARGETS_TO_BUILD "${LLVM_NATIVE_ARCH}" have_native_backend)
  224. list(FIND link_components "engine" engine_required)
  225. if( NOT engine_required EQUAL -1 )
  226. list(FIND LLVM_TARGETS_WITH_JIT "${LLVM_NATIVE_ARCH}" have_jit)
  227. if( NOT have_native_backend EQUAL -1 AND NOT have_jit EQUAL -1 )
  228. list(APPEND link_components "jit")
  229. list(APPEND link_components "native")
  230. else()
  231. list(APPEND link_components "interpreter")
  232. endif()
  233. endif()
  234. list(FIND link_components "native" native_required)
  235. if( NOT native_required EQUAL -1 )
  236. if( NOT have_native_backend EQUAL -1 )
  237. list(APPEND link_components ${LLVM_NATIVE_ARCH})
  238. endif()
  239. endif()
  240. # Translate symbolic component names to real libraries:
  241. llvm_expand_pseudo_components(link_components ${link_components})
  242. foreach(c ${link_components})
  243. if( c STREQUAL "native" )
  244. # already processed
  245. elseif( c STREQUAL "backend" )
  246. # same case as in `native'.
  247. elseif( c STREQUAL "engine" )
  248. # already processed
  249. elseif( c STREQUAL "all" )
  250. list(APPEND expanded_components ${LLVM_AVAILABLE_LIBS})
  251. else( NOT idx LESS 0 )
  252. # Canonize the component name:
  253. string(TOUPPER "${c}" capitalized)
  254. list(FIND capitalized_libs LLVM${capitalized} lib_idx)
  255. if( lib_idx LESS 0 )
  256. # The component is unknown. Maybe is an omitted target?
  257. is_llvm_target_library(${c} iltl_result OMITTED_TARGETS)
  258. if(iltl_result)
  259. # A missing library to a directly referenced omitted target would be bad.
  260. message(FATAL_ERROR "Library '${c}' is a direct reference to a target library for an omitted target.")
  261. else()
  262. # If it is not an omitted target we should assume it is a component
  263. # that hasn't yet been processed by CMake. Missing components will
  264. # cause errors later in the configuration, so we can safely assume
  265. # that this is valid here.
  266. list(APPEND expanded_components LLVM${c})
  267. endif()
  268. else( lib_idx LESS 0 )
  269. list(GET LLVM_AVAILABLE_LIBS ${lib_idx} canonical_lib)
  270. list(APPEND expanded_components ${canonical_lib})
  271. endif( lib_idx LESS 0 )
  272. endif( c STREQUAL "native" )
  273. endforeach(c)
  274. set(${out_libs} ${expanded_components} PARENT_SCOPE)
  275. endfunction()
  276. # Perform a post-order traversal of the dependency graph.
  277. # This duplicates the algorithm used by llvm-config, originally
  278. # in tools/llvm-config/llvm-config.cpp, function ComputeLibsForComponents.
  279. function(expand_topologically name required_libs visited_libs)
  280. list(FIND visited_libs ${name} found)
  281. if( found LESS 0 )
  282. list(APPEND visited_libs ${name})
  283. set(visited_libs ${visited_libs} PARENT_SCOPE)
  284. get_property(lib_deps GLOBAL PROPERTY LLVMBUILD_LIB_DEPS_${name})
  285. foreach( lib_dep ${lib_deps} )
  286. expand_topologically(${lib_dep} "${required_libs}" "${visited_libs}")
  287. set(required_libs ${required_libs} PARENT_SCOPE)
  288. set(visited_libs ${visited_libs} PARENT_SCOPE)
  289. endforeach()
  290. list(APPEND required_libs ${name})
  291. set(required_libs ${required_libs} PARENT_SCOPE)
  292. endif()
  293. endfunction()
  294. # Expand dependencies while topologically sorting the list of libraries:
  295. function(llvm_expand_dependencies out_libs)
  296. set(expanded_components ${ARGN})
  297. set(required_libs)
  298. set(visited_libs)
  299. foreach( lib ${expanded_components} )
  300. expand_topologically(${lib} "${required_libs}" "${visited_libs}")
  301. endforeach()
  302. if(required_libs)
  303. list(REVERSE required_libs)
  304. endif()
  305. set(${out_libs} ${required_libs} PARENT_SCOPE)
  306. endfunction()
  307. function(explicit_map_components_to_libraries out_libs)
  308. llvm_map_components_to_libnames(link_libs ${ARGN})
  309. llvm_expand_dependencies(expanded_components ${link_libs})
  310. # Return just the libraries included in this build:
  311. set(result)
  312. foreach(c ${expanded_components})
  313. if( TARGET ${c} )
  314. set(result ${result} ${c})
  315. endif()
  316. endforeach(c)
  317. set(${out_libs} ${result} PARENT_SCOPE)
  318. endfunction(explicit_map_components_to_libraries)