[cmake] Unbreak LLVM-Config.cmake / llvm_expand_dependencies.
[oota-llvm.git] / cmake / modules / LLVM-Config.cmake
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
6
7 function(link_system_libs target)
8   message(AUTHOR_WARNING "link_system_libs no longer needed")
9 endfunction()
10
11
12 function(is_llvm_target_library library return_var)
13   # Sets variable `return_var' to ON if `library' corresponds to a
14   # LLVM supported target. To OFF if it doesn't.
15   set(${return_var} OFF PARENT_SCOPE)
16   string(TOUPPER "${library}" capitalized_lib)
17   string(TOUPPER "${LLVM_ALL_TARGETS}" targets)
18   foreach(t ${targets})
19     if( capitalized_lib STREQUAL t OR
20         capitalized_lib STREQUAL "LLVM${t}" OR
21         capitalized_lib STREQUAL "LLVM${t}CODEGEN" OR
22         capitalized_lib STREQUAL "LLVM${t}ASMPARSER" OR
23         capitalized_lib STREQUAL "LLVM${t}ASMPRINTER" OR
24         capitalized_lib STREQUAL "LLVM${t}DISASSEMBLER" OR
25         capitalized_lib STREQUAL "LLVM${t}INFO" )
26       set(${return_var} ON PARENT_SCOPE)
27       break()
28     endif()
29   endforeach()
30 endfunction(is_llvm_target_library)
31
32
33 macro(llvm_config executable)
34   explicit_llvm_config(${executable} ${ARGN})
35 endmacro(llvm_config)
36
37
38 function(explicit_llvm_config executable)
39   set( link_components ${ARGN} )
40
41   llvm_map_components_to_libnames(LIBRARIES ${link_components})
42   get_target_property(t ${executable} TYPE)
43   if("x${t}" STREQUAL "xSTATIC_LIBRARY")
44     target_link_libraries(${executable} ${cmake_2_8_12_INTERFACE} ${LIBRARIES})
45   elseif("x${t}" STREQUAL "xSHARED_LIBRARY" OR "x${t}" STREQUAL "xMODULE_LIBRARY")
46     target_link_libraries(${executable} ${cmake_2_8_12_PRIVATE} ${LIBRARIES})
47   else()
48     # Use plain form for legacy user.
49     target_link_libraries(${executable} ${LIBRARIES})
50   endif()
51 endfunction(explicit_llvm_config)
52
53
54 # This is Deprecated
55 function(llvm_map_components_to_libraries OUT_VAR)
56   message(AUTHOR_WARNING "Using llvm_map_components_to_libraries() is deprecated. Use llvm_map_components_to_libnames() instead")
57   explicit_map_components_to_libraries(result ${ARGN})
58   set( ${OUT_VAR} ${result} ${sys_result} PARENT_SCOPE )
59 endfunction(llvm_map_components_to_libraries)
60
61 # This is a variant intended for the final user:
62 # Map LINK_COMPONENTS to actual libnames.
63 function(llvm_map_components_to_libnames out_libs)
64   set( link_components ${ARGN} )
65   if(NOT LLVM_AVAILABLE_LIBS)
66     # Inside LLVM itself available libs are in a global property.
67     get_property(LLVM_AVAILABLE_LIBS GLOBAL PROPERTY LLVM_LIBS)
68   endif()
69   string(TOUPPER "${LLVM_AVAILABLE_LIBS}" capitalized_libs)
70
71   # Expand some keywords:
72   list(FIND LLVM_TARGETS_TO_BUILD "${LLVM_NATIVE_ARCH}" have_native_backend)
73   list(FIND link_components "engine" engine_required)
74   if( NOT engine_required EQUAL -1 )
75     list(FIND LLVM_TARGETS_WITH_JIT "${LLVM_NATIVE_ARCH}" have_jit)
76     if( NOT have_native_backend EQUAL -1 AND NOT have_jit EQUAL -1 )
77       list(APPEND link_components "jit")
78       list(APPEND link_components "native")
79     else()
80       list(APPEND link_components "interpreter")
81     endif()
82   endif()
83   list(FIND link_components "native" native_required)
84   if( NOT native_required EQUAL -1 )
85     if( NOT have_native_backend EQUAL -1 )
86       list(APPEND link_components ${LLVM_NATIVE_ARCH})
87     endif()
88   endif()
89
90   # Translate symbolic component names to real libraries:
91   foreach(c ${link_components})
92     # add codegen, asmprinter, asmparser, disassembler
93     list(FIND LLVM_TARGETS_TO_BUILD ${c} idx)
94     if( NOT idx LESS 0 )
95       if( TARGET LLVM${c}CodeGen )
96         list(APPEND expanded_components "LLVM${c}CodeGen")
97       else()
98         if( TARGET LLVM${c} )
99           list(APPEND expanded_components "LLVM${c}")
100         else()
101           message(FATAL_ERROR "Target ${c} is not in the set of libraries.")
102         endif()
103       endif()
104       if( TARGET LLVM${c}AsmPrinter )
105         list(APPEND expanded_components "LLVM${c}AsmPrinter")
106       endif()
107       if( TARGET LLVM${c}AsmParser )
108         list(APPEND expanded_components "LLVM${c}AsmParser")
109       endif()
110       if( TARGET LLVM${c}Desc )
111         list(APPEND expanded_components "LLVM${c}Desc")
112       endif()
113       if( TARGET LLVM${c}Info )
114         list(APPEND expanded_components "LLVM${c}Info")
115       endif()
116       if( TARGET LLVM${c}Disassembler )
117         list(APPEND expanded_components "LLVM${c}Disassembler")
118       endif()
119     elseif( c STREQUAL "native" )
120       # already processed
121     elseif( c STREQUAL "nativecodegen" )
122       list(APPEND expanded_components "LLVM${LLVM_NATIVE_ARCH}CodeGen")
123       if( TARGET LLVM${LLVM_NATIVE_ARCH}Desc )
124         list(APPEND expanded_components "LLVM${LLVM_NATIVE_ARCH}Desc")
125       endif()
126       if( TARGET LLVM${LLVM_NATIVE_ARCH}Info )
127         list(APPEND expanded_components "LLVM${LLVM_NATIVE_ARCH}Info")
128       endif()
129     elseif( c STREQUAL "backend" )
130       # same case as in `native'.
131     elseif( c STREQUAL "engine" )
132       # already processed
133     elseif( c STREQUAL "all" )
134       list(APPEND expanded_components ${LLVM_AVAILABLE_LIBS})
135     else( NOT idx LESS 0 )
136       # Canonize the component name:
137       string(TOUPPER "${c}" capitalized)
138       list(FIND capitalized_libs LLVM${capitalized} lib_idx)
139       if( lib_idx LESS 0 )
140         # The component is unknown. Maybe is an omitted target?
141         is_llvm_target_library(${c} iltl_result)
142         if( NOT iltl_result )
143           message(FATAL_ERROR "Library `${c}' not found in list of llvm libraries.")
144         endif()
145       else( lib_idx LESS 0 )
146         list(GET LLVM_AVAILABLE_LIBS ${lib_idx} canonical_lib)
147         list(APPEND expanded_components ${canonical_lib})
148       endif( lib_idx LESS 0 )
149     endif( NOT idx LESS 0 )
150   endforeach(c)
151
152   set(${out_libs} ${expanded_components} PARENT_SCOPE)
153 endfunction()
154
155 # Perform a post-order traversal of the dependency graph.
156 # This duplicates the algorithm used by llvm-config, originally
157 # in tools/llvm-config/llvm-config.cpp, function ComputeLibsForComponents.
158 function(expand_topologically name required_libs visited_libs)
159   list(FIND visited_libs ${name} found)
160   if( found LESS 0 )
161     list(APPEND visited_libs ${name})
162     set(visited_libs ${visited_libs} PARENT_SCOPE)
163
164     get_property(lib_deps GLOBAL PROPERTY LLVMBUILD_LIB_DEPS_${name})
165     foreach( lib_dep ${lib_deps} )
166       expand_topologically(${lib_dep} "${required_libs}" "${visited_libs}")
167       set(required_libs ${required_libs} PARENT_SCOPE)
168       set(visited_libs ${visited_libs} PARENT_SCOPE)
169     endforeach()
170
171     list(APPEND required_libs ${name})
172     set(required_libs ${required_libs} PARENT_SCOPE)
173   endif()
174 endfunction()
175
176 # Expand dependencies while topologically sorting the list of libraries:
177 function(llvm_expand_dependencies out_libs)
178   set(expanded_components ${ARGN})
179
180   set(required_libs)
181   set(visited_libs)
182   foreach( lib ${expanded_components} )
183     expand_topologically(${lib} "${required_libs}" "${visited_libs}")
184   endforeach()
185
186   list(REVERSE required_libs)
187   set(${out_libs} ${required_libs} PARENT_SCOPE)
188 endfunction()
189
190 function(explicit_map_components_to_libraries out_libs)
191   llvm_map_components_to_libnames(link_libs ${ARGN})
192   llvm_expand_dependencies(expanded_components ${link_libs})
193   # Return just the libraries included in this build:
194   set(result)
195   foreach(c ${expanded_components})
196     if( TARGET ${c} )
197       set(result ${result} ${c})
198     endif()
199   endforeach(c)
200   set(${out_libs} ${result} PARENT_SCOPE)
201 endfunction(explicit_map_components_to_libraries)