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/llvm-tblgen${CMAKE_EXECUTABLE_SUFFIX}" )
23 # Looking for bin/Debug/llvm-tblgen is a complete hack. How can we get
25 if( NOT EXISTS "${CLANG_PATH_TO_LLVM_BUILD}/bin/Debug/llvm-tblgen${CMAKE_EXECUTABLE_SUFFIX}" )
26 message(FATAL_ERROR "Please set CLANG_PATH_TO_LLVM_BUILD to a directory containing a LLVM build.")
30 list(APPEND CMAKE_MODULE_PATH "${CLANG_PATH_TO_LLVM_BUILD}/share/llvm/cmake")
32 get_filename_component(PATH_TO_LLVM_BUILD ${CLANG_PATH_TO_LLVM_BUILD}
37 include("${CLANG_PATH_TO_LLVM_BUILD}/share/llvm/cmake/LLVMConfig.cmake")
38 include(HandleLLVMOptions)
40 set(PACKAGE_VERSION "${LLVM_PACKAGE_VERSION}")
42 set(LLVM_MAIN_INCLUDE_DIR "${LLVM_MAIN_SRC_DIR}/include")
43 set(LLVM_BINARY_DIR ${CMAKE_BINARY_DIR})
45 set(CMAKE_INCLUDE_CURRENT_DIR ON)
46 include_directories("${PATH_TO_LLVM_BUILD}/include" "${LLVM_MAIN_INCLUDE_DIR}")
47 link_directories("${PATH_TO_LLVM_BUILD}/lib")
49 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}")
52 # FIXME: This is an utter hack.
53 set(LLVM_TABLEGEN_EXE "${PATH_TO_LLVM_BUILD}/bin/Debug/llvm-tblgen${CMAKE_EXECUTABLE_SUFFIX}")
56 set( CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin )
57 set( CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib )
58 set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib )
60 set( CLANG_BUILT_STANDALONE 1 )
63 set(CLANG_RESOURCE_DIR "" CACHE STRING
64 "Relative directory from the Clang binary to its resource files.")
66 set(C_INCLUDE_DIRS "" CACHE STRING
67 "Colon separated list of directories clang will search for headers.")
69 set(CLANG_VENDOR "" CACHE STRING
70 "Vendor-specific text for showing with version information.")
73 add_definitions( -DCLANG_VENDOR="${CLANG_VENDOR} " )
76 set(CLANG_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
77 set(CLANG_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
79 if( CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR AND NOT MSVC_IDE )
80 message(FATAL_ERROR "In-source builds are not allowed. CMake would overwrite "
81 "the makefiles distributed with LLVM. Please create a directory and run cmake "
82 "from there, passing the path to this source directory as the last argument. "
83 "This process created the file `CMakeCache.txt' and the directory "
84 "`CMakeFiles'. Please delete them.")
87 if( NOT CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR )
89 tablegenned_files_on_include_dir
90 "${CLANG_SOURCE_DIR}/include/clang/*.inc")
91 if( tablegenned_files_on_include_dir )
92 message(FATAL_ERROR "Apparently there is a previous in-source build, "
93 "probably as the result of running `configure' and `make' on "
94 "${CLANG_SOURCE_DIR}. This may cause problems. The suspicious files are:\n"
95 "${tablegenned_files_on_include_dir}\nPlease clean the source directory.")
99 # Compute the Clang version from the LLVM version.
100 string(REGEX MATCH "[0-9]+\\.[0-9]+(\\.[0-9]+)?" CLANG_VERSION
102 message(STATUS "Clang version: ${CLANG_VERSION}")
104 string(REGEX REPLACE "([0-9]+)\\.[0-9]+(\\.[0-9]+)?" "\\1" CLANG_VERSION_MAJOR
106 string(REGEX REPLACE "[0-9]+\\.([0-9]+)(\\.[0-9]+)?" "\\1" CLANG_VERSION_MINOR
108 if (${CLANG_VERSION} MATCHES "[0-9]+\\.[0-9]+\\.[0-9]+")
109 set(CLANG_HAS_VERSION_PATCHLEVEL 1)
110 string(REGEX REPLACE "[0-9]+\\.[0-9]+\\.([0-9]+)" "\\1" CLANG_VERSION_PATCHLEVEL
113 set(CLANG_HAS_VERSION_PATCHLEVEL 0)
116 # Configure the Version.inc file.
118 ${CMAKE_CURRENT_SOURCE_DIR}/include/clang/Basic/Version.inc.in
119 ${CMAKE_CURRENT_BINARY_DIR}/include/clang/Basic/Version.inc)
121 # Add appropriate flags for GCC
122 if (LLVM_COMPILER_IS_GCC_COMPATIBLE)
123 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")
127 set(CMAKE_MODULE_LINKER_FLAGS "-Wl,-flat_namespace -Wl,-undefined -Wl,suppress")
131 ${CLANG_SOURCE_DIR}/include/clang/Config/config.h.cmake
132 ${CLANG_BINARY_DIR}/include/clang/Config/config.h)
134 include(LLVMParseArguments)
136 function(clang_tablegen)
138 # clang_tablegen output-file [tablegen-arg ...] SOURCE source-file
139 # [[TARGET cmake-target-name] [DEPENDS extra-dependency ...]]
141 # Generates a custom command for invoking tblgen as
143 # tblgen source-file -o=output-file tablegen-arg ...
145 # and, if cmake-target-name is provided, creates a custom target for
146 # executing the custom command depending on output-file. It is
147 # possible to list more files to depend after DEPENDS.
149 parse_arguments( CTG "SOURCE;TARGET;DEPENDS" "" ${ARGN} )
152 message(FATAL_ERROR "SOURCE source-file required by clang_tablegen")
155 set( LLVM_TARGET_DEFINITIONS ${CTG_SOURCE} )
156 tablegen( CLANG ${CTG_DEFAULT_ARGS} )
158 list( GET CTG_DEFAULT_ARGS 0 output_file )
160 add_custom_target( ${CTG_TARGET} DEPENDS ${output_file} ${CTG_DEPENDS} )
161 set_target_properties( ${CTG_TARGET} PROPERTIES FOLDER "Clang tablegenning")
163 endfunction(clang_tablegen)
165 macro(add_clang_library name)
166 llvm_process_sources(srcs ${ARGN})
167 if(MSVC_IDE OR XCODE)
168 string( REGEX MATCHALL "/[^/]+" split_path ${CMAKE_CURRENT_SOURCE_DIR})
169 list( GET split_path -1 dir)
170 file( GLOB_RECURSE headers
171 ../../../include/clang/StaticAnalyzer${dir}/*.h
172 ../../../include/clang/StaticAnalyzer${dir}/*.td
173 ../../../include/clang/StaticAnalyzer${dir}/*.def
174 ../../include/clang${dir}/*.h
175 ../../include/clang${dir}/*.td
176 ../../include/clang${dir}/*.def)
177 set(srcs ${srcs} ${headers})
178 endif(MSVC_IDE OR XCODE)
181 elseif (SHARED_LIBRARY)
186 add_library( ${name} ${libkind} ${srcs} )
187 if( LLVM_COMMON_DEPENDS )
188 add_dependencies( ${name} ${LLVM_COMMON_DEPENDS} )
189 endif( LLVM_COMMON_DEPENDS )
191 target_link_libraries( ${name} ${LLVM_USED_LIBS} )
192 llvm_config( ${name} ${LLVM_LINK_COMPONENTS} )
193 target_link_libraries( ${name} ${LLVM_COMMON_LIBS} )
194 link_system_libs( ${name} )
196 add_dependencies(${name} ClangDiagnosticCommon)
198 get_target_property(cflag ${name} COMPILE_FLAGS)
202 set(cflag "${cflag} /Za")
203 set_target_properties(${name} PROPERTIES COMPILE_FLAGS ${cflag})
205 install(TARGETS ${name}
206 LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX}
207 ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX}
208 RUNTIME DESTINATION bin)
209 set_target_properties(${name} PROPERTIES FOLDER "Clang libraries")
210 endmacro(add_clang_library)
212 macro(add_clang_executable name)
213 add_llvm_executable( ${name} ${ARGN} )
214 set_target_properties(${name} PROPERTIES FOLDER "Clang executables")
215 endmacro(add_clang_executable)
217 include_directories(BEFORE
218 ${CMAKE_CURRENT_BINARY_DIR}/include
219 ${CMAKE_CURRENT_SOURCE_DIR}/include
222 install(DIRECTORY include/
227 PATTERN "config.h" EXCLUDE
228 PATTERN ".svn" EXCLUDE
231 install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/include/
234 PATTERN "CMakeFiles" EXCLUDE
238 add_definitions( -D_GNU_SOURCE )
240 # Clang version information
241 set(CLANG_EXECUTABLE_VERSION
242 "${CLANG_VERSION_MAJOR}.${CLANG_VERSION_MINOR}" CACHE STRING
243 "Version number that will be placed into the clang executable, in the form XX.YY")
244 set(LIBCLANG_LIBRARY_VERSION
245 "${CLANG_VERSION_MAJOR}.${CLANG_VERSION_MINOR}" CACHE STRING
246 "Version number that will be placed into the libclang library , in the form XX.YY")
247 mark_as_advanced(CLANG_EXECUTABLE_VERSION LIBCLANG_LIBRARY_VERSION)
249 add_subdirectory(utils/TableGen)
251 option(CLANG_BUILD_EXAMPLES "Build CLANG example programs by default." OFF)
252 add_subdirectory(examples)
254 add_subdirectory(include)
255 add_subdirectory(lib)
256 add_subdirectory(tools)
257 add_subdirectory(runtime)
260 add_subdirectory(test)
262 if( LLVM_INCLUDE_TESTS )
263 if( NOT CLANG_BUILT_STANDALONE )
264 add_subdirectory(unittests)
268 # Workaround for MSVS10 to avoid the Dialog Hell
269 # FIXME: This could be removed with future version of CMake.
270 if( CLANG_BUILT_STANDALONE AND MSVC_VERSION EQUAL 1600 )
271 set(CLANG_SLN_FILENAME "${CMAKE_CURRENT_BINARY_DIR}/Clang.sln")
272 if( EXISTS "${CLANG_SLN_FILENAME}" )
273 file(APPEND "${CLANG_SLN_FILENAME}" "\n# This should be regenerated!\n")
277 set(BUG_REPORT_URL "http://llvm.org/bugs/" CACHE STRING
278 "Default URL where bug reports are to be submitted.")