dgregor accidentally killed this assert, but on investigation, it can fire
[clang.git] / CMakeLists.txt
blobf7989bc1feb6365403a7535f9f371838c620bdd0
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   include_directories("${PATH_TO_LLVM_BUILD}/include" "${LLVM_MAIN_INCLUDE_DIR}")
42   if( NOT PATH_TO_LLVM_BUILD STREQUAL LLVM_MAIN_SRC_DIR )
43     include_directories("${LLVM_MAIN_INCLUDE_DIR}")
44   endif()
45   link_directories("${PATH_TO_LLVM_BUILD}/lib")
47   set(LLVM_TABLEGEN_EXE "${PATH_TO_LLVM_BUILD}/bin/tblgen")
49   set( CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin )
50   set( CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib )
51   set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib )
53   add_definitions( -D__STDC_LIMIT_MACROS )
54   add_definitions( -D__STDC_CONSTANT_MACROS )
55 endif()
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.")
72 endif()
74 if( NOT CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR )
75   file(GLOB_RECURSE
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.")
83   endif()
84 endif()
86 # Compute the Clang version from the LLVM version.
87 string(REGEX MATCH "[0-9]+\\.[0-9]+(\\.[0-9]+)?" CLANG_VERSION
88   ${PACKAGE_VERSION})
89 message(STATUS "Clang version: ${CLANG_VERSION}")
91 string(REGEX REPLACE "([0-9]+)\\.[0-9]+(\\.[0-9]+)?" "\\1" CLANG_VERSION_MAJOR
92   ${CLANG_VERSION})
93 string(REGEX REPLACE "[0-9]+\\.([0-9]+)(\\.[0-9]+)?" "\\1" CLANG_VERSION_MINOR
94   ${CLANG_VERSION})
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
98     ${CLANG_VERSION})
99 else()
100   set(CLANG_HAS_VERSION_PATCHLEVEL 0)
101 endif()
103 # Configure the Version.inc file.
104 configure_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")
111 endif ()
113 if (APPLE)
114   set(CMAKE_MODULE_LINKER_FLAGS "-Wl,-flat_namespace -Wl,-undefined -Wl,suppress")
115 endif ()
117 configure_file(
118   ${CLANG_SOURCE_DIR}/include/clang/Config/config.h.cmake
119   ${CLANG_BINARY_DIR}/include/clang/Config/config.h)
121 macro(add_clang_library name)
122   llvm_process_sources(srcs ${ARGN})
123   if(MSVC_IDE OR XCODE)
124     string( REGEX MATCHALL "/[^/]+" split_path ${CMAKE_CURRENT_SOURCE_DIR})
125     list( GET split_path -1 dir)
126     file( GLOB_RECURSE headers
127       ../../include/clang${dir}/*.h
128       ../../include/clang${dir}/*.td
129       ../../include/clang${dir}/*.def)
130     set(srcs ${srcs} ${headers})
131   endif(MSVC_IDE OR XCODE)
132   if (MODULE)
133     set(libkind MODULE)
134   elseif (SHARED_LIBRARY)
135     set(libkind SHARED)
136   else()
137     set(libkind)
138   endif()
139   add_library( ${name} ${libkind} ${srcs} )
140   if( LLVM_COMMON_DEPENDS )
141     add_dependencies( ${name} ${LLVM_COMMON_DEPENDS} )
142   endif( LLVM_COMMON_DEPENDS )
143   if( LLVM_USED_LIBS )
144     foreach(lib ${LLVM_USED_LIBS})
145       target_link_libraries( ${name} ${lib} )
146     endforeach(lib)
147   endif( LLVM_USED_LIBS )
148   if( LLVM_LINK_COMPONENTS )
149     llvm_config(${name} ${LLVM_LINK_COMPONENTS})
150   endif( LLVM_LINK_COMPONENTS )
151   if (LLVM_COMMON_LIBS)
152     target_link_libraries(${name} ${LLVM_COMMON_LIBS})
153   endif()
154   if( NOT MINGW )
155     get_system_libs(llvm_system_libs)
156     if( llvm_system_libs )
157       target_link_libraries(${name} ${llvm_system_libs})
158     endif()
159   endif()
160   add_dependencies(${name} ClangDiagnosticCommon)
161   if(MSVC)
162     get_target_property(cflag ${name} COMPILE_FLAGS)
163     if(NOT cflag)
164       set(cflag "")
165     endif(NOT cflag)
166     set(cflag "${cflag} /Za")
167     set_target_properties(${name} PROPERTIES COMPILE_FLAGS ${cflag})
168   endif(MSVC)
169   install(TARGETS ${name}
170     LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX}
171     ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX})
172 endmacro(add_clang_library)
174 macro(add_clang_executable name)
175   add_llvm_executable( ${name} ${ARGN} )
176 endmacro(add_clang_executable)
178 include_directories(
179   ${CMAKE_CURRENT_SOURCE_DIR}/include
180   ${CMAKE_CURRENT_BINARY_DIR}/include
181   )
183 install(DIRECTORY include/
184   DESTINATION include
185   FILES_MATCHING
186   PATTERN "*.def"
187   PATTERN "*.h"
188   PATTERN "*.td"
189   PATTERN ".svn" EXCLUDE
190   )
192 install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/include/
193   DESTINATION include
194   FILES_MATCHING
195   PATTERN "CMakeFiles" EXCLUDE
196   PATTERN "*.inc"
197   )
199 add_definitions( -D_GNU_SOURCE -DHAVE_CLANG_CONFIG_H )
201 option(CLANG_BUILD_EXAMPLES "Build CLANG example programs." OFF)
202 if(CLANG_BUILD_EXAMPLES)
203   add_subdirectory(examples)
204 endif ()
206 add_subdirectory(include)
207 add_subdirectory(lib)
208 add_subdirectory(tools)
209 add_subdirectory(runtime)
211 # TODO: docs.
212 add_subdirectory(test)