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)
16 if ( LLVM_ENABLE_ZLIB AND HAVE_LIBZ )
17 set(system_libs ${system_libs} z)
21 set(${return_var} ${system_libs} PARENT_SCOPE)
22 endfunction(get_system_libs)
25 function(link_system_libs target)
26 get_system_libs(llvm_system_libs)
27 target_link_libraries(${target} ${llvm_system_libs})
28 endfunction(link_system_libs)
31 function(is_llvm_target_library library return_var)
32 # Sets variable `return_var' to ON if `library' corresponds to a
33 # LLVM supported target. To OFF if it doesn't.
34 set(${return_var} OFF PARENT_SCOPE)
35 string(TOUPPER "${library}" capitalized_lib)
36 string(TOUPPER "${LLVM_ALL_TARGETS}" targets)
38 if( capitalized_lib STREQUAL t OR
39 capitalized_lib STREQUAL "LLVM${t}" OR
40 capitalized_lib STREQUAL "LLVM${t}CODEGEN" OR
41 capitalized_lib STREQUAL "LLVM${t}ASMPARSER" OR
42 capitalized_lib STREQUAL "LLVM${t}ASMPRINTER" OR
43 capitalized_lib STREQUAL "LLVM${t}DISASSEMBLER" OR
44 capitalized_lib STREQUAL "LLVM${t}INFO" )
45 set(${return_var} ON PARENT_SCOPE)
49 endfunction(is_llvm_target_library)
52 macro(llvm_config executable)
53 explicit_llvm_config(${executable} ${ARGN})
57 function(explicit_llvm_config executable)
58 set( link_components ${ARGN} )
60 explicit_map_components_to_libraries(LIBRARIES ${link_components})
61 target_link_libraries(${executable} ${LIBRARIES})
62 endfunction(explicit_llvm_config)
65 # This is a variant intended for the final user:
66 function(llvm_map_components_to_libraries OUT_VAR)
67 explicit_map_components_to_libraries(result ${ARGN})
68 get_system_libs(sys_result)
69 set( ${OUT_VAR} ${result} ${sys_result} PARENT_SCOPE )
70 endfunction(llvm_map_components_to_libraries)
73 function(explicit_map_components_to_libraries out_libs)
74 set( link_components ${ARGN} )
75 get_property(llvm_libs GLOBAL PROPERTY LLVM_LIBS)
76 string(TOUPPER "${llvm_libs}" capitalized_libs)
78 # Expand some keywords:
79 list(FIND LLVM_TARGETS_TO_BUILD "${LLVM_NATIVE_ARCH}" have_native_backend)
80 list(FIND link_components "engine" engine_required)
81 if( NOT engine_required EQUAL -1 )
82 list(FIND LLVM_TARGETS_WITH_JIT "${LLVM_NATIVE_ARCH}" have_jit)
83 if( NOT have_native_backend EQUAL -1 AND NOT have_jit EQUAL -1 )
84 list(APPEND link_components "jit")
85 list(APPEND link_components "native")
87 list(APPEND link_components "interpreter")
90 list(FIND link_components "native" native_required)
91 if( NOT native_required EQUAL -1 )
92 if( NOT have_native_backend EQUAL -1 )
93 list(APPEND link_components ${LLVM_NATIVE_ARCH})
97 # Translate symbolic component names to real libraries:
98 foreach(c ${link_components})
99 # add codegen, asmprinter, asmparser, disassembler
100 list(FIND LLVM_TARGETS_TO_BUILD ${c} idx)
102 list(FIND llvm_libs "LLVM${c}CodeGen" idx)
104 list(APPEND expanded_components "LLVM${c}CodeGen")
106 list(FIND llvm_libs "LLVM${c}" idx)
108 list(APPEND expanded_components "LLVM${c}")
110 message(FATAL_ERROR "Target ${c} is not in the set of libraries.")
113 list(FIND llvm_libs "LLVM${c}AsmPrinter" asmidx)
114 if( NOT asmidx LESS 0 )
115 list(APPEND expanded_components "LLVM${c}AsmPrinter")
117 list(FIND llvm_libs "LLVM${c}AsmParser" asmidx)
118 if( NOT asmidx LESS 0 )
119 list(APPEND expanded_components "LLVM${c}AsmParser")
121 list(FIND llvm_libs "LLVM${c}Info" asmidx)
122 if( NOT asmidx LESS 0 )
123 list(APPEND expanded_components "LLVM${c}Info")
125 list(FIND llvm_libs "LLVM${c}Disassembler" asmidx)
126 if( NOT asmidx LESS 0 )
127 list(APPEND expanded_components "LLVM${c}Disassembler")
129 elseif( c STREQUAL "native" )
131 elseif( c STREQUAL "nativecodegen" )
132 list(APPEND expanded_components "LLVM${LLVM_NATIVE_ARCH}CodeGen")
133 elseif( c STREQUAL "backend" )
134 # same case as in `native'.
135 elseif( c STREQUAL "engine" )
137 elseif( c STREQUAL "all" )
138 list(APPEND expanded_components ${llvm_libs})
139 else( NOT idx LESS 0 )
140 # Canonize the component name:
141 string(TOUPPER "${c}" capitalized)
142 list(FIND capitalized_libs LLVM${capitalized} lib_idx)
144 # The component is unknown. Maybe is an omitted target?
145 is_llvm_target_library(${c} iltl_result)
146 if( NOT iltl_result )
147 message(FATAL_ERROR "Library `${c}' not found in list of llvm libraries.")
149 else( lib_idx LESS 0 )
150 list(GET llvm_libs ${lib_idx} canonical_lib)
151 list(APPEND expanded_components ${canonical_lib})
152 endif( lib_idx LESS 0 )
153 endif( NOT idx LESS 0 )
155 # Expand dependencies while topologically sorting the list of libraries:
156 list(LENGTH expanded_components lst_size)
159 while( cursor LESS lst_size )
160 list(GET expanded_components ${cursor} lib)
161 get_property(lib_deps GLOBAL PROPERTY LLVMBUILD_LIB_DEPS_${lib})
162 list(APPEND expanded_components ${lib_deps})
163 # Remove duplicates at the front:
164 list(REVERSE expanded_components)
165 list(REMOVE_DUPLICATES expanded_components)
166 list(REVERSE expanded_components)
167 list(APPEND processed ${lib})
168 # Find the maximum index that doesn't have to be re-processed:
169 while(NOT "${expanded_components}" MATCHES "^${processed}.*" )
170 list(REMOVE_AT processed -1)
172 list(LENGTH processed cursor)
173 list(LENGTH expanded_components lst_size)
174 endwhile( cursor LESS lst_size )
175 # Return just the libraries included in this build:
177 foreach(c ${expanded_components})
178 list(FIND llvm_libs ${c} lib_idx)
179 if( NOT lib_idx LESS 0 )
180 set(result ${result} ${c})
183 set(${out_libs} ${result} PARENT_SCOPE)
184 endfunction(explicit_map_components_to_libraries)