Oscar Fuentes | 67410b3 | 2011-02-03 20:57:53 +0000 | [diff] [blame] | 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) |
| 6 | |
| 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.") |
| 11 | |
| 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() |
| 21 | |
| 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() |
| 25 | |
| 26 | list(APPEND CMAKE_MODULE_PATH "${CLANG_PATH_TO_LLVM_BUILD}/share/llvm/cmake") |
| 27 | |
| 28 | get_filename_component(PATH_TO_LLVM_BUILD ${CLANG_PATH_TO_LLVM_BUILD} |
| 29 | ABSOLUTE) |
| 30 | |
| 31 | include(AddLLVM) |
| 32 | include(TableGen) |
| 33 | include("${CLANG_PATH_TO_LLVM_BUILD}/share/llvm/cmake/LLVM.cmake") |
| 34 | include(HandleLLVMOptions) |
| 35 | |
| 36 | set(PACKAGE_VERSION "${LLVM_PACKAGE_VERSION}") |
| 37 | |
| 38 | set(LLVM_MAIN_INCLUDE_DIR "${LLVM_MAIN_SRC_DIR}/include") |
| 39 | set(LLVM_BINARY_DIR ${CMAKE_BINARY_DIR}) |
| 40 | |
| 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") |
| 46 | |
| 47 | set(LLVM_TABLEGEN_EXE "${PATH_TO_LLVM_BUILD}/bin/tblgen") |
| 48 | |
| 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 ) |
| 52 | |
| 53 | add_definitions( -D__STDC_LIMIT_MACROS ) |
| 54 | add_definitions( -D__STDC_CONSTANT_MACROS ) |
| 55 | endif() |
Douglas Gregor | 34d9ffa | 2009-09-16 21:59:05 +0000 | [diff] [blame] | 56 | |
Oscar Fuentes | 2100fe9 | 2011-02-03 22:48:20 +0000 | [diff] [blame] | 57 | set(CLANG_RESOURCE_DIR "" CACHE STRING |
| 58 | "Relative directory from the Clang binary to its resource files.") |
| 59 | |
| 60 | set(C_INCLUDE_DIRS "" CACHE STRING |
| 61 | "Colon separated list of directories clang will search for headers.") |
| 62 | |
Daniel Dunbar | 91ee77a | 2009-09-17 00:07:10 +0000 | [diff] [blame] | 63 | set(CLANG_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}) |
| 64 | set(CLANG_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}) |
| 65 | |
Chandler Carruth | 63e9c0d | 2010-04-17 20:12:02 +0000 | [diff] [blame] | 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() |
| 73 | |
| 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() |
| 85 | |
Daniel Dunbar | c4b8e92 | 2010-06-25 23:34:47 +0000 | [diff] [blame] | 86 | # Compute the Clang version from the LLVM version. |
Michael J. Spencer | 5a7f349 | 2010-09-10 21:13:16 +0000 | [diff] [blame] | 87 | string(REGEX MATCH "[0-9]+\\.[0-9]+(\\.[0-9]+)?" CLANG_VERSION |
Daniel Dunbar | c4b8e92 | 2010-06-25 23:34:47 +0000 | [diff] [blame] | 88 | ${PACKAGE_VERSION}) |
Douglas Gregor | 34d9ffa | 2009-09-16 21:59:05 +0000 | [diff] [blame] | 89 | message(STATUS "Clang version: ${CLANG_VERSION}") |
Douglas Gregor | 7f7b748 | 2009-08-23 05:28:29 +0000 | [diff] [blame] | 90 | |
Daniel Dunbar | a510767 | 2010-06-25 17:33:46 +0000 | [diff] [blame] | 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() |
| 102 | |
| 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) |
| 107 | |
Douglas Gregor | 9df3faf | 2009-09-18 14:47:57 +0000 | [diff] [blame] | 108 | # Add appropriate flags for GCC |
| 109 | if (CMAKE_COMPILER_IS_GNUCXX) |
Oscar Fuentes | 827c573 | 2010-10-15 00:16:22 +0000 | [diff] [blame] | 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") |
Douglas Gregor | 9df3faf | 2009-09-18 14:47:57 +0000 | [diff] [blame] | 111 | endif () |
| 112 | |
Douglas Gregor | eb5dc49 | 2010-06-08 19:23:49 +0000 | [diff] [blame] | 113 | if (APPLE) |
| 114 | set(CMAKE_MODULE_LINKER_FLAGS "-Wl,-flat_namespace -Wl,-undefined -Wl,suppress") |
| 115 | endif () |
| 116 | |
Oscar Fuentes | 2100fe9 | 2011-02-03 22:48:20 +0000 | [diff] [blame] | 117 | configure_file( |
| 118 | ${CLANG_SOURCE_DIR}/include/clang/Config/config.h.cmake |
| 119 | ${CLANG_BINARY_DIR}/include/clang/Config/config.h) |
| 120 | |
Oscar Fuentes | d2f4e5e | 2008-10-26 00:56:18 +0000 | [diff] [blame] | 121 | macro(add_clang_library name) |
Oscar Fuentes | 9917401 | 2011-01-03 17:00:02 +0000 | [diff] [blame] | 122 | llvm_process_sources(srcs ${ARGN}) |
Ted Kremenek | bf5de3f | 2009-03-25 20:34:07 +0000 | [diff] [blame] | 123 | if(MSVC_IDE OR XCODE) |
Cedric Venet | fe2f808 | 2008-11-02 16:28:53 +0000 | [diff] [blame] | 124 | string( REGEX MATCHALL "/[^/]+" split_path ${CMAKE_CURRENT_SOURCE_DIR}) |
| 125 | list( GET split_path -1 dir) |
Michael J. Spencer | 5a7f349 | 2010-09-10 21:13:16 +0000 | [diff] [blame] | 126 | file( GLOB_RECURSE headers |
Ted Kremenek | b26bd74 | 2011-02-10 01:03:09 +0000 | [diff] [blame^] | 127 | ../../../include/clang/StaticAnalyzer${dir}/*.h |
| 128 | ../../../include/clang/StaticAnalyzer${dir}/*.td |
| 129 | ../../../include/clang/StaticAnalyzer${dir}/*.def |
Douglas Gregor | f5216f2 | 2009-06-17 18:31:02 +0000 | [diff] [blame] | 130 | ../../include/clang${dir}/*.h |
| 131 | ../../include/clang${dir}/*.td |
| 132 | ../../include/clang${dir}/*.def) |
Cedric Venet | fe2f808 | 2008-11-02 16:28:53 +0000 | [diff] [blame] | 133 | set(srcs ${srcs} ${headers}) |
Ted Kremenek | bf5de3f | 2009-03-25 20:34:07 +0000 | [diff] [blame] | 134 | endif(MSVC_IDE OR XCODE) |
Douglas Gregor | eb5dc49 | 2010-06-08 19:23:49 +0000 | [diff] [blame] | 135 | if (MODULE) |
| 136 | set(libkind MODULE) |
| 137 | elseif (SHARED_LIBRARY) |
Douglas Gregor | ac47bc7 | 2009-09-25 06:35:15 +0000 | [diff] [blame] | 138 | set(libkind SHARED) |
| 139 | else() |
| 140 | set(libkind) |
| 141 | endif() |
| 142 | add_library( ${name} ${libkind} ${srcs} ) |
Oscar Fuentes | d2f4e5e | 2008-10-26 00:56:18 +0000 | [diff] [blame] | 143 | if( LLVM_COMMON_DEPENDS ) |
| 144 | add_dependencies( ${name} ${LLVM_COMMON_DEPENDS} ) |
| 145 | endif( LLVM_COMMON_DEPENDS ) |
Douglas Gregor | ac47bc7 | 2009-09-25 06:35:15 +0000 | [diff] [blame] | 146 | if( LLVM_USED_LIBS ) |
| 147 | foreach(lib ${LLVM_USED_LIBS}) |
| 148 | target_link_libraries( ${name} ${lib} ) |
| 149 | endforeach(lib) |
| 150 | endif( LLVM_USED_LIBS ) |
Benjamin Kramer | bd78554 | 2009-10-11 12:22:00 +0000 | [diff] [blame] | 151 | if( LLVM_LINK_COMPONENTS ) |
| 152 | llvm_config(${name} ${LLVM_LINK_COMPONENTS}) |
| 153 | endif( LLVM_LINK_COMPONENTS ) |
Michael J. Spencer | 5a7f349 | 2010-09-10 21:13:16 +0000 | [diff] [blame] | 154 | if (LLVM_COMMON_LIBS) |
| 155 | target_link_libraries(${name} ${LLVM_COMMON_LIBS}) |
| 156 | endif() |
Michael J. Spencer | 28709c1 | 2010-09-11 02:13:48 +0000 | [diff] [blame] | 157 | if( NOT MINGW ) |
| 158 | get_system_libs(llvm_system_libs) |
| 159 | if( llvm_system_libs ) |
| 160 | target_link_libraries(${name} ${llvm_system_libs}) |
| 161 | endif() |
| 162 | endif() |
Douglas Gregor | a393e9e | 2009-03-16 23:06:59 +0000 | [diff] [blame] | 163 | add_dependencies(${name} ClangDiagnosticCommon) |
Cedric Venet | 1c212a0 | 2008-12-13 11:00:04 +0000 | [diff] [blame] | 164 | if(MSVC) |
| 165 | get_target_property(cflag ${name} COMPILE_FLAGS) |
| 166 | if(NOT cflag) |
| 167 | set(cflag "") |
| 168 | endif(NOT cflag) |
| 169 | set(cflag "${cflag} /Za") |
| 170 | set_target_properties(${name} PROPERTIES COMPILE_FLAGS ${cflag}) |
| 171 | endif(MSVC) |
Oscar Fuentes | d2f4e5e | 2008-10-26 00:56:18 +0000 | [diff] [blame] | 172 | install(TARGETS ${name} |
Oscar Fuentes | bfb06ea | 2009-10-27 19:42:21 +0000 | [diff] [blame] | 173 | LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX} |
Oscar Fuentes | 755f3df | 2009-06-12 02:54:12 +0000 | [diff] [blame] | 174 | ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX}) |
Oscar Fuentes | d2f4e5e | 2008-10-26 00:56:18 +0000 | [diff] [blame] | 175 | endmacro(add_clang_library) |
| 176 | |
| 177 | macro(add_clang_executable name) |
Oscar Fuentes | 9917401 | 2011-01-03 17:00:02 +0000 | [diff] [blame] | 178 | add_llvm_executable( ${name} ${ARGN} ) |
Oscar Fuentes | d2f4e5e | 2008-10-26 00:56:18 +0000 | [diff] [blame] | 179 | endmacro(add_clang_executable) |
| 180 | |
| 181 | include_directories( |
| 182 | ${CMAKE_CURRENT_SOURCE_DIR}/include |
Douglas Gregor | a393e9e | 2009-03-16 23:06:59 +0000 | [diff] [blame] | 183 | ${CMAKE_CURRENT_BINARY_DIR}/include |
Oscar Fuentes | d2f4e5e | 2008-10-26 00:56:18 +0000 | [diff] [blame] | 184 | ) |
| 185 | |
Oscar Fuentes | 82cac29 | 2009-10-27 19:59:34 +0000 | [diff] [blame] | 186 | install(DIRECTORY include/ |
| 187 | DESTINATION include |
| 188 | FILES_MATCHING |
| 189 | PATTERN "*.def" |
| 190 | PATTERN "*.h" |
| 191 | PATTERN "*.td" |
Chris Lattner | bc6ec75 | 2008-11-11 18:39:10 +0000 | [diff] [blame] | 192 | PATTERN ".svn" EXCLUDE |
| 193 | ) |
| 194 | |
Kovarththanan Rajaratnam | ec700a6 | 2010-04-01 14:24:41 +0000 | [diff] [blame] | 195 | install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/include/ |
| 196 | DESTINATION include |
| 197 | FILES_MATCHING |
Chris Lattner | d7d5bb1 | 2010-04-25 04:59:35 +0000 | [diff] [blame] | 198 | PATTERN "CMakeFiles" EXCLUDE |
Kovarththanan Rajaratnam | ec700a6 | 2010-04-01 14:24:41 +0000 | [diff] [blame] | 199 | PATTERN "*.inc" |
| 200 | ) |
| 201 | |
Oscar Fuentes | 2100fe9 | 2011-02-03 22:48:20 +0000 | [diff] [blame] | 202 | add_definitions( -D_GNU_SOURCE -DHAVE_CLANG_CONFIG_H ) |
Oscar Fuentes | d2f4e5e | 2008-10-26 00:56:18 +0000 | [diff] [blame] | 203 | |
Daniel Dunbar | 45088e2 | 2009-11-17 09:32:51 +0000 | [diff] [blame] | 204 | option(CLANG_BUILD_EXAMPLES "Build CLANG example programs." OFF) |
| 205 | if(CLANG_BUILD_EXAMPLES) |
| 206 | add_subdirectory(examples) |
| 207 | endif () |
| 208 | |
Douglas Gregor | a393e9e | 2009-03-16 23:06:59 +0000 | [diff] [blame] | 209 | add_subdirectory(include) |
Oscar Fuentes | d2f4e5e | 2008-10-26 00:56:18 +0000 | [diff] [blame] | 210 | add_subdirectory(lib) |
Daniel Dunbar | cbcd98b | 2009-03-24 02:52:57 +0000 | [diff] [blame] | 211 | add_subdirectory(tools) |
Michael J. Spencer | 48263ba | 2010-12-16 03:28:42 +0000 | [diff] [blame] | 212 | add_subdirectory(runtime) |
Oscar Fuentes | d2f4e5e | 2008-10-26 00:56:18 +0000 | [diff] [blame] | 213 | |
| 214 | # TODO: docs. |
Douglas Gregor | 291fbde | 2009-09-15 22:30:13 +0000 | [diff] [blame] | 215 | add_subdirectory(test) |