blob: af720fcb2143c02a48e928f694e68b7041928d41 [file] [log] [blame]
Michael J. Spencer93c9b2e2010-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 )
David Majnemer61eae2e2013-10-07 01:00:07 +00005 set(system_libs ${system_libs} imagehlp psapi shell32)
Michael J. Spencer93c9b2e2010-09-13 23:59:48 +00006 elseif( CMAKE_HOST_UNIX )
Chandler Carruthef7f9682013-01-04 23:19:55 +00007 if( HAVE_LIBRT )
8 set(system_libs ${system_libs} rt)
9 endif()
Michael J. Spencer93c9b2e2010-09-13 23:59:48 +000010 if( HAVE_LIBDL )
Chandler Carruthef7f9682013-01-04 23:19:55 +000011 set(system_libs ${system_libs} ${CMAKE_DL_LIBS})
Michael J. Spencer93c9b2e2010-09-13 23:59:48 +000012 endif()
Chandler Carruthf11f1e42013-08-12 09:49:17 +000013 if(LLVM_ENABLE_TERMINFO)
14 if(HAVE_TERMINFO)
15 set(system_libs ${system_libs} ${TERMINFO_LIBS})
Chandler Carruthcad7e5e2013-08-07 08:47:36 +000016 endif()
17 endif()
Michael J. Spencer93c9b2e2010-09-13 23:59:48 +000018 if( LLVM_ENABLE_THREADS AND HAVE_LIBPTHREAD )
Chandler Carruthef7f9682013-01-04 23:19:55 +000019 set(system_libs ${system_libs} pthread)
Michael J. Spencer93c9b2e2010-09-13 23:59:48 +000020 endif()
Alexey Samsonov2fb337e2013-04-23 08:28:39 +000021 if ( LLVM_ENABLE_ZLIB AND HAVE_LIBZ )
22 set(system_libs ${system_libs} z)
23 endif()
Michael J. Spencer93c9b2e2010-09-13 23:59:48 +000024 endif( MINGW )
25 endif( NOT MSVC )
26 set(${return_var} ${system_libs} PARENT_SCOPE)
27endfunction(get_system_libs)
28
29
Oscar Fuentes978e5282011-03-29 20:51:08 +000030function(link_system_libs target)
31 get_system_libs(llvm_system_libs)
32 target_link_libraries(${target} ${llvm_system_libs})
33endfunction(link_system_libs)
34
35
Michael J. Spencer93c9b2e2010-09-13 23:59:48 +000036function(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)
42 foreach(t ${targets})
Oscar Fuentes638b8b72011-03-15 14:53:53 +000043 if( capitalized_lib STREQUAL t OR
NAKAMURA Takumi78293372014-02-02 16:46:35 +000044 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" )
Michael J. Spencer93c9b2e2010-09-13 23:59:48 +000050 set(${return_var} ON PARENT_SCOPE)
51 break()
52 endif()
53 endforeach()
54endfunction(is_llvm_target_library)
55
56
57macro(llvm_config executable)
58 explicit_llvm_config(${executable} ${ARGN})
59endmacro(llvm_config)
60
61
62function(explicit_llvm_config executable)
63 set( link_components ${ARGN} )
64
NAKAMURA Takumi623055b2014-02-10 03:24:19 +000065 llvm_map_components_to_libnames(LIBRARIES ${link_components})
Michael J. Spencer93c9b2e2010-09-13 23:59:48 +000066 target_link_libraries(${executable} ${LIBRARIES})
67endfunction(explicit_llvm_config)
68
69
70# This is a variant intended for the final user:
71function(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 )
75endfunction(llvm_map_components_to_libraries)
76
NAKAMURA Takumi906dad82014-02-04 14:42:04 +000077# Map LINK_COMPONENTS to actual libnames.
78function(llvm_map_components_to_libnames out_libs)
Michael J. Spencer93c9b2e2010-09-13 23:59:48 +000079 set( link_components ${ARGN} )
NAKAMURA Takumi12fedb02014-02-21 14:17:07 +000080 if(NOT LLVM_AVAILABLE_LIBS)
81 # Inside LLVM itself available libs are in a global property.
82 get_property(LLVM_AVAILABLE_LIBS GLOBAL PROPERTY LLVM_LIBS)
83 endif()
84 string(TOUPPER "${LLVM_AVAILABLE_LIBS}" capitalized_libs)
Oscar Fuentese5035062011-03-09 14:44:46 +000085
86 # Expand some keywords:
Oscar Fuentes465f9362011-03-23 17:42:13 +000087 list(FIND LLVM_TARGETS_TO_BUILD "${LLVM_NATIVE_ARCH}" have_native_backend)
Oscar Fuentese5035062011-03-09 14:44:46 +000088 list(FIND link_components "engine" engine_required)
Oscar Fuentes465f9362011-03-23 17:42:13 +000089 if( NOT engine_required EQUAL -1 )
90 list(FIND LLVM_TARGETS_WITH_JIT "${LLVM_NATIVE_ARCH}" have_jit)
91 if( NOT have_native_backend EQUAL -1 AND NOT have_jit EQUAL -1 )
92 list(APPEND link_components "jit")
93 list(APPEND link_components "native")
94 else()
95 list(APPEND link_components "interpreter")
96 endif()
Oscar Fuentese5035062011-03-09 14:44:46 +000097 endif()
98 list(FIND link_components "native" native_required)
Oscar Fuentes465f9362011-03-23 17:42:13 +000099 if( NOT native_required EQUAL -1 )
100 if( NOT have_native_backend EQUAL -1 )
101 list(APPEND link_components ${LLVM_NATIVE_ARCH})
102 endif()
Oscar Fuentese5035062011-03-09 14:44:46 +0000103 endif()
104
Oscar Fuentes18811d52010-09-28 22:38:39 +0000105 # Translate symbolic component names to real libraries:
Michael J. Spencer93c9b2e2010-09-13 23:59:48 +0000106 foreach(c ${link_components})
107 # add codegen, asmprinter, asmparser, disassembler
108 list(FIND LLVM_TARGETS_TO_BUILD ${c} idx)
109 if( NOT idx LESS 0 )
NAKAMURA Takumi1956c492014-02-21 14:16:52 +0000110 if( TARGET LLVM${c}CodeGen )
NAKAMURA Takumi78293372014-02-02 16:46:35 +0000111 list(APPEND expanded_components "LLVM${c}CodeGen")
Michael J. Spencer93c9b2e2010-09-13 23:59:48 +0000112 else()
NAKAMURA Takumi1956c492014-02-21 14:16:52 +0000113 if( TARGET LLVM${c} )
NAKAMURA Takumi78293372014-02-02 16:46:35 +0000114 list(APPEND expanded_components "LLVM${c}")
115 else()
116 message(FATAL_ERROR "Target ${c} is not in the set of libraries.")
117 endif()
Michael J. Spencer93c9b2e2010-09-13 23:59:48 +0000118 endif()
NAKAMURA Takumi1956c492014-02-21 14:16:52 +0000119 if( TARGET LLVM${c}AsmPrinter )
Michael J. Spencer93c9b2e2010-09-13 23:59:48 +0000120 list(APPEND expanded_components "LLVM${c}AsmPrinter")
121 endif()
NAKAMURA Takumi1956c492014-02-21 14:16:52 +0000122 if( TARGET LLVM${c}AsmParser )
Michael J. Spencer93c9b2e2010-09-13 23:59:48 +0000123 list(APPEND expanded_components "LLVM${c}AsmParser")
124 endif()
NAKAMURA Takumi1956c492014-02-21 14:16:52 +0000125 if( TARGET LLVM${c}Info )
Michael J. Spencer93c9b2e2010-09-13 23:59:48 +0000126 list(APPEND expanded_components "LLVM${c}Info")
127 endif()
NAKAMURA Takumi1956c492014-02-21 14:16:52 +0000128 if( TARGET LLVM${c}Disassembler )
Michael J. Spencer93c9b2e2010-09-13 23:59:48 +0000129 list(APPEND expanded_components "LLVM${c}Disassembler")
130 endif()
131 elseif( c STREQUAL "native" )
Oscar Fuentese5035062011-03-09 14:44:46 +0000132 # already processed
Michael J. Spencer93c9b2e2010-09-13 23:59:48 +0000133 elseif( c STREQUAL "nativecodegen" )
134 list(APPEND expanded_components "LLVM${LLVM_NATIVE_ARCH}CodeGen")
135 elseif( c STREQUAL "backend" )
136 # same case as in `native'.
137 elseif( c STREQUAL "engine" )
Oscar Fuentese5035062011-03-09 14:44:46 +0000138 # already processed
Michael J. Spencer93c9b2e2010-09-13 23:59:48 +0000139 elseif( c STREQUAL "all" )
NAKAMURA Takumi12fedb02014-02-21 14:17:07 +0000140 list(APPEND expanded_components ${LLVM_AVAILABLE_LIBS})
Michael J. Spencer93c9b2e2010-09-13 23:59:48 +0000141 else( NOT idx LESS 0 )
Oscar Fuentes18811d52010-09-28 22:38:39 +0000142 # Canonize the component name:
143 string(TOUPPER "${c}" capitalized)
144 list(FIND capitalized_libs LLVM${capitalized} lib_idx)
145 if( lib_idx LESS 0 )
Chandler Carruth68b23112011-07-29 23:52:01 +0000146 # The component is unknown. Maybe is an omitted target?
147 is_llvm_target_library(${c} iltl_result)
148 if( NOT iltl_result )
149 message(FATAL_ERROR "Library `${c}' not found in list of llvm libraries.")
150 endif()
Oscar Fuentes18811d52010-09-28 22:38:39 +0000151 else( lib_idx LESS 0 )
NAKAMURA Takumi12fedb02014-02-21 14:17:07 +0000152 list(GET LLVM_AVAILABLE_LIBS ${lib_idx} canonical_lib)
Chandler Carruth68b23112011-07-29 23:52:01 +0000153 list(APPEND expanded_components ${canonical_lib})
Oscar Fuentes18811d52010-09-28 22:38:39 +0000154 endif( lib_idx LESS 0 )
Michael J. Spencer93c9b2e2010-09-13 23:59:48 +0000155 endif( NOT idx LESS 0 )
156 endforeach(c)
NAKAMURA Takumi906dad82014-02-04 14:42:04 +0000157
158 set(${out_libs} ${expanded_components} PARENT_SCOPE)
159endfunction()
160
161# Expand dependencies while topologically sorting the list of libraries:
162function(llvm_expand_dependencies out_libs)
163 set(expanded_components ${ARGN})
Michael J. Spencer93c9b2e2010-09-13 23:59:48 +0000164 list(LENGTH expanded_components lst_size)
Oscar Fuentes18811d52010-09-28 22:38:39 +0000165 set(cursor 0)
166 set(processed)
167 while( cursor LESS lst_size )
168 list(GET expanded_components ${cursor} lib)
Daniel Dunbarfaaa76d2011-11-29 01:31:52 +0000169 get_property(lib_deps GLOBAL PROPERTY LLVMBUILD_LIB_DEPS_${lib})
Chandler Carruth68b23112011-07-29 23:52:01 +0000170 list(APPEND expanded_components ${lib_deps})
Oscar Fuentes18811d52010-09-28 22:38:39 +0000171 # Remove duplicates at the front:
172 list(REVERSE expanded_components)
173 list(REMOVE_DUPLICATES expanded_components)
174 list(REVERSE expanded_components)
175 list(APPEND processed ${lib})
176 # Find the maximum index that doesn't have to be re-processed:
177 while(NOT "${expanded_components}" MATCHES "^${processed}.*" )
178 list(REMOVE_AT processed -1)
179 endwhile()
180 list(LENGTH processed cursor)
Michael J. Spencer93c9b2e2010-09-13 23:59:48 +0000181 list(LENGTH expanded_components lst_size)
Oscar Fuentes18811d52010-09-28 22:38:39 +0000182 endwhile( cursor LESS lst_size )
NAKAMURA Takumi906dad82014-02-04 14:42:04 +0000183 set(${out_libs} ${expanded_components} PARENT_SCOPE)
184endfunction()
185
186function(explicit_map_components_to_libraries out_libs)
187 llvm_map_components_to_libnames(link_libs ${ARGN})
188 llvm_expand_dependencies(expanded_components ${link_libs})
Oscar Fuentes18811d52010-09-28 22:38:39 +0000189 # Return just the libraries included in this build:
190 set(result)
191 foreach(c ${expanded_components})
NAKAMURA Takumi1956c492014-02-21 14:16:52 +0000192 if( TARGET ${c} )
Oscar Fuentes18811d52010-09-28 22:38:39 +0000193 set(result ${result} ${c})
194 endif()
195 endforeach(c)
Michael J. Spencer93c9b2e2010-09-13 23:59:48 +0000196 set(${out_libs} ${result} PARENT_SCOPE)
197endfunction(explicit_map_components_to_libraries)