blob: fd1cfa8690e5faddb8670a831650245f71ddcaec [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 )
7 if( HAVE_LIBDL )
8 set(system_libs ${system_libs} ${CMAKE_DL_LIBS})
9 endif()
10 if( LLVM_ENABLE_THREADS AND HAVE_LIBPTHREAD )
11 set(system_libs ${system_libs} pthread)
12 endif()
13 endif( MINGW )
14 endif( NOT MSVC )
15 set(${return_var} ${system_libs} PARENT_SCOPE)
16endfunction(get_system_libs)
17
18
19function(is_llvm_target_library library return_var)
20 # Sets variable `return_var' to ON if `library' corresponds to a
21 # LLVM supported target. To OFF if it doesn't.
22 set(${return_var} OFF PARENT_SCOPE)
23 string(TOUPPER "${library}" capitalized_lib)
24 string(TOUPPER "${LLVM_ALL_TARGETS}" targets)
25 foreach(t ${targets})
Oscar Fuentes67044152011-03-15 14:53:53 +000026 if( capitalized_lib STREQUAL t OR
27 capitalized_lib STREQUAL "LLVM${t}" OR
Michael J. Spencer3a210e22010-09-13 23:59:48 +000028 capitalized_lib STREQUAL "LLVM${t}CODEGEN" OR
29 capitalized_lib STREQUAL "LLVM${t}ASMPARSER" OR
30 capitalized_lib STREQUAL "LLVM${t}ASMPRINTER" OR
31 capitalized_lib STREQUAL "LLVM${t}DISASSEMBLER" OR
32 capitalized_lib STREQUAL "LLVM${t}INFO" )
33 set(${return_var} ON PARENT_SCOPE)
34 break()
35 endif()
36 endforeach()
37endfunction(is_llvm_target_library)
38
39
40macro(llvm_config executable)
41 explicit_llvm_config(${executable} ${ARGN})
42endmacro(llvm_config)
43
44
45function(explicit_llvm_config executable)
46 set( link_components ${ARGN} )
47
48 explicit_map_components_to_libraries(LIBRARIES ${link_components})
49 target_link_libraries(${executable} ${LIBRARIES})
50endfunction(explicit_llvm_config)
51
52
53# This is a variant intended for the final user:
54function(llvm_map_components_to_libraries OUT_VAR)
55 explicit_map_components_to_libraries(result ${ARGN})
56 get_system_libs(sys_result)
57 set( ${OUT_VAR} ${result} ${sys_result} PARENT_SCOPE )
58endfunction(llvm_map_components_to_libraries)
59
60
61function(explicit_map_components_to_libraries out_libs)
62 set( link_components ${ARGN} )
Oscar Fuentes6d857ca2011-02-18 22:06:14 +000063 get_property(llvm_libs GLOBAL PROPERTY LLVM_LIBS)
Oscar Fuentes44258d12010-09-28 22:38:39 +000064 string(TOUPPER "${llvm_libs}" capitalized_libs)
Oscar Fuentes5eae24e2011-03-09 14:44:46 +000065
66 # Expand some keywords:
Oscar Fuentesdb3480c2011-03-23 17:42:13 +000067 list(FIND LLVM_TARGETS_TO_BUILD "${LLVM_NATIVE_ARCH}" have_native_backend)
Oscar Fuentes5eae24e2011-03-09 14:44:46 +000068 list(FIND link_components "engine" engine_required)
Oscar Fuentesdb3480c2011-03-23 17:42:13 +000069 if( NOT engine_required EQUAL -1 )
70 list(FIND LLVM_TARGETS_WITH_JIT "${LLVM_NATIVE_ARCH}" have_jit)
71 if( NOT have_native_backend EQUAL -1 AND NOT have_jit EQUAL -1 )
72 list(APPEND link_components "jit")
73 list(APPEND link_components "native")
74 else()
75 list(APPEND link_components "interpreter")
76 endif()
Oscar Fuentes5eae24e2011-03-09 14:44:46 +000077 endif()
78 list(FIND link_components "native" native_required)
Oscar Fuentesdb3480c2011-03-23 17:42:13 +000079 if( NOT native_required EQUAL -1 )
80 if( NOT have_native_backend EQUAL -1 )
81 list(APPEND link_components ${LLVM_NATIVE_ARCH})
82 endif()
Oscar Fuentes5eae24e2011-03-09 14:44:46 +000083 endif()
84
Oscar Fuentes44258d12010-09-28 22:38:39 +000085 # Translate symbolic component names to real libraries:
Michael J. Spencer3a210e22010-09-13 23:59:48 +000086 foreach(c ${link_components})
87 # add codegen, asmprinter, asmparser, disassembler
88 list(FIND LLVM_TARGETS_TO_BUILD ${c} idx)
89 if( NOT idx LESS 0 )
90 list(FIND llvm_libs "LLVM${c}CodeGen" idx)
91 if( NOT idx LESS 0 )
92 list(APPEND expanded_components "LLVM${c}CodeGen")
93 else()
94 list(FIND llvm_libs "LLVM${c}" idx)
95 if( NOT idx LESS 0 )
96 list(APPEND expanded_components "LLVM${c}")
97 else()
98 message(FATAL_ERROR "Target ${c} is not in the set of libraries.")
99 endif()
100 endif()
101 list(FIND llvm_libs "LLVM${c}AsmPrinter" asmidx)
102 if( NOT asmidx LESS 0 )
103 list(APPEND expanded_components "LLVM${c}AsmPrinter")
104 endif()
105 list(FIND llvm_libs "LLVM${c}AsmParser" asmidx)
106 if( NOT asmidx LESS 0 )
107 list(APPEND expanded_components "LLVM${c}AsmParser")
108 endif()
109 list(FIND llvm_libs "LLVM${c}Info" asmidx)
110 if( NOT asmidx LESS 0 )
111 list(APPEND expanded_components "LLVM${c}Info")
112 endif()
113 list(FIND llvm_libs "LLVM${c}Disassembler" asmidx)
114 if( NOT asmidx LESS 0 )
115 list(APPEND expanded_components "LLVM${c}Disassembler")
116 endif()
117 elseif( c STREQUAL "native" )
Oscar Fuentes5eae24e2011-03-09 14:44:46 +0000118 # already processed
Michael J. Spencer3a210e22010-09-13 23:59:48 +0000119 elseif( c STREQUAL "nativecodegen" )
120 list(APPEND expanded_components "LLVM${LLVM_NATIVE_ARCH}CodeGen")
121 elseif( c STREQUAL "backend" )
122 # same case as in `native'.
123 elseif( c STREQUAL "engine" )
Oscar Fuentes5eae24e2011-03-09 14:44:46 +0000124 # already processed
Michael J. Spencer3a210e22010-09-13 23:59:48 +0000125 elseif( c STREQUAL "all" )
126 list(APPEND expanded_components ${llvm_libs})
127 else( NOT idx LESS 0 )
Oscar Fuentes44258d12010-09-28 22:38:39 +0000128 # Canonize the component name:
129 string(TOUPPER "${c}" capitalized)
130 list(FIND capitalized_libs LLVM${capitalized} lib_idx)
131 if( lib_idx LESS 0 )
132 # The component is unkown. Maybe is an ommitted target?
133 is_llvm_target_library(${c} iltl_result)
134 if( NOT iltl_result )
135 message(FATAL_ERROR "Library `${c}' not found in list of llvm libraries.")
136 endif()
137 else( lib_idx LESS 0 )
138 list(GET llvm_libs ${lib_idx} canonical_lib)
139 list(APPEND expanded_components ${canonical_lib})
140 endif( lib_idx LESS 0 )
Michael J. Spencer3a210e22010-09-13 23:59:48 +0000141 endif( NOT idx LESS 0 )
142 endforeach(c)
Oscar Fuentes44258d12010-09-28 22:38:39 +0000143 # Expand dependencies while topologically sorting the list of libraries:
Michael J. Spencer3a210e22010-09-13 23:59:48 +0000144 list(LENGTH expanded_components lst_size)
Oscar Fuentes44258d12010-09-28 22:38:39 +0000145 set(cursor 0)
146 set(processed)
147 while( cursor LESS lst_size )
148 list(GET expanded_components ${cursor} lib)
149 list(APPEND expanded_components ${MSVC_LIB_DEPS_${lib}})
150 # Remove duplicates at the front:
151 list(REVERSE expanded_components)
152 list(REMOVE_DUPLICATES expanded_components)
153 list(REVERSE expanded_components)
154 list(APPEND processed ${lib})
155 # Find the maximum index that doesn't have to be re-processed:
156 while(NOT "${expanded_components}" MATCHES "^${processed}.*" )
157 list(REMOVE_AT processed -1)
158 endwhile()
159 list(LENGTH processed cursor)
Michael J. Spencer3a210e22010-09-13 23:59:48 +0000160 list(LENGTH expanded_components lst_size)
Oscar Fuentes44258d12010-09-28 22:38:39 +0000161 endwhile( cursor LESS lst_size )
162 # Return just the libraries included in this build:
163 set(result)
164 foreach(c ${expanded_components})
165 list(FIND llvm_libs ${c} lib_idx)
166 if( NOT lib_idx LESS 0 )
167 set(result ${result} ${c})
168 endif()
169 endforeach(c)
Michael J. Spencer3a210e22010-09-13 23:59:48 +0000170 set(${out_libs} ${result} PARENT_SCOPE)
171endfunction(explicit_map_components_to_libraries)
172
173
174# The library dependency data is contained in the file
175# LLVMLibDeps.cmake on this directory. It is automatically generated
176# by tools/llvm-config/CMakeLists.txt when the build comprises all the
177# targets and we are on a environment Posix enough to build the
178# llvm-config script. This, in practice, just excludes MSVC.
179
180# When you remove or rename a library from the build, be sure to
181# remove its file from lib/ as well, or the GenLibDeps.pl script will
182# include it on its analysis!
183
184# The format generated by GenLibDeps.pl
185
Oscar Fuentes44258d12010-09-28 22:38:39 +0000186# LLVMARMAsmPrinter.o: LLVMARMCodeGen.o libLLVMAsmPrinter.a libLLVMCodeGen.a libLLVMCore.a libLLVMSupport.a libLLVMTarget.a
Michael J. Spencer3a210e22010-09-13 23:59:48 +0000187
188# is translated to:
189
Oscar Fuentes44258d12010-09-28 22:38:39 +0000190# set(MSVC_LIB_DEPS_LLVMARMAsmPrinter LLVMARMCodeGen LLVMAsmPrinter LLVMCodeGen LLVMCore LLVMSupport LLVMTarget)
Michael J. Spencer3a210e22010-09-13 23:59:48 +0000191
Oscar Fuentes44258d12010-09-28 22:38:39 +0000192# It is necessary to remove the `lib' prefix and the `.a'.
Michael J. Spencer3a210e22010-09-13 23:59:48 +0000193
194# This 'sed' script should do the trick:
195# sed -e s'#\.a##g' -e 's#libLLVM#LLVM#g' -e 's#: # #' -e 's#\(.*\)#set(MSVC_LIB_DEPS_\1)#' ~/llvm/tools/llvm-config/LibDeps.txt
196
197include(LLVMLibDeps)