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( 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.")
26 list(APPEND CMAKE_MODULE_PATH "${CLANG_PATH_TO_LLVM_BUILD}/share/llvm/cmake")
28 get_filename_component(PATH_TO_LLVM_BUILD ${CLANG_PATH_TO_LLVM_BUILD}
33 include("${CLANG_PATH_TO_LLVM_BUILD}/share/llvm/cmake/LLVM.cmake")
34 include(HandleLLVMOptions)
36 set(PACKAGE_VERSION "${LLVM_PACKAGE_VERSION}")
38 set(LLVM_MAIN_INCLUDE_DIR "${LLVM_MAIN_SRC_DIR}/include")
39 set(LLVM_BINARY_DIR ${CMAKE_BINARY_DIR})
41 set(CMAKE_INCLUDE_CURRENT_DIR ON)
42 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}")
46 link_directories("${PATH_TO_LLVM_BUILD}/lib")
48 set(LLVM_TABLEGEN_EXE "${PATH_TO_LLVM_BUILD}/bin/tblgen")
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 )
54 set( CLANG_BUILT_STANDALONE 1 )
57 set(CLANG_RESOURCE_DIR "" CACHE STRING
58 "Relative directory from the Clang binary to its resource files.")
60 set(C_INCLUDE_DIRS "" CACHE STRING
61 "Colon separated list of directories clang will search for headers.")
63 set(CLANG_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
64 set(CLANG_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
66 if( CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR AND NOT MSVC_IDE )
67 message(FATAL_ERROR "In-source builds are not allowed. CMake would overwrite "
68 "the makefiles distributed with LLVM. Please create a directory and run cmake "
69 "from there, passing the path to this source directory as the last argument. "
70 "This process created the file `CMakeCache.txt' and the directory "
71 "`CMakeFiles'. Please delete them.")
74 if( NOT CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR )
76 tablegenned_files_on_include_dir
77 "${CLANG_SOURCE_DIR}/include/clang/*.inc")
78 if( tablegenned_files_on_include_dir )
79 message(FATAL_ERROR "Apparently there is a previous in-source build, "
80 "probably as the result of running `configure' and `make' on "
81 "${CLANG_SOURCE_DIR}. This may cause problems. The suspicious files are:\n"
82 "${tablegenned_files_on_include_dir}\nPlease clean the source directory.")
86 # Compute the Clang version from the LLVM version.
87 string(REGEX MATCH "[0-9]+\\.[0-9]+(\\.[0-9]+)?" CLANG_VERSION
89 message(STATUS "Clang version: ${CLANG_VERSION}")
91 string(REGEX REPLACE "([0-9]+)\\.[0-9]+(\\.[0-9]+)?" "\\1" CLANG_VERSION_MAJOR
93 string(REGEX REPLACE "[0-9]+\\.([0-9]+)(\\.[0-9]+)?" "\\1" CLANG_VERSION_MINOR
95 if (${CLANG_VERSION} MATCHES "[0-9]+\\.[0-9]+\\.[0-9]+")
96 set(CLANG_HAS_VERSION_PATCHLEVEL 1)
97 string(REGEX REPLACE "[0-9]+\\.[0-9]+\\.([0-9]+)" "\\1" CLANG_VERSION_PATCHLEVEL
100 set(CLANG_HAS_VERSION_PATCHLEVEL 0)
103 # Configure the Version.inc file.
105 ${CMAKE_CURRENT_SOURCE_DIR}/include/clang/Basic/Version.inc.in
106 ${CMAKE_CURRENT_BINARY_DIR}/include/clang/Basic/Version.inc)
108 # Add appropriate flags for GCC
109 if (CMAKE_COMPILER_IS_GNUCXX)
110 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")
114 set(CMAKE_MODULE_LINKER_FLAGS "-Wl,-flat_namespace -Wl,-undefined -Wl,suppress")
118 ${CLANG_SOURCE_DIR}/include/clang/Config/config.h.cmake
119 ${CLANG_BINARY_DIR}/include/clang/Config/config.h)
121 include(LLVMParseArguments)
123 function(clang_tablegen)
125 # clang_tablegen output-file [tablegen-arg ...] SOURCE source-file
126 # [[TARGET cmake-target-name] [DEPENDS extra-dependency ...]]
128 # Generates a custom command for invoking tblgen as
130 # tblgen source-file -o=output-file tablegen-arg ...
132 # and, if cmake-target-name is provided, creates a custom target for
133 # executing the custom command depending on output-file. It is
134 # possible to list more files to depend after DEPENDS.
136 parse_arguments( CTG "SOURCE;TARGET;DEPENDS" "" ${ARGN} )
139 message(FATAL_ERROR "SOURCE source-file required by clang_tablegen")
142 set( LLVM_TARGET_DEFINITIONS ${CTG_SOURCE} )
143 tablegen( ${CTG_DEFAULT_ARGS} )
145 list( GET CTG_DEFAULT_ARGS 0 output_file )
147 add_custom_target( ${CTG_TARGET} DEPENDS ${output_file} ${CTG_DEPENDS} )
148 set_target_properties( ${CTG_TARGET} PROPERTIES FOLDER "Clang tablegenning")
150 endfunction(clang_tablegen)
152 macro(add_clang_library name)
153 llvm_process_sources(srcs ${ARGN})
154 if(MSVC_IDE OR XCODE)
155 string( REGEX MATCHALL "/[^/]+" split_path ${CMAKE_CURRENT_SOURCE_DIR})
156 list( GET split_path -1 dir)
157 file( GLOB_RECURSE headers
158 ../../../include/clang/StaticAnalyzer${dir}/*.h
159 ../../../include/clang/StaticAnalyzer${dir}/*.td
160 ../../../include/clang/StaticAnalyzer${dir}/*.def
161 ../../include/clang${dir}/*.h
162 ../../include/clang${dir}/*.td
163 ../../include/clang${dir}/*.def)
164 set(srcs ${srcs} ${headers})
165 endif(MSVC_IDE OR XCODE)
168 elseif (SHARED_LIBRARY)
173 add_library( ${name} ${libkind} ${srcs} )
174 if( LLVM_COMMON_DEPENDS )
175 add_dependencies( ${name} ${LLVM_COMMON_DEPENDS} )
176 endif( LLVM_COMMON_DEPENDS )
178 foreach(lib ${LLVM_USED_LIBS})
179 target_link_libraries( ${name} ${lib} )
181 endif( LLVM_USED_LIBS )
182 if( LLVM_LINK_COMPONENTS )
183 llvm_config(${name} ${LLVM_LINK_COMPONENTS})
184 endif( LLVM_LINK_COMPONENTS )
185 if (LLVM_COMMON_LIBS)
186 target_link_libraries(${name} ${LLVM_COMMON_LIBS})
189 get_system_libs(llvm_system_libs)
190 if( llvm_system_libs )
191 target_link_libraries(${name} ${llvm_system_libs})
194 add_dependencies(${name} ClangDiagnosticCommon)
196 get_target_property(cflag ${name} COMPILE_FLAGS)
200 set(cflag "${cflag} /Za")
201 set_target_properties(${name} PROPERTIES COMPILE_FLAGS ${cflag})
203 install(TARGETS ${name}
204 LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX}
205 ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX})
206 set_target_properties(${name} PROPERTIES FOLDER "Clang libraries")
207 endmacro(add_clang_library)
209 macro(add_clang_executable name)
210 add_llvm_executable( ${name} ${ARGN} )
211 set_target_properties(${name} PROPERTIES FOLDER "Clang executables")
212 endmacro(add_clang_executable)
215 ${CMAKE_CURRENT_SOURCE_DIR}/include
216 ${CMAKE_CURRENT_BINARY_DIR}/include
219 install(DIRECTORY include/
225 PATTERN ".svn" EXCLUDE
228 install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/include/
231 PATTERN "CMakeFiles" EXCLUDE
235 add_definitions( -D_GNU_SOURCE -DHAVE_CLANG_CONFIG_H )
237 # Clang version information
238 set(CLANG_EXECUTABLE_VERSION
239 "${CLANG_VERSION_MAJOR}.${CLANG_VERSION_MINOR}" CACHE STRING
240 "Version number that will be placed into the clang executable, in the form XX.YY")
241 set(LIBCLANG_LIBRARY_VERSION
242 "${CLANG_VERSION_MAJOR}.${CLANG_VERSION_MINOR}" CACHE STRING
243 "Version number that will be placed into the libclang library , in the form XX.YY")
244 mark_as_advanced(CLANG_EXECUTABLE_VERSION LIBCLANG_LIBRARY_VERSION)
247 option(CLANG_BUILD_EXAMPLES "Build CLANG example programs." OFF)
248 if(CLANG_BUILD_EXAMPLES)
249 add_subdirectory(examples)
252 add_subdirectory(include)
253 add_subdirectory(lib)
254 add_subdirectory(tools)
255 add_subdirectory(runtime)
258 add_subdirectory(test)
260 if( LLVM_INCLUDE_TESTS )
261 if( NOT CLANG_BUILT_STANDALONE )
262 add_subdirectory(unittests)
266 # Workaround for MSVS10 to avoid the Dialog Hell
267 # FIXME: This could be removed with future version of CMake.
268 if( CLANG_BUILT_STANDALONE AND MSVC_VERSION EQUAL 1600 )
269 set(CLANG_SLN_FILENAME "${CMAKE_CURRENT_BINARY_DIR}/Clang.sln")
270 if( EXISTS "${CLANG_SLN_FILENAME}" )
271 file(APPEND "${CLANG_SLN_FILENAME}" "\n# This should be regenerated!\n")