Michael J. Spencer | 3a210e2 | 2010-09-13 23:59:48 +0000 | [diff] [blame] | 1 | function(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 ) |
Chandler Carruth | 73c35d8 | 2013-01-04 23:19:55 +0000 | [diff] [blame] | 7 | if( HAVE_LIBRT ) |
| 8 | set(system_libs ${system_libs} rt) |
| 9 | endif() |
Michael J. Spencer | 3a210e2 | 2010-09-13 23:59:48 +0000 | [diff] [blame] | 10 | if( HAVE_LIBDL ) |
Chandler Carruth | 73c35d8 | 2013-01-04 23:19:55 +0000 | [diff] [blame] | 11 | set(system_libs ${system_libs} ${CMAKE_DL_LIBS}) |
Michael J. Spencer | 3a210e2 | 2010-09-13 23:59:48 +0000 | [diff] [blame] | 12 | endif() |
| 13 | if( LLVM_ENABLE_THREADS AND HAVE_LIBPTHREAD ) |
Chandler Carruth | 73c35d8 | 2013-01-04 23:19:55 +0000 | [diff] [blame] | 14 | set(system_libs ${system_libs} pthread) |
Michael J. Spencer | 3a210e2 | 2010-09-13 23:59:48 +0000 | [diff] [blame] | 15 | endif() |
Alexey Samsonov | ee03c94 | 2013-04-23 08:28:39 +0000 | [diff] [blame] | 16 | if ( LLVM_ENABLE_ZLIB AND HAVE_LIBZ ) |
| 17 | set(system_libs ${system_libs} z) |
| 18 | endif() |
Michael J. Spencer | 3a210e2 | 2010-09-13 23:59:48 +0000 | [diff] [blame] | 19 | endif( MINGW ) |
| 20 | endif( NOT MSVC ) |
| 21 | set(${return_var} ${system_libs} PARENT_SCOPE) |
| 22 | endfunction(get_system_libs) |
| 23 | |
| 24 | |
Oscar Fuentes | 23f0bfe | 2011-03-29 20:51:08 +0000 | [diff] [blame] | 25 | function(link_system_libs target) |
| 26 | get_system_libs(llvm_system_libs) |
| 27 | target_link_libraries(${target} ${llvm_system_libs}) |
| 28 | endfunction(link_system_libs) |
| 29 | |
| 30 | |
Michael J. Spencer | 3a210e2 | 2010-09-13 23:59:48 +0000 | [diff] [blame] | 31 | function(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}) |
Oscar Fuentes | 6704415 | 2011-03-15 14:53:53 +0000 | [diff] [blame] | 38 | if( capitalized_lib STREQUAL t OR |
| 39 | capitalized_lib STREQUAL "LLVM${t}" OR |
Michael J. Spencer | 3a210e2 | 2010-09-13 23:59:48 +0000 | [diff] [blame] | 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() |
| 49 | endfunction(is_llvm_target_library) |
| 50 | |
| 51 | |
| 52 | macro(llvm_config executable) |
| 53 | explicit_llvm_config(${executable} ${ARGN}) |
| 54 | endmacro(llvm_config) |
| 55 | |
| 56 | |
| 57 | function(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}) |
| 62 | endfunction(explicit_llvm_config) |
| 63 | |
| 64 | |
| 65 | # This is a variant intended for the final user: |
| 66 | function(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 ) |
| 70 | endfunction(llvm_map_components_to_libraries) |
| 71 | |
| 72 | |
| 73 | function(explicit_map_components_to_libraries out_libs) |
| 74 | set( link_components ${ARGN} ) |
Oscar Fuentes | 6d857ca | 2011-02-18 22:06:14 +0000 | [diff] [blame] | 75 | get_property(llvm_libs GLOBAL PROPERTY LLVM_LIBS) |
Oscar Fuentes | 44258d1 | 2010-09-28 22:38:39 +0000 | [diff] [blame] | 76 | string(TOUPPER "${llvm_libs}" capitalized_libs) |
Oscar Fuentes | 5eae24e | 2011-03-09 14:44:46 +0000 | [diff] [blame] | 77 | |
| 78 | # Expand some keywords: |
Oscar Fuentes | db3480c | 2011-03-23 17:42:13 +0000 | [diff] [blame] | 79 | list(FIND LLVM_TARGETS_TO_BUILD "${LLVM_NATIVE_ARCH}" have_native_backend) |
Oscar Fuentes | 5eae24e | 2011-03-09 14:44:46 +0000 | [diff] [blame] | 80 | list(FIND link_components "engine" engine_required) |
Oscar Fuentes | db3480c | 2011-03-23 17:42:13 +0000 | [diff] [blame] | 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() |
Oscar Fuentes | 5eae24e | 2011-03-09 14:44:46 +0000 | [diff] [blame] | 89 | endif() |
| 90 | list(FIND link_components "native" native_required) |
Oscar Fuentes | db3480c | 2011-03-23 17:42:13 +0000 | [diff] [blame] | 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() |
Oscar Fuentes | 5eae24e | 2011-03-09 14:44:46 +0000 | [diff] [blame] | 95 | endif() |
| 96 | |
Oscar Fuentes | 44258d1 | 2010-09-28 22:38:39 +0000 | [diff] [blame] | 97 | # Translate symbolic component names to real libraries: |
Michael J. Spencer | 3a210e2 | 2010-09-13 23:59:48 +0000 | [diff] [blame] | 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" ) |
Oscar Fuentes | 5eae24e | 2011-03-09 14:44:46 +0000 | [diff] [blame] | 130 | # already processed |
Michael J. Spencer | 3a210e2 | 2010-09-13 23:59:48 +0000 | [diff] [blame] | 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" ) |
Oscar Fuentes | 5eae24e | 2011-03-09 14:44:46 +0000 | [diff] [blame] | 136 | # already processed |
Michael J. Spencer | 3a210e2 | 2010-09-13 23:59:48 +0000 | [diff] [blame] | 137 | elseif( c STREQUAL "all" ) |
| 138 | list(APPEND expanded_components ${llvm_libs}) |
| 139 | else( NOT idx LESS 0 ) |
Oscar Fuentes | 44258d1 | 2010-09-28 22:38:39 +0000 | [diff] [blame] | 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 ) |
Chandler Carruth | 2e1513d | 2011-07-29 23:52:01 +0000 | [diff] [blame] | 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() |
Oscar Fuentes | 44258d1 | 2010-09-28 22:38:39 +0000 | [diff] [blame] | 149 | else( lib_idx LESS 0 ) |
Chandler Carruth | 2e1513d | 2011-07-29 23:52:01 +0000 | [diff] [blame] | 150 | list(GET llvm_libs ${lib_idx} canonical_lib) |
| 151 | list(APPEND expanded_components ${canonical_lib}) |
Oscar Fuentes | 44258d1 | 2010-09-28 22:38:39 +0000 | [diff] [blame] | 152 | endif( lib_idx LESS 0 ) |
Michael J. Spencer | 3a210e2 | 2010-09-13 23:59:48 +0000 | [diff] [blame] | 153 | endif( NOT idx LESS 0 ) |
| 154 | endforeach(c) |
Oscar Fuentes | 44258d1 | 2010-09-28 22:38:39 +0000 | [diff] [blame] | 155 | # Expand dependencies while topologically sorting the list of libraries: |
Michael J. Spencer | 3a210e2 | 2010-09-13 23:59:48 +0000 | [diff] [blame] | 156 | list(LENGTH expanded_components lst_size) |
Oscar Fuentes | 44258d1 | 2010-09-28 22:38:39 +0000 | [diff] [blame] | 157 | set(cursor 0) |
| 158 | set(processed) |
| 159 | while( cursor LESS lst_size ) |
| 160 | list(GET expanded_components ${cursor} lib) |
Daniel Dunbar | 288bc5c | 2011-11-29 01:31:52 +0000 | [diff] [blame] | 161 | get_property(lib_deps GLOBAL PROPERTY LLVMBUILD_LIB_DEPS_${lib}) |
Chandler Carruth | 2e1513d | 2011-07-29 23:52:01 +0000 | [diff] [blame] | 162 | list(APPEND expanded_components ${lib_deps}) |
Oscar Fuentes | 44258d1 | 2010-09-28 22:38:39 +0000 | [diff] [blame] | 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) |
Michael J. Spencer | 3a210e2 | 2010-09-13 23:59:48 +0000 | [diff] [blame] | 173 | list(LENGTH expanded_components lst_size) |
Oscar Fuentes | 44258d1 | 2010-09-28 22:38:39 +0000 | [diff] [blame] | 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) |
Michael J. Spencer | 3a210e2 | 2010-09-13 23:59:48 +0000 | [diff] [blame] | 183 | set(${out_libs} ${result} PARENT_SCOPE) |
| 184 | endfunction(explicit_map_components_to_libraries) |