CMake: Moved hard-coded library dependencies to its own file.
[oota-llvm.git] / cmake / modules / LLVMConfig.cmake
1 include(FindPerl)
2 include(LLVMLibDeps)
3
4 function(get_system_libs return_var)
5   # Returns in `return_var' a list of system libraries used by LLVM.
6   if( NOT MSVC )
7     if( MINGW )
8       set(system_libs ${system_libs} imagehlp psapi)
9     elseif( CMAKE_HOST_UNIX )
10       if( HAVE_LIBDL )
11         set(system_libs ${system_libs} dl)
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 macro(llvm_config executable)
23   explicit_llvm_config(${executable} ${ARGN})
24 endmacro(llvm_config)
25
26
27 function(explicit_llvm_config executable)
28   set( link_components ${ARGN} )
29
30   explicit_map_components_to_libraries(LIBRARIES ${link_components})
31   target_link_libraries(${executable} ${LIBRARIES})
32 endfunction(explicit_llvm_config)
33
34
35 function(explicit_map_components_to_libraries out_libs)
36   set( link_components ${ARGN} )
37   foreach(c ${link_components})
38     # add codegen, asmprinter, asmparser
39     list(FIND LLVM_TARGETS_TO_BUILD ${c} idx)
40     if( NOT idx LESS 0 )
41       list(FIND llvm_libs "LLVM${c}CodeGen" idx)
42       if( NOT idx LESS 0 )
43         list(APPEND expanded_components "LLVM${c}CodeGen")
44       else()
45         list(FIND llvm_libs "LLVM${c}" idx)
46         if( NOT idx LESS 0 )
47           list(APPEND expanded_components "LLVM${c}")
48         else()
49           message(FATAL_ERROR "Target ${c} is not in the set of libraries.")
50         endif()
51       endif()
52       list(FIND llvm_libs "LLVM${c}AsmPrinter" asmidx)
53       if( NOT asmidx LESS 0 )
54         list(APPEND expanded_components "LLVM${c}AsmPrinter")
55       endif()
56       list(FIND llvm_libs "LLVM${c}AsmParser" asmidx)
57       if( NOT asmidx LESS 0 )
58         list(APPEND expanded_components "LLVM${c}AsmParser")
59       endif()
60       list(FIND llvm_libs "LLVM${c}Info" asmidx)
61       if( NOT asmidx LESS 0 )
62         list(APPEND expanded_components "LLVM${c}Info")
63       endif()
64     elseif( c STREQUAL "native" )
65       # TODO: we assume ARCH is X86. In this case, we must use nativecodegen
66       # component instead. Do nothing, as in llvm-config script.
67     elseif( c STREQUAL "nativecodegen" )
68       # TODO: we assume ARCH is X86.
69       list(APPEND expanded_components "LLVMX86CodeGen")
70     elseif( c STREQUAL "backend" )
71       # same case as in `native'.
72     elseif( c STREQUAL "engine" )
73       # TODO: as we assume we are on X86, this is `jit'.
74       list(APPEND expanded_components "LLVMJIT")
75     elseif( c STREQUAL "all" )
76       list(APPEND expanded_components ${llvm_libs})
77     else( NOT idx LESS 0 )
78       list(APPEND expanded_components LLVM${c})
79     endif( NOT idx LESS 0 )
80   endforeach(c)
81   # We must match capitalization.
82   string(TOUPPER "${llvm_libs}" capitalized_libs)
83   list(REMOVE_DUPLICATES expanded_components)
84   set(curr_idx 0)
85   list(LENGTH expanded_components lst_size)
86   while( ${curr_idx} LESS ${lst_size} )
87     list(GET expanded_components ${curr_idx} c)
88     string(TOUPPER "${c}" capitalized)
89     list(FIND capitalized_libs ${capitalized} idx)
90     if( idx LESS 0 )
91       message(FATAL_ERROR "Library ${c} not found in list of llvm libraries.")
92     endif( idx LESS 0 )
93     list(GET llvm_libs ${idx} canonical_lib)
94     list(APPEND result ${canonical_lib})
95     list(APPEND result ${MSVC_LIB_DEPS_${canonical_lib}})
96     list(APPEND expanded_components ${MSVC_LIB_DEPS_${canonical_lib}})
97     list(REMOVE_DUPLICATES expanded_components)
98     list(LENGTH expanded_components lst_size)
99     math(EXPR curr_idx "${curr_idx} + 1")
100   endwhile( ${curr_idx} LESS ${lst_size} )
101   list(REMOVE_DUPLICATES result)
102   set(${out_libs} ${result} PARENT_SCOPE)
103 endfunction(explicit_map_components_to_libraries)