xref: /netbsd-src/external/apache2/llvm/dist/llvm/cmake/modules/LLVM-Config.cmake (revision 82d56013d7b633d116a93943de88e08335357a7c)
17330f729Sjoergfunction(get_system_libs return_var)
27330f729Sjoerg  message(AUTHOR_WARNING "get_system_libs no longer needed")
37330f729Sjoerg  set(${return_var} "" PARENT_SCOPE)
47330f729Sjoergendfunction()
57330f729Sjoerg
67330f729Sjoerg
77330f729Sjoergfunction(link_system_libs target)
87330f729Sjoerg  message(AUTHOR_WARNING "link_system_libs no longer needed")
97330f729Sjoergendfunction()
107330f729Sjoerg
117330f729Sjoerg# is_llvm_target_library(
127330f729Sjoerg#   library
137330f729Sjoerg#     Name of the LLVM library to check
147330f729Sjoerg#   return_var
157330f729Sjoerg#     Output variable name
167330f729Sjoerg#   ALL_TARGETS;INCLUDED_TARGETS;OMITTED_TARGETS
177330f729Sjoerg#     ALL_TARGETS - default looks at the full list of known targets
187330f729Sjoerg#     INCLUDED_TARGETS - looks only at targets being configured
197330f729Sjoerg#     OMITTED_TARGETS - looks only at targets that are not being configured
207330f729Sjoerg# )
217330f729Sjoergfunction(is_llvm_target_library library return_var)
227330f729Sjoerg  cmake_parse_arguments(ARG "ALL_TARGETS;INCLUDED_TARGETS;OMITTED_TARGETS" "" "" ${ARGN})
237330f729Sjoerg  # Sets variable `return_var' to ON if `library' corresponds to a
247330f729Sjoerg  # LLVM supported target. To OFF if it doesn't.
257330f729Sjoerg  set(${return_var} OFF PARENT_SCOPE)
267330f729Sjoerg  string(TOUPPER "${library}" capitalized_lib)
277330f729Sjoerg  if(ARG_INCLUDED_TARGETS)
287330f729Sjoerg    string(TOUPPER "${LLVM_TARGETS_TO_BUILD}" targets)
297330f729Sjoerg  elseif(ARG_OMITTED_TARGETS)
307330f729Sjoerg    set(omitted_targets ${LLVM_ALL_TARGETS})
31*82d56013Sjoerg    if (LLVM_TARGETS_TO_BUILD)
327330f729Sjoerg      list(REMOVE_ITEM omitted_targets ${LLVM_TARGETS_TO_BUILD})
33*82d56013Sjoerg    endif()
347330f729Sjoerg    string(TOUPPER "${omitted_targets}" targets)
357330f729Sjoerg  else()
367330f729Sjoerg    string(TOUPPER "${LLVM_ALL_TARGETS}" targets)
377330f729Sjoerg  endif()
387330f729Sjoerg  foreach(t ${targets})
397330f729Sjoerg    if( capitalized_lib STREQUAL t OR
407330f729Sjoerg        capitalized_lib STREQUAL "${t}" OR
417330f729Sjoerg        capitalized_lib STREQUAL "${t}DESC" OR
427330f729Sjoerg        capitalized_lib STREQUAL "${t}CODEGEN" OR
437330f729Sjoerg        capitalized_lib STREQUAL "${t}ASMPARSER" OR
447330f729Sjoerg        capitalized_lib STREQUAL "${t}ASMPRINTER" OR
457330f729Sjoerg        capitalized_lib STREQUAL "${t}DISASSEMBLER" OR
467330f729Sjoerg        capitalized_lib STREQUAL "${t}INFO" OR
477330f729Sjoerg        capitalized_lib STREQUAL "${t}UTILS" )
487330f729Sjoerg      set(${return_var} ON PARENT_SCOPE)
497330f729Sjoerg      break()
507330f729Sjoerg    endif()
517330f729Sjoerg  endforeach()
527330f729Sjoergendfunction(is_llvm_target_library)
537330f729Sjoerg
547330f729Sjoergfunction(is_llvm_target_specifier library return_var)
557330f729Sjoerg  is_llvm_target_library(${library} ${return_var} ${ARGN})
567330f729Sjoerg  string(TOUPPER "${library}" capitalized_lib)
577330f729Sjoerg  if(NOT ${return_var})
587330f729Sjoerg    if( capitalized_lib STREQUAL "ALLTARGETSASMPARSERS" OR
597330f729Sjoerg        capitalized_lib STREQUAL "ALLTARGETSDESCS" OR
607330f729Sjoerg        capitalized_lib STREQUAL "ALLTARGETSDISASSEMBLERS" OR
617330f729Sjoerg        capitalized_lib STREQUAL "ALLTARGETSINFOS" OR
627330f729Sjoerg        capitalized_lib STREQUAL "NATIVE" OR
637330f729Sjoerg        capitalized_lib STREQUAL "NATIVECODEGEN" )
647330f729Sjoerg      set(${return_var} ON PARENT_SCOPE)
657330f729Sjoerg    endif()
667330f729Sjoerg  endif()
677330f729Sjoergendfunction()
687330f729Sjoerg
697330f729Sjoergmacro(llvm_config executable)
707330f729Sjoerg  cmake_parse_arguments(ARG "USE_SHARED" "" "" ${ARGN})
717330f729Sjoerg  set(link_components ${ARG_UNPARSED_ARGUMENTS})
727330f729Sjoerg
737330f729Sjoerg  if(ARG_USE_SHARED)
747330f729Sjoerg    # If USE_SHARED is specified, then we link against libLLVM,
757330f729Sjoerg    # but also against the component libraries below. This is
767330f729Sjoerg    # done in case libLLVM does not contain all of the components
777330f729Sjoerg    # the target requires.
787330f729Sjoerg    #
797330f729Sjoerg    # Strip LLVM_DYLIB_COMPONENTS out of link_components.
807330f729Sjoerg    # To do this, we need special handling for "all", since that
817330f729Sjoerg    # may imply linking to libraries that are not included in
827330f729Sjoerg    # libLLVM.
837330f729Sjoerg
847330f729Sjoerg    if (DEFINED link_components AND DEFINED LLVM_DYLIB_COMPONENTS)
857330f729Sjoerg      if("${LLVM_DYLIB_COMPONENTS}" STREQUAL "all")
867330f729Sjoerg        set(link_components "")
877330f729Sjoerg      else()
887330f729Sjoerg        list(REMOVE_ITEM link_components ${LLVM_DYLIB_COMPONENTS})
897330f729Sjoerg      endif()
907330f729Sjoerg    endif()
917330f729Sjoerg
927330f729Sjoerg    target_link_libraries(${executable} PRIVATE LLVM)
937330f729Sjoerg  endif()
947330f729Sjoerg
957330f729Sjoerg  explicit_llvm_config(${executable} ${link_components})
967330f729Sjoergendmacro(llvm_config)
977330f729Sjoerg
987330f729Sjoerg
997330f729Sjoergfunction(explicit_llvm_config executable)
1007330f729Sjoerg  set( link_components ${ARGN} )
1017330f729Sjoerg
1027330f729Sjoerg  llvm_map_components_to_libnames(LIBRARIES ${link_components})
1037330f729Sjoerg  get_target_property(t ${executable} TYPE)
1047330f729Sjoerg  if(t STREQUAL "STATIC_LIBRARY")
1057330f729Sjoerg    target_link_libraries(${executable} INTERFACE ${LIBRARIES})
1067330f729Sjoerg  elseif(t STREQUAL "EXECUTABLE" OR t STREQUAL "SHARED_LIBRARY" OR t STREQUAL "MODULE_LIBRARY")
1077330f729Sjoerg    target_link_libraries(${executable} PRIVATE ${LIBRARIES})
1087330f729Sjoerg  else()
1097330f729Sjoerg    # Use plain form for legacy user.
1107330f729Sjoerg    target_link_libraries(${executable} ${LIBRARIES})
1117330f729Sjoerg  endif()
1127330f729Sjoergendfunction(explicit_llvm_config)
1137330f729Sjoerg
1147330f729Sjoerg
1157330f729Sjoerg# This is Deprecated
1167330f729Sjoergfunction(llvm_map_components_to_libraries OUT_VAR)
1177330f729Sjoerg  message(AUTHOR_WARNING "Using llvm_map_components_to_libraries() is deprecated. Use llvm_map_components_to_libnames() instead")
1187330f729Sjoerg  explicit_map_components_to_libraries(result ${ARGN})
1197330f729Sjoerg  set( ${OUT_VAR} ${result} ${sys_result} PARENT_SCOPE )
1207330f729Sjoergendfunction(llvm_map_components_to_libraries)
1217330f729Sjoerg
1227330f729Sjoerg# Expand pseudo-components into real components.
1237330f729Sjoerg# Does not cover 'native', 'backend', or 'engine' as these require special
1247330f729Sjoerg# handling. Also does not cover 'all' as we only have a list of the libnames
1257330f729Sjoerg# available and not a list of the components.
1267330f729Sjoergfunction(llvm_expand_pseudo_components out_components)
1277330f729Sjoerg  set( link_components ${ARGN} )
1287330f729Sjoerg  foreach(c ${link_components})
1297330f729Sjoerg    # add codegen, asmprinter, asmparser, disassembler
1307330f729Sjoerg    list(FIND LLVM_TARGETS_TO_BUILD ${c} idx)
1317330f729Sjoerg    if( NOT idx LESS 0 )
1327330f729Sjoerg      if( TARGET LLVM${c}CodeGen )
1337330f729Sjoerg        list(APPEND expanded_components "${c}CodeGen")
1347330f729Sjoerg      else()
1357330f729Sjoerg        if( TARGET LLVM${c} )
1367330f729Sjoerg          list(APPEND expanded_components "${c}")
1377330f729Sjoerg        else()
1387330f729Sjoerg          message(FATAL_ERROR "Target ${c} is not in the set of libraries.")
1397330f729Sjoerg        endif()
1407330f729Sjoerg      endif()
1417330f729Sjoerg      if( TARGET LLVM${c}AsmPrinter )
1427330f729Sjoerg        list(APPEND expanded_components "${c}AsmPrinter")
1437330f729Sjoerg      endif()
1447330f729Sjoerg      if( TARGET LLVM${c}AsmParser )
1457330f729Sjoerg        list(APPEND expanded_components "${c}AsmParser")
1467330f729Sjoerg      endif()
1477330f729Sjoerg      if( TARGET LLVM${c}Desc )
1487330f729Sjoerg        list(APPEND expanded_components "${c}Desc")
1497330f729Sjoerg      endif()
1507330f729Sjoerg      if( TARGET LLVM${c}Disassembler )
1517330f729Sjoerg        list(APPEND expanded_components "${c}Disassembler")
1527330f729Sjoerg      endif()
1537330f729Sjoerg      if( TARGET LLVM${c}Info )
1547330f729Sjoerg        list(APPEND expanded_components "${c}Info")
1557330f729Sjoerg      endif()
1567330f729Sjoerg      if( TARGET LLVM${c}Utils )
1577330f729Sjoerg        list(APPEND expanded_components "${c}Utils")
1587330f729Sjoerg      endif()
1597330f729Sjoerg    elseif( c STREQUAL "nativecodegen" )
1607330f729Sjoerg      if( TARGET LLVM${LLVM_NATIVE_ARCH}CodeGen )
1617330f729Sjoerg        list(APPEND expanded_components "${LLVM_NATIVE_ARCH}CodeGen")
1627330f729Sjoerg      endif()
1637330f729Sjoerg      if( TARGET LLVM${LLVM_NATIVE_ARCH}Desc )
1647330f729Sjoerg        list(APPEND expanded_components "${LLVM_NATIVE_ARCH}Desc")
1657330f729Sjoerg      endif()
1667330f729Sjoerg      if( TARGET LLVM${LLVM_NATIVE_ARCH}Info )
1677330f729Sjoerg        list(APPEND expanded_components "${LLVM_NATIVE_ARCH}Info")
1687330f729Sjoerg      endif()
1697330f729Sjoerg    elseif( c STREQUAL "AllTargetsCodeGens" )
1707330f729Sjoerg      # Link all the codegens from all the targets
1717330f729Sjoerg      foreach(t ${LLVM_TARGETS_TO_BUILD})
1727330f729Sjoerg        if( TARGET LLVM${t}CodeGen)
1737330f729Sjoerg          list(APPEND expanded_components "${t}CodeGen")
1747330f729Sjoerg        endif()
1757330f729Sjoerg      endforeach(t)
1767330f729Sjoerg    elseif( c STREQUAL "AllTargetsAsmParsers" )
1777330f729Sjoerg      # Link all the asm parsers from all the targets
1787330f729Sjoerg      foreach(t ${LLVM_TARGETS_TO_BUILD})
1797330f729Sjoerg        if( TARGET LLVM${t}AsmParser )
1807330f729Sjoerg          list(APPEND expanded_components "${t}AsmParser")
1817330f729Sjoerg        endif()
1827330f729Sjoerg      endforeach(t)
1837330f729Sjoerg    elseif( c STREQUAL "AllTargetsDescs" )
1847330f729Sjoerg      # Link all the descs from all the targets
1857330f729Sjoerg      foreach(t ${LLVM_TARGETS_TO_BUILD})
1867330f729Sjoerg        if( TARGET LLVM${t}Desc )
1877330f729Sjoerg          list(APPEND expanded_components "${t}Desc")
1887330f729Sjoerg        endif()
1897330f729Sjoerg      endforeach(t)
1907330f729Sjoerg    elseif( c STREQUAL "AllTargetsDisassemblers" )
1917330f729Sjoerg      # Link all the disassemblers from all the targets
1927330f729Sjoerg      foreach(t ${LLVM_TARGETS_TO_BUILD})
1937330f729Sjoerg        if( TARGET LLVM${t}Disassembler )
1947330f729Sjoerg          list(APPEND expanded_components "${t}Disassembler")
1957330f729Sjoerg        endif()
1967330f729Sjoerg      endforeach(t)
1977330f729Sjoerg    elseif( c STREQUAL "AllTargetsInfos" )
1987330f729Sjoerg      # Link all the infos from all the targets
1997330f729Sjoerg      foreach(t ${LLVM_TARGETS_TO_BUILD})
2007330f729Sjoerg        if( TARGET LLVM${t}Info )
2017330f729Sjoerg          list(APPEND expanded_components "${t}Info")
2027330f729Sjoerg        endif()
2037330f729Sjoerg      endforeach(t)
2047330f729Sjoerg    else()
2057330f729Sjoerg      list(APPEND expanded_components "${c}")
2067330f729Sjoerg    endif()
2077330f729Sjoerg  endforeach()
2087330f729Sjoerg  set(${out_components} ${expanded_components} PARENT_SCOPE)
2097330f729Sjoergendfunction(llvm_expand_pseudo_components out_components)
2107330f729Sjoerg
2117330f729Sjoerg# This is a variant intended for the final user:
2127330f729Sjoerg# Map LINK_COMPONENTS to actual libnames.
2137330f729Sjoergfunction(llvm_map_components_to_libnames out_libs)
2147330f729Sjoerg  set( link_components ${ARGN} )
2157330f729Sjoerg  if(NOT LLVM_AVAILABLE_LIBS)
2167330f729Sjoerg    # Inside LLVM itself available libs are in a global property.
2177330f729Sjoerg    get_property(LLVM_AVAILABLE_LIBS GLOBAL PROPERTY LLVM_LIBS)
2187330f729Sjoerg  endif()
2197330f729Sjoerg  string(TOUPPER "${LLVM_AVAILABLE_LIBS}" capitalized_libs)
2207330f729Sjoerg
2217330f729Sjoerg  get_property(LLVM_TARGETS_CONFIGURED GLOBAL PROPERTY LLVM_TARGETS_CONFIGURED)
2227330f729Sjoerg
2237330f729Sjoerg  # Generally in our build system we avoid order-dependence. Unfortunately since
2247330f729Sjoerg  # not all targets create the same set of libraries we actually need to ensure
2257330f729Sjoerg  # that all build targets associated with a target are added before we can
2267330f729Sjoerg  # process target dependencies.
2277330f729Sjoerg  if(NOT LLVM_TARGETS_CONFIGURED)
2287330f729Sjoerg    foreach(c ${link_components})
2297330f729Sjoerg      is_llvm_target_specifier(${c} iltl_result ALL_TARGETS)
2307330f729Sjoerg      if(iltl_result)
2317330f729Sjoerg        message(FATAL_ERROR "Specified target library before target registration is complete.")
2327330f729Sjoerg      endif()
2337330f729Sjoerg    endforeach()
2347330f729Sjoerg  endif()
2357330f729Sjoerg
2367330f729Sjoerg  # Expand some keywords:
2377330f729Sjoerg  list(FIND LLVM_TARGETS_TO_BUILD "${LLVM_NATIVE_ARCH}" have_native_backend)
2387330f729Sjoerg  list(FIND link_components "engine" engine_required)
2397330f729Sjoerg  if( NOT engine_required EQUAL -1 )
2407330f729Sjoerg    list(FIND LLVM_TARGETS_WITH_JIT "${LLVM_NATIVE_ARCH}" have_jit)
2417330f729Sjoerg    if( NOT have_native_backend EQUAL -1 AND NOT have_jit EQUAL -1 )
2427330f729Sjoerg      list(APPEND link_components "jit")
2437330f729Sjoerg      list(APPEND link_components "native")
2447330f729Sjoerg    else()
2457330f729Sjoerg      list(APPEND link_components "interpreter")
2467330f729Sjoerg    endif()
2477330f729Sjoerg  endif()
2487330f729Sjoerg  list(FIND link_components "native" native_required)
2497330f729Sjoerg  if( NOT native_required EQUAL -1 )
2507330f729Sjoerg    if( NOT have_native_backend EQUAL -1 )
2517330f729Sjoerg      list(APPEND link_components ${LLVM_NATIVE_ARCH})
2527330f729Sjoerg    endif()
2537330f729Sjoerg  endif()
2547330f729Sjoerg
2557330f729Sjoerg  # Translate symbolic component names to real libraries:
2567330f729Sjoerg  llvm_expand_pseudo_components(link_components ${link_components})
2577330f729Sjoerg  foreach(c ${link_components})
258*82d56013Sjoerg    get_property(c_rename GLOBAL PROPERTY LLVM_COMPONENT_NAME_${c})
259*82d56013Sjoerg    if(c_rename)
260*82d56013Sjoerg        set(c ${c_rename})
261*82d56013Sjoerg    endif()
2627330f729Sjoerg    if( c STREQUAL "native" )
2637330f729Sjoerg      # already processed
2647330f729Sjoerg    elseif( c STREQUAL "backend" )
2657330f729Sjoerg      # same case as in `native'.
2667330f729Sjoerg    elseif( c STREQUAL "engine" )
2677330f729Sjoerg      # already processed
2687330f729Sjoerg    elseif( c STREQUAL "all" )
269*82d56013Sjoerg      get_property(all_components GLOBAL PROPERTY LLVM_COMPONENT_LIBS)
270*82d56013Sjoerg      list(APPEND expanded_components ${all_components})
271*82d56013Sjoerg    else()
2727330f729Sjoerg      # Canonize the component name:
2737330f729Sjoerg      string(TOUPPER "${c}" capitalized)
2747330f729Sjoerg      list(FIND capitalized_libs LLVM${capitalized} lib_idx)
2757330f729Sjoerg      if( lib_idx LESS 0 )
2767330f729Sjoerg        # The component is unknown. Maybe is an omitted target?
2777330f729Sjoerg        is_llvm_target_library(${c} iltl_result OMITTED_TARGETS)
2787330f729Sjoerg        if(iltl_result)
2797330f729Sjoerg          # A missing library to a directly referenced omitted target would be bad.
2807330f729Sjoerg          message(FATAL_ERROR "Library '${c}' is a direct reference to a target library for an omitted target.")
2817330f729Sjoerg        else()
2827330f729Sjoerg          # If it is not an omitted target we should assume it is a component
2837330f729Sjoerg          # that hasn't yet been processed by CMake. Missing components will
2847330f729Sjoerg          # cause errors later in the configuration, so we can safely assume
2857330f729Sjoerg          # that this is valid here.
2867330f729Sjoerg          list(APPEND expanded_components LLVM${c})
2877330f729Sjoerg        endif()
2887330f729Sjoerg      else( lib_idx LESS 0 )
2897330f729Sjoerg        list(GET LLVM_AVAILABLE_LIBS ${lib_idx} canonical_lib)
2907330f729Sjoerg        list(APPEND expanded_components ${canonical_lib})
2917330f729Sjoerg      endif( lib_idx LESS 0 )
2927330f729Sjoerg    endif( c STREQUAL "native" )
2937330f729Sjoerg  endforeach(c)
2947330f729Sjoerg
2957330f729Sjoerg  set(${out_libs} ${expanded_components} PARENT_SCOPE)
2967330f729Sjoergendfunction()
2977330f729Sjoerg
2987330f729Sjoerg# Perform a post-order traversal of the dependency graph.
2997330f729Sjoerg# This duplicates the algorithm used by llvm-config, originally
3007330f729Sjoerg# in tools/llvm-config/llvm-config.cpp, function ComputeLibsForComponents.
3017330f729Sjoergfunction(expand_topologically name required_libs visited_libs)
3027330f729Sjoerg  list(FIND visited_libs ${name} found)
3037330f729Sjoerg  if( found LESS 0 )
3047330f729Sjoerg    list(APPEND visited_libs ${name})
3057330f729Sjoerg    set(visited_libs ${visited_libs} PARENT_SCOPE)
3067330f729Sjoerg
307*82d56013Sjoerg    #
308*82d56013Sjoerg    get_property(libname GLOBAL PROPERTY LLVM_COMPONENT_NAME_${name})
309*82d56013Sjoerg    if(libname)
310*82d56013Sjoerg      set(cname LLVM${libname})
311*82d56013Sjoerg    elseif(TARGET ${name})
312*82d56013Sjoerg      set(cname ${name})
313*82d56013Sjoerg    elseif(TARGET LLVM${name})
314*82d56013Sjoerg      set(cname LLVM${name})
315*82d56013Sjoerg    else()
316*82d56013Sjoerg      message(FATAL_ERROR "unknown component ${name}")
317*82d56013Sjoerg    endif()
318*82d56013Sjoerg
319*82d56013Sjoerg    get_property(lib_deps TARGET ${cname} PROPERTY LLVM_LINK_COMPONENTS)
3207330f729Sjoerg    foreach( lib_dep ${lib_deps} )
3217330f729Sjoerg      expand_topologically(${lib_dep} "${required_libs}" "${visited_libs}")
3227330f729Sjoerg      set(required_libs ${required_libs} PARENT_SCOPE)
3237330f729Sjoerg      set(visited_libs ${visited_libs} PARENT_SCOPE)
3247330f729Sjoerg    endforeach()
3257330f729Sjoerg
326*82d56013Sjoerg    list(APPEND required_libs ${cname})
3277330f729Sjoerg    set(required_libs ${required_libs} PARENT_SCOPE)
3287330f729Sjoerg  endif()
3297330f729Sjoergendfunction()
3307330f729Sjoerg
3317330f729Sjoerg# Expand dependencies while topologically sorting the list of libraries:
3327330f729Sjoergfunction(llvm_expand_dependencies out_libs)
3337330f729Sjoerg  set(expanded_components ${ARGN})
3347330f729Sjoerg
3357330f729Sjoerg  set(required_libs)
3367330f729Sjoerg  set(visited_libs)
3377330f729Sjoerg  foreach( lib ${expanded_components} )
3387330f729Sjoerg    expand_topologically(${lib} "${required_libs}" "${visited_libs}")
3397330f729Sjoerg  endforeach()
3407330f729Sjoerg
3417330f729Sjoerg  if(required_libs)
3427330f729Sjoerg    list(REVERSE required_libs)
3437330f729Sjoerg  endif()
3447330f729Sjoerg  set(${out_libs} ${required_libs} PARENT_SCOPE)
3457330f729Sjoergendfunction()
3467330f729Sjoerg
3477330f729Sjoergfunction(explicit_map_components_to_libraries out_libs)
3487330f729Sjoerg  llvm_map_components_to_libnames(link_libs ${ARGN})
3497330f729Sjoerg  llvm_expand_dependencies(expanded_components ${link_libs})
3507330f729Sjoerg  # Return just the libraries included in this build:
3517330f729Sjoerg  set(result)
3527330f729Sjoerg  foreach(c ${expanded_components})
3537330f729Sjoerg    if( TARGET ${c} )
3547330f729Sjoerg      set(result ${result} ${c})
3557330f729Sjoerg    endif()
3567330f729Sjoerg  endforeach(c)
3577330f729Sjoerg  set(${out_libs} ${result} PARENT_SCOPE)
3587330f729Sjoergendfunction(explicit_map_components_to_libraries)
359