blob: ec7831da3bb7066ad54a929a5bbeb3555fe7821e [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:
67 list(FIND link_components "engine" engine_required)
Oscar Fuentes879d3a92011-03-12 16:48:54 +000068 if( NOT engine_required STREQUAL "-1" )
Oscar Fuentes5eae24e2011-03-09 14:44:46 +000069 # TODO: as we assume we are on X86, this is `jit'.
70 list(APPEND link_components "jit")
71 list(APPEND link_components "native")
72 endif()
73 list(FIND link_components "native" native_required)
Oscar Fuentes879d3a92011-03-12 16:48:54 +000074 if( NOT native_required STREQUAL "-1" )
Oscar Fuentes5eae24e2011-03-09 14:44:46 +000075 list(APPEND link_components "X86")
76 endif()
77
Oscar Fuentes44258d12010-09-28 22:38:39 +000078 # Translate symbolic component names to real libraries:
Michael J. Spencer3a210e22010-09-13 23:59:48 +000079 foreach(c ${link_components})
80 # add codegen, asmprinter, asmparser, disassembler
81 list(FIND LLVM_TARGETS_TO_BUILD ${c} idx)
82 if( NOT idx LESS 0 )
83 list(FIND llvm_libs "LLVM${c}CodeGen" idx)
84 if( NOT idx LESS 0 )
85 list(APPEND expanded_components "LLVM${c}CodeGen")
86 else()
87 list(FIND llvm_libs "LLVM${c}" idx)
88 if( NOT idx LESS 0 )
89 list(APPEND expanded_components "LLVM${c}")
90 else()
91 message(FATAL_ERROR "Target ${c} is not in the set of libraries.")
92 endif()
93 endif()
94 list(FIND llvm_libs "LLVM${c}AsmPrinter" asmidx)
95 if( NOT asmidx LESS 0 )
96 list(APPEND expanded_components "LLVM${c}AsmPrinter")
97 endif()
98 list(FIND llvm_libs "LLVM${c}AsmParser" asmidx)
99 if( NOT asmidx LESS 0 )
100 list(APPEND expanded_components "LLVM${c}AsmParser")
101 endif()
102 list(FIND llvm_libs "LLVM${c}Info" asmidx)
103 if( NOT asmidx LESS 0 )
104 list(APPEND expanded_components "LLVM${c}Info")
105 endif()
106 list(FIND llvm_libs "LLVM${c}Disassembler" asmidx)
107 if( NOT asmidx LESS 0 )
108 list(APPEND expanded_components "LLVM${c}Disassembler")
109 endif()
110 elseif( c STREQUAL "native" )
Oscar Fuentes5eae24e2011-03-09 14:44:46 +0000111 # already processed
Michael J. Spencer3a210e22010-09-13 23:59:48 +0000112 elseif( c STREQUAL "nativecodegen" )
113 list(APPEND expanded_components "LLVM${LLVM_NATIVE_ARCH}CodeGen")
114 elseif( c STREQUAL "backend" )
115 # same case as in `native'.
116 elseif( c STREQUAL "engine" )
Oscar Fuentes5eae24e2011-03-09 14:44:46 +0000117 # already processed
Michael J. Spencer3a210e22010-09-13 23:59:48 +0000118 elseif( c STREQUAL "all" )
119 list(APPEND expanded_components ${llvm_libs})
120 else( NOT idx LESS 0 )
Oscar Fuentes44258d12010-09-28 22:38:39 +0000121 # Canonize the component name:
122 string(TOUPPER "${c}" capitalized)
123 list(FIND capitalized_libs LLVM${capitalized} lib_idx)
124 if( lib_idx LESS 0 )
125 # The component is unkown. Maybe is an ommitted target?
126 is_llvm_target_library(${c} iltl_result)
127 if( NOT iltl_result )
128 message(FATAL_ERROR "Library `${c}' not found in list of llvm libraries.")
129 endif()
130 else( lib_idx LESS 0 )
131 list(GET llvm_libs ${lib_idx} canonical_lib)
132 list(APPEND expanded_components ${canonical_lib})
133 endif( lib_idx LESS 0 )
Michael J. Spencer3a210e22010-09-13 23:59:48 +0000134 endif( NOT idx LESS 0 )
135 endforeach(c)
Oscar Fuentes44258d12010-09-28 22:38:39 +0000136 # Expand dependencies while topologically sorting the list of libraries:
Michael J. Spencer3a210e22010-09-13 23:59:48 +0000137 list(LENGTH expanded_components lst_size)
Oscar Fuentes44258d12010-09-28 22:38:39 +0000138 set(cursor 0)
139 set(processed)
140 while( cursor LESS lst_size )
141 list(GET expanded_components ${cursor} lib)
142 list(APPEND expanded_components ${MSVC_LIB_DEPS_${lib}})
143 # Remove duplicates at the front:
144 list(REVERSE expanded_components)
145 list(REMOVE_DUPLICATES expanded_components)
146 list(REVERSE expanded_components)
147 list(APPEND processed ${lib})
148 # Find the maximum index that doesn't have to be re-processed:
149 while(NOT "${expanded_components}" MATCHES "^${processed}.*" )
150 list(REMOVE_AT processed -1)
151 endwhile()
152 list(LENGTH processed cursor)
Michael J. Spencer3a210e22010-09-13 23:59:48 +0000153 list(LENGTH expanded_components lst_size)
Oscar Fuentes44258d12010-09-28 22:38:39 +0000154 endwhile( cursor LESS lst_size )
155 # Return just the libraries included in this build:
156 set(result)
157 foreach(c ${expanded_components})
158 list(FIND llvm_libs ${c} lib_idx)
159 if( NOT lib_idx LESS 0 )
160 set(result ${result} ${c})
161 endif()
162 endforeach(c)
Michael J. Spencer3a210e22010-09-13 23:59:48 +0000163 set(${out_libs} ${result} PARENT_SCOPE)
164endfunction(explicit_map_components_to_libraries)
165
166
167# The library dependency data is contained in the file
168# LLVMLibDeps.cmake on this directory. It is automatically generated
169# by tools/llvm-config/CMakeLists.txt when the build comprises all the
170# targets and we are on a environment Posix enough to build the
171# llvm-config script. This, in practice, just excludes MSVC.
172
173# When you remove or rename a library from the build, be sure to
174# remove its file from lib/ as well, or the GenLibDeps.pl script will
175# include it on its analysis!
176
177# The format generated by GenLibDeps.pl
178
Oscar Fuentes44258d12010-09-28 22:38:39 +0000179# LLVMARMAsmPrinter.o: LLVMARMCodeGen.o libLLVMAsmPrinter.a libLLVMCodeGen.a libLLVMCore.a libLLVMSupport.a libLLVMTarget.a
Michael J. Spencer3a210e22010-09-13 23:59:48 +0000180
181# is translated to:
182
Oscar Fuentes44258d12010-09-28 22:38:39 +0000183# set(MSVC_LIB_DEPS_LLVMARMAsmPrinter LLVMARMCodeGen LLVMAsmPrinter LLVMCodeGen LLVMCore LLVMSupport LLVMTarget)
Michael J. Spencer3a210e22010-09-13 23:59:48 +0000184
Oscar Fuentes44258d12010-09-28 22:38:39 +0000185# It is necessary to remove the `lib' prefix and the `.a'.
Michael J. Spencer3a210e22010-09-13 23:59:48 +0000186
187# This 'sed' script should do the trick:
188# 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
189
190include(LLVMLibDeps)