blob: 21775d193f2ffb53ac63cf0dc533b8ac9a3cd5a8 [file] [log] [blame]
Oscar Fuentes67410b32011-02-03 20:57:53 +00001# If we are not building as a part of LLVM, build Clang as an
2# standalone project, using LLVM as an external library:
3if( CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR )
4 project(Clang)
5 cmake_minimum_required(VERSION 2.8)
6
7 set(CLANG_PATH_TO_LLVM_SOURCE "" CACHE PATH
8 "Path to LLVM source code. Not necessary if using an installed LLVM.")
9 set(CLANG_PATH_TO_LLVM_BUILD "" CACHE PATH
10 "Path to the directory where LLVM was built or installed.")
11
12 if( CLANG_PATH_TO_LLVM_SOURCE )
13 if( NOT EXISTS "${CLANG_PATH_TO_LLVM_SOURCE}/cmake/config-ix.cmake" )
14 message(FATAL_ERROR "Please set CLANG_PATH_TO_LLVM_SOURCE to the root directory of LLVM source code.")
15 else()
16 get_filename_component(LLVM_MAIN_SRC_DIR ${CLANG_PATH_TO_LLVM_SOURCE}
17 ABSOLUTE)
18 list(APPEND CMAKE_MODULE_PATH "${LLVM_MAIN_SRC_DIR}/cmake/modules")
19 endif()
20 endif()
21
22 if( NOT EXISTS "${CLANG_PATH_TO_LLVM_BUILD}/bin/tblgen${CMAKE_EXECUTABLE_SUFFIX}" )
23 message(FATAL_ERROR "Please set CLANG_PATH_TO_LLVM_BUILD to a directory containing a LLVM build.")
24 endif()
25
26 list(APPEND CMAKE_MODULE_PATH "${CLANG_PATH_TO_LLVM_BUILD}/share/llvm/cmake")
27
28 get_filename_component(PATH_TO_LLVM_BUILD ${CLANG_PATH_TO_LLVM_BUILD}
29 ABSOLUTE)
30
31 include(AddLLVM)
32 include(TableGen)
33 include("${CLANG_PATH_TO_LLVM_BUILD}/share/llvm/cmake/LLVM.cmake")
34 include(HandleLLVMOptions)
35
36 set(PACKAGE_VERSION "${LLVM_PACKAGE_VERSION}")
37
38 set(LLVM_MAIN_INCLUDE_DIR "${LLVM_MAIN_SRC_DIR}/include")
39 set(LLVM_BINARY_DIR ${CMAKE_BINARY_DIR})
40
Oscar Fuentesb641f082011-02-14 20:14:11 +000041 set(CMAKE_INCLUDE_CURRENT_DIR ON)
Oscar Fuentes67410b32011-02-03 20:57:53 +000042 include_directories("${PATH_TO_LLVM_BUILD}/include" "${LLVM_MAIN_INCLUDE_DIR}")
43 if( NOT PATH_TO_LLVM_BUILD STREQUAL LLVM_MAIN_SRC_DIR )
44 include_directories("${LLVM_MAIN_INCLUDE_DIR}")
45 endif()
46 link_directories("${PATH_TO_LLVM_BUILD}/lib")
47
48 set(LLVM_TABLEGEN_EXE "${PATH_TO_LLVM_BUILD}/bin/tblgen")
49
50 set( CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin )
51 set( CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib )
52 set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib )
53
54 add_definitions( -D__STDC_LIMIT_MACROS )
55 add_definitions( -D__STDC_CONSTANT_MACROS )
Jeffrey Yasskin718b01d2011-02-15 07:54:28 +000056
57 set( CLANG_BUILT_STANDALONE 1 )
Oscar Fuentes67410b32011-02-03 20:57:53 +000058endif()
Douglas Gregor34d9ffa2009-09-16 21:59:05 +000059
Oscar Fuentes2100fe92011-02-03 22:48:20 +000060set(CLANG_RESOURCE_DIR "" CACHE STRING
61 "Relative directory from the Clang binary to its resource files.")
62
63set(C_INCLUDE_DIRS "" CACHE STRING
64 "Colon separated list of directories clang will search for headers.")
65
Daniel Dunbar91ee77a2009-09-17 00:07:10 +000066set(CLANG_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
67set(CLANG_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
68
Chandler Carruth63e9c0d2010-04-17 20:12:02 +000069if( CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR AND NOT MSVC_IDE )
70 message(FATAL_ERROR "In-source builds are not allowed. CMake would overwrite "
71"the makefiles distributed with LLVM. Please create a directory and run cmake "
72"from there, passing the path to this source directory as the last argument. "
73"This process created the file `CMakeCache.txt' and the directory "
74"`CMakeFiles'. Please delete them.")
75endif()
76
77if( NOT CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR )
78 file(GLOB_RECURSE
79 tablegenned_files_on_include_dir
80 "${CLANG_SOURCE_DIR}/include/clang/*.inc")
81 if( tablegenned_files_on_include_dir )
82 message(FATAL_ERROR "Apparently there is a previous in-source build, "
83"probably as the result of running `configure' and `make' on "
84"${CLANG_SOURCE_DIR}. This may cause problems. The suspicious files are:\n"
85"${tablegenned_files_on_include_dir}\nPlease clean the source directory.")
86 endif()
87endif()
88
Daniel Dunbarc4b8e922010-06-25 23:34:47 +000089# Compute the Clang version from the LLVM version.
Michael J. Spencer5a7f3492010-09-10 21:13:16 +000090string(REGEX MATCH "[0-9]+\\.[0-9]+(\\.[0-9]+)?" CLANG_VERSION
Daniel Dunbarc4b8e922010-06-25 23:34:47 +000091 ${PACKAGE_VERSION})
Douglas Gregor34d9ffa2009-09-16 21:59:05 +000092message(STATUS "Clang version: ${CLANG_VERSION}")
Douglas Gregor7f7b7482009-08-23 05:28:29 +000093
Daniel Dunbara5107672010-06-25 17:33:46 +000094string(REGEX REPLACE "([0-9]+)\\.[0-9]+(\\.[0-9]+)?" "\\1" CLANG_VERSION_MAJOR
95 ${CLANG_VERSION})
96string(REGEX REPLACE "[0-9]+\\.([0-9]+)(\\.[0-9]+)?" "\\1" CLANG_VERSION_MINOR
97 ${CLANG_VERSION})
98if (${CLANG_VERSION} MATCHES "[0-9]+\\.[0-9]+\\.[0-9]+")
99 set(CLANG_HAS_VERSION_PATCHLEVEL 1)
100 string(REGEX REPLACE "[0-9]+\\.[0-9]+\\.([0-9]+)" "\\1" CLANG_VERSION_PATCHLEVEL
101 ${CLANG_VERSION})
102else()
103 set(CLANG_HAS_VERSION_PATCHLEVEL 0)
104endif()
105
106# Configure the Version.inc file.
107configure_file(
108 ${CMAKE_CURRENT_SOURCE_DIR}/include/clang/Basic/Version.inc.in
109 ${CMAKE_CURRENT_BINARY_DIR}/include/clang/Basic/Version.inc)
110
Douglas Gregor9df3faf2009-09-18 14:47:57 +0000111# Add appropriate flags for GCC
112if (CMAKE_COMPILER_IS_GNUCXX)
Oscar Fuentes827c5732010-10-15 00:16:22 +0000113 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-common -Woverloaded-virtual -Wcast-qual -fno-strict-aliasing -pedantic -Wno-long-long -Wall -W -Wno-unused-parameter -Wwrite-strings")
Douglas Gregor9df3faf2009-09-18 14:47:57 +0000114endif ()
115
Douglas Gregoreb5dc492010-06-08 19:23:49 +0000116if (APPLE)
117 set(CMAKE_MODULE_LINKER_FLAGS "-Wl,-flat_namespace -Wl,-undefined -Wl,suppress")
118endif ()
119
Oscar Fuentes2100fe92011-02-03 22:48:20 +0000120configure_file(
121 ${CLANG_SOURCE_DIR}/include/clang/Config/config.h.cmake
122 ${CLANG_BINARY_DIR}/include/clang/Config/config.h)
123
Oscar Fuentesd2f4e5e2008-10-26 00:56:18 +0000124macro(add_clang_library name)
Oscar Fuentes99174012011-01-03 17:00:02 +0000125 llvm_process_sources(srcs ${ARGN})
Ted Kremenekbf5de3f2009-03-25 20:34:07 +0000126 if(MSVC_IDE OR XCODE)
Cedric Venetfe2f8082008-11-02 16:28:53 +0000127 string( REGEX MATCHALL "/[^/]+" split_path ${CMAKE_CURRENT_SOURCE_DIR})
128 list( GET split_path -1 dir)
Michael J. Spencer5a7f3492010-09-10 21:13:16 +0000129 file( GLOB_RECURSE headers
Ted Kremenekb26bd742011-02-10 01:03:09 +0000130 ../../../include/clang/StaticAnalyzer${dir}/*.h
131 ../../../include/clang/StaticAnalyzer${dir}/*.td
132 ../../../include/clang/StaticAnalyzer${dir}/*.def
Douglas Gregorf5216f22009-06-17 18:31:02 +0000133 ../../include/clang${dir}/*.h
134 ../../include/clang${dir}/*.td
135 ../../include/clang${dir}/*.def)
Cedric Venetfe2f8082008-11-02 16:28:53 +0000136 set(srcs ${srcs} ${headers})
Ted Kremenekbf5de3f2009-03-25 20:34:07 +0000137 endif(MSVC_IDE OR XCODE)
Douglas Gregoreb5dc492010-06-08 19:23:49 +0000138 if (MODULE)
139 set(libkind MODULE)
140 elseif (SHARED_LIBRARY)
Douglas Gregorac47bc72009-09-25 06:35:15 +0000141 set(libkind SHARED)
142 else()
143 set(libkind)
144 endif()
145 add_library( ${name} ${libkind} ${srcs} )
Oscar Fuentesd2f4e5e2008-10-26 00:56:18 +0000146 if( LLVM_COMMON_DEPENDS )
147 add_dependencies( ${name} ${LLVM_COMMON_DEPENDS} )
148 endif( LLVM_COMMON_DEPENDS )
Douglas Gregorac47bc72009-09-25 06:35:15 +0000149 if( LLVM_USED_LIBS )
150 foreach(lib ${LLVM_USED_LIBS})
151 target_link_libraries( ${name} ${lib} )
152 endforeach(lib)
153 endif( LLVM_USED_LIBS )
Benjamin Kramerbd785542009-10-11 12:22:00 +0000154 if( LLVM_LINK_COMPONENTS )
155 llvm_config(${name} ${LLVM_LINK_COMPONENTS})
156 endif( LLVM_LINK_COMPONENTS )
Michael J. Spencer5a7f3492010-09-10 21:13:16 +0000157 if (LLVM_COMMON_LIBS)
158 target_link_libraries(${name} ${LLVM_COMMON_LIBS})
159 endif()
Michael J. Spencer28709c12010-09-11 02:13:48 +0000160 if( NOT MINGW )
161 get_system_libs(llvm_system_libs)
162 if( llvm_system_libs )
163 target_link_libraries(${name} ${llvm_system_libs})
164 endif()
165 endif()
Douglas Gregora393e9e2009-03-16 23:06:59 +0000166 add_dependencies(${name} ClangDiagnosticCommon)
Cedric Venet1c212a02008-12-13 11:00:04 +0000167 if(MSVC)
168 get_target_property(cflag ${name} COMPILE_FLAGS)
169 if(NOT cflag)
170 set(cflag "")
171 endif(NOT cflag)
172 set(cflag "${cflag} /Za")
173 set_target_properties(${name} PROPERTIES COMPILE_FLAGS ${cflag})
174 endif(MSVC)
Oscar Fuentesd2f4e5e2008-10-26 00:56:18 +0000175 install(TARGETS ${name}
Oscar Fuentesbfb06ea2009-10-27 19:42:21 +0000176 LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX}
Oscar Fuentes755f3df2009-06-12 02:54:12 +0000177 ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX})
Oscar Fuentesd2f4e5e2008-10-26 00:56:18 +0000178endmacro(add_clang_library)
179
180macro(add_clang_executable name)
Oscar Fuentes99174012011-01-03 17:00:02 +0000181 add_llvm_executable( ${name} ${ARGN} )
Oscar Fuentesd2f4e5e2008-10-26 00:56:18 +0000182endmacro(add_clang_executable)
183
184include_directories(
185 ${CMAKE_CURRENT_SOURCE_DIR}/include
Douglas Gregora393e9e2009-03-16 23:06:59 +0000186 ${CMAKE_CURRENT_BINARY_DIR}/include
Oscar Fuentesd2f4e5e2008-10-26 00:56:18 +0000187 )
188
Oscar Fuentes82cac292009-10-27 19:59:34 +0000189install(DIRECTORY include/
190 DESTINATION include
191 FILES_MATCHING
192 PATTERN "*.def"
193 PATTERN "*.h"
194 PATTERN "*.td"
Chris Lattnerbc6ec752008-11-11 18:39:10 +0000195 PATTERN ".svn" EXCLUDE
196 )
197
Kovarththanan Rajaratnamec700a62010-04-01 14:24:41 +0000198install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/include/
199 DESTINATION include
200 FILES_MATCHING
Chris Lattnerd7d5bb12010-04-25 04:59:35 +0000201 PATTERN "CMakeFiles" EXCLUDE
Kovarththanan Rajaratnamec700a62010-04-01 14:24:41 +0000202 PATTERN "*.inc"
203 )
204
Oscar Fuentes2100fe92011-02-03 22:48:20 +0000205add_definitions( -D_GNU_SOURCE -DHAVE_CLANG_CONFIG_H )
Oscar Fuentesd2f4e5e2008-10-26 00:56:18 +0000206
Daniel Dunbar45088e22009-11-17 09:32:51 +0000207option(CLANG_BUILD_EXAMPLES "Build CLANG example programs." OFF)
208if(CLANG_BUILD_EXAMPLES)
209 add_subdirectory(examples)
210endif ()
211
Douglas Gregora393e9e2009-03-16 23:06:59 +0000212add_subdirectory(include)
Oscar Fuentesd2f4e5e2008-10-26 00:56:18 +0000213add_subdirectory(lib)
Daniel Dunbarcbcd98b2009-03-24 02:52:57 +0000214add_subdirectory(tools)
Michael J. Spencer48263ba2010-12-16 03:28:42 +0000215add_subdirectory(runtime)
Oscar Fuentesd2f4e5e2008-10-26 00:56:18 +0000216
217# TODO: docs.
Douglas Gregor291fbde2009-09-15 22:30:13 +0000218add_subdirectory(test)
Jeffrey Yasskin718b01d2011-02-15 07:54:28 +0000219
220# FIXME: unittests require gtest.
221if( NOT CLANG_BUILT_STANDALONE )
222 add_subdirectory(unittests)
223endif()