]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lld/CMakeLists.txt
Merge lld trunk r321017 to contrib/llvm/tools/lld.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lld / CMakeLists.txt
1 # Check if lld is built as a standalone project.
2 if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
3   project(lld)
4   cmake_minimum_required(VERSION 3.4.3)
5
6   set(CMAKE_INCLUDE_CURRENT_DIR ON)
7   set(LLD_BUILT_STANDALONE TRUE)
8
9   find_program(LLVM_CONFIG_PATH "llvm-config" DOC "Path to llvm-config binary")
10   if(NOT LLVM_CONFIG_PATH)
11     message(FATAL_ERROR "llvm-config not found: specify LLVM_CONFIG_PATH")
12   endif()
13
14   execute_process(COMMAND "${LLVM_CONFIG_PATH}"
15                           "--obj-root"
16                           "--includedir"
17                           "--cmakedir"
18                           "--src-root"
19                   RESULT_VARIABLE HAD_ERROR
20                   OUTPUT_VARIABLE LLVM_CONFIG_OUTPUT
21                   OUTPUT_STRIP_TRAILING_WHITESPACE)
22   if(HAD_ERROR)
23     message(FATAL_ERROR "llvm-config failed with status ${HAD_ERROR}")
24   endif()
25
26   string(REGEX REPLACE "[ \t]*[\r\n]+[ \t]*" ";" LLVM_CONFIG_OUTPUT "${LLVM_CONFIG_OUTPUT}")
27
28   list(GET LLVM_CONFIG_OUTPUT 0 OBJ_ROOT)
29   list(GET LLVM_CONFIG_OUTPUT 1 MAIN_INCLUDE_DIR)
30   list(GET LLVM_CONFIG_OUTPUT 2 LLVM_CMAKE_PATH)
31   list(GET LLVM_CONFIG_OUTPUT 3 MAIN_SRC_DIR)
32
33   set(LLVM_OBJ_ROOT ${OBJ_ROOT} CACHE PATH "path to LLVM build tree")
34   set(LLVM_MAIN_INCLUDE_DIR ${MAIN_INCLUDE_DIR} CACHE PATH "path to llvm/include")
35   set(LLVM_MAIN_SRC_DIR ${MAIN_SRC_DIR} CACHE PATH "Path to LLVM source tree")
36
37   file(TO_CMAKE_PATH ${LLVM_OBJ_ROOT} LLVM_BINARY_DIR)
38
39   if(NOT EXISTS "${LLVM_CMAKE_PATH}/LLVMConfig.cmake")
40     message(FATAL_ERROR "LLVMConfig.cmake not found")
41   endif()
42   include("${LLVM_CMAKE_PATH}/LLVMConfig.cmake")
43
44   list(APPEND CMAKE_MODULE_PATH "${LLVM_CMAKE_PATH}")
45
46   set(PACKAGE_VERSION "${LLVM_PACKAGE_VERSION}")
47   include_directories("${LLVM_BINARY_DIR}/include" ${LLVM_INCLUDE_DIRS})
48   link_directories(${LLVM_LIBRARY_DIRS})
49
50   set(LLVM_LIBRARY_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/lib${LLVM_LIBDIR_SUFFIX})
51   set(LLVM_RUNTIME_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/bin)
52   find_program(LLVM_TABLEGEN_EXE "llvm-tblgen" ${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH)
53
54   include(AddLLVM)
55   include(TableGen)
56   include(HandleLLVMOptions)
57
58   if(LLVM_INCLUDE_TESTS)
59     set(Python_ADDITIONAL_VERSIONS 2.7)
60     include(FindPythonInterp)
61     if(NOT PYTHONINTERP_FOUND)
62       message(FATAL_ERROR
63 "Unable to find Python interpreter, required for testing.
64
65 Please install Python or specify the PYTHON_EXECUTABLE CMake variable.")
66     endif()
67
68     if(${PYTHON_VERSION_STRING} VERSION_LESS 2.7)
69       message(FATAL_ERROR "Python 2.7 or newer is required")
70     endif()
71
72     # Check prebuilt llvm/utils.
73     if(EXISTS ${LLVM_TOOLS_BINARY_DIR}/FileCheck${CMAKE_EXECUTABLE_SUFFIX}
74         AND EXISTS ${LLVM_TOOLS_BINARY_DIR}/not${CMAKE_EXECUTABLE_SUFFIX})
75       set(LLVM_UTILS_PROVIDED ON)
76     endif()
77
78     if(EXISTS ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py)
79       # Note: path not really used, except for checking if lit was found
80       set(LLVM_LIT ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py)
81       if(NOT LLVM_UTILS_PROVIDED)
82         add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/FileCheck utils/FileCheck)
83         add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/not utils/not)
84         set(LLVM_UTILS_PROVIDED ON)
85         set(LLD_TEST_DEPS FileCheck not)
86       endif()
87       set(UNITTEST_DIR ${LLVM_MAIN_SRC_DIR}/utils/unittest)
88       if(EXISTS ${UNITTEST_DIR}/googletest/include/gtest/gtest.h
89           AND NOT EXISTS ${LLVM_LIBRARY_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}gtest${CMAKE_STATIC_LIBRARY_SUFFIX}
90           AND EXISTS ${UNITTEST_DIR}/CMakeLists.txt)
91         add_subdirectory(${UNITTEST_DIR} utils/unittest)
92       endif()
93     else()
94       # Seek installed Lit.
95       find_program(LLVM_LIT
96                    NAMES llvm-lit lit.py lit
97                    PATHS "${LLVM_MAIN_SRC_DIR}/utils/lit"
98                    DOC "Path to lit.py")
99     endif()
100
101     if(LLVM_LIT)
102       # Define the default arguments to use with 'lit', and an option for the user
103       # to override.
104       set(LIT_ARGS_DEFAULT "-sv")
105       if (MSVC OR XCODE)
106         set(LIT_ARGS_DEFAULT "${LIT_ARGS_DEFAULT} --no-progress-bar")
107       endif()
108       set(LLVM_LIT_ARGS "${LIT_ARGS_DEFAULT}" CACHE STRING "Default options for lit")
109
110       # On Win32 hosts, provide an option to specify the path to the GnuWin32 tools.
111       if(WIN32 AND NOT CYGWIN)
112         set(LLVM_LIT_TOOLS_DIR "" CACHE PATH "Path to GnuWin32 tools")
113       endif()
114     else()
115       set(LLVM_INCLUDE_TESTS OFF)
116     endif()
117   endif()
118 endif()
119
120 set(LLD_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
121 set(LLD_INCLUDE_DIR ${LLD_SOURCE_DIR}/include )
122 set(LLD_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
123
124 # Compute the LLD version from the LLVM version.
125 string(REGEX MATCH "[0-9]+\\.[0-9]+(\\.[0-9]+)?" LLD_VERSION
126   ${PACKAGE_VERSION})
127 message(STATUS "LLD version: ${LLD_VERSION}")
128
129 string(REGEX REPLACE "([0-9]+)\\.[0-9]+(\\.[0-9]+)?" "\\1" LLD_VERSION_MAJOR
130   ${LLD_VERSION})
131 string(REGEX REPLACE "[0-9]+\\.([0-9]+)(\\.[0-9]+)?" "\\1" LLD_VERSION_MINOR
132   ${LLD_VERSION})
133
134 # Determine LLD revision and repository.
135 # TODO: Figure out a way to get the revision and the repository on windows.
136 if ( NOT CMAKE_SYSTEM_NAME MATCHES "Windows" )
137   execute_process(COMMAND ${CMAKE_SOURCE_DIR}/utils/GetSourceVersion ${LLD_SOURCE_DIR}
138                   OUTPUT_VARIABLE LLD_REVISION)
139
140   execute_process(COMMAND ${CMAKE_SOURCE_DIR}/utils/GetRepositoryPath ${LLD_SOURCE_DIR}
141                   OUTPUT_VARIABLE LLD_REPOSITORY)
142   if ( LLD_REPOSITORY )
143     # Replace newline characters with spaces
144     string(REGEX REPLACE "(\r?\n)+" " " LLD_REPOSITORY ${LLD_REPOSITORY})
145     # Remove leading spaces
146     STRING(REGEX REPLACE "^[ \t\r\n]+" "" LLD_REPOSITORY "${LLD_REPOSITORY}" )
147     # Remove trailing spaces
148     string(REGEX REPLACE "(\ )+$" "" LLD_REPOSITORY ${LLD_REPOSITORY})
149   endif()
150
151   if ( LLD_REVISION )
152     # Replace newline characters with spaces
153     string(REGEX REPLACE "(\r?\n)+" " " LLD_REVISION ${LLD_REVISION})
154     # Remove leading spaces
155     STRING(REGEX REPLACE "^[ \t\r\n]+" "" LLD_REVISION "${LLD_REVISION}" )
156     # Remove trailing spaces
157     string(REGEX REPLACE "(\ )+$" "" LLD_REVISION ${LLD_REVISION})
158   endif()
159 endif ()
160
161 # Configure the Version.inc file.
162 configure_file(
163   ${CMAKE_CURRENT_SOURCE_DIR}/include/lld/Common/Version.inc.in
164   ${CMAKE_CURRENT_BINARY_DIR}/include/lld/Common/Version.inc)
165
166
167 if (CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR)
168   message(FATAL_ERROR "In-source builds are not allowed. CMake would overwrite "
169 "the makefiles distributed with LLVM. Please create a directory and run cmake "
170 "from there, passing the path to this source directory as the last argument. "
171 "This process created the file `CMakeCache.txt' and the directory "
172 "`CMakeFiles'. Please delete them.")
173 endif()
174
175 list (APPEND CMAKE_MODULE_PATH "${LLD_SOURCE_DIR}/cmake/modules")
176
177 include(AddLLD)
178
179 option(LLD_USE_VTUNE
180        "Enable VTune user task tracking."
181        OFF)
182 if (LLD_USE_VTUNE)
183   find_package(VTune)
184   if (VTUNE_FOUND)
185     include_directories(${VTune_INCLUDE_DIRS})
186     list(APPEND LLVM_COMMON_LIBS ${VTune_LIBRARIES})
187     add_definitions(-DLLD_HAS_VTUNE)
188   endif()
189 endif()
190
191 option(LLD_BUILD_TOOLS
192   "Build the lld tools. If OFF, just generate build targets." ON)
193
194 if (MSVC)
195   add_definitions(-wd4530) # Suppress 'warning C4530: C++ exception handler used, but unwind semantics are not enabled.'
196   add_definitions(-wd4062) # Suppress 'warning C4062: enumerator X in switch of enum Y is not handled' from system header.
197 endif()
198
199 include_directories(BEFORE
200   ${CMAKE_CURRENT_BINARY_DIR}/include
201   ${CMAKE_CURRENT_SOURCE_DIR}/include
202   )
203
204 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
205   install(DIRECTORY include/
206     DESTINATION include
207     FILES_MATCHING
208     PATTERN "*.h"
209     PATTERN ".svn" EXCLUDE
210     )
211 endif()
212
213 add_subdirectory(Common)
214 add_subdirectory(lib)
215 add_subdirectory(tools/lld)
216
217 if (LLVM_INCLUDE_TESTS)
218   add_subdirectory(test)
219   add_subdirectory(unittests)
220 endif()
221
222 add_subdirectory(docs)
223 add_subdirectory(COFF)
224 add_subdirectory(ELF)
225 add_subdirectory(MinGW)
226 add_subdirectory(wasm)