blob: e8308f680b025931b052ea2341fe1886c47fcc5e [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})
26 if( capitalized_lib STREQUAL "LLVM${t}" OR
27 capitalized_lib STREQUAL "LLVM${t}CODEGEN" OR
28 capitalized_lib STREQUAL "LLVM${t}ASMPARSER" OR
29 capitalized_lib STREQUAL "LLVM${t}ASMPRINTER" OR
30 capitalized_lib STREQUAL "LLVM${t}DISASSEMBLER" OR
31 capitalized_lib STREQUAL "LLVM${t}INFO" )
32 set(${return_var} ON PARENT_SCOPE)
33 break()
34 endif()
35 endforeach()
36endfunction(is_llvm_target_library)
37
38
39macro(llvm_config executable)
40 explicit_llvm_config(${executable} ${ARGN})
41endmacro(llvm_config)
42
43
44function(explicit_llvm_config executable)
45 set( link_components ${ARGN} )
46
47 explicit_map_components_to_libraries(LIBRARIES ${link_components})
48 target_link_libraries(${executable} ${LIBRARIES})
49endfunction(explicit_llvm_config)
50
51
52# This is a variant intended for the final user:
53function(llvm_map_components_to_libraries OUT_VAR)
54 explicit_map_components_to_libraries(result ${ARGN})
55 get_system_libs(sys_result)
56 set( ${OUT_VAR} ${result} ${sys_result} PARENT_SCOPE )
57endfunction(llvm_map_components_to_libraries)
58
59
60function(explicit_map_components_to_libraries out_libs)
61 set( link_components ${ARGN} )
Oscar Fuentes6d857ca2011-02-18 22:06:14 +000062 get_property(llvm_libs GLOBAL PROPERTY LLVM_LIBS)
Oscar Fuentes44258d12010-09-28 22:38:39 +000063 string(TOUPPER "${llvm_libs}" capitalized_libs)
Oscar Fuentes5eae24e2011-03-09 14:44:46 +000064
65 # Expand some keywords:
66 list(FIND link_components "engine" engine_required)
67 if( engine_required )
68 # TODO: as we assume we are on X86, this is `jit'.
69 list(APPEND link_components "jit")
70 list(APPEND link_components "native")
71 endif()
72 list(FIND link_components "native" native_required)
73 if( native_required )
74 list(APPEND link_components "X86")
75 endif()
76
Oscar Fuentes44258d12010-09-28 22:38:39 +000077 # Translate symbolic component names to real libraries:
Michael J. Spencer3a210e22010-09-13 23:59:48 +000078 foreach(c ${link_components})
79 # add codegen, asmprinter, asmparser, disassembler
80 list(FIND LLVM_TARGETS_TO_BUILD ${c} idx)
81 if( NOT idx LESS 0 )
82 list(FIND llvm_libs "LLVM${c}CodeGen" idx)
83 if( NOT idx LESS 0 )
84 list(APPEND expanded_components "LLVM${c}CodeGen")
85 else()
86 list(FIND llvm_libs "LLVM${c}" idx)
87 if( NOT idx LESS 0 )
88 list(APPEND expanded_components "LLVM${c}")
89 else()
90 message(FATAL_ERROR "Target ${c} is not in the set of libraries.")
91 endif()
92 endif()
93 list(FIND llvm_libs "LLVM${c}AsmPrinter" asmidx)
94 if( NOT asmidx LESS 0 )
95 list(APPEND expanded_components "LLVM${c}AsmPrinter")
96 endif()
97 list(FIND llvm_libs "LLVM${c}AsmParser" asmidx)
98 if( NOT asmidx LESS 0 )
99 list(APPEND expanded_components "LLVM${c}AsmParser")
100 endif()
101 list(FIND llvm_libs "LLVM${c}Info" asmidx)
102 if( NOT asmidx LESS 0 )
103 list(APPEND expanded_components "LLVM${c}Info")
104 endif()
105 list(FIND llvm_libs "LLVM${c}Disassembler" asmidx)
106 if( NOT asmidx LESS 0 )
107 list(APPEND expanded_components "LLVM${c}Disassembler")
108 endif()
109 elseif( c STREQUAL "native" )
Oscar Fuentes5eae24e2011-03-09 14:44:46 +0000110 # already processed
Michael J. Spencer3a210e22010-09-13 23:59:48 +0000111 elseif( c STREQUAL "nativecodegen" )
112 list(APPEND expanded_components "LLVM${LLVM_NATIVE_ARCH}CodeGen")
113 elseif( c STREQUAL "backend" )
114 # same case as in `native'.
115 elseif( c STREQUAL "engine" )
Oscar Fuentes5eae24e2011-03-09 14:44:46 +0000116 # already processed
Michael J. Spencer3a210e22010-09-13 23:59:48 +0000117 elseif( c STREQUAL "all" )
118 list(APPEND expanded_components ${llvm_libs})
119 else( NOT idx LESS 0 )
Oscar Fuentes44258d12010-09-28 22:38:39 +0000120 # Canonize the component name:
121 string(TOUPPER "${c}" capitalized)
122 list(FIND capitalized_libs LLVM${capitalized} lib_idx)
123 if( lib_idx LESS 0 )
124 # The component is unkown. Maybe is an ommitted target?
125 is_llvm_target_library(${c} iltl_result)
126 if( NOT iltl_result )
127 message(FATAL_ERROR "Library `${c}' not found in list of llvm libraries.")
128 endif()
129 else( lib_idx LESS 0 )
130 list(GET llvm_libs ${lib_idx} canonical_lib)
131 list(APPEND expanded_components ${canonical_lib})
132 endif( lib_idx LESS 0 )
Michael J. Spencer3a210e22010-09-13 23:59:48 +0000133 endif( NOT idx LESS 0 )
134 endforeach(c)
Oscar Fuentes44258d12010-09-28 22:38:39 +0000135 # Expand dependencies while topologically sorting the list of libraries:
Michael J. Spencer3a210e22010-09-13 23:59:48 +0000136 list(LENGTH expanded_components lst_size)
Oscar Fuentes44258d12010-09-28 22:38:39 +0000137 set(cursor 0)
138 set(processed)
139 while( cursor LESS lst_size )
140 list(GET expanded_components ${cursor} lib)
141 list(APPEND expanded_components ${MSVC_LIB_DEPS_${lib}})
142 # Remove duplicates at the front:
143 list(REVERSE expanded_components)
144 list(REMOVE_DUPLICATES expanded_components)
145 list(REVERSE expanded_components)
146 list(APPEND processed ${lib})
147 # Find the maximum index that doesn't have to be re-processed:
148 while(NOT "${expanded_components}" MATCHES "^${processed}.*" )
149 list(REMOVE_AT processed -1)
150 endwhile()
151 list(LENGTH processed cursor)
Michael J. Spencer3a210e22010-09-13 23:59:48 +0000152 list(LENGTH expanded_components lst_size)
Oscar Fuentes44258d12010-09-28 22:38:39 +0000153 endwhile( cursor LESS lst_size )
154 # Return just the libraries included in this build:
155 set(result)
156 foreach(c ${expanded_components})
157 list(FIND llvm_libs ${c} lib_idx)
158 if( NOT lib_idx LESS 0 )
159 set(result ${result} ${c})
160 endif()
161 endforeach(c)
Michael J. Spencer3a210e22010-09-13 23:59:48 +0000162 set(${out_libs} ${result} PARENT_SCOPE)
163endfunction(explicit_map_components_to_libraries)
164
165
166# The library dependency data is contained in the file
167# LLVMLibDeps.cmake on this directory. It is automatically generated
168# by tools/llvm-config/CMakeLists.txt when the build comprises all the
169# targets and we are on a environment Posix enough to build the
170# llvm-config script. This, in practice, just excludes MSVC.
171
172# When you remove or rename a library from the build, be sure to
173# remove its file from lib/ as well, or the GenLibDeps.pl script will
174# include it on its analysis!
175
176# The format generated by GenLibDeps.pl
177
Oscar Fuentes44258d12010-09-28 22:38:39 +0000178# LLVMARMAsmPrinter.o: LLVMARMCodeGen.o libLLVMAsmPrinter.a libLLVMCodeGen.a libLLVMCore.a libLLVMSupport.a libLLVMTarget.a
Michael J. Spencer3a210e22010-09-13 23:59:48 +0000179
180# is translated to:
181
Oscar Fuentes44258d12010-09-28 22:38:39 +0000182# set(MSVC_LIB_DEPS_LLVMARMAsmPrinter LLVMARMCodeGen LLVMAsmPrinter LLVMCodeGen LLVMCore LLVMSupport LLVMTarget)
Michael J. Spencer3a210e22010-09-13 23:59:48 +0000183
Oscar Fuentes44258d12010-09-28 22:38:39 +0000184# It is necessary to remove the `lib' prefix and the `.a'.
Michael J. Spencer3a210e22010-09-13 23:59:48 +0000185
186# This 'sed' script should do the trick:
187# 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
188
189include(LLVMLibDeps)