blob: 2ddc0b2bf89a34c14dc06cc77063f80e7aab89f5 [file] [log] [blame]
Michael J. Spencer3a210e22010-09-13 23:59:48 +00001function(get_system_libs return_var)
2 # Returns in `return_var' a list of system libraries used by LLVM.
3 if( NOT MSVC )
4 if( MINGW )
5 set(system_libs ${system_libs} imagehlp psapi)
6 elseif( CMAKE_HOST_UNIX )
Chandler Carruth73c35d82013-01-04 23:19:55 +00007 if( HAVE_LIBRT )
8 set(system_libs ${system_libs} rt)
9 endif()
Michael J. Spencer3a210e22010-09-13 23:59:48 +000010 if( HAVE_LIBDL )
Chandler Carruth73c35d82013-01-04 23:19:55 +000011 set(system_libs ${system_libs} ${CMAKE_DL_LIBS})
Michael J. Spencer3a210e22010-09-13 23:59:48 +000012 endif()
13 if( LLVM_ENABLE_THREADS AND HAVE_LIBPTHREAD )
Chandler Carruth73c35d82013-01-04 23:19:55 +000014 set(system_libs ${system_libs} pthread)
Michael J. Spencer3a210e22010-09-13 23:59:48 +000015 endif()
Alexey Samsonovee03c942013-04-23 08:28:39 +000016 if ( LLVM_ENABLE_ZLIB AND HAVE_LIBZ )
17 set(system_libs ${system_libs} z)
18 endif()
Michael J. Spencer3a210e22010-09-13 23:59:48 +000019 endif( MINGW )
20 endif( NOT MSVC )
21 set(${return_var} ${system_libs} PARENT_SCOPE)
22endfunction(get_system_libs)
23
24
Oscar Fuentes23f0bfe2011-03-29 20:51:08 +000025function(link_system_libs target)
26 get_system_libs(llvm_system_libs)
27 target_link_libraries(${target} ${llvm_system_libs})
28endfunction(link_system_libs)
29
30
Michael J. Spencer3a210e22010-09-13 23:59:48 +000031function(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)
37 foreach(t ${targets})
Oscar Fuentes67044152011-03-15 14:53:53 +000038 if( capitalized_lib STREQUAL t OR
39 capitalized_lib STREQUAL "LLVM${t}" OR
Michael J. Spencer3a210e22010-09-13 23:59:48 +000040 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)
46 break()
47 endif()
48 endforeach()
49endfunction(is_llvm_target_library)
50
51
52macro(llvm_config executable)
53 explicit_llvm_config(${executable} ${ARGN})
54endmacro(llvm_config)
55
56
57function(explicit_llvm_config executable)
58 set( link_components ${ARGN} )
59
60 explicit_map_components_to_libraries(LIBRARIES ${link_components})
61 target_link_libraries(${executable} ${LIBRARIES})
62endfunction(explicit_llvm_config)
63
64
65# This is a variant intended for the final user:
66function(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 )
70endfunction(llvm_map_components_to_libraries)
71
72
73function(explicit_map_components_to_libraries out_libs)
74 set( link_components ${ARGN} )
Oscar Fuentes6d857ca2011-02-18 22:06:14 +000075 get_property(llvm_libs GLOBAL PROPERTY LLVM_LIBS)
Oscar Fuentes44258d12010-09-28 22:38:39 +000076 string(TOUPPER "${llvm_libs}" capitalized_libs)
Oscar Fuentes5eae24e2011-03-09 14:44:46 +000077
78 # Expand some keywords:
Oscar Fuentesdb3480c2011-03-23 17:42:13 +000079 list(FIND LLVM_TARGETS_TO_BUILD "${LLVM_NATIVE_ARCH}" have_native_backend)
Oscar Fuentes5eae24e2011-03-09 14:44:46 +000080 list(FIND link_components "engine" engine_required)
Oscar Fuentesdb3480c2011-03-23 17:42:13 +000081 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")
86 else()
87 list(APPEND link_components "interpreter")
88 endif()
Oscar Fuentes5eae24e2011-03-09 14:44:46 +000089 endif()
90 list(FIND link_components "native" native_required)
Oscar Fuentesdb3480c2011-03-23 17:42:13 +000091 if( NOT native_required EQUAL -1 )
92 if( NOT have_native_backend EQUAL -1 )
93 list(APPEND link_components ${LLVM_NATIVE_ARCH})
94 endif()
Oscar Fuentes5eae24e2011-03-09 14:44:46 +000095 endif()
96
Oscar Fuentes44258d12010-09-28 22:38:39 +000097 # Translate symbolic component names to real libraries:
Michael J. Spencer3a210e22010-09-13 23:59:48 +000098 foreach(c ${link_components})
99 # add codegen, asmprinter, asmparser, disassembler
100 list(FIND LLVM_TARGETS_TO_BUILD ${c} idx)
101 if( NOT idx LESS 0 )
102 list(FIND llvm_libs "LLVM${c}CodeGen" idx)
103 if( NOT idx LESS 0 )
104 list(APPEND expanded_components "LLVM${c}CodeGen")
105 else()
106 list(FIND llvm_libs "LLVM${c}" idx)
107 if( NOT idx LESS 0 )
108 list(APPEND expanded_components "LLVM${c}")
109 else()
110 message(FATAL_ERROR "Target ${c} is not in the set of libraries.")
111 endif()
112 endif()
113 list(FIND llvm_libs "LLVM${c}AsmPrinter" asmidx)
114 if( NOT asmidx LESS 0 )
115 list(APPEND expanded_components "LLVM${c}AsmPrinter")
116 endif()
117 list(FIND llvm_libs "LLVM${c}AsmParser" asmidx)
118 if( NOT asmidx LESS 0 )
119 list(APPEND expanded_components "LLVM${c}AsmParser")
120 endif()
121 list(FIND llvm_libs "LLVM${c}Info" asmidx)
122 if( NOT asmidx LESS 0 )
123 list(APPEND expanded_components "LLVM${c}Info")
124 endif()
125 list(FIND llvm_libs "LLVM${c}Disassembler" asmidx)
126 if( NOT asmidx LESS 0 )
127 list(APPEND expanded_components "LLVM${c}Disassembler")
128 endif()
129 elseif( c STREQUAL "native" )
Oscar Fuentes5eae24e2011-03-09 14:44:46 +0000130 # already processed
Michael J. Spencer3a210e22010-09-13 23:59:48 +0000131 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" )
Oscar Fuentes5eae24e2011-03-09 14:44:46 +0000136 # already processed
Michael J. Spencer3a210e22010-09-13 23:59:48 +0000137 elseif( c STREQUAL "all" )
138 list(APPEND expanded_components ${llvm_libs})
139 else( NOT idx LESS 0 )
Oscar Fuentes44258d12010-09-28 22:38:39 +0000140 # Canonize the component name:
141 string(TOUPPER "${c}" capitalized)
142 list(FIND capitalized_libs LLVM${capitalized} lib_idx)
143 if( lib_idx LESS 0 )
Chandler Carruth2e1513d2011-07-29 23:52:01 +0000144 # 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.")
148 endif()
Oscar Fuentes44258d12010-09-28 22:38:39 +0000149 else( lib_idx LESS 0 )
Chandler Carruth2e1513d2011-07-29 23:52:01 +0000150 list(GET llvm_libs ${lib_idx} canonical_lib)
151 list(APPEND expanded_components ${canonical_lib})
Oscar Fuentes44258d12010-09-28 22:38:39 +0000152 endif( lib_idx LESS 0 )
Michael J. Spencer3a210e22010-09-13 23:59:48 +0000153 endif( NOT idx LESS 0 )
154 endforeach(c)
Oscar Fuentes44258d12010-09-28 22:38:39 +0000155 # Expand dependencies while topologically sorting the list of libraries:
Michael J. Spencer3a210e22010-09-13 23:59:48 +0000156 list(LENGTH expanded_components lst_size)
Oscar Fuentes44258d12010-09-28 22:38:39 +0000157 set(cursor 0)
158 set(processed)
159 while( cursor LESS lst_size )
160 list(GET expanded_components ${cursor} lib)
Daniel Dunbar288bc5c2011-11-29 01:31:52 +0000161 get_property(lib_deps GLOBAL PROPERTY LLVMBUILD_LIB_DEPS_${lib})
Chandler Carruth2e1513d2011-07-29 23:52:01 +0000162 list(APPEND expanded_components ${lib_deps})
Oscar Fuentes44258d12010-09-28 22:38:39 +0000163 # 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)
171 endwhile()
172 list(LENGTH processed cursor)
Michael J. Spencer3a210e22010-09-13 23:59:48 +0000173 list(LENGTH expanded_components lst_size)
Oscar Fuentes44258d12010-09-28 22:38:39 +0000174 endwhile( cursor LESS lst_size )
175 # Return just the libraries included in this build:
176 set(result)
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})
181 endif()
182 endforeach(c)
Michael J. Spencer3a210e22010-09-13 23:59:48 +0000183 set(${out_libs} ${result} PARENT_SCOPE)
184endfunction(explicit_map_components_to_libraries)