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