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 ) |
| 7 | if( HAVE_LIBDL ) |
| 8 | set(system_libs ${system_libs} ${CMAKE_DL_LIBS}) |
| 9 | 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) |
| 16 | endfunction(get_system_libs) |
| 17 | |
| 18 | |
| 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) |
| 25 | foreach(t ${targets}) |
Oscar Fuentes | 6704415 | 2011-03-15 14:53:53 +0000 | [diff] [blame] | 26 | if( capitalized_lib STREQUAL t OR |
| 27 | capitalized_lib STREQUAL "LLVM${t}" OR |
Michael J. Spencer | 3a210e2 | 2010-09-13 23:59:48 +0000 | [diff] [blame] | 28 | capitalized_lib STREQUAL "LLVM${t}CODEGEN" OR |
| 29 | capitalized_lib STREQUAL "LLVM${t}ASMPARSER" OR |
| 30 | capitalized_lib STREQUAL "LLVM${t}ASMPRINTER" OR |
| 31 | capitalized_lib STREQUAL "LLVM${t}DISASSEMBLER" OR |
| 32 | capitalized_lib STREQUAL "LLVM${t}INFO" ) |
| 33 | set(${return_var} ON PARENT_SCOPE) |
| 34 | break() |
| 35 | endif() |
| 36 | endforeach() |
| 37 | endfunction(is_llvm_target_library) |
| 38 | |
| 39 | |
| 40 | macro(llvm_config executable) |
| 41 | explicit_llvm_config(${executable} ${ARGN}) |
| 42 | endmacro(llvm_config) |
| 43 | |
| 44 | |
| 45 | function(explicit_llvm_config executable) |
| 46 | set( link_components ${ARGN} ) |
| 47 | |
| 48 | explicit_map_components_to_libraries(LIBRARIES ${link_components}) |
| 49 | target_link_libraries(${executable} ${LIBRARIES}) |
| 50 | endfunction(explicit_llvm_config) |
| 51 | |
| 52 | |
| 53 | # This is a variant intended for the final user: |
| 54 | function(llvm_map_components_to_libraries OUT_VAR) |
| 55 | explicit_map_components_to_libraries(result ${ARGN}) |
| 56 | get_system_libs(sys_result) |
| 57 | set( ${OUT_VAR} ${result} ${sys_result} PARENT_SCOPE ) |
| 58 | endfunction(llvm_map_components_to_libraries) |
| 59 | |
| 60 | |
| 61 | function(explicit_map_components_to_libraries out_libs) |
| 62 | set( link_components ${ARGN} ) |
Oscar Fuentes | 6d857ca | 2011-02-18 22:06:14 +0000 | [diff] [blame] | 63 | get_property(llvm_libs GLOBAL PROPERTY LLVM_LIBS) |
Oscar Fuentes | 44258d1 | 2010-09-28 22:38:39 +0000 | [diff] [blame] | 64 | string(TOUPPER "${llvm_libs}" capitalized_libs) |
Oscar Fuentes | 5eae24e | 2011-03-09 14:44:46 +0000 | [diff] [blame] | 65 | |
| 66 | # Expand some keywords: |
| 67 | list(FIND link_components "engine" engine_required) |
Oscar Fuentes | 879d3a9 | 2011-03-12 16:48:54 +0000 | [diff] [blame] | 68 | if( NOT engine_required STREQUAL "-1" ) |
Oscar Fuentes | 5eae24e | 2011-03-09 14:44:46 +0000 | [diff] [blame] | 69 | # TODO: as we assume we are on X86, this is `jit'. |
| 70 | list(APPEND link_components "jit") |
| 71 | list(APPEND link_components "native") |
| 72 | endif() |
| 73 | list(FIND link_components "native" native_required) |
Oscar Fuentes | 879d3a9 | 2011-03-12 16:48:54 +0000 | [diff] [blame] | 74 | if( NOT native_required STREQUAL "-1" ) |
Oscar Fuentes | 5eae24e | 2011-03-09 14:44:46 +0000 | [diff] [blame] | 75 | list(APPEND link_components "X86") |
| 76 | endif() |
| 77 | |
Oscar Fuentes | 44258d1 | 2010-09-28 22:38:39 +0000 | [diff] [blame] | 78 | # Translate symbolic component names to real libraries: |
Michael J. Spencer | 3a210e2 | 2010-09-13 23:59:48 +0000 | [diff] [blame] | 79 | foreach(c ${link_components}) |
| 80 | # add codegen, asmprinter, asmparser, disassembler |
| 81 | list(FIND LLVM_TARGETS_TO_BUILD ${c} idx) |
| 82 | if( NOT idx LESS 0 ) |
| 83 | list(FIND llvm_libs "LLVM${c}CodeGen" idx) |
| 84 | if( NOT idx LESS 0 ) |
| 85 | list(APPEND expanded_components "LLVM${c}CodeGen") |
| 86 | else() |
| 87 | list(FIND llvm_libs "LLVM${c}" idx) |
| 88 | if( NOT idx LESS 0 ) |
| 89 | list(APPEND expanded_components "LLVM${c}") |
| 90 | else() |
| 91 | message(FATAL_ERROR "Target ${c} is not in the set of libraries.") |
| 92 | endif() |
| 93 | endif() |
| 94 | list(FIND llvm_libs "LLVM${c}AsmPrinter" asmidx) |
| 95 | if( NOT asmidx LESS 0 ) |
| 96 | list(APPEND expanded_components "LLVM${c}AsmPrinter") |
| 97 | endif() |
| 98 | list(FIND llvm_libs "LLVM${c}AsmParser" asmidx) |
| 99 | if( NOT asmidx LESS 0 ) |
| 100 | list(APPEND expanded_components "LLVM${c}AsmParser") |
| 101 | endif() |
| 102 | list(FIND llvm_libs "LLVM${c}Info" asmidx) |
| 103 | if( NOT asmidx LESS 0 ) |
| 104 | list(APPEND expanded_components "LLVM${c}Info") |
| 105 | endif() |
| 106 | list(FIND llvm_libs "LLVM${c}Disassembler" asmidx) |
| 107 | if( NOT asmidx LESS 0 ) |
| 108 | list(APPEND expanded_components "LLVM${c}Disassembler") |
| 109 | endif() |
| 110 | elseif( c STREQUAL "native" ) |
Oscar Fuentes | 5eae24e | 2011-03-09 14:44:46 +0000 | [diff] [blame] | 111 | # already processed |
Michael J. Spencer | 3a210e2 | 2010-09-13 23:59:48 +0000 | [diff] [blame] | 112 | elseif( c STREQUAL "nativecodegen" ) |
| 113 | list(APPEND expanded_components "LLVM${LLVM_NATIVE_ARCH}CodeGen") |
| 114 | elseif( c STREQUAL "backend" ) |
| 115 | # same case as in `native'. |
| 116 | elseif( c STREQUAL "engine" ) |
Oscar Fuentes | 5eae24e | 2011-03-09 14:44:46 +0000 | [diff] [blame] | 117 | # already processed |
Michael J. Spencer | 3a210e2 | 2010-09-13 23:59:48 +0000 | [diff] [blame] | 118 | elseif( c STREQUAL "all" ) |
| 119 | list(APPEND expanded_components ${llvm_libs}) |
| 120 | else( NOT idx LESS 0 ) |
Oscar Fuentes | 44258d1 | 2010-09-28 22:38:39 +0000 | [diff] [blame] | 121 | # Canonize the component name: |
| 122 | string(TOUPPER "${c}" capitalized) |
| 123 | list(FIND capitalized_libs LLVM${capitalized} lib_idx) |
| 124 | if( lib_idx LESS 0 ) |
| 125 | # The component is unkown. Maybe is an ommitted target? |
| 126 | is_llvm_target_library(${c} iltl_result) |
| 127 | if( NOT iltl_result ) |
| 128 | message(FATAL_ERROR "Library `${c}' not found in list of llvm libraries.") |
| 129 | endif() |
| 130 | else( lib_idx LESS 0 ) |
| 131 | list(GET llvm_libs ${lib_idx} canonical_lib) |
| 132 | list(APPEND expanded_components ${canonical_lib}) |
| 133 | endif( lib_idx LESS 0 ) |
Michael J. Spencer | 3a210e2 | 2010-09-13 23:59:48 +0000 | [diff] [blame] | 134 | endif( NOT idx LESS 0 ) |
| 135 | endforeach(c) |
Oscar Fuentes | 44258d1 | 2010-09-28 22:38:39 +0000 | [diff] [blame] | 136 | # Expand dependencies while topologically sorting the list of libraries: |
Michael J. Spencer | 3a210e2 | 2010-09-13 23:59:48 +0000 | [diff] [blame] | 137 | list(LENGTH expanded_components lst_size) |
Oscar Fuentes | 44258d1 | 2010-09-28 22:38:39 +0000 | [diff] [blame] | 138 | set(cursor 0) |
| 139 | set(processed) |
| 140 | while( cursor LESS lst_size ) |
| 141 | list(GET expanded_components ${cursor} lib) |
| 142 | list(APPEND expanded_components ${MSVC_LIB_DEPS_${lib}}) |
| 143 | # Remove duplicates at the front: |
| 144 | list(REVERSE expanded_components) |
| 145 | list(REMOVE_DUPLICATES expanded_components) |
| 146 | list(REVERSE expanded_components) |
| 147 | list(APPEND processed ${lib}) |
| 148 | # Find the maximum index that doesn't have to be re-processed: |
| 149 | while(NOT "${expanded_components}" MATCHES "^${processed}.*" ) |
| 150 | list(REMOVE_AT processed -1) |
| 151 | endwhile() |
| 152 | list(LENGTH processed cursor) |
Michael J. Spencer | 3a210e2 | 2010-09-13 23:59:48 +0000 | [diff] [blame] | 153 | list(LENGTH expanded_components lst_size) |
Oscar Fuentes | 44258d1 | 2010-09-28 22:38:39 +0000 | [diff] [blame] | 154 | endwhile( cursor LESS lst_size ) |
| 155 | # Return just the libraries included in this build: |
| 156 | set(result) |
| 157 | foreach(c ${expanded_components}) |
| 158 | list(FIND llvm_libs ${c} lib_idx) |
| 159 | if( NOT lib_idx LESS 0 ) |
| 160 | set(result ${result} ${c}) |
| 161 | endif() |
| 162 | endforeach(c) |
Michael J. Spencer | 3a210e2 | 2010-09-13 23:59:48 +0000 | [diff] [blame] | 163 | set(${out_libs} ${result} PARENT_SCOPE) |
| 164 | endfunction(explicit_map_components_to_libraries) |
| 165 | |
| 166 | |
| 167 | # The library dependency data is contained in the file |
| 168 | # LLVMLibDeps.cmake on this directory. It is automatically generated |
| 169 | # by tools/llvm-config/CMakeLists.txt when the build comprises all the |
| 170 | # targets and we are on a environment Posix enough to build the |
| 171 | # llvm-config script. This, in practice, just excludes MSVC. |
| 172 | |
| 173 | # When you remove or rename a library from the build, be sure to |
| 174 | # remove its file from lib/ as well, or the GenLibDeps.pl script will |
| 175 | # include it on its analysis! |
| 176 | |
| 177 | # The format generated by GenLibDeps.pl |
| 178 | |
Oscar Fuentes | 44258d1 | 2010-09-28 22:38:39 +0000 | [diff] [blame] | 179 | # LLVMARMAsmPrinter.o: LLVMARMCodeGen.o libLLVMAsmPrinter.a libLLVMCodeGen.a libLLVMCore.a libLLVMSupport.a libLLVMTarget.a |
Michael J. Spencer | 3a210e2 | 2010-09-13 23:59:48 +0000 | [diff] [blame] | 180 | |
| 181 | # is translated to: |
| 182 | |
Oscar Fuentes | 44258d1 | 2010-09-28 22:38:39 +0000 | [diff] [blame] | 183 | # set(MSVC_LIB_DEPS_LLVMARMAsmPrinter LLVMARMCodeGen LLVMAsmPrinter LLVMCodeGen LLVMCore LLVMSupport LLVMTarget) |
Michael J. Spencer | 3a210e2 | 2010-09-13 23:59:48 +0000 | [diff] [blame] | 184 | |
Oscar Fuentes | 44258d1 | 2010-09-28 22:38:39 +0000 | [diff] [blame] | 185 | # It is necessary to remove the `lib' prefix and the `.a'. |
Michael J. Spencer | 3a210e2 | 2010-09-13 23:59:48 +0000 | [diff] [blame] | 186 | |
| 187 | # This 'sed' script should do the trick: |
| 188 | # 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 |
| 189 | |
| 190 | include(LLVMLibDeps) |