blob: c623bfaba8d123aceecbf5d7d6cbf3e406232f29 [file] [log] [blame]
Oscar Fuentesfd6daa82009-05-27 15:49:33 +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 )
Oscar Fuentesa5619db2009-11-19 23:21:43 +00008 set(system_libs ${system_libs} ${CMAKE_DL_LIBS})
Oscar Fuentesfd6daa82009-05-27 15:49:33 +00009 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
Oscar Fuentes6b3ebf02010-08-02 20:48:01 +000019function(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 Fuentescc767222010-08-02 21:24:12 +000026 if( capitalized_lib STREQUAL "LLVM${t}" OR
27 capitalized_lib STREQUAL "LLVM${t}CODEGEN" OR
Oscar Fuentes6b3ebf02010-08-02 20:48:01 +000028 capitalized_lib STREQUAL "LLVM${t}ASMPARSER" OR
Oscar Fuentescc767222010-08-02 21:24:12 +000029 capitalized_lib STREQUAL "LLVM${t}ASMPRINTER" OR
Oscar Fuentes6b3ebf02010-08-02 20:48:01 +000030 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
Oscar Fuentes0b2bdff2008-11-16 04:13:19 +000039macro(llvm_config executable)
Douglas Gregor5d3c4e12009-06-23 18:30:17 +000040 explicit_llvm_config(${executable} ${ARGN})
Oscar Fuentes0b2bdff2008-11-16 04:13:19 +000041endmacro(llvm_config)
Oscar Fuentes00905d52008-09-22 01:08:49 +000042
43
Douglas Gregor92aa9782009-06-04 19:53:37 +000044function(explicit_llvm_config executable)
Oscar Fuentes0b2bdff2008-11-16 04:13:19 +000045 set( link_components ${ARGN} )
Oscar Fuentes827283f2008-11-15 22:51:03 +000046
Douglas Gregor92aa9782009-06-04 19:53:37 +000047 explicit_map_components_to_libraries(LIBRARIES ${link_components})
Oscar Fuentes4688b412008-10-31 01:24:51 +000048 target_link_libraries(${executable} ${LIBRARIES})
Douglas Gregor92aa9782009-06-04 19:53:37 +000049endfunction(explicit_llvm_config)
Oscar Fuentes4688b412008-10-31 01:24:51 +000050
51
Oscar Fuentes8ccbab42010-08-09 03:26:43 +000052# 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})
Oscar Fuentes19559e12010-08-09 03:47:11 +000055 get_system_libs(sys_result)
56 set( ${OUT_VAR} ${result} ${sys_result} )
Oscar Fuentes8ccbab42010-08-09 03:26:43 +000057endfunction(llvm_map_components_to_libraries)
58
59
Douglas Gregor92aa9782009-06-04 19:53:37 +000060function(explicit_map_components_to_libraries out_libs)
Oscar Fuentes0b2bdff2008-11-16 04:13:19 +000061 set( link_components ${ARGN} )
Oscar Fuentes4688b412008-10-31 01:24:51 +000062 foreach(c ${link_components})
Daniel Dunbard2628562009-11-25 04:46:58 +000063 # add codegen, asmprinter, asmparser, disassembler
Oscar Fuentes827283f2008-11-15 22:51:03 +000064 list(FIND LLVM_TARGETS_TO_BUILD ${c} idx)
65 if( NOT idx LESS 0 )
Oscar Fuentes0b2bdff2008-11-16 04:13:19 +000066 list(FIND llvm_libs "LLVM${c}CodeGen" idx)
67 if( NOT idx LESS 0 )
68 list(APPEND expanded_components "LLVM${c}CodeGen")
69 else()
70 list(FIND llvm_libs "LLVM${c}" idx)
71 if( NOT idx LESS 0 )
72 list(APPEND expanded_components "LLVM${c}")
73 else()
74 message(FATAL_ERROR "Target ${c} is not in the set of libraries.")
75 endif()
76 endif()
Oscar Fuentes827283f2008-11-15 22:51:03 +000077 list(FIND llvm_libs "LLVM${c}AsmPrinter" asmidx)
78 if( NOT asmidx LESS 0 )
79 list(APPEND expanded_components "LLVM${c}AsmPrinter")
80 endif()
Daniel Dunbarc7df3cb2009-07-17 20:42:00 +000081 list(FIND llvm_libs "LLVM${c}AsmParser" asmidx)
82 if( NOT asmidx LESS 0 )
83 list(APPEND expanded_components "LLVM${c}AsmParser")
84 endif()
Daniel Dunbar1258e702009-07-15 07:52:36 +000085 list(FIND llvm_libs "LLVM${c}Info" asmidx)
86 if( NOT asmidx LESS 0 )
87 list(APPEND expanded_components "LLVM${c}Info")
88 endif()
Daniel Dunbard2628562009-11-25 04:46:58 +000089 list(FIND llvm_libs "LLVM${c}Disassembler" asmidx)
90 if( NOT asmidx LESS 0 )
91 list(APPEND expanded_components "LLVM${c}Disassembler")
92 endif()
Oscar Fuentes827283f2008-11-15 22:51:03 +000093 elseif( c STREQUAL "native" )
Duncan Sands42bbd4a2009-08-19 12:41:52 +000094 list(APPEND expanded_components "LLVM${LLVM_NATIVE_ARCH}CodeGen")
Oscar Fuentes4688b412008-10-31 01:24:51 +000095 elseif( c STREQUAL "nativecodegen" )
Duncan Sands42bbd4a2009-08-19 12:41:52 +000096 list(APPEND expanded_components "LLVM${LLVM_NATIVE_ARCH}CodeGen")
Oscar Fuentes4688b412008-10-31 01:24:51 +000097 elseif( c STREQUAL "backend" )
98 # same case as in `native'.
99 elseif( c STREQUAL "engine" )
100 # TODO: as we assume we are on X86, this is `jit'.
101 list(APPEND expanded_components "LLVMJIT")
Oscar Fuentes4688b412008-10-31 01:24:51 +0000102 elseif( c STREQUAL "all" )
103 list(APPEND expanded_components ${llvm_libs})
Oscar Fuentes827283f2008-11-15 22:51:03 +0000104 else( NOT idx LESS 0 )
Oscar Fuentes4688b412008-10-31 01:24:51 +0000105 list(APPEND expanded_components LLVM${c})
Oscar Fuentes827283f2008-11-15 22:51:03 +0000106 endif( NOT idx LESS 0 )
Oscar Fuentes4688b412008-10-31 01:24:51 +0000107 endforeach(c)
108 # We must match capitalization.
109 string(TOUPPER "${llvm_libs}" capitalized_libs)
Oscar Fuentes0b2bdff2008-11-16 04:13:19 +0000110 list(REMOVE_DUPLICATES expanded_components)
Oscar Fuentes4688b412008-10-31 01:24:51 +0000111 list(LENGTH expanded_components lst_size)
Oscar Fuentes9f2e0e22009-08-12 04:05:26 +0000112 set(result "")
113 while( 0 LESS ${lst_size} )
114 list(GET expanded_components 0 c)
Oscar Fuentes4688b412008-10-31 01:24:51 +0000115 string(TOUPPER "${c}" capitalized)
116 list(FIND capitalized_libs ${capitalized} idx)
Oscar Fuentescc767222010-08-02 21:24:12 +0000117 set(add_it ON)
Oscar Fuentes4688b412008-10-31 01:24:51 +0000118 if( idx LESS 0 )
Oscar Fuentes6b3ebf02010-08-02 20:48:01 +0000119 # The library is unkown. Maybe is an ommitted target?
120 is_llvm_target_library(${c} iltl_result)
Oscar Fuentescc767222010-08-02 21:24:12 +0000121 if( NOT iltl_result )
Oscar Fuentes6b3ebf02010-08-02 20:48:01 +0000122 message(FATAL_ERROR "Library ${c} not found in list of llvm libraries.")
123 endif()
Oscar Fuentescc767222010-08-02 21:24:12 +0000124 set(add_it OFF)
Oscar Fuentes4688b412008-10-31 01:24:51 +0000125 endif( idx LESS 0 )
126 list(GET llvm_libs ${idx} canonical_lib)
Oscar Fuentes9f2e0e22009-08-12 04:05:26 +0000127 list(REMOVE_ITEM result ${canonical_lib})
Oscar Fuentes9f2e0e22009-08-12 04:05:26 +0000128 foreach(c ${MSVC_LIB_DEPS_${canonical_lib}})
129 list(REMOVE_ITEM expanded_components ${c})
130 endforeach()
Oscar Fuentescc767222010-08-02 21:24:12 +0000131 if( add_it )
132 list(APPEND result ${canonical_lib})
133 list(APPEND expanded_components ${MSVC_LIB_DEPS_${canonical_lib}})
134 endif()
Oscar Fuentes9f2e0e22009-08-12 04:05:26 +0000135 list(REMOVE_AT expanded_components 0)
Oscar Fuentes4688b412008-10-31 01:24:51 +0000136 list(LENGTH expanded_components lst_size)
Oscar Fuentes9f2e0e22009-08-12 04:05:26 +0000137 endwhile( 0 LESS ${lst_size} )
Oscar Fuentes4688b412008-10-31 01:24:51 +0000138 set(${out_libs} ${result} PARENT_SCOPE)
Douglas Gregor92aa9782009-06-04 19:53:37 +0000139endfunction(explicit_map_components_to_libraries)
Oscar Fuentesac552472009-08-14 16:59:41 +0000140
141
142# The library dependency data is contained in the file
143# LLVMLibDeps.cmake on this directory. It is automatically generated
144# by tools/llvm-config/CMakeLists.txt when the build comprises all the
145# targets and we are on a environment Posix enough to build the
146# llvm-config script. This, in practice, just excludes MSVC.
147
148# When you remove or rename a library from the build, be sure to
149# remove its file from lib/ as well, or the GenLibDeps.pl script will
150# include it on its analysis!
151
152# The format generated by GenLibDeps.pl
153
154# LLVMARMAsmPrinter.o: LLVMARMCodeGen.o libLLVMAsmPrinter.a libLLVMCodeGen.a libLLVMCore.a libLLVMSupport.a libLLVMTarget.a
155
156# is translated to:
157
158# set(MSVC_LIB_DEPS_LLVMARMAsmPrinter LLVMARMCodeGen LLVMAsmPrinter LLVMCodeGen LLVMCore LLVMSupport LLVMTarget)
159
160# It is necessary to remove the `lib' prefix and the `.a'.
161
162# This 'sed' script should do the trick:
163# 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
164
165include(LLVMLibDeps)