blob: 349544edc3352181283aada90a831541c84c14d7 [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)
64 # Translate symbolic component names to real libraries:
Michael J. Spencer3a210e22010-09-13 23:59:48 +000065 foreach(c ${link_components})
66 # add codegen, asmprinter, asmparser, disassembler
67 list(FIND LLVM_TARGETS_TO_BUILD ${c} idx)
68 if( NOT idx LESS 0 )
69 list(FIND llvm_libs "LLVM${c}CodeGen" idx)
70 if( NOT idx LESS 0 )
71 list(APPEND expanded_components "LLVM${c}CodeGen")
72 else()
73 list(FIND llvm_libs "LLVM${c}" idx)
74 if( NOT idx LESS 0 )
75 list(APPEND expanded_components "LLVM${c}")
76 else()
77 message(FATAL_ERROR "Target ${c} is not in the set of libraries.")
78 endif()
79 endif()
80 list(FIND llvm_libs "LLVM${c}AsmPrinter" asmidx)
81 if( NOT asmidx LESS 0 )
82 list(APPEND expanded_components "LLVM${c}AsmPrinter")
83 endif()
84 list(FIND llvm_libs "LLVM${c}AsmParser" asmidx)
85 if( NOT asmidx LESS 0 )
86 list(APPEND expanded_components "LLVM${c}AsmParser")
87 endif()
88 list(FIND llvm_libs "LLVM${c}Info" asmidx)
89 if( NOT asmidx LESS 0 )
90 list(APPEND expanded_components "LLVM${c}Info")
91 endif()
92 list(FIND llvm_libs "LLVM${c}Disassembler" asmidx)
93 if( NOT asmidx LESS 0 )
94 list(APPEND expanded_components "LLVM${c}Disassembler")
95 endif()
96 elseif( c STREQUAL "native" )
97 list(APPEND expanded_components "LLVM${LLVM_NATIVE_ARCH}CodeGen")
98 elseif( c STREQUAL "nativecodegen" )
99 list(APPEND expanded_components "LLVM${LLVM_NATIVE_ARCH}CodeGen")
100 elseif( c STREQUAL "backend" )
101 # same case as in `native'.
102 elseif( c STREQUAL "engine" )
103 # TODO: as we assume we are on X86, this is `jit'.
104 list(APPEND expanded_components "LLVMJIT")
105 elseif( c STREQUAL "all" )
106 list(APPEND expanded_components ${llvm_libs})
107 else( NOT idx LESS 0 )
Oscar Fuentes44258d12010-09-28 22:38:39 +0000108 # Canonize the component name:
109 string(TOUPPER "${c}" capitalized)
110 list(FIND capitalized_libs LLVM${capitalized} lib_idx)
111 if( lib_idx LESS 0 )
112 # The component is unkown. Maybe is an ommitted target?
113 is_llvm_target_library(${c} iltl_result)
114 if( NOT iltl_result )
115 message(FATAL_ERROR "Library `${c}' not found in list of llvm libraries.")
116 endif()
117 else( lib_idx LESS 0 )
118 list(GET llvm_libs ${lib_idx} canonical_lib)
119 list(APPEND expanded_components ${canonical_lib})
120 endif( lib_idx LESS 0 )
Michael J. Spencer3a210e22010-09-13 23:59:48 +0000121 endif( NOT idx LESS 0 )
122 endforeach(c)
Oscar Fuentes44258d12010-09-28 22:38:39 +0000123 # Expand dependencies while topologically sorting the list of libraries:
Michael J. Spencer3a210e22010-09-13 23:59:48 +0000124 list(LENGTH expanded_components lst_size)
Oscar Fuentes44258d12010-09-28 22:38:39 +0000125 set(cursor 0)
126 set(processed)
127 while( cursor LESS lst_size )
128 list(GET expanded_components ${cursor} lib)
129 list(APPEND expanded_components ${MSVC_LIB_DEPS_${lib}})
130 # Remove duplicates at the front:
131 list(REVERSE expanded_components)
132 list(REMOVE_DUPLICATES expanded_components)
133 list(REVERSE expanded_components)
134 list(APPEND processed ${lib})
135 # Find the maximum index that doesn't have to be re-processed:
136 while(NOT "${expanded_components}" MATCHES "^${processed}.*" )
137 list(REMOVE_AT processed -1)
138 endwhile()
139 list(LENGTH processed cursor)
Michael J. Spencer3a210e22010-09-13 23:59:48 +0000140 list(LENGTH expanded_components lst_size)
Oscar Fuentes44258d12010-09-28 22:38:39 +0000141 endwhile( cursor LESS lst_size )
142 # Return just the libraries included in this build:
143 set(result)
144 foreach(c ${expanded_components})
145 list(FIND llvm_libs ${c} lib_idx)
146 if( NOT lib_idx LESS 0 )
147 set(result ${result} ${c})
148 endif()
149 endforeach(c)
Michael J. Spencer3a210e22010-09-13 23:59:48 +0000150 set(${out_libs} ${result} PARENT_SCOPE)
151endfunction(explicit_map_components_to_libraries)
152
153
154# The library dependency data is contained in the file
155# LLVMLibDeps.cmake on this directory. It is automatically generated
156# by tools/llvm-config/CMakeLists.txt when the build comprises all the
157# targets and we are on a environment Posix enough to build the
158# llvm-config script. This, in practice, just excludes MSVC.
159
160# When you remove or rename a library from the build, be sure to
161# remove its file from lib/ as well, or the GenLibDeps.pl script will
162# include it on its analysis!
163
164# The format generated by GenLibDeps.pl
165
Oscar Fuentes44258d12010-09-28 22:38:39 +0000166# LLVMARMAsmPrinter.o: LLVMARMCodeGen.o libLLVMAsmPrinter.a libLLVMCodeGen.a libLLVMCore.a libLLVMSupport.a libLLVMTarget.a
Michael J. Spencer3a210e22010-09-13 23:59:48 +0000167
168# is translated to:
169
Oscar Fuentes44258d12010-09-28 22:38:39 +0000170# set(MSVC_LIB_DEPS_LLVMARMAsmPrinter LLVMARMCodeGen LLVMAsmPrinter LLVMCodeGen LLVMCore LLVMSupport LLVMTarget)
Michael J. Spencer3a210e22010-09-13 23:59:48 +0000171
Oscar Fuentes44258d12010-09-28 22:38:39 +0000172# It is necessary to remove the `lib' prefix and the `.a'.
Michael J. Spencer3a210e22010-09-13 23:59:48 +0000173
174# This 'sed' script should do the trick:
175# 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
176
177include(LLVMLibDeps)