Home | History | Annotate | Download | only in modules
      1 function(get_system_libs return_var)
      2   # Returns in `return_var' a list of system libraries used by LLVM.
      3   if( NOT MSVC )
      4     if( MINGW )
      5       set(system_libs ${system_libs} imagehlp psapi)
      6     elseif( CMAKE_HOST_UNIX )
      7       if( HAVE_LIBRT )
      8         set(system_libs ${system_libs} rt)
      9       endif()
     10       if( HAVE_LIBDL )
     11         set(system_libs ${system_libs} ${CMAKE_DL_LIBS})
     12       endif()
     13       if( LLVM_ENABLE_THREADS AND HAVE_LIBPTHREAD )
     14         set(system_libs ${system_libs} pthread)
     15       endif()
     16     endif( MINGW )
     17   endif( NOT MSVC )
     18   set(${return_var} ${system_libs} PARENT_SCOPE)
     19 endfunction(get_system_libs)
     20 
     21 
     22 function(link_system_libs target)
     23   get_system_libs(llvm_system_libs)
     24   target_link_libraries(${target} ${llvm_system_libs})
     25 endfunction(link_system_libs)
     26 
     27 
     28 function(is_llvm_target_library library return_var)
     29   # Sets variable `return_var' to ON if `library' corresponds to a
     30   # LLVM supported target. To OFF if it doesn't.
     31   set(${return_var} OFF PARENT_SCOPE)
     32   string(TOUPPER "${library}" capitalized_lib)
     33   string(TOUPPER "${LLVM_ALL_TARGETS}" targets)
     34   foreach(t ${targets})
     35     if( capitalized_lib STREQUAL t OR
     36 	capitalized_lib STREQUAL "LLVM${t}" OR
     37 	capitalized_lib STREQUAL "LLVM${t}CODEGEN" OR
     38 	capitalized_lib STREQUAL "LLVM${t}ASMPARSER" OR
     39 	capitalized_lib STREQUAL "LLVM${t}ASMPRINTER" OR
     40 	capitalized_lib STREQUAL "LLVM${t}DISASSEMBLER" OR
     41 	capitalized_lib STREQUAL "LLVM${t}INFO" )
     42       set(${return_var} ON PARENT_SCOPE)
     43       break()
     44     endif()
     45   endforeach()
     46 endfunction(is_llvm_target_library)
     47 
     48 
     49 macro(llvm_config executable)
     50   explicit_llvm_config(${executable} ${ARGN})
     51 endmacro(llvm_config)
     52 
     53 
     54 function(explicit_llvm_config executable)
     55   set( link_components ${ARGN} )
     56 
     57   explicit_map_components_to_libraries(LIBRARIES ${link_components})
     58   target_link_libraries(${executable} ${LIBRARIES})
     59 endfunction(explicit_llvm_config)
     60 
     61 
     62 # This is a variant intended for the final user:
     63 function(llvm_map_components_to_libraries OUT_VAR)
     64   explicit_map_components_to_libraries(result ${ARGN})
     65   get_system_libs(sys_result)
     66   set( ${OUT_VAR} ${result} ${sys_result} PARENT_SCOPE )
     67 endfunction(llvm_map_components_to_libraries)
     68 
     69 
     70 function(explicit_map_components_to_libraries out_libs)
     71   set( link_components ${ARGN} )
     72   get_property(llvm_libs GLOBAL PROPERTY LLVM_LIBS)
     73   string(TOUPPER "${llvm_libs}" capitalized_libs)
     74 
     75   # Expand some keywords:
     76   list(FIND LLVM_TARGETS_TO_BUILD "${LLVM_NATIVE_ARCH}" have_native_backend)
     77   list(FIND link_components "engine" engine_required)
     78   if( NOT engine_required EQUAL -1 )
     79     list(FIND LLVM_TARGETS_WITH_JIT "${LLVM_NATIVE_ARCH}" have_jit)
     80     if( NOT have_native_backend EQUAL -1 AND NOT have_jit EQUAL -1 )
     81       list(APPEND link_components "jit")
     82       list(APPEND link_components "native")
     83     else()
     84       list(APPEND link_components "interpreter")
     85     endif()
     86   endif()
     87   list(FIND link_components "native" native_required)
     88   if( NOT native_required EQUAL -1 )
     89     if( NOT have_native_backend EQUAL -1 )
     90       list(APPEND link_components ${LLVM_NATIVE_ARCH})
     91     endif()
     92   endif()
     93 
     94   # Translate symbolic component names to real libraries:
     95   foreach(c ${link_components})
     96     # add codegen, asmprinter, asmparser, disassembler
     97     list(FIND LLVM_TARGETS_TO_BUILD ${c} idx)
     98     if( NOT idx LESS 0 )
     99       list(FIND llvm_libs "LLVM${c}CodeGen" idx)
    100       if( NOT idx LESS 0 )
    101 	list(APPEND expanded_components "LLVM${c}CodeGen")
    102       else()
    103 	list(FIND llvm_libs "LLVM${c}" idx)
    104 	if( NOT idx LESS 0 )
    105 	  list(APPEND expanded_components "LLVM${c}")
    106 	else()
    107 	  message(FATAL_ERROR "Target ${c} is not in the set of libraries.")
    108 	endif()
    109       endif()
    110       list(FIND llvm_libs "LLVM${c}AsmPrinter" asmidx)
    111       if( NOT asmidx LESS 0 )
    112         list(APPEND expanded_components "LLVM${c}AsmPrinter")
    113       endif()
    114       list(FIND llvm_libs "LLVM${c}AsmParser" asmidx)
    115       if( NOT asmidx LESS 0 )
    116         list(APPEND expanded_components "LLVM${c}AsmParser")
    117       endif()
    118       list(FIND llvm_libs "LLVM${c}Info" asmidx)
    119       if( NOT asmidx LESS 0 )
    120         list(APPEND expanded_components "LLVM${c}Info")
    121       endif()
    122       list(FIND llvm_libs "LLVM${c}Disassembler" asmidx)
    123       if( NOT asmidx LESS 0 )
    124         list(APPEND expanded_components "LLVM${c}Disassembler")
    125       endif()
    126     elseif( c STREQUAL "native" )
    127       # already processed
    128     elseif( c STREQUAL "nativecodegen" )
    129       list(APPEND expanded_components "LLVM${LLVM_NATIVE_ARCH}CodeGen")
    130     elseif( c STREQUAL "backend" )
    131       # same case as in `native'.
    132     elseif( c STREQUAL "engine" )
    133       # already processed
    134     elseif( c STREQUAL "all" )
    135       list(APPEND expanded_components ${llvm_libs})
    136     else( NOT idx LESS 0 )
    137       # Canonize the component name:
    138       string(TOUPPER "${c}" capitalized)
    139       list(FIND capitalized_libs LLVM${capitalized} lib_idx)
    140       if( lib_idx LESS 0 )
    141         # The component is unknown. Maybe is an omitted target?
    142         is_llvm_target_library(${c} iltl_result)
    143         if( NOT iltl_result )
    144           message(FATAL_ERROR "Library `${c}' not found in list of llvm libraries.")
    145         endif()
    146       else( lib_idx LESS 0 )
    147         list(GET llvm_libs ${lib_idx} canonical_lib)
    148         list(APPEND expanded_components ${canonical_lib})
    149       endif( lib_idx LESS 0 )
    150     endif( NOT idx LESS 0 )
    151   endforeach(c)
    152   # Expand dependencies while topologically sorting the list of libraries:
    153   list(LENGTH expanded_components lst_size)
    154   set(cursor 0)
    155   set(processed)
    156   while( cursor LESS lst_size )
    157     list(GET expanded_components ${cursor} lib)
    158     get_property(lib_deps GLOBAL PROPERTY LLVMBUILD_LIB_DEPS_${lib})
    159     list(APPEND expanded_components ${lib_deps})
    160     # Remove duplicates at the front:
    161     list(REVERSE expanded_components)
    162     list(REMOVE_DUPLICATES expanded_components)
    163     list(REVERSE expanded_components)
    164     list(APPEND processed ${lib})
    165     # Find the maximum index that doesn't have to be re-processed:
    166     while(NOT "${expanded_components}" MATCHES "^${processed}.*" )
    167       list(REMOVE_AT processed -1)
    168     endwhile()
    169     list(LENGTH processed cursor)
    170     list(LENGTH expanded_components lst_size)
    171   endwhile( cursor LESS lst_size )
    172   # Return just the libraries included in this build:
    173   set(result)
    174   foreach(c ${expanded_components})
    175     list(FIND llvm_libs ${c} lib_idx)
    176     if( NOT lib_idx LESS 0 )
    177       set(result ${result} ${c})
    178     endif()
    179   endforeach(c)
    180   set(${out_libs} ${result} PARENT_SCOPE)
    181 endfunction(explicit_map_components_to_libraries)
    182