1 function(get_system_libs return_var)
2 # Returns in `return_var' a list of system libraries used by LLVM.
5 set(system_libs ${system_libs} imagehlp psapi)
6 elseif( CMAKE_HOST_UNIX )
8 set(system_libs ${system_libs} ${CMAKE_DL_LIBS})
10 if( LLVM_ENABLE_THREADS AND HAVE_LIBPTHREAD )
11 set(system_libs ${system_libs} pthread)
15 set(${return_var} ${system_libs} PARENT_SCOPE)
16 endfunction(get_system_libs)
19 function(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)
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)
36 endfunction(is_llvm_target_library)
39 macro(llvm_config executable)
40 explicit_llvm_config(${executable} ${ARGN})
44 function(explicit_llvm_config executable)
45 set( link_components ${ARGN} )
47 explicit_map_components_to_libraries(LIBRARIES ${link_components})
48 target_link_libraries(${executable} ${LIBRARIES})
49 endfunction(explicit_llvm_config)
52 # This is a variant intended for the final user:
53 function(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 )
57 endfunction(llvm_map_components_to_libraries)
60 function(explicit_map_components_to_libraries out_libs)
61 set( link_components ${ARGN} )
62 string(TOUPPER "${llvm_libs}" capitalized_libs)
63 # Translate symbolic component names to real libraries:
64 foreach(c ${link_components})
65 # add codegen, asmprinter, asmparser, disassembler
66 list(FIND LLVM_TARGETS_TO_BUILD ${c} idx)
68 list(FIND llvm_libs "LLVM${c}CodeGen" idx)
70 list(APPEND expanded_components "LLVM${c}CodeGen")
72 list(FIND llvm_libs "LLVM${c}" idx)
74 list(APPEND expanded_components "LLVM${c}")
76 message(FATAL_ERROR "Target ${c} is not in the set of libraries.")
79 list(FIND llvm_libs "LLVM${c}AsmPrinter" asmidx)
80 if( NOT asmidx LESS 0 )
81 list(APPEND expanded_components "LLVM${c}AsmPrinter")
83 list(FIND llvm_libs "LLVM${c}AsmParser" asmidx)
84 if( NOT asmidx LESS 0 )
85 list(APPEND expanded_components "LLVM${c}AsmParser")
87 list(FIND llvm_libs "LLVM${c}Info" asmidx)
88 if( NOT asmidx LESS 0 )
89 list(APPEND expanded_components "LLVM${c}Info")
91 list(FIND llvm_libs "LLVM${c}Disassembler" asmidx)
92 if( NOT asmidx LESS 0 )
93 list(APPEND expanded_components "LLVM${c}Disassembler")
95 elseif( c STREQUAL "native" )
96 list(APPEND expanded_components "LLVM${LLVM_NATIVE_ARCH}CodeGen")
97 elseif( c STREQUAL "nativecodegen" )
98 list(APPEND expanded_components "LLVM${LLVM_NATIVE_ARCH}CodeGen")
99 elseif( c STREQUAL "backend" )
100 # same case as in `native'.
101 elseif( c STREQUAL "engine" )
102 # TODO: as we assume we are on X86, this is `jit'.
103 list(APPEND expanded_components "LLVMJIT")
104 elseif( c STREQUAL "all" )
105 list(APPEND expanded_components ${llvm_libs})
106 else( NOT idx LESS 0 )
107 # Canonize the component name:
108 string(TOUPPER "${c}" capitalized)
109 list(FIND capitalized_libs LLVM${capitalized} lib_idx)
111 # The component is unkown. Maybe is an ommitted target?
112 is_llvm_target_library(${c} iltl_result)
113 if( NOT iltl_result )
114 message(FATAL_ERROR "Library `${c}' not found in list of llvm libraries.")
116 else( lib_idx LESS 0 )
117 list(GET llvm_libs ${lib_idx} canonical_lib)
118 list(APPEND expanded_components ${canonical_lib})
119 endif( lib_idx LESS 0 )
120 endif( NOT idx LESS 0 )
122 # Expand dependencies while topologically sorting the list of libraries:
123 list(LENGTH expanded_components lst_size)
126 while( cursor LESS lst_size )
127 list(GET expanded_components ${cursor} lib)
128 list(APPEND expanded_components ${MSVC_LIB_DEPS_${lib}})
129 # Remove duplicates at the front:
130 list(REVERSE expanded_components)
131 list(REMOVE_DUPLICATES expanded_components)
132 list(REVERSE expanded_components)
133 list(APPEND processed ${lib})
134 # Find the maximum index that doesn't have to be re-processed:
135 while(NOT "${expanded_components}" MATCHES "^${processed}.*" )
136 list(REMOVE_AT processed -1)
138 list(LENGTH processed cursor)
139 list(LENGTH expanded_components lst_size)
140 endwhile( cursor LESS lst_size )
141 # Return just the libraries included in this build:
143 foreach(c ${expanded_components})
144 list(FIND llvm_libs ${c} lib_idx)
145 if( NOT lib_idx LESS 0 )
146 set(result ${result} ${c})
149 set(${out_libs} ${result} PARENT_SCOPE)
150 endfunction(explicit_map_components_to_libraries)
153 # The library dependency data is contained in the file
154 # LLVMLibDeps.cmake on this directory. It is automatically generated
155 # by tools/llvm-config/CMakeLists.txt when the build comprises all the
156 # targets and we are on a environment Posix enough to build the
157 # llvm-config script. This, in practice, just excludes MSVC.
159 # When you remove or rename a library from the build, be sure to
160 # remove its file from lib/ as well, or the GenLibDeps.pl script will
161 # include it on its analysis!
163 # The format generated by GenLibDeps.pl
165 # LLVMARMAsmPrinter.o: LLVMARMCodeGen.o libLLVMAsmPrinter.a libLLVMCodeGen.a libLLVMCore.a libLLVMSupport.a libLLVMTarget.a
169 # set(MSVC_LIB_DEPS_LLVMARMAsmPrinter LLVMARMCodeGen LLVMAsmPrinter LLVMCodeGen LLVMCore LLVMSupport LLVMTarget)
171 # It is necessary to remove the `lib' prefix and the `.a'.
173 # This 'sed' script should do the trick:
174 # 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