blob: 080015bfc4b94187cb56181ce85396f8f9ed0247 [file] [log] [blame]
Michael J. Spencer773a8fb2011-12-18 08:27:59 +00001# 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 Kledzik1a6615d2012-03-08 00:18:30 +00004
5
Michael J. Spencer773a8fb2011-12-18 08:27:59 +00006if (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 Wennborg2391fdd2013-08-24 00:24:15 +000031 option(LLVM_INSTALL_TOOLCHAIN_ONLY
32 "Only include toolchain files in the 'install' target." OFF)
33
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000034 include(AddLLVM)
Michael J. Spencerdb145082012-12-10 23:52:34 +000035 include(TableGen)
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000036 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 Wennborg306db8f2013-08-23 18:18:13 +000049 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. Spencerdb145082012-12-10 23:52:34 +000052 # FIXME: This is an utter hack.
Hans Wennborg306db8f2013-08-23 18:18:13 +000053 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. Spencerdb145082012-12-10 23:52:34 +000056 endif()
57
Hans Wennborg306db8f2013-08-23 18:18:13 +000058 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. Spencer773a8fb2011-12-18 08:27:59 +000061 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)
66endif()
67
68set(LLD_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
69set(LLD_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
70
71if (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.")
77endif()
78
Michael J. Spencerd4eb47c2013-04-06 00:56:40 +000079list (APPEND CMAKE_MODULE_PATH "${LLD_SOURCE_DIR}/cmake/modules")
80
81option(LLD_USE_VTUNE
82 "Enable VTune user task tracking."
83 OFF)
84if (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()
91endif()
92
Michael J. Spencerc4048062013-01-05 04:16:52 +000093# lld requires c++11 to build. Make sure that we have a compiler and standard
94# library combination that can do that.
95if (MSVC11)
96 # Do nothing, we're good.
97elseif (NOT MSVC)
98 # gcc and clang require the -std=c++0x or -std=c++11 flag.
Rui Ueyamafe81cf82013-05-14 19:53:41 +000099 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. Spencere753cbc2012-03-09 05:27:43 +0000104 endif()
Michael J. Spencerc4048062013-01-05 04:16:52 +0000105else()
106 message(FATAL_ERROR "The selected compiler does not support c++11 which is "
107 "required to build lld.")
Nick Kledzik1a6615d2012-03-08 00:18:30 +0000108endif()
109
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000110macro(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 Wennborg2391fdd2013-08-24 00:24:15 +0000136 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. Spencer773a8fb2011-12-18 08:27:59 +0000141 set_target_properties(${name} PROPERTIES FOLDER "lld libraries")
142endmacro(add_lld_library)
143
144macro(add_lld_executable name)
145 add_llvm_executable(${name} ${ARGN})
146 set_target_properties(${name} PROPERTIES FOLDER "lld executables")
147endmacro(add_lld_executable)
148
149include_directories(BEFORE
150 ${CMAKE_CURRENT_BINARY_DIR}/include
151 ${CMAKE_CURRENT_SOURCE_DIR}/include
152 )
153
Hans Wennborg2391fdd2013-08-24 00:24:15 +0000154if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
155 install(DIRECTORY include/
156 DESTINATION include
157 FILES_MATCHING
158 PATTERN "*.h"
159 PATTERN ".svn" EXCLUDE
160 )
161endif()
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000162
163add_subdirectory(lib)
164add_subdirectory(tools)
Michael J. Spencera55e37f2013-03-01 00:03:36 +0000165add_subdirectory(utils)
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000166
167add_subdirectory(test)
Michael J. Spencer800de032012-12-19 00:51:07 +0000168
169if (LLVM_INCLUDE_TESTS AND NOT LLD_BUILT_STANDALONE)
170 add_subdirectory(unittests)
171endif()