451fc56c6bfafb14a4d75c454e2f14a187b4b20c
[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   target_link_libraries(${executable} ${LIBRARIES})
43 endfunction(explicit_llvm_config)
44
45
46 # This is a variant intended for the final user:
47 function(llvm_map_components_to_libraries OUT_VAR)
48   explicit_map_components_to_libraries(result ${ARGN})
49   set( ${OUT_VAR} ${result} ${sys_result} PARENT_SCOPE )
50 endfunction(llvm_map_components_to_libraries)
51
52 # Map LINK_COMPONENTS to actual libnames.
53 function(llvm_map_components_to_libnames out_libs)
54   set( link_components ${ARGN} )
55   if(NOT LLVM_AVAILABLE_LIBS)
56     # Inside LLVM itself available libs are in a global property.
57     get_property(LLVM_AVAILABLE_LIBS GLOBAL PROPERTY LLVM_LIBS)
58   endif()
59   string(TOUPPER "${LLVM_AVAILABLE_LIBS}" capitalized_libs)
60
61   # Expand some keywords:
62   list(FIND LLVM_TARGETS_TO_BUILD "${LLVM_NATIVE_ARCH}" have_native_backend)
63   list(FIND link_components "engine" engine_required)
64   if( NOT engine_required EQUAL -1 )
65     list(FIND LLVM_TARGETS_WITH_JIT "${LLVM_NATIVE_ARCH}" have_jit)
66     if( NOT have_native_backend EQUAL -1 AND NOT have_jit EQUAL -1 )
67       list(APPEND link_components "jit")
68       list(APPEND link_components "native")
69     else()
70       list(APPEND link_components "interpreter")
71     endif()
72   endif()
73   list(FIND link_components "native" native_required)
74   if( NOT native_required EQUAL -1 )
75     if( NOT have_native_backend EQUAL -1 )
76       list(APPEND link_components ${LLVM_NATIVE_ARCH})
77     endif()
78   endif()
79
80   # Translate symbolic component names to real libraries:
81   foreach(c ${link_components})
82     # add codegen, asmprinter, asmparser, disassembler
83     list(FIND LLVM_TARGETS_TO_BUILD ${c} idx)
84     if( NOT idx LESS 0 )
85       if( TARGET LLVM${c}CodeGen )
86         list(APPEND expanded_components "LLVM${c}CodeGen")
87       else()
88         if( TARGET LLVM${c} )
89           list(APPEND expanded_components "LLVM${c}")
90         else()
91           message(FATAL_ERROR "Target ${c} is not in the set of libraries.")
92         endif()
93       endif()
94       if( TARGET LLVM${c}AsmPrinter )
95         list(APPEND expanded_components "LLVM${c}AsmPrinter")
96       endif()
97       if( TARGET LLVM${c}AsmParser )
98         list(APPEND expanded_components "LLVM${c}AsmParser")
99       endif()
100       if( TARGET LLVM${c}Info )
101         list(APPEND expanded_components "LLVM${c}Info")
102       endif()
103       if( TARGET LLVM${c}Disassembler )
104         list(APPEND expanded_components "LLVM${c}Disassembler")
105       endif()
106     elseif( c STREQUAL "native" )
107       # already processed
108     elseif( c STREQUAL "nativecodegen" )
109       list(APPEND expanded_components "LLVM${LLVM_NATIVE_ARCH}CodeGen")
110     elseif( c STREQUAL "backend" )
111       # same case as in `native'.
112     elseif( c STREQUAL "engine" )
113       # already processed
114     elseif( c STREQUAL "all" )
115       list(APPEND expanded_components ${LLVM_AVAILABLE_LIBS})
116     else( NOT idx LESS 0 )
117       # Canonize the component name:
118       string(TOUPPER "${c}" capitalized)
119       list(FIND capitalized_libs LLVM${capitalized} lib_idx)
120       if( lib_idx LESS 0 )
121         # The component is unknown. Maybe is an omitted target?
122         is_llvm_target_library(${c} iltl_result)
123         if( NOT iltl_result )
124           message(FATAL_ERROR "Library `${c}' not found in list of llvm libraries.")
125         endif()
126       else( lib_idx LESS 0 )
127         list(GET LLVM_AVAILABLE_LIBS ${lib_idx} canonical_lib)
128         list(APPEND expanded_components ${canonical_lib})
129       endif( lib_idx LESS 0 )
130     endif( NOT idx LESS 0 )
131   endforeach(c)
132
133   set(${out_libs} ${expanded_components} PARENT_SCOPE)
134 endfunction()
135
136 # Expand dependencies while topologically sorting the list of libraries:
137 function(llvm_expand_dependencies out_libs)
138   set(expanded_components ${ARGN})
139   list(LENGTH expanded_components lst_size)
140   set(cursor 0)
141   set(processed)
142   while( cursor LESS lst_size )
143     list(GET expanded_components ${cursor} lib)
144     get_property(lib_deps GLOBAL PROPERTY LLVMBUILD_LIB_DEPS_${lib})
145     list(APPEND expanded_components ${lib_deps})
146     # Remove duplicates at the front:
147     list(REVERSE expanded_components)
148     list(REMOVE_DUPLICATES expanded_components)
149     list(REVERSE expanded_components)
150     list(APPEND processed ${lib})
151     # Find the maximum index that doesn't have to be re-processed:
152     while(NOT "${expanded_components}" MATCHES "^${processed}.*" )
153       list(REMOVE_AT processed -1)
154     endwhile()
155     list(LENGTH processed cursor)
156     list(LENGTH expanded_components lst_size)
157   endwhile( cursor LESS lst_size )
158   set(${out_libs} ${expanded_components} PARENT_SCOPE)
159 endfunction()
160
161 function(explicit_map_components_to_libraries out_libs)
162   llvm_map_components_to_libnames(link_libs ${ARGN})
163   llvm_expand_dependencies(expanded_components ${link_libs})
164   # Return just the libraries included in this build:
165   set(result)
166   foreach(c ${expanded_components})
167     if( TARGET ${c} )
168       set(result ${result} ${c})
169     endif()
170   endforeach(c)
171   set(${out_libs} ${result} PARENT_SCOPE)
172 endfunction(explicit_map_components_to_libraries)