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)
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_THREADS AND HAVE_LIBPTHREAD )
14 set(system_libs ${system_libs} pthread)
18 set(${return_var} ${system_libs} PARENT_SCOPE)
19 endfunction(get_system_libs)
22 function(link_system_libs target)
23 get_system_libs(llvm_system_libs)
24 target_link_libraries(${target} ${llvm_system_libs})
25 endfunction(link_system_libs)
28 function(is_llvm_target_library library return_var)
29 # Sets variable `return_var' to ON if `library' corresponds to a
30 # LLVM supported target. To OFF if it doesn't.
31 set(${return_var} OFF PARENT_SCOPE)
32 string(TOUPPER "${library}" capitalized_lib)
33 string(TOUPPER "${LLVM_ALL_TARGETS}" targets)
35 if( capitalized_lib STREQUAL t OR
36 capitalized_lib STREQUAL "LLVM${t}" OR
37 capitalized_lib STREQUAL "LLVM${t}CODEGEN" OR
38 capitalized_lib STREQUAL "LLVM${t}ASMPARSER" OR
39 capitalized_lib STREQUAL "LLVM${t}ASMPRINTER" OR
40 capitalized_lib STREQUAL "LLVM${t}DISASSEMBLER" OR
41 capitalized_lib STREQUAL "LLVM${t}INFO" )
42 set(${return_var} ON PARENT_SCOPE)
46 endfunction(is_llvm_target_library)
49 macro(llvm_config executable)
50 explicit_llvm_config(${executable} ${ARGN})
54 function(explicit_llvm_config executable)
55 set( link_components ${ARGN} )
57 explicit_map_components_to_libraries(LIBRARIES ${link_components})
58 target_link_libraries(${executable} ${LIBRARIES})
59 endfunction(explicit_llvm_config)
62 # This is a variant intended for the final user:
63 function(llvm_map_components_to_libraries OUT_VAR)
64 explicit_map_components_to_libraries(result ${ARGN})
65 get_system_libs(sys_result)
66 set( ${OUT_VAR} ${result} ${sys_result} PARENT_SCOPE )
67 endfunction(llvm_map_components_to_libraries)
70 function(explicit_map_components_to_libraries out_libs)
71 set( link_components ${ARGN} )
72 get_property(llvm_libs GLOBAL PROPERTY LLVM_LIBS)
73 string(TOUPPER "${llvm_libs}" capitalized_libs)
75 # Expand some keywords:
76 list(FIND LLVM_TARGETS_TO_BUILD "${LLVM_NATIVE_ARCH}" have_native_backend)
77 list(FIND link_components "engine" engine_required)
78 if( NOT engine_required EQUAL -1 )
79 list(FIND LLVM_TARGETS_WITH_JIT "${LLVM_NATIVE_ARCH}" have_jit)
80 if( NOT have_native_backend EQUAL -1 AND NOT have_jit EQUAL -1 )
81 list(APPEND link_components "jit")
82 list(APPEND link_components "native")
84 list(APPEND link_components "interpreter")
87 list(FIND link_components "native" native_required)
88 if( NOT native_required EQUAL -1 )
89 if( NOT have_native_backend EQUAL -1 )
90 list(APPEND link_components ${LLVM_NATIVE_ARCH})
94 # Translate symbolic component names to real libraries:
95 foreach(c ${link_components})
96 # add codegen, asmprinter, asmparser, disassembler
97 list(FIND LLVM_TARGETS_TO_BUILD ${c} idx)
99 list(FIND llvm_libs "LLVM${c}CodeGen" idx)
101 list(APPEND expanded_components "LLVM${c}CodeGen")
103 list(FIND llvm_libs "LLVM${c}" idx)
105 list(APPEND expanded_components "LLVM${c}")
107 message(FATAL_ERROR "Target ${c} is not in the set of libraries.")
110 list(FIND llvm_libs "LLVM${c}AsmPrinter" asmidx)
111 if( NOT asmidx LESS 0 )
112 list(APPEND expanded_components "LLVM${c}AsmPrinter")
114 list(FIND llvm_libs "LLVM${c}AsmParser" asmidx)
115 if( NOT asmidx LESS 0 )
116 list(APPEND expanded_components "LLVM${c}AsmParser")
118 list(FIND llvm_libs "LLVM${c}Info" asmidx)
119 if( NOT asmidx LESS 0 )
120 list(APPEND expanded_components "LLVM${c}Info")
122 list(FIND llvm_libs "LLVM${c}Disassembler" asmidx)
123 if( NOT asmidx LESS 0 )
124 list(APPEND expanded_components "LLVM${c}Disassembler")
126 elseif( c STREQUAL "native" )
128 elseif( c STREQUAL "nativecodegen" )
129 list(APPEND expanded_components "LLVM${LLVM_NATIVE_ARCH}CodeGen")
130 elseif( c STREQUAL "backend" )
131 # same case as in `native'.
132 elseif( c STREQUAL "engine" )
134 elseif( c STREQUAL "all" )
135 list(APPEND expanded_components ${llvm_libs})
136 else( NOT idx LESS 0 )
137 # Canonize the component name:
138 string(TOUPPER "${c}" capitalized)
139 list(FIND capitalized_libs LLVM${capitalized} lib_idx)
141 # The component is unknown. Maybe is an omitted target?
142 is_llvm_target_library(${c} iltl_result)
143 if( NOT iltl_result )
144 message(FATAL_ERROR "Library `${c}' not found in list of llvm libraries.")
146 else( lib_idx LESS 0 )
147 list(GET llvm_libs ${lib_idx} canonical_lib)
148 list(APPEND expanded_components ${canonical_lib})
149 endif( lib_idx LESS 0 )
150 endif( NOT idx LESS 0 )
152 # Expand dependencies while topologically sorting the list of libraries:
153 list(LENGTH expanded_components lst_size)
156 while( cursor LESS lst_size )
157 list(GET expanded_components ${cursor} lib)
158 get_property(lib_deps GLOBAL PROPERTY LLVMBUILD_LIB_DEPS_${lib})
159 list(APPEND expanded_components ${lib_deps})
160 # Remove duplicates at the front:
161 list(REVERSE expanded_components)
162 list(REMOVE_DUPLICATES expanded_components)
163 list(REVERSE expanded_components)
164 list(APPEND processed ${lib})
165 # Find the maximum index that doesn't have to be re-processed:
166 while(NOT "${expanded_components}" MATCHES "^${processed}.*" )
167 list(REMOVE_AT processed -1)
169 list(LENGTH processed cursor)
170 list(LENGTH expanded_components lst_size)
171 endwhile( cursor LESS lst_size )
172 # Return just the libraries included in this build:
174 foreach(c ${expanded_components})
175 list(FIND llvm_libs ${c} lib_idx)
176 if( NOT lib_idx LESS 0 )
177 set(result ${result} ${c})
180 set(${out_libs} ${result} PARENT_SCOPE)
181 endfunction(explicit_map_components_to_libraries)