1function(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_LIBRT )
8        set(system_libs ${system_libs} rt)
9      endif()
10      if( HAVE_LIBDL )
11        set(system_libs ${system_libs} ${CMAKE_DL_LIBS})
12      endif()
13      if( LLVM_ENABLE_THREADS AND HAVE_LIBPTHREAD )
14        set(system_libs ${system_libs} pthread)
15      endif()
16      if ( LLVM_ENABLE_ZLIB AND HAVE_LIBZ )
17        set(system_libs ${system_libs} z)
18      endif()
19    endif( MINGW )
20  endif( NOT MSVC )
21  set(${return_var} ${system_libs} PARENT_SCOPE)
22endfunction(get_system_libs)
23
24
25function(link_system_libs target)
26  get_system_libs(llvm_system_libs)
27  target_link_libraries(${target} ${llvm_system_libs})
28endfunction(link_system_libs)
29
30
31function(is_llvm_target_library library return_var)
32  # Sets variable `return_var' to ON if `library' corresponds to a
33  # LLVM supported target. To OFF if it doesn't.
34  set(${return_var} OFF PARENT_SCOPE)
35  string(TOUPPER "${library}" capitalized_lib)
36  string(TOUPPER "${LLVM_ALL_TARGETS}" targets)
37  foreach(t ${targets})
38    if( capitalized_lib STREQUAL t OR
39	capitalized_lib STREQUAL "LLVM${t}" OR
40	capitalized_lib STREQUAL "LLVM${t}CODEGEN" OR
41	capitalized_lib STREQUAL "LLVM${t}ASMPARSER" OR
42	capitalized_lib STREQUAL "LLVM${t}ASMPRINTER" OR
43	capitalized_lib STREQUAL "LLVM${t}DISASSEMBLER" OR
44	capitalized_lib STREQUAL "LLVM${t}INFO" )
45      set(${return_var} ON PARENT_SCOPE)
46      break()
47    endif()
48  endforeach()
49endfunction(is_llvm_target_library)
50
51
52macro(llvm_config executable)
53  explicit_llvm_config(${executable} ${ARGN})
54endmacro(llvm_config)
55
56
57function(explicit_llvm_config executable)
58  set( link_components ${ARGN} )
59
60  explicit_map_components_to_libraries(LIBRARIES ${link_components})
61  target_link_libraries(${executable} ${LIBRARIES})
62endfunction(explicit_llvm_config)
63
64
65# This is a variant intended for the final user:
66function(llvm_map_components_to_libraries OUT_VAR)
67  explicit_map_components_to_libraries(result ${ARGN})
68  get_system_libs(sys_result)
69  set( ${OUT_VAR} ${result} ${sys_result} PARENT_SCOPE )
70endfunction(llvm_map_components_to_libraries)
71
72
73function(explicit_map_components_to_libraries out_libs)
74  set( link_components ${ARGN} )
75  get_property(llvm_libs GLOBAL PROPERTY LLVM_LIBS)
76  string(TOUPPER "${llvm_libs}" capitalized_libs)
77
78  # Expand some keywords:
79  list(FIND LLVM_TARGETS_TO_BUILD "${LLVM_NATIVE_ARCH}" have_native_backend)
80  list(FIND link_components "engine" engine_required)
81  if( NOT engine_required EQUAL -1 )
82    list(FIND LLVM_TARGETS_WITH_JIT "${LLVM_NATIVE_ARCH}" have_jit)
83    if( NOT have_native_backend EQUAL -1 AND NOT have_jit EQUAL -1 )
84      list(APPEND link_components "jit")
85      list(APPEND link_components "native")
86    else()
87      list(APPEND link_components "interpreter")
88    endif()
89  endif()
90  list(FIND link_components "native" native_required)
91  if( NOT native_required EQUAL -1 )
92    if( NOT have_native_backend EQUAL -1 )
93      list(APPEND link_components ${LLVM_NATIVE_ARCH})
94    endif()
95  endif()
96
97  # Translate symbolic component names to real libraries:
98  foreach(c ${link_components})
99    # add codegen, asmprinter, asmparser, disassembler
100    list(FIND LLVM_TARGETS_TO_BUILD ${c} idx)
101    if( NOT idx LESS 0 )
102      list(FIND llvm_libs "LLVM${c}CodeGen" idx)
103      if( NOT idx LESS 0 )
104	list(APPEND expanded_components "LLVM${c}CodeGen")
105      else()
106	list(FIND llvm_libs "LLVM${c}" idx)
107	if( NOT idx LESS 0 )
108	  list(APPEND expanded_components "LLVM${c}")
109	else()
110	  message(FATAL_ERROR "Target ${c} is not in the set of libraries.")
111	endif()
112      endif()
113      list(FIND llvm_libs "LLVM${c}AsmPrinter" asmidx)
114      if( NOT asmidx LESS 0 )
115        list(APPEND expanded_components "LLVM${c}AsmPrinter")
116      endif()
117      list(FIND llvm_libs "LLVM${c}AsmParser" asmidx)
118      if( NOT asmidx LESS 0 )
119        list(APPEND expanded_components "LLVM${c}AsmParser")
120      endif()
121      list(FIND llvm_libs "LLVM${c}Info" asmidx)
122      if( NOT asmidx LESS 0 )
123        list(APPEND expanded_components "LLVM${c}Info")
124      endif()
125      list(FIND llvm_libs "LLVM${c}Disassembler" asmidx)
126      if( NOT asmidx LESS 0 )
127        list(APPEND expanded_components "LLVM${c}Disassembler")
128      endif()
129    elseif( c STREQUAL "native" )
130      # already processed
131    elseif( c STREQUAL "nativecodegen" )
132      list(APPEND expanded_components "LLVM${LLVM_NATIVE_ARCH}CodeGen")
133    elseif( c STREQUAL "backend" )
134      # same case as in `native'.
135    elseif( c STREQUAL "engine" )
136      # already processed
137    elseif( c STREQUAL "all" )
138      list(APPEND expanded_components ${llvm_libs})
139    else( NOT idx LESS 0 )
140      # Canonize the component name:
141      string(TOUPPER "${c}" capitalized)
142      list(FIND capitalized_libs LLVM${capitalized} lib_idx)
143      if( lib_idx LESS 0 )
144        # The component is unknown. Maybe is an omitted target?
145        is_llvm_target_library(${c} iltl_result)
146        if( NOT iltl_result )
147          message(FATAL_ERROR "Library `${c}' not found in list of llvm libraries.")
148        endif()
149      else( lib_idx LESS 0 )
150        list(GET llvm_libs ${lib_idx} canonical_lib)
151        list(APPEND expanded_components ${canonical_lib})
152      endif( lib_idx LESS 0 )
153    endif( NOT idx LESS 0 )
154  endforeach(c)
155  # Expand dependencies while topologically sorting the list of libraries:
156  list(LENGTH expanded_components lst_size)
157  set(cursor 0)
158  set(processed)
159  while( cursor LESS lst_size )
160    list(GET expanded_components ${cursor} lib)
161    get_property(lib_deps GLOBAL PROPERTY LLVMBUILD_LIB_DEPS_${lib})
162    list(APPEND expanded_components ${lib_deps})
163    # Remove duplicates at the front:
164    list(REVERSE expanded_components)
165    list(REMOVE_DUPLICATES expanded_components)
166    list(REVERSE expanded_components)
167    list(APPEND processed ${lib})
168    # Find the maximum index that doesn't have to be re-processed:
169    while(NOT "${expanded_components}" MATCHES "^${processed}.*" )
170      list(REMOVE_AT processed -1)
171    endwhile()
172    list(LENGTH processed cursor)
173    list(LENGTH expanded_components lst_size)
174  endwhile( cursor LESS lst_size )
175  # Return just the libraries included in this build:
176  set(result)
177  foreach(c ${expanded_components})
178    list(FIND llvm_libs ${c} lib_idx)
179    if( NOT lib_idx LESS 0 )
180      set(result ${result} ${c})
181    endif()
182  endforeach(c)
183  set(${out_libs} ${result} PARENT_SCOPE)
184endfunction(explicit_map_components_to_libraries)
185