1 function(get_system_libs return_var)
2 # Returns in `return_var' a list of system libraries used by LLVM.
5 set(system_libs ${system_libs} imagehlp psapi shell32)
6 elseif( CMAKE_HOST_UNIX )
8 set(system_libs ${system_libs} rt)
11 set(system_libs ${system_libs} ${CMAKE_DL_LIBS})
13 if(LLVM_ENABLE_TERMINFO)
15 set(system_libs ${system_libs} ${TERMINFO_LIBS})
18 if( LLVM_ENABLE_THREADS AND HAVE_LIBPTHREAD )
19 set(system_libs ${system_libs} pthread)
21 if ( LLVM_ENABLE_ZLIB AND HAVE_LIBZ )
22 set(system_libs ${system_libs} z)
26 set(${return_var} ${system_libs} PARENT_SCOPE)
27 endfunction(get_system_libs)
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)
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)
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)
54 endfunction(is_llvm_target_library)
57 macro(llvm_config executable)
58 explicit_llvm_config(${executable} ${ARGN})
62 function(explicit_llvm_config executable)
63 set( link_components ${ARGN} )
65 explicit_map_components_to_libraries(LIBRARIES ${link_components})
66 target_link_libraries(${executable} ${LIBRARIES})
67 endfunction(explicit_llvm_config)
70 # This is a variant intended for the final user:
71 function(llvm_map_components_to_libraries OUT_VAR)
72 explicit_map_components_to_libraries(result ${ARGN})
73 get_system_libs(sys_result)
74 set( ${OUT_VAR} ${result} ${sys_result} PARENT_SCOPE )
75 endfunction(llvm_map_components_to_libraries)
77 # Map LINK_COMPONENTS to actual libnames.
78 function(llvm_map_components_to_libnames out_libs)
79 set( link_components ${ARGN} )
80 get_property(llvm_libs GLOBAL PROPERTY LLVM_LIBS)
81 string(TOUPPER "${llvm_libs}" capitalized_libs)
83 # Expand some keywords:
84 list(FIND LLVM_TARGETS_TO_BUILD "${LLVM_NATIVE_ARCH}" have_native_backend)
85 list(FIND link_components "engine" engine_required)
86 if( NOT engine_required EQUAL -1 )
87 list(FIND LLVM_TARGETS_WITH_JIT "${LLVM_NATIVE_ARCH}" have_jit)
88 if( NOT have_native_backend EQUAL -1 AND NOT have_jit EQUAL -1 )
89 list(APPEND link_components "jit")
90 list(APPEND link_components "native")
92 list(APPEND link_components "interpreter")
95 list(FIND link_components "native" native_required)
96 if( NOT native_required EQUAL -1 )
97 if( NOT have_native_backend EQUAL -1 )
98 list(APPEND link_components ${LLVM_NATIVE_ARCH})
102 # Translate symbolic component names to real libraries:
103 foreach(c ${link_components})
104 # add codegen, asmprinter, asmparser, disassembler
105 list(FIND LLVM_TARGETS_TO_BUILD ${c} idx)
107 list(FIND llvm_libs "LLVM${c}CodeGen" idx)
109 list(APPEND expanded_components "LLVM${c}CodeGen")
111 list(FIND llvm_libs "LLVM${c}" idx)
113 list(APPEND expanded_components "LLVM${c}")
115 message(FATAL_ERROR "Target ${c} is not in the set of libraries.")
118 list(FIND llvm_libs "LLVM${c}AsmPrinter" asmidx)
119 if( NOT asmidx LESS 0 )
120 list(APPEND expanded_components "LLVM${c}AsmPrinter")
122 list(FIND llvm_libs "LLVM${c}AsmParser" asmidx)
123 if( NOT asmidx LESS 0 )
124 list(APPEND expanded_components "LLVM${c}AsmParser")
126 list(FIND llvm_libs "LLVM${c}Info" asmidx)
127 if( NOT asmidx LESS 0 )
128 list(APPEND expanded_components "LLVM${c}Info")
130 list(FIND llvm_libs "LLVM${c}Disassembler" asmidx)
131 if( NOT asmidx LESS 0 )
132 list(APPEND expanded_components "LLVM${c}Disassembler")
134 elseif( c STREQUAL "native" )
136 elseif( c STREQUAL "nativecodegen" )
137 list(APPEND expanded_components "LLVM${LLVM_NATIVE_ARCH}CodeGen")
138 elseif( c STREQUAL "backend" )
139 # same case as in `native'.
140 elseif( c STREQUAL "engine" )
142 elseif( c STREQUAL "all" )
143 list(APPEND expanded_components ${llvm_libs})
144 else( NOT idx LESS 0 )
145 # Canonize the component name:
146 string(TOUPPER "${c}" capitalized)
147 list(FIND capitalized_libs LLVM${capitalized} lib_idx)
149 # The component is unknown. Maybe is an omitted target?
150 is_llvm_target_library(${c} iltl_result)
151 if( NOT iltl_result )
152 message(FATAL_ERROR "Library `${c}' not found in list of llvm libraries.")
154 else( lib_idx LESS 0 )
155 list(GET llvm_libs ${lib_idx} canonical_lib)
156 list(APPEND expanded_components ${canonical_lib})
157 endif( lib_idx LESS 0 )
158 endif( NOT idx LESS 0 )
161 set(${out_libs} ${expanded_components} PARENT_SCOPE)
164 # Expand dependencies while topologically sorting the list of libraries:
165 function(llvm_expand_dependencies out_libs)
166 set(expanded_components ${ARGN})
167 list(LENGTH expanded_components lst_size)
170 while( cursor LESS lst_size )
171 list(GET expanded_components ${cursor} lib)
172 get_property(lib_deps GLOBAL PROPERTY LLVMBUILD_LIB_DEPS_${lib})
173 list(APPEND expanded_components ${lib_deps})
174 # Remove duplicates at the front:
175 list(REVERSE expanded_components)
176 list(REMOVE_DUPLICATES expanded_components)
177 list(REVERSE expanded_components)
178 list(APPEND processed ${lib})
179 # Find the maximum index that doesn't have to be re-processed:
180 while(NOT "${expanded_components}" MATCHES "^${processed}.*" )
181 list(REMOVE_AT processed -1)
183 list(LENGTH processed cursor)
184 list(LENGTH expanded_components lst_size)
185 endwhile( cursor LESS lst_size )
186 set(${out_libs} ${expanded_components} PARENT_SCOPE)
189 function(explicit_map_components_to_libraries out_libs)
190 llvm_map_components_to_libnames(link_libs ${ARGN})
191 llvm_expand_dependencies(expanded_components ${link_libs})
192 get_property(llvm_libs GLOBAL PROPERTY LLVM_LIBS)
193 # Return just the libraries included in this build:
195 foreach(c ${expanded_components})
196 list(FIND llvm_libs ${c} lib_idx)
197 if( NOT lib_idx LESS 0 )
198 set(result ${result} ${c})
201 set(${out_libs} ${result} PARENT_SCOPE)
202 endfunction(explicit_map_components_to_libraries)