[analyzer] Reflect changes for tablegen'ing the checkers.
[clang.git] / CMakeLists.txt
blobfb5933fd95dc405c250b37fd00a17e5a16140ca8
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 )
4   project(Clang)
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.")
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()
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()
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}
29     ABSOLUTE)
31   include(AddLLVM)
32   include(TableGen)
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}")
45   endif()
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   add_definitions( -D__STDC_LIMIT_MACROS )
55   add_definitions( -D__STDC_CONSTANT_MACROS )
56 endif()
58 set(CLANG_RESOURCE_DIR "" CACHE STRING
59   "Relative directory from the Clang binary to its resource files.")
61 set(C_INCLUDE_DIRS "" CACHE STRING
62   "Colon separated list of directories clang will search for headers.")
64 set(CLANG_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
65 set(CLANG_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
67 if( CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR AND NOT MSVC_IDE )
68   message(FATAL_ERROR "In-source builds are not allowed. CMake would overwrite "
69 "the makefiles distributed with LLVM. Please create a directory and run cmake "
70 "from there, passing the path to this source directory as the last argument. "
71 "This process created the file `CMakeCache.txt' and the directory "
72 "`CMakeFiles'. Please delete them.")
73 endif()
75 if( NOT CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR )
76   file(GLOB_RECURSE
77     tablegenned_files_on_include_dir
78     "${CLANG_SOURCE_DIR}/include/clang/*.inc")
79   if( tablegenned_files_on_include_dir )
80     message(FATAL_ERROR "Apparently there is a previous in-source build, "
81 "probably as the result of running `configure' and `make' on "
82 "${CLANG_SOURCE_DIR}. This may cause problems. The suspicious files are:\n"
83 "${tablegenned_files_on_include_dir}\nPlease clean the source directory.")
84   endif()
85 endif()
87 # Compute the Clang version from the LLVM version.
88 string(REGEX MATCH "[0-9]+\\.[0-9]+(\\.[0-9]+)?" CLANG_VERSION
89   ${PACKAGE_VERSION})
90 message(STATUS "Clang version: ${CLANG_VERSION}")
92 string(REGEX REPLACE "([0-9]+)\\.[0-9]+(\\.[0-9]+)?" "\\1" CLANG_VERSION_MAJOR
93   ${CLANG_VERSION})
94 string(REGEX REPLACE "[0-9]+\\.([0-9]+)(\\.[0-9]+)?" "\\1" CLANG_VERSION_MINOR
95   ${CLANG_VERSION})
96 if (${CLANG_VERSION} MATCHES "[0-9]+\\.[0-9]+\\.[0-9]+")
97   set(CLANG_HAS_VERSION_PATCHLEVEL 1)
98   string(REGEX REPLACE "[0-9]+\\.[0-9]+\\.([0-9]+)" "\\1" CLANG_VERSION_PATCHLEVEL
99     ${CLANG_VERSION})
100 else()
101   set(CLANG_HAS_VERSION_PATCHLEVEL 0)
102 endif()
104 # Configure the Version.inc file.
105 configure_file(
106   ${CMAKE_CURRENT_SOURCE_DIR}/include/clang/Basic/Version.inc.in
107   ${CMAKE_CURRENT_BINARY_DIR}/include/clang/Basic/Version.inc)
109 # Add appropriate flags for GCC
110 if (CMAKE_COMPILER_IS_GNUCXX)
111   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")
112 endif ()
114 if (APPLE)
115   set(CMAKE_MODULE_LINKER_FLAGS "-Wl,-flat_namespace -Wl,-undefined -Wl,suppress")
116 endif ()
118 configure_file(
119   ${CLANG_SOURCE_DIR}/include/clang/Config/config.h.cmake
120   ${CLANG_BINARY_DIR}/include/clang/Config/config.h)
122 macro(add_clang_library name)
123   llvm_process_sources(srcs ${ARGN})
124   if(MSVC_IDE OR XCODE)
125     string( REGEX MATCHALL "/[^/]+" split_path ${CMAKE_CURRENT_SOURCE_DIR})
126     list( GET split_path -1 dir)
127     file( GLOB_RECURSE headers
128       ../../../include/clang/StaticAnalyzer${dir}/*.h
129       ../../../include/clang/StaticAnalyzer${dir}/*.td
130       ../../../include/clang/StaticAnalyzer${dir}/*.def
131       ../../include/clang${dir}/*.h
132       ../../include/clang${dir}/*.td
133       ../../include/clang${dir}/*.def)
134     set(srcs ${srcs} ${headers})
135   endif(MSVC_IDE OR XCODE)
136   if (MODULE)
137     set(libkind MODULE)
138   elseif (SHARED_LIBRARY)
139     set(libkind SHARED)
140   else()
141     set(libkind)
142   endif()
143   add_library( ${name} ${libkind} ${srcs} )
144   if( LLVM_COMMON_DEPENDS )
145     add_dependencies( ${name} ${LLVM_COMMON_DEPENDS} )
146   endif( LLVM_COMMON_DEPENDS )
147   if( LLVM_USED_LIBS )
148     foreach(lib ${LLVM_USED_LIBS})
149       target_link_libraries( ${name} ${lib} )
150     endforeach(lib)
151   endif( LLVM_USED_LIBS )
152   if( LLVM_LINK_COMPONENTS )
153     llvm_config(${name} ${LLVM_LINK_COMPONENTS})
154   endif( LLVM_LINK_COMPONENTS )
155   if (LLVM_COMMON_LIBS)
156     target_link_libraries(${name} ${LLVM_COMMON_LIBS})
157   endif()
158   if( NOT MINGW )
159     get_system_libs(llvm_system_libs)
160     if( llvm_system_libs )
161       target_link_libraries(${name} ${llvm_system_libs})
162     endif()
163   endif()
164   add_dependencies(${name} ClangDiagnosticCommon)
165   if(MSVC)
166     get_target_property(cflag ${name} COMPILE_FLAGS)
167     if(NOT cflag)
168       set(cflag "")
169     endif(NOT cflag)
170     set(cflag "${cflag} /Za")
171     set_target_properties(${name} PROPERTIES COMPILE_FLAGS ${cflag})
172   endif(MSVC)
173   install(TARGETS ${name}
174     LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX}
175     ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX})
176 endmacro(add_clang_library)
178 macro(add_clang_executable name)
179   add_llvm_executable( ${name} ${ARGN} )
180 endmacro(add_clang_executable)
182 include_directories(
183   ${CMAKE_CURRENT_SOURCE_DIR}/include
184   ${CMAKE_CURRENT_BINARY_DIR}/include
185   )
187 install(DIRECTORY include/
188   DESTINATION include
189   FILES_MATCHING
190   PATTERN "*.def"
191   PATTERN "*.h"
192   PATTERN "*.td"
193   PATTERN ".svn" EXCLUDE
194   )
196 install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/include/
197   DESTINATION include
198   FILES_MATCHING
199   PATTERN "CMakeFiles" EXCLUDE
200   PATTERN "*.inc"
201   )
203 add_definitions( -D_GNU_SOURCE -DHAVE_CLANG_CONFIG_H )
205 option(CLANG_BUILD_EXAMPLES "Build CLANG example programs." OFF)
206 if(CLANG_BUILD_EXAMPLES)
207   add_subdirectory(examples)
208 endif ()
210 add_subdirectory(include)
211 add_subdirectory(lib)
212 add_subdirectory(tools)
213 add_subdirectory(runtime)
215 # TODO: docs.
216 add_subdirectory(test)