1 # If we are not building as a part of LLVM, build Clang as an
2 # standalone project, using LLVM as an external library:
3 if( CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR )
5 cmake_minimum_required(VERSION 2.8)
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.")
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.")
16 get_filename_component(LLVM_MAIN_SRC_DIR ${CLANG_PATH_TO_LLVM_SOURCE}
18 list(APPEND CMAKE_MODULE_PATH "${LLVM_MAIN_SRC_DIR}/cmake/modules")
22 if (EXISTS "${CLANG_PATH_TO_LLVM_BUILD}/bin/llvm-config${CMAKE_EXECUTABLE_SUFFIX}")
23 set (PATH_TO_LLVM_CONFIG "${CLANG_PATH_TO_LLVM_BUILD}/bin/llvm-config${CMAKE_EXECUTABLE_SUFFIX}")
24 elseif (EXISTS "${CLANG_PATH_TO_LLVM_BUILD}/bin/Debug/llvm-config${CMAKE_EXECUTABLE_SUFFIX}")
25 # Looking for bin/Debug/llvm-config is a complete hack. How can we get
27 set (PATH_TO_LLVM_CONFIG "${CLANG_PATH_TO_LLVM_BUILD}/bin/Debug/llvm-config${CMAKE_EXECUTABLE_SUFFIX}")
29 message(FATAL_ERROR "Please set CLANG_PATH_TO_LLVM_BUILD to a directory containing a LLVM build.")
32 list(APPEND CMAKE_MODULE_PATH "${CLANG_PATH_TO_LLVM_BUILD}/share/llvm/cmake")
34 get_filename_component(PATH_TO_LLVM_BUILD ${CLANG_PATH_TO_LLVM_BUILD}
37 option(LLVM_INSTALL_TOOLCHAIN_ONLY "Only include toolchain files in the 'install' target." OFF)
41 include("${CLANG_PATH_TO_LLVM_BUILD}/share/llvm/cmake/LLVMConfig.cmake")
42 include(HandleLLVMOptions)
44 set(PACKAGE_VERSION "${LLVM_PACKAGE_VERSION}")
46 set(LLVM_MAIN_INCLUDE_DIR "${LLVM_MAIN_SRC_DIR}/include")
47 set(LLVM_BINARY_DIR ${CMAKE_BINARY_DIR})
49 set(CMAKE_INCLUDE_CURRENT_DIR ON)
50 include_directories("${PATH_TO_LLVM_BUILD}/include" "${LLVM_MAIN_INCLUDE_DIR}")
51 link_directories("${PATH_TO_LLVM_BUILD}/lib")
53 exec_program("${PATH_TO_LLVM_CONFIG} --bindir" OUTPUT_VARIABLE LLVM_BINARY_DIR)
54 set(LLVM_TABLEGEN_EXE "${LLVM_BINARY_DIR}/llvm-tblgen${CMAKE_EXECUTABLE_SUFFIX}")
56 # Define the default arguments to use with 'lit', and an option for the user
58 set(LIT_ARGS_DEFAULT "-sv")
60 set(LIT_ARGS_DEFAULT "${LIT_ARGS_DEFAULT} --no-progress-bar")
62 set(LLVM_LIT_ARGS "${LIT_ARGS_DEFAULT}" CACHE STRING "Default options for lit")
64 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 )
68 set( CLANG_BUILT_STANDALONE 1 )
72 set(CLANG_HAVE_LIBXML 1)
76 set(CLANG_RESOURCE_DIR "" CACHE STRING
77 "Relative directory from the Clang binary to its resource files.")
79 set(C_INCLUDE_DIRS "" CACHE STRING
80 "Colon separated list of directories clang will search for headers.")
82 set(GCC_INSTALL_PREFIX "" CACHE PATH "Directory where gcc is installed." )
83 set(DEFAULT_SYSROOT "" CACHE PATH
84 "Default <path> to all compiler invocations for --sysroot=<path>." )
86 set(CLANG_VENDOR "" CACHE STRING
87 "Vendor-specific text for showing with version information.")
90 add_definitions( -DCLANG_VENDOR="${CLANG_VENDOR} " )
93 set(CLANG_REPOSITORY_STRING "" CACHE STRING
94 "Vendor-specific text for showing the repository the source is taken from.")
96 if(CLANG_REPOSITORY_STRING)
97 add_definitions(-DCLANG_REPOSITORY_STRING="${CLANG_REPOSITORY_STRING}")
100 set(CLANG_VENDOR_UTI "org.llvm.clang" CACHE STRING
101 "Vendor-specific uti.")
103 set(CLANG_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
104 set(CLANG_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
106 if( CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR AND NOT MSVC_IDE )
107 message(FATAL_ERROR "In-source builds are not allowed. CMake would overwrite "
108 "the makefiles distributed with LLVM. Please create a directory and run cmake "
109 "from there, passing the path to this source directory as the last argument. "
110 "This process created the file `CMakeCache.txt' and the directory "
111 "`CMakeFiles'. Please delete them.")
114 if( NOT CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR )
116 tablegenned_files_on_include_dir
117 "${CLANG_SOURCE_DIR}/include/clang/*.inc")
118 if( tablegenned_files_on_include_dir )
119 message(FATAL_ERROR "Apparently there is a previous in-source build, "
120 "probably as the result of running `configure' and `make' on "
121 "${CLANG_SOURCE_DIR}. This may cause problems. The suspicious files are:\n"
122 "${tablegenned_files_on_include_dir}\nPlease clean the source directory.")
126 # Compute the Clang version from the LLVM version.
127 string(REGEX MATCH "[0-9]+\\.[0-9]+(\\.[0-9]+)?" CLANG_VERSION
129 message(STATUS "Clang version: ${CLANG_VERSION}")
131 string(REGEX REPLACE "([0-9]+)\\.[0-9]+(\\.[0-9]+)?" "\\1" CLANG_VERSION_MAJOR
133 string(REGEX REPLACE "[0-9]+\\.([0-9]+)(\\.[0-9]+)?" "\\1" CLANG_VERSION_MINOR
135 if (${CLANG_VERSION} MATCHES "[0-9]+\\.[0-9]+\\.[0-9]+")
136 set(CLANG_HAS_VERSION_PATCHLEVEL 1)
137 string(REGEX REPLACE "[0-9]+\\.[0-9]+\\.([0-9]+)" "\\1" CLANG_VERSION_PATCHLEVEL
140 set(CLANG_HAS_VERSION_PATCHLEVEL 0)
143 # Configure the Version.inc file.
145 ${CMAKE_CURRENT_SOURCE_DIR}/include/clang/Basic/Version.inc.in
146 ${CMAKE_CURRENT_BINARY_DIR}/include/clang/Basic/Version.inc)
148 # Add appropriate flags for GCC
149 if (LLVM_COMPILER_IS_GCC_COMPATIBLE)
150 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-common -Woverloaded-virtual -Wcast-qual -fno-strict-aliasing")
152 # Enable -pedantic for Clang even if it's not enabled for LLVM.
153 if (NOT LLVM_ENABLE_PEDANTIC)
154 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic -Wno-long-long")
157 check_cxx_compiler_flag("-Werror -Wnested-anon-types" CXX_SUPPORTS_NO_NESTED_ANON_TYPES_FLAG)
158 if( CXX_SUPPORTS_NO_NESTED_ANON_TYPES_FLAG )
159 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-nested-anon-types" )
164 set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -Wl,-flat_namespace -Wl,-undefined -Wl,suppress")
168 ${CLANG_SOURCE_DIR}/include/clang/Config/config.h.cmake
169 ${CLANG_BINARY_DIR}/include/clang/Config/config.h)
171 include(LLVMParseArguments)
173 function(clang_tablegen)
175 # clang_tablegen output-file [tablegen-arg ...] SOURCE source-file
176 # [[TARGET cmake-target-name] [DEPENDS extra-dependency ...]]
178 # Generates a custom command for invoking tblgen as
180 # tblgen source-file -o=output-file tablegen-arg ...
182 # and, if cmake-target-name is provided, creates a custom target for
183 # executing the custom command depending on output-file. It is
184 # possible to list more files to depend after DEPENDS.
186 parse_arguments( CTG "SOURCE;TARGET;DEPENDS" "" ${ARGN} )
189 message(FATAL_ERROR "SOURCE source-file required by clang_tablegen")
192 set( LLVM_TARGET_DEFINITIONS ${CTG_SOURCE} )
193 tablegen( CLANG ${CTG_DEFAULT_ARGS} )
195 list( GET CTG_DEFAULT_ARGS 0 output_file )
197 add_custom_target( ${CTG_TARGET} DEPENDS ${output_file} ${CTG_DEPENDS} )
198 set_target_properties( ${CTG_TARGET} PROPERTIES FOLDER "Clang tablegenning")
200 endfunction(clang_tablegen)
202 # FIXME: Generalize and move to llvm.
203 function(add_clang_symbol_exports target_name export_file)
204 # Makefile.rules contains special cases for different platforms.
205 # We restrict ourselves to Darwin for the time being.
206 if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
207 add_custom_command(OUTPUT symbol.exports
208 COMMAND sed -e "s/^/_/" < ${export_file} > symbol.exports
209 DEPENDS ${export_file}
211 COMMENT "Creating export file for ${target_name}")
212 add_custom_target(${target_name}_exports DEPENDS symbol.exports)
213 set_property(DIRECTORY APPEND
214 PROPERTY ADDITIONAL_MAKE_CLEAN_FILES symbol.exports)
216 get_property(srcs TARGET ${target_name} PROPERTY SOURCES)
218 get_filename_component(extension ${src} EXT)
219 if(extension STREQUAL ".cpp")
220 set(first_source_file ${src})
225 # Force re-linking when the exports file changes. Actually, it
226 # forces recompilation of the source file. The LINK_DEPENDS target
227 # property only works for makefile-based generators.
228 set_property(SOURCE ${first_source_file} APPEND PROPERTY
229 OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/symbol.exports)
231 set_property(TARGET ${target_name} APPEND_STRING PROPERTY
232 LINK_FLAGS " -Wl,-exported_symbols_list,${CMAKE_CURRENT_BINARY_DIR}/symbol.exports")
233 add_dependencies(${target_name} ${target_name}_exports)
235 endfunction(add_clang_symbol_exports)
237 macro(add_clang_library name)
238 llvm_process_sources(srcs ${ARGN})
239 if(MSVC_IDE OR XCODE)
241 file(RELATIVE_PATH lib_path
242 ${CLANG_SOURCE_DIR}/lib/
243 ${CMAKE_CURRENT_SOURCE_DIR}
245 if(NOT lib_path MATCHES "^[.][.]")
246 file( GLOB_RECURSE headers
247 ${CLANG_SOURCE_DIR}/include/clang/${lib_path}/*.h
248 ${CLANG_SOURCE_DIR}/include/clang/${lib_path}/*.def
250 set_source_files_properties(${headers} PROPERTIES HEADER_FILE_ONLY ON)
252 file( GLOB_RECURSE tds
253 ${CLANG_SOURCE_DIR}/include/clang/${lib_path}/*.td
255 source_group("TableGen descriptions" FILES ${tds})
256 set_source_files_properties(${tds}} PROPERTIES HEADER_FILE_ONLY ON)
258 set(srcs ${srcs} ${headers} ${tds})
260 endif(MSVC_IDE OR XCODE)
263 elseif (SHARED_LIBRARY)
268 add_library( ${name} ${libkind} ${srcs} )
269 if( LLVM_COMMON_DEPENDS )
270 add_dependencies( ${name} ${LLVM_COMMON_DEPENDS} )
271 endif( LLVM_COMMON_DEPENDS )
273 llvm_config( ${name} ${LLVM_LINK_COMPONENTS} )
274 target_link_libraries( ${name} ${LLVM_COMMON_LIBS} )
275 link_system_libs( ${name} )
277 if (SHARED_LIBRARY AND EXPORTED_SYMBOL_FILE)
278 add_clang_symbol_exports( ${name} ${EXPORTED_SYMBOL_FILE} )
281 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY OR ${name} STREQUAL "libclang")
282 install(TARGETS ${name}
283 LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX}
284 ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX}
285 RUNTIME DESTINATION bin)
288 set_target_properties(${name} PROPERTIES FOLDER "Clang libraries")
289 endmacro(add_clang_library)
291 macro(add_clang_executable name)
292 add_llvm_executable( ${name} ${ARGN} )
293 set_target_properties(${name} PROPERTIES FOLDER "Clang executables")
294 endmacro(add_clang_executable)
296 include_directories(BEFORE
297 ${CMAKE_CURRENT_BINARY_DIR}/include
298 ${CMAKE_CURRENT_SOURCE_DIR}/include
301 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
302 install(DIRECTORY include/
307 PATTERN "config.h" EXCLUDE
308 PATTERN ".svn" EXCLUDE
311 install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/include/
314 PATTERN "CMakeFiles" EXCLUDE
319 install(DIRECTORY include/clang-c
323 PATTERN ".svn" EXCLUDE
326 add_definitions( -D_GNU_SOURCE )
328 option(CLANG_ENABLE_ARCMT "Build ARCMT." ON)
329 option(CLANG_ENABLE_REWRITER "Build rewriter." ON)
330 option(CLANG_ENABLE_STATIC_ANALYZER "Build static analyzer." ON)
332 if (NOT CLANG_ENABLE_REWRITER AND CLANG_ENABLE_ARCMT)
333 message(FATAL_ERROR "Cannot disable rewriter while enabling ARCMT")
336 if (NOT CLANG_ENABLE_REWRITER AND CLANG_ENABLE_STATIC_ANALYZER)
337 message(FATAL_ERROR "Cannot disable rewriter while enabling static analyzer")
340 if (NOT CLANG_ENABLE_STATIC_ANALYZER AND CLANG_ENABLE_ARCMT)
341 message(FATAL_ERROR "Cannot disable static analyzer while enabling ARCMT")
344 if(CLANG_ENABLE_ARCMT)
345 add_definitions(-DCLANG_ENABLE_ARCMT)
347 if(CLANG_ENABLE_REWRITER)
348 add_definitions(-DCLANG_ENABLE_REWRITER)
350 if(CLANG_ENABLE_STATIC_ANALYZER)
351 add_definitions(-DCLANG_ENABLE_STATIC_ANALYZER)
354 # Clang version information
355 set(CLANG_EXECUTABLE_VERSION
356 "${CLANG_VERSION_MAJOR}.${CLANG_VERSION_MINOR}" CACHE STRING
357 "Version number that will be placed into the clang executable, in the form XX.YY")
358 set(LIBCLANG_LIBRARY_VERSION
359 "${CLANG_VERSION_MAJOR}.${CLANG_VERSION_MINOR}" CACHE STRING
360 "Version number that will be placed into the libclang library , in the form XX.YY")
361 mark_as_advanced(CLANG_EXECUTABLE_VERSION LIBCLANG_LIBRARY_VERSION)
363 add_subdirectory(utils/TableGen)
365 add_subdirectory(include)
366 add_subdirectory(lib)
367 add_subdirectory(tools)
368 add_subdirectory(runtime)
370 option(CLANG_BUILD_EXAMPLES "Build CLANG example programs by default." OFF)
371 add_subdirectory(examples)
373 option(CLANG_INCLUDE_TESTS
374 "Generate build targets for the Clang unit tests."
375 ${LLVM_INCLUDE_TESTS})
377 if( CLANG_INCLUDE_TESTS )
378 add_subdirectory(test)
379 add_subdirectory(unittests)
382 option(CLANG_INCLUDE_DOCS "Generate build targets for the Clang docs."
383 ${LLVM_INCLUDE_DOCS})
384 if( CLANG_INCLUDE_DOCS )
385 add_subdirectory(docs)
388 # Workaround for MSVS10 to avoid the Dialog Hell
389 # FIXME: This could be removed with future version of CMake.
390 if( CLANG_BUILT_STANDALONE AND MSVC_VERSION EQUAL 1600 )
391 set(CLANG_SLN_FILENAME "${CMAKE_CURRENT_BINARY_DIR}/Clang.sln")
392 if( EXISTS "${CLANG_SLN_FILENAME}" )
393 file(APPEND "${CLANG_SLN_FILENAME}" "\n# This should be regenerated!\n")
397 set(BUG_REPORT_URL "http://llvm.org/bugs/" CACHE STRING
398 "Default URL where bug reports are to be submitted.")
400 set(CLANG_ORDER_FILE "" CACHE FILEPATH
401 "Order file to use when compiling clang in order to improve startup time.")