| Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 1 | # If we are not building as a part of LLVM, build lld as a standalone project, | 
|  | 2 | # using LLVM as an external library. | 
|  | 3 |  | 
| Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 4 |  | 
|  | 5 |  | 
| Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 6 | if (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR) | 
|  | 7 | project(lld) | 
|  | 8 | cmake_minimum_required(VERSION 2.8) | 
|  | 9 |  | 
|  | 10 | set(LLD_PATH_TO_LLVM_SOURCE "" CACHE PATH | 
|  | 11 | "Path to LLVM source code. Not necessary if using an installed LLVM.") | 
|  | 12 | set(LLD_PATH_TO_LLVM_BUILD "" CACHE PATH | 
|  | 13 | "Path to the directory where LLVM was built or installed.") | 
|  | 14 |  | 
|  | 15 | if (LLD_PATH_TO_LLVM_SOURCE) | 
|  | 16 | if (NOT EXISTS "${LLD_PATH_TO_LLVM_SOURCE}/cmake/config-ix.cmake") | 
|  | 17 | message(FATAL_ERROR "Please set LLD_PATH_TO_LLVM_SOURCE to the root " | 
|  | 18 | "directory of LLVM source code.") | 
|  | 19 | else() | 
|  | 20 | get_filename_component(LLVM_MAIN_SRC_DIR ${LLD_PATH_TO_LLVM_SOURCE} | 
|  | 21 | ABSOLUTE) | 
|  | 22 | list(APPEND CMAKE_MODULE_PATH "${LLVM_MAIN_SRC_DIR}/cmake/modules") | 
|  | 23 | endif() | 
|  | 24 | endif() | 
|  | 25 |  | 
|  | 26 | list(APPEND CMAKE_MODULE_PATH "${LLD_PATH_TO_LLVM_BUILD}/share/llvm/cmake") | 
|  | 27 |  | 
|  | 28 | get_filename_component(PATH_TO_LLVM_BUILD ${LLD_PATH_TO_LLVM_BUILD} | 
|  | 29 | ABSOLUTE) | 
|  | 30 |  | 
| Hans Wennborg | 2391fdd | 2013-08-24 00:24:15 +0000 | [diff] [blame^] | 31 | option(LLVM_INSTALL_TOOLCHAIN_ONLY | 
|  | 32 | "Only include toolchain files in the 'install' target." OFF) | 
|  | 33 |  | 
| Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 34 | include(AddLLVM) | 
| Michael J. Spencer | db14508 | 2012-12-10 23:52:34 +0000 | [diff] [blame] | 35 | include(TableGen) | 
| Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 36 | include("${LLD_PATH_TO_LLVM_BUILD}/share/llvm/cmake/LLVMConfig.cmake") | 
|  | 37 | include(HandleLLVMOptions) | 
|  | 38 |  | 
|  | 39 | set(PACKAGE_VERSION "${LLVM_PACKAGE_VERSION}") | 
|  | 40 |  | 
|  | 41 | set(LLVM_MAIN_INCLUDE_DIR "${LLVM_MAIN_SRC_DIR}/include") | 
|  | 42 | set(LLVM_BINARY_DIR ${CMAKE_BINARY_DIR}) | 
|  | 43 |  | 
|  | 44 | set(CMAKE_INCLUDE_CURRENT_DIR ON) | 
|  | 45 | include_directories("${PATH_TO_LLVM_BUILD}/include" | 
|  | 46 | "${LLVM_MAIN_INCLUDE_DIR}") | 
|  | 47 | link_directories("${PATH_TO_LLVM_BUILD}/lib") | 
|  | 48 |  | 
| Hans Wennborg | 306db8f | 2013-08-23 18:18:13 +0000 | [diff] [blame] | 49 | if (EXISTS "${LLD_PATH_TO_LLVM_BUILD}/bin/llvm-config${CMAKE_EXECUTABLE_SUFFIX}") | 
|  | 50 | set (PATH_TO_LLVM_CONFIG "${LLD_PATH_TO_LLVM_BUILD}/bin/llvm-config${CMAKE_EXECUTABLE_SUFFIX}") | 
|  | 51 | elseif (EXISTS "${LLD_PATH_TO_LLVM_BUILD}/bin/Debug/llvm-config${CMAKE_EXECUTABLE_SUFFIX}") | 
| Michael J. Spencer | db14508 | 2012-12-10 23:52:34 +0000 | [diff] [blame] | 52 | # FIXME: This is an utter hack. | 
| Hans Wennborg | 306db8f | 2013-08-23 18:18:13 +0000 | [diff] [blame] | 53 | set (PATH_TO_LLVM_CONFIG "${LLD_PATH_TO_LLVM_BUILD}/bin/Debug/llvm-config${CMAKE_EXECUTABLE_SUFFIX}") | 
|  | 54 | else() | 
|  | 55 | message(FATAL_ERROR "Please set LLD_PATH_TO_LLVM_BUILD to a directory containing a LLVM build.") | 
| Michael J. Spencer | db14508 | 2012-12-10 23:52:34 +0000 | [diff] [blame] | 56 | endif() | 
|  | 57 |  | 
| Hans Wennborg | 306db8f | 2013-08-23 18:18:13 +0000 | [diff] [blame] | 58 | exec_program("${PATH_TO_LLVM_CONFIG} --bindir" OUTPUT_VARIABLE LLVM_BINARY_DIR) | 
|  | 59 | set(LLVM_TABLEGEN_EXE "${LLVM_BINARY_DIR}/llvm-tblgen${CMAKE_EXECUTABLE_SUFFIX}") | 
|  | 60 |  | 
| Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 61 | set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) | 
|  | 62 | set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) | 
|  | 63 | set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) | 
|  | 64 |  | 
|  | 65 | set(LLD_BUILT_STANDALONE 1) | 
|  | 66 | endif() | 
|  | 67 |  | 
|  | 68 | set(LLD_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}) | 
|  | 69 | set(LLD_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}) | 
|  | 70 |  | 
|  | 71 | if (CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR) | 
|  | 72 | message(FATAL_ERROR "In-source builds are not allowed. CMake would overwrite " | 
|  | 73 | "the makefiles distributed with LLVM. Please create a directory and run cmake " | 
|  | 74 | "from there, passing the path to this source directory as the last argument. " | 
|  | 75 | "This process created the file `CMakeCache.txt' and the directory " | 
|  | 76 | "`CMakeFiles'. Please delete them.") | 
|  | 77 | endif() | 
|  | 78 |  | 
| Michael J. Spencer | d4eb47c | 2013-04-06 00:56:40 +0000 | [diff] [blame] | 79 | list (APPEND CMAKE_MODULE_PATH "${LLD_SOURCE_DIR}/cmake/modules") | 
|  | 80 |  | 
|  | 81 | option(LLD_USE_VTUNE | 
|  | 82 | "Enable VTune user task tracking." | 
|  | 83 | OFF) | 
|  | 84 | if (LLD_USE_VTUNE) | 
|  | 85 | find_package(VTune) | 
|  | 86 | if (VTUNE_FOUND) | 
|  | 87 | include_directories(${VTune_INCLUDE_DIRS}) | 
|  | 88 | list(APPEND LLVM_COMMON_LIBS ${VTune_LIBRARIES}) | 
|  | 89 | add_definitions(-DLLD_HAS_VTUNE) | 
|  | 90 | endif() | 
|  | 91 | endif() | 
|  | 92 |  | 
| Michael J. Spencer | c404806 | 2013-01-05 04:16:52 +0000 | [diff] [blame] | 93 | # lld requires c++11 to build. Make sure that we have a compiler and standard | 
|  | 94 | # library combination that can do that. | 
|  | 95 | if (MSVC11) | 
|  | 96 | # Do nothing, we're good. | 
|  | 97 | elseif (NOT MSVC) | 
|  | 98 | # gcc and clang require the -std=c++0x or -std=c++11 flag. | 
| Rui Ueyama | fe81cf8 | 2013-05-14 19:53:41 +0000 | [diff] [blame] | 99 | if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU|Clang" AND | 
|  | 100 | NOT ("${CMAKE_CXX_FLAGS}" MATCHES ".*-std=(c|gnu)\\+\\+(0x|11).*")) | 
|  | 101 | message(FATAL_ERROR | 
|  | 102 | "lld requires c++11. Clang and gcc require -std=c++0x or -std=c++11 to " | 
|  | 103 | "enter this mode. Please set CMAKE_CXX_FLAGS accordingly.") | 
| Michael J. Spencer | e753cbc | 2012-03-09 05:27:43 +0000 | [diff] [blame] | 104 | endif() | 
| Michael J. Spencer | c404806 | 2013-01-05 04:16:52 +0000 | [diff] [blame] | 105 | else() | 
|  | 106 | message(FATAL_ERROR "The selected compiler does not support c++11 which is " | 
|  | 107 | "required to build lld.") | 
| Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 108 | endif() | 
|  | 109 |  | 
| Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 110 | macro(add_lld_library name) | 
|  | 111 | llvm_process_sources(srcs ${ARGN}) | 
|  | 112 | if (MSVC_IDE OR XCODE) | 
|  | 113 | string(REGEX MATCHALL "/[^/]+" split_path ${CMAKE_CURRENT_SOURCE_DIR}) | 
|  | 114 | list(GET split_path -1 dir) | 
|  | 115 | file(GLOB_RECURSE headers | 
|  | 116 | ../../include/lld${dir}/*.h) | 
|  | 117 | set(srcs ${srcs} ${headers}) | 
|  | 118 | endif() | 
|  | 119 | if (MODULE) | 
|  | 120 | set(libkind MODULE) | 
|  | 121 | elseif (SHARED_LIBRARY) | 
|  | 122 | set(libkind SHARED) | 
|  | 123 | else() | 
|  | 124 | set(libkind) | 
|  | 125 | endif() | 
|  | 126 | add_library(${name} ${libkind} ${srcs}) | 
|  | 127 | if (LLVM_COMMON_DEPENDS) | 
|  | 128 | add_dependencies(${name} ${LLVM_COMMON_DEPENDS}) | 
|  | 129 | endif() | 
|  | 130 |  | 
|  | 131 | target_link_libraries(${name} ${LLVM_USED_LIBS}) | 
|  | 132 | llvm_config(${name} ${LLVM_LINK_COMPONENTS}) | 
|  | 133 | target_link_libraries(${name} ${LLVM_COMMON_LIBS}) | 
|  | 134 | link_system_libs(${name}) | 
|  | 135 |  | 
| Hans Wennborg | 2391fdd | 2013-08-24 00:24:15 +0000 | [diff] [blame^] | 136 | if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY) | 
|  | 137 | install(TARGETS ${name} | 
|  | 138 | LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX} | 
|  | 139 | ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX}) | 
|  | 140 | endif() | 
| Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 141 | set_target_properties(${name} PROPERTIES FOLDER "lld libraries") | 
|  | 142 | endmacro(add_lld_library) | 
|  | 143 |  | 
|  | 144 | macro(add_lld_executable name) | 
|  | 145 | add_llvm_executable(${name} ${ARGN}) | 
|  | 146 | set_target_properties(${name} PROPERTIES FOLDER "lld executables") | 
|  | 147 | endmacro(add_lld_executable) | 
|  | 148 |  | 
|  | 149 | include_directories(BEFORE | 
|  | 150 | ${CMAKE_CURRENT_BINARY_DIR}/include | 
|  | 151 | ${CMAKE_CURRENT_SOURCE_DIR}/include | 
|  | 152 | ) | 
|  | 153 |  | 
| Hans Wennborg | 2391fdd | 2013-08-24 00:24:15 +0000 | [diff] [blame^] | 154 | if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY) | 
|  | 155 | install(DIRECTORY include/ | 
|  | 156 | DESTINATION include | 
|  | 157 | FILES_MATCHING | 
|  | 158 | PATTERN "*.h" | 
|  | 159 | PATTERN ".svn" EXCLUDE | 
|  | 160 | ) | 
|  | 161 | endif() | 
| Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 162 |  | 
|  | 163 | add_subdirectory(lib) | 
|  | 164 | add_subdirectory(tools) | 
| Michael J. Spencer | a55e37f | 2013-03-01 00:03:36 +0000 | [diff] [blame] | 165 | add_subdirectory(utils) | 
| Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 166 |  | 
|  | 167 | add_subdirectory(test) | 
| Michael J. Spencer | 800de03 | 2012-12-19 00:51:07 +0000 | [diff] [blame] | 168 |  | 
|  | 169 | if (LLVM_INCLUDE_TESTS AND NOT LLD_BUILT_STANDALONE) | 
|  | 170 | add_subdirectory(unittests) | 
|  | 171 | endif() |