Michael J. Spencer | 93c9b2e | 2010-09-13 23:59:48 +0000 | [diff] [blame] | 1 | function(get_system_libs return_var) |
NAKAMURA Takumi | af2c113 | 2014-02-23 06:27:04 +0000 | [diff] [blame] | 2 | message(AUTHOR_WARNING "get_system_libs no longer needed") |
| 3 | set(${return_var} "" PARENT_SCOPE) |
| 4 | endfunction() |
Michael J. Spencer | 93c9b2e | 2010-09-13 23:59:48 +0000 | [diff] [blame] | 5 | |
| 6 | |
Oscar Fuentes | 978e528 | 2011-03-29 20:51:08 +0000 | [diff] [blame] | 7 | function(link_system_libs target) |
NAKAMURA Takumi | af2c113 | 2014-02-23 06:27:04 +0000 | [diff] [blame] | 8 | message(AUTHOR_WARNING "link_system_libs no longer needed") |
| 9 | endfunction() |
Oscar Fuentes | 978e528 | 2011-03-29 20:51:08 +0000 | [diff] [blame] | 10 | |
| 11 | |
Michael J. Spencer | 93c9b2e | 2010-09-13 23:59:48 +0000 | [diff] [blame] | 12 | function(is_llvm_target_library library return_var) |
| 13 | # Sets variable `return_var' to ON if `library' corresponds to a |
| 14 | # LLVM supported target. To OFF if it doesn't. |
| 15 | set(${return_var} OFF PARENT_SCOPE) |
| 16 | string(TOUPPER "${library}" capitalized_lib) |
| 17 | string(TOUPPER "${LLVM_ALL_TARGETS}" targets) |
| 18 | foreach(t ${targets}) |
Oscar Fuentes | 638b8b7 | 2011-03-15 14:53:53 +0000 | [diff] [blame] | 19 | if( capitalized_lib STREQUAL t OR |
NAKAMURA Takumi | 7829337 | 2014-02-02 16:46:35 +0000 | [diff] [blame] | 20 | capitalized_lib STREQUAL "LLVM${t}" OR |
| 21 | capitalized_lib STREQUAL "LLVM${t}CODEGEN" OR |
| 22 | capitalized_lib STREQUAL "LLVM${t}ASMPARSER" OR |
| 23 | capitalized_lib STREQUAL "LLVM${t}ASMPRINTER" OR |
| 24 | capitalized_lib STREQUAL "LLVM${t}DISASSEMBLER" OR |
| 25 | capitalized_lib STREQUAL "LLVM${t}INFO" ) |
Michael J. Spencer | 93c9b2e | 2010-09-13 23:59:48 +0000 | [diff] [blame] | 26 | set(${return_var} ON PARENT_SCOPE) |
| 27 | break() |
| 28 | endif() |
| 29 | endforeach() |
| 30 | endfunction(is_llvm_target_library) |
| 31 | |
| 32 | |
| 33 | macro(llvm_config executable) |
Andrew Wilkins | bb6d95f | 2015-09-05 08:27:33 +0000 | [diff] [blame] | 34 | cmake_parse_arguments(ARG "USE_SHARED" "" "" ${ARGN}) |
| 35 | set(link_components ${ARG_UNPARSED_ARGUMENTS}) |
| 36 | |
| 37 | if(USE_SHARED) |
| 38 | # If USE_SHARED is specified, then we link against libLLVM, |
| 39 | # but also against the component libraries below. This is |
| 40 | # done in case libLLVM does not contain all of the components |
| 41 | # the target requires. |
| 42 | # |
| 43 | # TODO strip LLVM_DYLIB_COMPONENTS out of link_components. |
| 44 | # To do this, we need special handling for "all", since that |
| 45 | # may imply linking to libraries that are not included in |
| 46 | # libLLVM. |
| 47 | target_link_libraries(${executable} LLVM) |
| 48 | endif() |
| 49 | |
| 50 | explicit_llvm_config(${executable} ${link_components}) |
Michael J. Spencer | 93c9b2e | 2010-09-13 23:59:48 +0000 | [diff] [blame] | 51 | endmacro(llvm_config) |
| 52 | |
| 53 | |
| 54 | function(explicit_llvm_config executable) |
| 55 | set( link_components ${ARGN} ) |
| 56 | |
NAKAMURA Takumi | 623055b | 2014-02-10 03:24:19 +0000 | [diff] [blame] | 57 | llvm_map_components_to_libnames(LIBRARIES ${link_components}) |
NAKAMURA Takumi | 955d27a | 2014-02-26 06:53:16 +0000 | [diff] [blame] | 58 | get_target_property(t ${executable} TYPE) |
Richard Smith | 571b0b9 | 2014-09-26 21:33:05 +0000 | [diff] [blame] | 59 | if("x${t}" STREQUAL "xSTATIC_LIBRARY") |
Chris Bieneman | 6a1b54a | 2015-03-23 20:03:57 +0000 | [diff] [blame] | 60 | target_link_libraries(${executable} INTERFACE ${LIBRARIES}) |
Richard Smith | 571b0b9 | 2014-09-26 21:33:05 +0000 | [diff] [blame] | 61 | elseif("x${t}" STREQUAL "xSHARED_LIBRARY" OR "x${t}" STREQUAL "xMODULE_LIBRARY") |
Chris Bieneman | 6a1b54a | 2015-03-23 20:03:57 +0000 | [diff] [blame] | 62 | target_link_libraries(${executable} PRIVATE ${LIBRARIES}) |
NAKAMURA Takumi | 955d27a | 2014-02-26 06:53:16 +0000 | [diff] [blame] | 63 | else() |
| 64 | # Use plain form for legacy user. |
| 65 | target_link_libraries(${executable} ${LIBRARIES}) |
| 66 | endif() |
Michael J. Spencer | 93c9b2e | 2010-09-13 23:59:48 +0000 | [diff] [blame] | 67 | endfunction(explicit_llvm_config) |
| 68 | |
| 69 | |
Dan Liew | 544f45b | 2014-07-28 13:36:50 +0000 | [diff] [blame] | 70 | # This is Deprecated |
Michael J. Spencer | 93c9b2e | 2010-09-13 23:59:48 +0000 | [diff] [blame] | 71 | function(llvm_map_components_to_libraries OUT_VAR) |
Dan Liew | 544f45b | 2014-07-28 13:36:50 +0000 | [diff] [blame] | 72 | message(AUTHOR_WARNING "Using llvm_map_components_to_libraries() is deprecated. Use llvm_map_components_to_libnames() instead") |
Michael J. Spencer | 93c9b2e | 2010-09-13 23:59:48 +0000 | [diff] [blame] | 73 | explicit_map_components_to_libraries(result ${ARGN}) |
Michael J. Spencer | 93c9b2e | 2010-09-13 23:59:48 +0000 | [diff] [blame] | 74 | set( ${OUT_VAR} ${result} ${sys_result} PARENT_SCOPE ) |
| 75 | endfunction(llvm_map_components_to_libraries) |
| 76 | |
Dan Liew | 544f45b | 2014-07-28 13:36:50 +0000 | [diff] [blame] | 77 | # This is a variant intended for the final user: |
NAKAMURA Takumi | 906dad8 | 2014-02-04 14:42:04 +0000 | [diff] [blame] | 78 | # Map LINK_COMPONENTS to actual libnames. |
| 79 | function(llvm_map_components_to_libnames out_libs) |
Michael J. Spencer | 93c9b2e | 2010-09-13 23:59:48 +0000 | [diff] [blame] | 80 | set( link_components ${ARGN} ) |
NAKAMURA Takumi | 12fedb0 | 2014-02-21 14:17:07 +0000 | [diff] [blame] | 81 | if(NOT LLVM_AVAILABLE_LIBS) |
| 82 | # Inside LLVM itself available libs are in a global property. |
| 83 | get_property(LLVM_AVAILABLE_LIBS GLOBAL PROPERTY LLVM_LIBS) |
| 84 | endif() |
| 85 | string(TOUPPER "${LLVM_AVAILABLE_LIBS}" capitalized_libs) |
Oscar Fuentes | e503506 | 2011-03-09 14:44:46 +0000 | [diff] [blame] | 86 | |
| 87 | # Expand some keywords: |
Oscar Fuentes | 465f936 | 2011-03-23 17:42:13 +0000 | [diff] [blame] | 88 | list(FIND LLVM_TARGETS_TO_BUILD "${LLVM_NATIVE_ARCH}" have_native_backend) |
Oscar Fuentes | e503506 | 2011-03-09 14:44:46 +0000 | [diff] [blame] | 89 | list(FIND link_components "engine" engine_required) |
Oscar Fuentes | 465f936 | 2011-03-23 17:42:13 +0000 | [diff] [blame] | 90 | if( NOT engine_required EQUAL -1 ) |
| 91 | list(FIND LLVM_TARGETS_WITH_JIT "${LLVM_NATIVE_ARCH}" have_jit) |
| 92 | if( NOT have_native_backend EQUAL -1 AND NOT have_jit EQUAL -1 ) |
| 93 | list(APPEND link_components "jit") |
| 94 | list(APPEND link_components "native") |
| 95 | else() |
| 96 | list(APPEND link_components "interpreter") |
| 97 | endif() |
Oscar Fuentes | e503506 | 2011-03-09 14:44:46 +0000 | [diff] [blame] | 98 | endif() |
| 99 | list(FIND link_components "native" native_required) |
Oscar Fuentes | 465f936 | 2011-03-23 17:42:13 +0000 | [diff] [blame] | 100 | if( NOT native_required EQUAL -1 ) |
| 101 | if( NOT have_native_backend EQUAL -1 ) |
| 102 | list(APPEND link_components ${LLVM_NATIVE_ARCH}) |
| 103 | endif() |
Oscar Fuentes | e503506 | 2011-03-09 14:44:46 +0000 | [diff] [blame] | 104 | endif() |
| 105 | |
Oscar Fuentes | 18811d5 | 2010-09-28 22:38:39 +0000 | [diff] [blame] | 106 | # Translate symbolic component names to real libraries: |
Michael J. Spencer | 93c9b2e | 2010-09-13 23:59:48 +0000 | [diff] [blame] | 107 | foreach(c ${link_components}) |
| 108 | # add codegen, asmprinter, asmparser, disassembler |
| 109 | list(FIND LLVM_TARGETS_TO_BUILD ${c} idx) |
| 110 | if( NOT idx LESS 0 ) |
NAKAMURA Takumi | 1956c49 | 2014-02-21 14:16:52 +0000 | [diff] [blame] | 111 | if( TARGET LLVM${c}CodeGen ) |
NAKAMURA Takumi | 7829337 | 2014-02-02 16:46:35 +0000 | [diff] [blame] | 112 | list(APPEND expanded_components "LLVM${c}CodeGen") |
Michael J. Spencer | 93c9b2e | 2010-09-13 23:59:48 +0000 | [diff] [blame] | 113 | else() |
NAKAMURA Takumi | 1956c49 | 2014-02-21 14:16:52 +0000 | [diff] [blame] | 114 | if( TARGET LLVM${c} ) |
NAKAMURA Takumi | 7829337 | 2014-02-02 16:46:35 +0000 | [diff] [blame] | 115 | list(APPEND expanded_components "LLVM${c}") |
| 116 | else() |
| 117 | message(FATAL_ERROR "Target ${c} is not in the set of libraries.") |
| 118 | endif() |
Michael J. Spencer | 93c9b2e | 2010-09-13 23:59:48 +0000 | [diff] [blame] | 119 | endif() |
NAKAMURA Takumi | 1956c49 | 2014-02-21 14:16:52 +0000 | [diff] [blame] | 120 | if( TARGET LLVM${c}AsmPrinter ) |
Michael J. Spencer | 93c9b2e | 2010-09-13 23:59:48 +0000 | [diff] [blame] | 121 | list(APPEND expanded_components "LLVM${c}AsmPrinter") |
| 122 | endif() |
NAKAMURA Takumi | 1956c49 | 2014-02-21 14:16:52 +0000 | [diff] [blame] | 123 | if( TARGET LLVM${c}AsmParser ) |
Michael J. Spencer | 93c9b2e | 2010-09-13 23:59:48 +0000 | [diff] [blame] | 124 | list(APPEND expanded_components "LLVM${c}AsmParser") |
| 125 | endif() |
NAKAMURA Takumi | 5c40508 | 2014-07-14 05:07:07 +0000 | [diff] [blame] | 126 | if( TARGET LLVM${c}Desc ) |
| 127 | list(APPEND expanded_components "LLVM${c}Desc") |
| 128 | endif() |
NAKAMURA Takumi | 1956c49 | 2014-02-21 14:16:52 +0000 | [diff] [blame] | 129 | if( TARGET LLVM${c}Info ) |
Michael J. Spencer | 93c9b2e | 2010-09-13 23:59:48 +0000 | [diff] [blame] | 130 | list(APPEND expanded_components "LLVM${c}Info") |
| 131 | endif() |
NAKAMURA Takumi | 1956c49 | 2014-02-21 14:16:52 +0000 | [diff] [blame] | 132 | if( TARGET LLVM${c}Disassembler ) |
Michael J. Spencer | 93c9b2e | 2010-09-13 23:59:48 +0000 | [diff] [blame] | 133 | list(APPEND expanded_components "LLVM${c}Disassembler") |
| 134 | endif() |
| 135 | elseif( c STREQUAL "native" ) |
Oscar Fuentes | e503506 | 2011-03-09 14:44:46 +0000 | [diff] [blame] | 136 | # already processed |
Michael J. Spencer | 93c9b2e | 2010-09-13 23:59:48 +0000 | [diff] [blame] | 137 | elseif( c STREQUAL "nativecodegen" ) |
| 138 | list(APPEND expanded_components "LLVM${LLVM_NATIVE_ARCH}CodeGen") |
NAKAMURA Takumi | 5c40508 | 2014-07-14 05:07:07 +0000 | [diff] [blame] | 139 | if( TARGET LLVM${LLVM_NATIVE_ARCH}Desc ) |
| 140 | list(APPEND expanded_components "LLVM${LLVM_NATIVE_ARCH}Desc") |
| 141 | endif() |
| 142 | if( TARGET LLVM${LLVM_NATIVE_ARCH}Info ) |
| 143 | list(APPEND expanded_components "LLVM${LLVM_NATIVE_ARCH}Info") |
| 144 | endif() |
Michael J. Spencer | 93c9b2e | 2010-09-13 23:59:48 +0000 | [diff] [blame] | 145 | elseif( c STREQUAL "backend" ) |
| 146 | # same case as in `native'. |
| 147 | elseif( c STREQUAL "engine" ) |
Oscar Fuentes | e503506 | 2011-03-09 14:44:46 +0000 | [diff] [blame] | 148 | # already processed |
Michael J. Spencer | 93c9b2e | 2010-09-13 23:59:48 +0000 | [diff] [blame] | 149 | elseif( c STREQUAL "all" ) |
NAKAMURA Takumi | 12fedb0 | 2014-02-21 14:17:07 +0000 | [diff] [blame] | 150 | list(APPEND expanded_components ${LLVM_AVAILABLE_LIBS}) |
Pete Cooper | 468d6d7 | 2015-04-20 18:22:05 +0000 | [diff] [blame] | 151 | elseif( c STREQUAL "AllTargetsAsmPrinters" ) |
| 152 | # Link all the asm printers from all the targets |
| 153 | foreach(t ${LLVM_TARGETS_TO_BUILD}) |
| 154 | if( TARGET LLVM${t}AsmPrinter ) |
| 155 | list(APPEND expanded_components "LLVM${t}AsmPrinter") |
| 156 | endif() |
| 157 | endforeach(t) |
| 158 | elseif( c STREQUAL "AllTargetsAsmParsers" ) |
| 159 | # Link all the asm parsers from all the targets |
| 160 | foreach(t ${LLVM_TARGETS_TO_BUILD}) |
| 161 | if( TARGET LLVM${t}AsmParser ) |
| 162 | list(APPEND expanded_components "LLVM${t}AsmParser") |
| 163 | endif() |
| 164 | endforeach(t) |
| 165 | elseif( c STREQUAL "AllTargetsDescs" ) |
| 166 | # Link all the descs from all the targets |
| 167 | foreach(t ${LLVM_TARGETS_TO_BUILD}) |
| 168 | if( TARGET LLVM${t}Desc ) |
| 169 | list(APPEND expanded_components "LLVM${t}Desc") |
| 170 | endif() |
| 171 | endforeach(t) |
| 172 | elseif( c STREQUAL "AllTargetsDisassemblers" ) |
| 173 | # Link all the disassemblers from all the targets |
| 174 | foreach(t ${LLVM_TARGETS_TO_BUILD}) |
| 175 | if( TARGET LLVM${t}Disassembler ) |
| 176 | list(APPEND expanded_components "LLVM${t}Disassembler") |
| 177 | endif() |
| 178 | endforeach(t) |
| 179 | elseif( c STREQUAL "AllTargetsInfos" ) |
| 180 | # Link all the infos from all the targets |
| 181 | foreach(t ${LLVM_TARGETS_TO_BUILD}) |
| 182 | if( TARGET LLVM${t}Info ) |
| 183 | list(APPEND expanded_components "LLVM${t}Info") |
| 184 | endif() |
| 185 | endforeach(t) |
Michael J. Spencer | 93c9b2e | 2010-09-13 23:59:48 +0000 | [diff] [blame] | 186 | else( NOT idx LESS 0 ) |
Oscar Fuentes | 18811d5 | 2010-09-28 22:38:39 +0000 | [diff] [blame] | 187 | # Canonize the component name: |
| 188 | string(TOUPPER "${c}" capitalized) |
| 189 | list(FIND capitalized_libs LLVM${capitalized} lib_idx) |
| 190 | if( lib_idx LESS 0 ) |
Chandler Carruth | 68b2311 | 2011-07-29 23:52:01 +0000 | [diff] [blame] | 191 | # The component is unknown. Maybe is an omitted target? |
| 192 | is_llvm_target_library(${c} iltl_result) |
| 193 | if( NOT iltl_result ) |
| 194 | message(FATAL_ERROR "Library `${c}' not found in list of llvm libraries.") |
| 195 | endif() |
Oscar Fuentes | 18811d5 | 2010-09-28 22:38:39 +0000 | [diff] [blame] | 196 | else( lib_idx LESS 0 ) |
NAKAMURA Takumi | 12fedb0 | 2014-02-21 14:17:07 +0000 | [diff] [blame] | 197 | list(GET LLVM_AVAILABLE_LIBS ${lib_idx} canonical_lib) |
Chandler Carruth | 68b2311 | 2011-07-29 23:52:01 +0000 | [diff] [blame] | 198 | list(APPEND expanded_components ${canonical_lib}) |
Oscar Fuentes | 18811d5 | 2010-09-28 22:38:39 +0000 | [diff] [blame] | 199 | endif( lib_idx LESS 0 ) |
Michael J. Spencer | 93c9b2e | 2010-09-13 23:59:48 +0000 | [diff] [blame] | 200 | endif( NOT idx LESS 0 ) |
| 201 | endforeach(c) |
NAKAMURA Takumi | 906dad8 | 2014-02-04 14:42:04 +0000 | [diff] [blame] | 202 | |
| 203 | set(${out_libs} ${expanded_components} PARENT_SCOPE) |
| 204 | endfunction() |
| 205 | |
Peter Zotov | f94eed6 | 2014-12-18 23:56:52 +0000 | [diff] [blame] | 206 | # Perform a post-order traversal of the dependency graph. |
| 207 | # This duplicates the algorithm used by llvm-config, originally |
| 208 | # in tools/llvm-config/llvm-config.cpp, function ComputeLibsForComponents. |
| 209 | function(expand_topologically name required_libs visited_libs) |
| 210 | list(FIND visited_libs ${name} found) |
| 211 | if( found LESS 0 ) |
| 212 | list(APPEND visited_libs ${name}) |
| 213 | set(visited_libs ${visited_libs} PARENT_SCOPE) |
| 214 | |
| 215 | get_property(lib_deps GLOBAL PROPERTY LLVMBUILD_LIB_DEPS_${name}) |
| 216 | foreach( lib_dep ${lib_deps} ) |
| 217 | expand_topologically(${lib_dep} "${required_libs}" "${visited_libs}") |
| 218 | set(required_libs ${required_libs} PARENT_SCOPE) |
| 219 | set(visited_libs ${visited_libs} PARENT_SCOPE) |
| 220 | endforeach() |
| 221 | |
| 222 | list(APPEND required_libs ${name}) |
| 223 | set(required_libs ${required_libs} PARENT_SCOPE) |
| 224 | endif() |
| 225 | endfunction() |
| 226 | |
NAKAMURA Takumi | 906dad8 | 2014-02-04 14:42:04 +0000 | [diff] [blame] | 227 | # Expand dependencies while topologically sorting the list of libraries: |
| 228 | function(llvm_expand_dependencies out_libs) |
| 229 | set(expanded_components ${ARGN}) |
Peter Zotov | f94eed6 | 2014-12-18 23:56:52 +0000 | [diff] [blame] | 230 | |
| 231 | set(required_libs) |
| 232 | set(visited_libs) |
| 233 | foreach( lib ${expanded_components} ) |
| 234 | expand_topologically(${lib} "${required_libs}" "${visited_libs}") |
| 235 | endforeach() |
| 236 | |
| 237 | list(REVERSE required_libs) |
| 238 | set(${out_libs} ${required_libs} PARENT_SCOPE) |
NAKAMURA Takumi | 906dad8 | 2014-02-04 14:42:04 +0000 | [diff] [blame] | 239 | endfunction() |
| 240 | |
| 241 | function(explicit_map_components_to_libraries out_libs) |
| 242 | llvm_map_components_to_libnames(link_libs ${ARGN}) |
| 243 | llvm_expand_dependencies(expanded_components ${link_libs}) |
Oscar Fuentes | 18811d5 | 2010-09-28 22:38:39 +0000 | [diff] [blame] | 244 | # Return just the libraries included in this build: |
| 245 | set(result) |
| 246 | foreach(c ${expanded_components}) |
NAKAMURA Takumi | 1956c49 | 2014-02-21 14:16:52 +0000 | [diff] [blame] | 247 | if( TARGET ${c} ) |
Oscar Fuentes | 18811d5 | 2010-09-28 22:38:39 +0000 | [diff] [blame] | 248 | set(result ${result} ${c}) |
| 249 | endif() |
| 250 | endforeach(c) |
Michael J. Spencer | 93c9b2e | 2010-09-13 23:59:48 +0000 | [diff] [blame] | 251 | set(${out_libs} ${result} PARENT_SCOPE) |
| 252 | endfunction(explicit_map_components_to_libraries) |