blob: 53d4165caec38ddff16cbc0d522155a99249b09a [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
Peter Collingbourne6ee5b932011-10-06 01:52:10 +000022 if( NOT EXISTS "${CLANG_PATH_TO_LLVM_BUILD}/bin/llvm-tblgen${CMAKE_EXECUTABLE_SUFFIX}" )
23 # Looking for bin/Debug/llvm-tblgen is a complete hack. How can we get
Douglas Gregor17031a22011-07-14 23:42:32 +000024 # around this?
Peter Collingbourne6ee5b932011-10-06 01:52:10 +000025 if( NOT EXISTS "${CLANG_PATH_TO_LLVM_BUILD}/bin/Debug/llvm-tblgen${CMAKE_EXECUTABLE_SUFFIX}" )
Douglas Gregor17031a22011-07-14 23:42:32 +000026 message(FATAL_ERROR "Please set CLANG_PATH_TO_LLVM_BUILD to a directory containing a LLVM build.")
27 endif()
Oscar Fuentes67410b32011-02-03 20:57:53 +000028 endif()
29
30 list(APPEND CMAKE_MODULE_PATH "${CLANG_PATH_TO_LLVM_BUILD}/share/llvm/cmake")
31
32 get_filename_component(PATH_TO_LLVM_BUILD ${CLANG_PATH_TO_LLVM_BUILD}
33 ABSOLUTE)
34
35 include(AddLLVM)
36 include(TableGen)
Oscar Fuentes5ec8a4d2011-04-10 16:17:31 +000037 include("${CLANG_PATH_TO_LLVM_BUILD}/share/llvm/cmake/LLVMConfig.cmake")
Oscar Fuentes67410b32011-02-03 20:57:53 +000038 include(HandleLLVMOptions)
39
40 set(PACKAGE_VERSION "${LLVM_PACKAGE_VERSION}")
41
42 set(LLVM_MAIN_INCLUDE_DIR "${LLVM_MAIN_SRC_DIR}/include")
43 set(LLVM_BINARY_DIR ${CMAKE_BINARY_DIR})
44
Oscar Fuentesb641f082011-02-14 20:14:11 +000045 set(CMAKE_INCLUDE_CURRENT_DIR ON)
Oscar Fuentes67410b32011-02-03 20:57:53 +000046 include_directories("${PATH_TO_LLVM_BUILD}/include" "${LLVM_MAIN_INCLUDE_DIR}")
Oscar Fuentes67410b32011-02-03 20:57:53 +000047 link_directories("${PATH_TO_LLVM_BUILD}/lib")
48
Peter Collingbourne6ee5b932011-10-06 01:52:10 +000049 if( EXISTS "${CLANG_PATH_TO_LLVM_BUILD}/bin/llvm-tblgen${CMAKE_EXECUTABLE_SUFFIX}" )
50 set(LLVM_TABLEGEN_EXE "${PATH_TO_LLVM_BUILD}/bin/llvm-tblgen${CMAKE_EXECUTABLE_SUFFIX}")
Douglas Gregor17031a22011-07-14 23:42:32 +000051 else()
52 # FIXME: This is an utter hack.
Peter Collingbourne6ee5b932011-10-06 01:52:10 +000053 set(LLVM_TABLEGEN_EXE "${PATH_TO_LLVM_BUILD}/bin/Debug/llvm-tblgen${CMAKE_EXECUTABLE_SUFFIX}")
Douglas Gregor17031a22011-07-14 23:42:32 +000054 endif()
Oscar Fuentes67410b32011-02-03 20:57:53 +000055
Chandler Carruth9224fb82012-07-02 20:52:55 +000056 # Define the default arguments to use with 'lit', and an option for the user
57 # to override.
58 set(LIT_ARGS_DEFAULT "-sv")
59 if (MSVC OR XCODE)
60 set(LIT_ARGS_DEFAULT "${LIT_ARGS_DEFAULT} --no-progress-bar")
61 endif()
62 set(LLVM_LIT_ARGS "${LIT_ARGS_DEFAULT}" CACHE STRING "Default options for lit")
63
Oscar Fuentes67410b32011-02-03 20:57:53 +000064 set( CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin )
65 set( CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib )
66 set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib )
67
Jeffrey Yasskin718b01d2011-02-15 07:54:28 +000068 set( CLANG_BUILT_STANDALONE 1 )
Oscar Fuentes67410b32011-02-03 20:57:53 +000069endif()
Douglas Gregor34d9ffa2009-09-16 21:59:05 +000070
Oscar Fuentes2100fe92011-02-03 22:48:20 +000071set(CLANG_RESOURCE_DIR "" CACHE STRING
72 "Relative directory from the Clang binary to its resource files.")
73
74set(C_INCLUDE_DIRS "" CACHE STRING
75 "Colon separated list of directories clang will search for headers.")
76
Sebastian Pop4762a2d2012-04-16 04:16:43 +000077set(GCC_INSTALL_PREFIX "" CACHE PATH "Directory where gcc is installed." )
78set(DEFAULT_SYSROOT "" CACHE PATH
79 "Default <path> to all compiler invocations for --sysroot=<path>." )
80
Oscar Fuentesf752d5b2011-05-20 15:57:59 +000081set(CLANG_VENDOR "" CACHE STRING
82 "Vendor-specific text for showing with version information.")
83
84if( CLANG_VENDOR )
85 add_definitions( -DCLANG_VENDOR="${CLANG_VENDOR} " )
86endif()
87
Daniel Dunbar91ee77a2009-09-17 00:07:10 +000088set(CLANG_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
89set(CLANG_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
90
Chandler Carruth63e9c0d2010-04-17 20:12:02 +000091if( CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR AND NOT MSVC_IDE )
92 message(FATAL_ERROR "In-source builds are not allowed. CMake would overwrite "
93"the makefiles distributed with LLVM. Please create a directory and run cmake "
94"from there, passing the path to this source directory as the last argument. "
95"This process created the file `CMakeCache.txt' and the directory "
96"`CMakeFiles'. Please delete them.")
97endif()
98
99if( NOT CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR )
100 file(GLOB_RECURSE
101 tablegenned_files_on_include_dir
102 "${CLANG_SOURCE_DIR}/include/clang/*.inc")
103 if( tablegenned_files_on_include_dir )
104 message(FATAL_ERROR "Apparently there is a previous in-source build, "
105"probably as the result of running `configure' and `make' on "
106"${CLANG_SOURCE_DIR}. This may cause problems. The suspicious files are:\n"
107"${tablegenned_files_on_include_dir}\nPlease clean the source directory.")
108 endif()
109endif()
110
Daniel Dunbarc4b8e922010-06-25 23:34:47 +0000111# Compute the Clang version from the LLVM version.
Michael J. Spencer5a7f3492010-09-10 21:13:16 +0000112string(REGEX MATCH "[0-9]+\\.[0-9]+(\\.[0-9]+)?" CLANG_VERSION
Daniel Dunbarc4b8e922010-06-25 23:34:47 +0000113 ${PACKAGE_VERSION})
Douglas Gregor34d9ffa2009-09-16 21:59:05 +0000114message(STATUS "Clang version: ${CLANG_VERSION}")
Douglas Gregor7f7b7482009-08-23 05:28:29 +0000115
Daniel Dunbara5107672010-06-25 17:33:46 +0000116string(REGEX REPLACE "([0-9]+)\\.[0-9]+(\\.[0-9]+)?" "\\1" CLANG_VERSION_MAJOR
117 ${CLANG_VERSION})
118string(REGEX REPLACE "[0-9]+\\.([0-9]+)(\\.[0-9]+)?" "\\1" CLANG_VERSION_MINOR
119 ${CLANG_VERSION})
120if (${CLANG_VERSION} MATCHES "[0-9]+\\.[0-9]+\\.[0-9]+")
121 set(CLANG_HAS_VERSION_PATCHLEVEL 1)
122 string(REGEX REPLACE "[0-9]+\\.[0-9]+\\.([0-9]+)" "\\1" CLANG_VERSION_PATCHLEVEL
123 ${CLANG_VERSION})
124else()
125 set(CLANG_HAS_VERSION_PATCHLEVEL 0)
126endif()
127
128# Configure the Version.inc file.
129configure_file(
130 ${CMAKE_CURRENT_SOURCE_DIR}/include/clang/Basic/Version.inc.in
131 ${CMAKE_CURRENT_BINARY_DIR}/include/clang/Basic/Version.inc)
132
Douglas Gregor9df3faf2009-09-18 14:47:57 +0000133# Add appropriate flags for GCC
Oscar Fuentesc5cd2522011-05-11 13:53:30 +0000134if (LLVM_COMPILER_IS_GCC_COMPATIBLE)
Oscar Fuentes827c5732010-10-15 00:16:22 +0000135 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 +0000136endif ()
137
Douglas Gregoreb5dc492010-06-08 19:23:49 +0000138if (APPLE)
139 set(CMAKE_MODULE_LINKER_FLAGS "-Wl,-flat_namespace -Wl,-undefined -Wl,suppress")
140endif ()
141
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +0000142# libxml2 is an optional dependency, required only to run validation
143# tests on XML output.
Douglas Gregor62d20a72012-08-07 20:42:31 +0000144find_package(LibXml2)
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +0000145
Oscar Fuentes2100fe92011-02-03 22:48:20 +0000146configure_file(
147 ${CLANG_SOURCE_DIR}/include/clang/Config/config.h.cmake
148 ${CLANG_BINARY_DIR}/include/clang/Config/config.h)
149
Oscar Fuentesc8da1ec2011-02-20 22:06:32 +0000150include(LLVMParseArguments)
151
152function(clang_tablegen)
153 # Syntax:
154 # clang_tablegen output-file [tablegen-arg ...] SOURCE source-file
155 # [[TARGET cmake-target-name] [DEPENDS extra-dependency ...]]
156 #
157 # Generates a custom command for invoking tblgen as
158 #
159 # tblgen source-file -o=output-file tablegen-arg ...
160 #
161 # and, if cmake-target-name is provided, creates a custom target for
162 # executing the custom command depending on output-file. It is
163 # possible to list more files to depend after DEPENDS.
164
165 parse_arguments( CTG "SOURCE;TARGET;DEPENDS" "" ${ARGN} )
166
167 if( NOT CTG_SOURCE )
168 message(FATAL_ERROR "SOURCE source-file required by clang_tablegen")
169 endif()
170
171 set( LLVM_TARGET_DEFINITIONS ${CTG_SOURCE} )
Peter Collingbourne51d77772011-10-06 13:03:08 +0000172 tablegen( CLANG ${CTG_DEFAULT_ARGS} )
Oscar Fuentesc8da1ec2011-02-20 22:06:32 +0000173
174 list( GET CTG_DEFAULT_ARGS 0 output_file )
175 if( CTG_TARGET )
176 add_custom_target( ${CTG_TARGET} DEPENDS ${output_file} ${CTG_DEPENDS} )
Oscar Fuentesa3f787c2011-02-20 22:06:44 +0000177 set_target_properties( ${CTG_TARGET} PROPERTIES FOLDER "Clang tablegenning")
Oscar Fuentesc8da1ec2011-02-20 22:06:32 +0000178 endif()
179endfunction(clang_tablegen)
180
Oscar Fuentesd2f4e5e2008-10-26 00:56:18 +0000181macro(add_clang_library name)
Oscar Fuentes99174012011-01-03 17:00:02 +0000182 llvm_process_sources(srcs ${ARGN})
Ted Kremenekbf5de3f2009-03-25 20:34:07 +0000183 if(MSVC_IDE OR XCODE)
Jordan Rose340d0d32012-10-23 21:54:03 +0000184 # Add public headers
185 file(RELATIVE_PATH lib_path
186 ${CLANG_SOURCE_DIR}/lib/
187 ${CMAKE_CURRENT_SOURCE_DIR}
188 )
189 if(NOT lib_path MATCHES "^[.][.]")
190 file( GLOB_RECURSE headers
191 ${CLANG_SOURCE_DIR}/include/clang/${lib_path}/*.h
192 ${CLANG_SOURCE_DIR}/include/clang/${lib_path}/*.def
193 )
194 set_source_files_properties(${headers} PROPERTIES HEADER_FILE_ONLY ON)
195
196 file( GLOB_RECURSE tds
197 ${CLANG_SOURCE_DIR}/include/clang/${lib_path}/*.td
198 )
199 source_group("TableGen descriptions" FILES ${tds})
200 set_source_files_properties(${tds}} PROPERTIES HEADER_FILE_ONLY ON)
201
202 set(srcs ${srcs} ${headers} ${tds})
203 endif()
Ted Kremenekbf5de3f2009-03-25 20:34:07 +0000204 endif(MSVC_IDE OR XCODE)
Douglas Gregoreb5dc492010-06-08 19:23:49 +0000205 if (MODULE)
206 set(libkind MODULE)
207 elseif (SHARED_LIBRARY)
Douglas Gregorac47bc72009-09-25 06:35:15 +0000208 set(libkind SHARED)
209 else()
210 set(libkind)
211 endif()
212 add_library( ${name} ${libkind} ${srcs} )
Oscar Fuentesd2f4e5e2008-10-26 00:56:18 +0000213 if( LLVM_COMMON_DEPENDS )
214 add_dependencies( ${name} ${LLVM_COMMON_DEPENDS} )
215 endif( LLVM_COMMON_DEPENDS )
Oscar Fuentesfb767c82011-03-29 20:51:00 +0000216
Oscar Fuentesfb767c82011-03-29 20:51:00 +0000217 llvm_config( ${name} ${LLVM_LINK_COMPONENTS} )
218 target_link_libraries( ${name} ${LLVM_COMMON_LIBS} )
219 link_system_libs( ${name} )
220
Oscar Fuentesd2f4e5e2008-10-26 00:56:18 +0000221 install(TARGETS ${name}
Oscar Fuentesbfb06ea2009-10-27 19:42:21 +0000222 LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX}
NAKAMURA Takumiee7a1982012-02-25 16:46:50 +0000223 ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX}
224 RUNTIME DESTINATION bin)
Oscar Fuentesa3f787c2011-02-20 22:06:44 +0000225 set_target_properties(${name} PROPERTIES FOLDER "Clang libraries")
Oscar Fuentesd2f4e5e2008-10-26 00:56:18 +0000226endmacro(add_clang_library)
227
228macro(add_clang_executable name)
Oscar Fuentes99174012011-01-03 17:00:02 +0000229 add_llvm_executable( ${name} ${ARGN} )
Oscar Fuentesa3f787c2011-02-20 22:06:44 +0000230 set_target_properties(${name} PROPERTIES FOLDER "Clang executables")
Oscar Fuentesd2f4e5e2008-10-26 00:56:18 +0000231endmacro(add_clang_executable)
232
Oscar Fuentes617508f2011-03-17 19:03:04 +0000233include_directories(BEFORE
Douglas Gregora393e9e2009-03-16 23:06:59 +0000234 ${CMAKE_CURRENT_BINARY_DIR}/include
Oscar Fuentes617508f2011-03-17 19:03:04 +0000235 ${CMAKE_CURRENT_SOURCE_DIR}/include
Oscar Fuentesd2f4e5e2008-10-26 00:56:18 +0000236 )
237
Oscar Fuentes82cac292009-10-27 19:59:34 +0000238install(DIRECTORY include/
239 DESTINATION include
240 FILES_MATCHING
241 PATTERN "*.def"
242 PATTERN "*.h"
Dylan Noblesmitheafa50b2012-02-01 22:22:37 +0000243 PATTERN "config.h" EXCLUDE
Chris Lattnerbc6ec752008-11-11 18:39:10 +0000244 PATTERN ".svn" EXCLUDE
245 )
246
Kovarththanan Rajaratnamec700a62010-04-01 14:24:41 +0000247install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/include/
248 DESTINATION include
249 FILES_MATCHING
Chris Lattnerd7d5bb12010-04-25 04:59:35 +0000250 PATTERN "CMakeFiles" EXCLUDE
Kovarththanan Rajaratnamec700a62010-04-01 14:24:41 +0000251 PATTERN "*.inc"
252 )
253
Dylan Noblesmithcc8a9452012-02-14 15:54:49 +0000254add_definitions( -D_GNU_SOURCE )
Oscar Fuentesd2f4e5e2008-10-26 00:56:18 +0000255
Douglas Gregor8435bf92011-02-25 19:24:02 +0000256# Clang version information
257set(CLANG_EXECUTABLE_VERSION
258 "${CLANG_VERSION_MAJOR}.${CLANG_VERSION_MINOR}" CACHE STRING
259 "Version number that will be placed into the clang executable, in the form XX.YY")
260set(LIBCLANG_LIBRARY_VERSION
261 "${CLANG_VERSION_MAJOR}.${CLANG_VERSION_MINOR}" CACHE STRING
262 "Version number that will be placed into the libclang library , in the form XX.YY")
263mark_as_advanced(CLANG_EXECUTABLE_VERSION LIBCLANG_LIBRARY_VERSION)
264
Peter Collingbourne51d77772011-10-06 13:03:08 +0000265add_subdirectory(utils/TableGen)
Douglas Gregor8435bf92011-02-25 19:24:02 +0000266
Douglas Gregora393e9e2009-03-16 23:06:59 +0000267add_subdirectory(include)
Oscar Fuentesd2f4e5e2008-10-26 00:56:18 +0000268add_subdirectory(lib)
Daniel Dunbarcbcd98b2009-03-24 02:52:57 +0000269add_subdirectory(tools)
Michael J. Spencer48263ba2010-12-16 03:28:42 +0000270add_subdirectory(runtime)
Oscar Fuentesd2f4e5e2008-10-26 00:56:18 +0000271
NAKAMURA Takumie3120ae2012-07-27 06:17:48 +0000272option(CLANG_BUILD_EXAMPLES "Build CLANG example programs by default." OFF)
273add_subdirectory(examples)
274
Oscar Fuentesd2f4e5e2008-10-26 00:56:18 +0000275# TODO: docs.
Douglas Gregor670d6ed2011-02-25 00:32:30 +0000276add_subdirectory(test)
Jeffrey Yasskin718b01d2011-02-15 07:54:28 +0000277
Douglas Gregor670d6ed2011-02-25 00:32:30 +0000278if( LLVM_INCLUDE_TESTS )
Douglas Gregorfd681572011-02-25 00:12:04 +0000279 if( NOT CLANG_BUILT_STANDALONE )
280 add_subdirectory(unittests)
281 endif()
Jeffrey Yasskin718b01d2011-02-15 07:54:28 +0000282endif()
NAKAMURA Takumi792f9752011-02-16 03:07:15 +0000283
284# Workaround for MSVS10 to avoid the Dialog Hell
285# FIXME: This could be removed with future version of CMake.
286if( CLANG_BUILT_STANDALONE AND MSVC_VERSION EQUAL 1600 )
287 set(CLANG_SLN_FILENAME "${CMAKE_CURRENT_BINARY_DIR}/Clang.sln")
288 if( EXISTS "${CLANG_SLN_FILENAME}" )
289 file(APPEND "${CLANG_SLN_FILENAME}" "\n# This should be regenerated!\n")
290 endif()
291endif()
Chad Rosierfc614272011-08-02 20:44:34 +0000292
293set(BUG_REPORT_URL "http://llvm.org/bugs/" CACHE STRING
294 "Default URL where bug reports are to be submitted.")
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +0000295