blob: 04e16547c5e75b368456249b7a9184e24df68c9a [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)
Oscar Fuentes5ec8a4d2011-04-10 16:17:31 +000033 include("${CLANG_PATH_TO_LLVM_BUILD}/share/llvm/cmake/LLVMConfig.cmake")
Oscar Fuentes67410b32011-02-03 20:57:53 +000034 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}")
Oscar Fuentes67410b32011-02-03 20:57:53 +000043 link_directories("${PATH_TO_LLVM_BUILD}/lib")
44
45 set(LLVM_TABLEGEN_EXE "${PATH_TO_LLVM_BUILD}/bin/tblgen")
46
47 set( CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin )
48 set( CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib )
49 set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib )
50
Jeffrey Yasskin718b01d2011-02-15 07:54:28 +000051 set( CLANG_BUILT_STANDALONE 1 )
Oscar Fuentes67410b32011-02-03 20:57:53 +000052endif()
Douglas Gregor34d9ffa2009-09-16 21:59:05 +000053
Oscar Fuentes2100fe92011-02-03 22:48:20 +000054set(CLANG_RESOURCE_DIR "" CACHE STRING
55 "Relative directory from the Clang binary to its resource files.")
56
57set(C_INCLUDE_DIRS "" CACHE STRING
58 "Colon separated list of directories clang will search for headers.")
59
Daniel Dunbar91ee77a2009-09-17 00:07:10 +000060set(CLANG_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
61set(CLANG_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
62
Chandler Carruth63e9c0d2010-04-17 20:12:02 +000063if( CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR AND NOT MSVC_IDE )
64 message(FATAL_ERROR "In-source builds are not allowed. CMake would overwrite "
65"the makefiles distributed with LLVM. Please create a directory and run cmake "
66"from there, passing the path to this source directory as the last argument. "
67"This process created the file `CMakeCache.txt' and the directory "
68"`CMakeFiles'. Please delete them.")
69endif()
70
71if( NOT CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR )
72 file(GLOB_RECURSE
73 tablegenned_files_on_include_dir
74 "${CLANG_SOURCE_DIR}/include/clang/*.inc")
75 if( tablegenned_files_on_include_dir )
76 message(FATAL_ERROR "Apparently there is a previous in-source build, "
77"probably as the result of running `configure' and `make' on "
78"${CLANG_SOURCE_DIR}. This may cause problems. The suspicious files are:\n"
79"${tablegenned_files_on_include_dir}\nPlease clean the source directory.")
80 endif()
81endif()
82
Daniel Dunbarc4b8e922010-06-25 23:34:47 +000083# Compute the Clang version from the LLVM version.
Michael J. Spencer5a7f3492010-09-10 21:13:16 +000084string(REGEX MATCH "[0-9]+\\.[0-9]+(\\.[0-9]+)?" CLANG_VERSION
Daniel Dunbarc4b8e922010-06-25 23:34:47 +000085 ${PACKAGE_VERSION})
Douglas Gregor34d9ffa2009-09-16 21:59:05 +000086message(STATUS "Clang version: ${CLANG_VERSION}")
Douglas Gregor7f7b7482009-08-23 05:28:29 +000087
Daniel Dunbara5107672010-06-25 17:33:46 +000088string(REGEX REPLACE "([0-9]+)\\.[0-9]+(\\.[0-9]+)?" "\\1" CLANG_VERSION_MAJOR
89 ${CLANG_VERSION})
90string(REGEX REPLACE "[0-9]+\\.([0-9]+)(\\.[0-9]+)?" "\\1" CLANG_VERSION_MINOR
91 ${CLANG_VERSION})
92if (${CLANG_VERSION} MATCHES "[0-9]+\\.[0-9]+\\.[0-9]+")
93 set(CLANG_HAS_VERSION_PATCHLEVEL 1)
94 string(REGEX REPLACE "[0-9]+\\.[0-9]+\\.([0-9]+)" "\\1" CLANG_VERSION_PATCHLEVEL
95 ${CLANG_VERSION})
96else()
97 set(CLANG_HAS_VERSION_PATCHLEVEL 0)
98endif()
99
100# Configure the Version.inc file.
101configure_file(
102 ${CMAKE_CURRENT_SOURCE_DIR}/include/clang/Basic/Version.inc.in
103 ${CMAKE_CURRENT_BINARY_DIR}/include/clang/Basic/Version.inc)
104
Douglas Gregor9df3faf2009-09-18 14:47:57 +0000105# Add appropriate flags for GCC
Oscar Fuentesc5cd2522011-05-11 13:53:30 +0000106if (LLVM_COMPILER_IS_GCC_COMPATIBLE)
Oscar Fuentes827c5732010-10-15 00:16:22 +0000107 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 +0000108endif ()
109
Douglas Gregoreb5dc492010-06-08 19:23:49 +0000110if (APPLE)
111 set(CMAKE_MODULE_LINKER_FLAGS "-Wl,-flat_namespace -Wl,-undefined -Wl,suppress")
112endif ()
113
Oscar Fuentes2100fe92011-02-03 22:48:20 +0000114configure_file(
115 ${CLANG_SOURCE_DIR}/include/clang/Config/config.h.cmake
116 ${CLANG_BINARY_DIR}/include/clang/Config/config.h)
117
Oscar Fuentesc8da1ec2011-02-20 22:06:32 +0000118include(LLVMParseArguments)
119
120function(clang_tablegen)
121 # Syntax:
122 # clang_tablegen output-file [tablegen-arg ...] SOURCE source-file
123 # [[TARGET cmake-target-name] [DEPENDS extra-dependency ...]]
124 #
125 # Generates a custom command for invoking tblgen as
126 #
127 # tblgen source-file -o=output-file tablegen-arg ...
128 #
129 # and, if cmake-target-name is provided, creates a custom target for
130 # executing the custom command depending on output-file. It is
131 # possible to list more files to depend after DEPENDS.
132
133 parse_arguments( CTG "SOURCE;TARGET;DEPENDS" "" ${ARGN} )
134
135 if( NOT CTG_SOURCE )
136 message(FATAL_ERROR "SOURCE source-file required by clang_tablegen")
137 endif()
138
139 set( LLVM_TARGET_DEFINITIONS ${CTG_SOURCE} )
140 tablegen( ${CTG_DEFAULT_ARGS} )
141
142 list( GET CTG_DEFAULT_ARGS 0 output_file )
143 if( CTG_TARGET )
144 add_custom_target( ${CTG_TARGET} DEPENDS ${output_file} ${CTG_DEPENDS} )
Oscar Fuentesa3f787c2011-02-20 22:06:44 +0000145 set_target_properties( ${CTG_TARGET} PROPERTIES FOLDER "Clang tablegenning")
Oscar Fuentesc8da1ec2011-02-20 22:06:32 +0000146 endif()
147endfunction(clang_tablegen)
148
Oscar Fuentesd2f4e5e2008-10-26 00:56:18 +0000149macro(add_clang_library name)
Oscar Fuentes99174012011-01-03 17:00:02 +0000150 llvm_process_sources(srcs ${ARGN})
Ted Kremenekbf5de3f2009-03-25 20:34:07 +0000151 if(MSVC_IDE OR XCODE)
Cedric Venetfe2f8082008-11-02 16:28:53 +0000152 string( REGEX MATCHALL "/[^/]+" split_path ${CMAKE_CURRENT_SOURCE_DIR})
153 list( GET split_path -1 dir)
Michael J. Spencer5a7f3492010-09-10 21:13:16 +0000154 file( GLOB_RECURSE headers
Ted Kremenekb26bd742011-02-10 01:03:09 +0000155 ../../../include/clang/StaticAnalyzer${dir}/*.h
156 ../../../include/clang/StaticAnalyzer${dir}/*.td
157 ../../../include/clang/StaticAnalyzer${dir}/*.def
Douglas Gregorf5216f22009-06-17 18:31:02 +0000158 ../../include/clang${dir}/*.h
159 ../../include/clang${dir}/*.td
160 ../../include/clang${dir}/*.def)
Cedric Venetfe2f8082008-11-02 16:28:53 +0000161 set(srcs ${srcs} ${headers})
Ted Kremenekbf5de3f2009-03-25 20:34:07 +0000162 endif(MSVC_IDE OR XCODE)
Douglas Gregoreb5dc492010-06-08 19:23:49 +0000163 if (MODULE)
164 set(libkind MODULE)
165 elseif (SHARED_LIBRARY)
Douglas Gregorac47bc72009-09-25 06:35:15 +0000166 set(libkind SHARED)
167 else()
168 set(libkind)
169 endif()
170 add_library( ${name} ${libkind} ${srcs} )
Oscar Fuentesd2f4e5e2008-10-26 00:56:18 +0000171 if( LLVM_COMMON_DEPENDS )
172 add_dependencies( ${name} ${LLVM_COMMON_DEPENDS} )
173 endif( LLVM_COMMON_DEPENDS )
Oscar Fuentesfb767c82011-03-29 20:51:00 +0000174
175 target_link_libraries( ${name} ${LLVM_USED_LIBS} )
176 llvm_config( ${name} ${LLVM_LINK_COMPONENTS} )
177 target_link_libraries( ${name} ${LLVM_COMMON_LIBS} )
178 link_system_libs( ${name} )
179
Douglas Gregora393e9e2009-03-16 23:06:59 +0000180 add_dependencies(${name} ClangDiagnosticCommon)
Cedric Venet1c212a02008-12-13 11:00:04 +0000181 if(MSVC)
182 get_target_property(cflag ${name} COMPILE_FLAGS)
183 if(NOT cflag)
184 set(cflag "")
185 endif(NOT cflag)
186 set(cflag "${cflag} /Za")
187 set_target_properties(${name} PROPERTIES COMPILE_FLAGS ${cflag})
188 endif(MSVC)
Oscar Fuentesd2f4e5e2008-10-26 00:56:18 +0000189 install(TARGETS ${name}
Oscar Fuentesbfb06ea2009-10-27 19:42:21 +0000190 LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX}
Oscar Fuentes755f3df2009-06-12 02:54:12 +0000191 ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX})
Oscar Fuentesa3f787c2011-02-20 22:06:44 +0000192 set_target_properties(${name} PROPERTIES FOLDER "Clang libraries")
Oscar Fuentesd2f4e5e2008-10-26 00:56:18 +0000193endmacro(add_clang_library)
194
195macro(add_clang_executable name)
Oscar Fuentes99174012011-01-03 17:00:02 +0000196 add_llvm_executable( ${name} ${ARGN} )
Oscar Fuentesa3f787c2011-02-20 22:06:44 +0000197 set_target_properties(${name} PROPERTIES FOLDER "Clang executables")
Oscar Fuentesd2f4e5e2008-10-26 00:56:18 +0000198endmacro(add_clang_executable)
199
Oscar Fuentes617508f2011-03-17 19:03:04 +0000200include_directories(BEFORE
Douglas Gregora393e9e2009-03-16 23:06:59 +0000201 ${CMAKE_CURRENT_BINARY_DIR}/include
Oscar Fuentes617508f2011-03-17 19:03:04 +0000202 ${CMAKE_CURRENT_SOURCE_DIR}/include
Oscar Fuentesd2f4e5e2008-10-26 00:56:18 +0000203 )
204
Oscar Fuentes82cac292009-10-27 19:59:34 +0000205install(DIRECTORY include/
206 DESTINATION include
207 FILES_MATCHING
208 PATTERN "*.def"
209 PATTERN "*.h"
Chris Lattnerbc6ec752008-11-11 18:39:10 +0000210 PATTERN ".svn" EXCLUDE
211 )
212
Kovarththanan Rajaratnamec700a62010-04-01 14:24:41 +0000213install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/include/
214 DESTINATION include
215 FILES_MATCHING
Chris Lattnerd7d5bb12010-04-25 04:59:35 +0000216 PATTERN "CMakeFiles" EXCLUDE
Kovarththanan Rajaratnamec700a62010-04-01 14:24:41 +0000217 PATTERN "*.inc"
218 )
219
Oscar Fuentes2100fe92011-02-03 22:48:20 +0000220add_definitions( -D_GNU_SOURCE -DHAVE_CLANG_CONFIG_H )
Oscar Fuentesd2f4e5e2008-10-26 00:56:18 +0000221
Douglas Gregor8435bf92011-02-25 19:24:02 +0000222# Clang version information
223set(CLANG_EXECUTABLE_VERSION
224 "${CLANG_VERSION_MAJOR}.${CLANG_VERSION_MINOR}" CACHE STRING
225 "Version number that will be placed into the clang executable, in the form XX.YY")
226set(LIBCLANG_LIBRARY_VERSION
227 "${CLANG_VERSION_MAJOR}.${CLANG_VERSION_MINOR}" CACHE STRING
228 "Version number that will be placed into the libclang library , in the form XX.YY")
229mark_as_advanced(CLANG_EXECUTABLE_VERSION LIBCLANG_LIBRARY_VERSION)
230
231
Daniel Dunbar45088e22009-11-17 09:32:51 +0000232option(CLANG_BUILD_EXAMPLES "Build CLANG example programs." OFF)
233if(CLANG_BUILD_EXAMPLES)
234 add_subdirectory(examples)
235endif ()
236
Douglas Gregora393e9e2009-03-16 23:06:59 +0000237add_subdirectory(include)
Oscar Fuentesd2f4e5e2008-10-26 00:56:18 +0000238add_subdirectory(lib)
Daniel Dunbarcbcd98b2009-03-24 02:52:57 +0000239add_subdirectory(tools)
Michael J. Spencer48263ba2010-12-16 03:28:42 +0000240add_subdirectory(runtime)
Oscar Fuentesd2f4e5e2008-10-26 00:56:18 +0000241
242# TODO: docs.
Douglas Gregor670d6ed2011-02-25 00:32:30 +0000243add_subdirectory(test)
Jeffrey Yasskin718b01d2011-02-15 07:54:28 +0000244
Douglas Gregor670d6ed2011-02-25 00:32:30 +0000245if( LLVM_INCLUDE_TESTS )
Douglas Gregorfd681572011-02-25 00:12:04 +0000246 if( NOT CLANG_BUILT_STANDALONE )
247 add_subdirectory(unittests)
248 endif()
Jeffrey Yasskin718b01d2011-02-15 07:54:28 +0000249endif()
NAKAMURA Takumi792f9752011-02-16 03:07:15 +0000250
251# Workaround for MSVS10 to avoid the Dialog Hell
252# FIXME: This could be removed with future version of CMake.
253if( CLANG_BUILT_STANDALONE AND MSVC_VERSION EQUAL 1600 )
254 set(CLANG_SLN_FILENAME "${CMAKE_CURRENT_BINARY_DIR}/Clang.sln")
255 if( EXISTS "${CLANG_SLN_FILENAME}" )
256 file(APPEND "${CLANG_SLN_FILENAME}" "\n# This should be regenerated!\n")
257 endif()
258endif()