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) |
Oscar Fuentes | 5ec8a4d | 2011-04-10 16:17:31 +0000 | [diff] [blame] | 33 | include("${CLANG_PATH_TO_LLVM_BUILD}/share/llvm/cmake/LLVMConfig.cmake") |
Oscar Fuentes | 67410b3 | 2011-02-03 20:57:53 +0000 | [diff] [blame] | 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 | |
Oscar Fuentes | b641f08 | 2011-02-14 20:14:11 +0000 | [diff] [blame] | 41 | set(CMAKE_INCLUDE_CURRENT_DIR ON) |
Oscar Fuentes | 67410b3 | 2011-02-03 20:57:53 +0000 | [diff] [blame] | 42 | include_directories("${PATH_TO_LLVM_BUILD}/include" "${LLVM_MAIN_INCLUDE_DIR}") |
Oscar Fuentes | 67410b3 | 2011-02-03 20:57:53 +0000 | [diff] [blame] | 43 | link_directories("${PATH_TO_LLVM_BUILD}/lib") |
| 44 | |
| 45 | set(LLVM_TABLEGEN_EXE "${PATH_TO_LLVM_BUILD}/bin/tblgen") |
| 46 | |
| 47 | set( CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin ) |
| 48 | set( CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib ) |
| 49 | set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib ) |
| 50 | |
Jeffrey Yasskin | 718b01d | 2011-02-15 07:54:28 +0000 | [diff] [blame] | 51 | set( CLANG_BUILT_STANDALONE 1 ) |
Oscar Fuentes | 67410b3 | 2011-02-03 20:57:53 +0000 | [diff] [blame] | 52 | endif() |
Douglas Gregor | 34d9ffa | 2009-09-16 21:59:05 +0000 | [diff] [blame] | 53 | |
Oscar Fuentes | 2100fe9 | 2011-02-03 22:48:20 +0000 | [diff] [blame] | 54 | set(CLANG_RESOURCE_DIR "" CACHE STRING |
| 55 | "Relative directory from the Clang binary to its resource files.") |
| 56 | |
| 57 | set(C_INCLUDE_DIRS "" CACHE STRING |
| 58 | "Colon separated list of directories clang will search for headers.") |
| 59 | |
Oscar Fuentes | f752d5b | 2011-05-20 15:57:59 +0000 | [diff] [blame] | 60 | set(CLANG_VENDOR "" CACHE STRING |
| 61 | "Vendor-specific text for showing with version information.") |
| 62 | |
| 63 | if( CLANG_VENDOR ) |
| 64 | add_definitions( -DCLANG_VENDOR="${CLANG_VENDOR} " ) |
| 65 | endif() |
| 66 | |
Daniel Dunbar | 91ee77a | 2009-09-17 00:07:10 +0000 | [diff] [blame] | 67 | set(CLANG_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}) |
| 68 | set(CLANG_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}) |
| 69 | |
Chandler Carruth | 63e9c0d | 2010-04-17 20:12:02 +0000 | [diff] [blame] | 70 | if( CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR AND NOT MSVC_IDE ) |
| 71 | message(FATAL_ERROR "In-source builds are not allowed. CMake would overwrite " |
| 72 | "the makefiles distributed with LLVM. Please create a directory and run cmake " |
| 73 | "from there, passing the path to this source directory as the last argument. " |
| 74 | "This process created the file `CMakeCache.txt' and the directory " |
| 75 | "`CMakeFiles'. Please delete them.") |
| 76 | endif() |
| 77 | |
| 78 | if( NOT CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR ) |
| 79 | file(GLOB_RECURSE |
| 80 | tablegenned_files_on_include_dir |
| 81 | "${CLANG_SOURCE_DIR}/include/clang/*.inc") |
| 82 | if( tablegenned_files_on_include_dir ) |
| 83 | message(FATAL_ERROR "Apparently there is a previous in-source build, " |
| 84 | "probably as the result of running `configure' and `make' on " |
| 85 | "${CLANG_SOURCE_DIR}. This may cause problems. The suspicious files are:\n" |
| 86 | "${tablegenned_files_on_include_dir}\nPlease clean the source directory.") |
| 87 | endif() |
| 88 | endif() |
| 89 | |
Daniel Dunbar | c4b8e92 | 2010-06-25 23:34:47 +0000 | [diff] [blame] | 90 | # Compute the Clang version from the LLVM version. |
Michael J. Spencer | 5a7f349 | 2010-09-10 21:13:16 +0000 | [diff] [blame] | 91 | string(REGEX MATCH "[0-9]+\\.[0-9]+(\\.[0-9]+)?" CLANG_VERSION |
Daniel Dunbar | c4b8e92 | 2010-06-25 23:34:47 +0000 | [diff] [blame] | 92 | ${PACKAGE_VERSION}) |
Douglas Gregor | 34d9ffa | 2009-09-16 21:59:05 +0000 | [diff] [blame] | 93 | message(STATUS "Clang version: ${CLANG_VERSION}") |
Douglas Gregor | 7f7b748 | 2009-08-23 05:28:29 +0000 | [diff] [blame] | 94 | |
Daniel Dunbar | a510767 | 2010-06-25 17:33:46 +0000 | [diff] [blame] | 95 | string(REGEX REPLACE "([0-9]+)\\.[0-9]+(\\.[0-9]+)?" "\\1" CLANG_VERSION_MAJOR |
| 96 | ${CLANG_VERSION}) |
| 97 | string(REGEX REPLACE "[0-9]+\\.([0-9]+)(\\.[0-9]+)?" "\\1" CLANG_VERSION_MINOR |
| 98 | ${CLANG_VERSION}) |
| 99 | if (${CLANG_VERSION} MATCHES "[0-9]+\\.[0-9]+\\.[0-9]+") |
| 100 | set(CLANG_HAS_VERSION_PATCHLEVEL 1) |
| 101 | string(REGEX REPLACE "[0-9]+\\.[0-9]+\\.([0-9]+)" "\\1" CLANG_VERSION_PATCHLEVEL |
| 102 | ${CLANG_VERSION}) |
| 103 | else() |
| 104 | set(CLANG_HAS_VERSION_PATCHLEVEL 0) |
| 105 | endif() |
| 106 | |
| 107 | # Configure the Version.inc file. |
| 108 | configure_file( |
| 109 | ${CMAKE_CURRENT_SOURCE_DIR}/include/clang/Basic/Version.inc.in |
| 110 | ${CMAKE_CURRENT_BINARY_DIR}/include/clang/Basic/Version.inc) |
| 111 | |
Douglas Gregor | 9df3faf | 2009-09-18 14:47:57 +0000 | [diff] [blame] | 112 | # Add appropriate flags for GCC |
Oscar Fuentes | c5cd252 | 2011-05-11 13:53:30 +0000 | [diff] [blame] | 113 | if (LLVM_COMPILER_IS_GCC_COMPATIBLE) |
Oscar Fuentes | 827c573 | 2010-10-15 00:16:22 +0000 | [diff] [blame] | 114 | 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] | 115 | endif () |
| 116 | |
Douglas Gregor | eb5dc49 | 2010-06-08 19:23:49 +0000 | [diff] [blame] | 117 | if (APPLE) |
| 118 | set(CMAKE_MODULE_LINKER_FLAGS "-Wl,-flat_namespace -Wl,-undefined -Wl,suppress") |
| 119 | endif () |
| 120 | |
Oscar Fuentes | 2100fe9 | 2011-02-03 22:48:20 +0000 | [diff] [blame] | 121 | configure_file( |
| 122 | ${CLANG_SOURCE_DIR}/include/clang/Config/config.h.cmake |
| 123 | ${CLANG_BINARY_DIR}/include/clang/Config/config.h) |
| 124 | |
Oscar Fuentes | c8da1ec | 2011-02-20 22:06:32 +0000 | [diff] [blame] | 125 | include(LLVMParseArguments) |
| 126 | |
| 127 | function(clang_tablegen) |
| 128 | # Syntax: |
| 129 | # clang_tablegen output-file [tablegen-arg ...] SOURCE source-file |
| 130 | # [[TARGET cmake-target-name] [DEPENDS extra-dependency ...]] |
| 131 | # |
| 132 | # Generates a custom command for invoking tblgen as |
| 133 | # |
| 134 | # tblgen source-file -o=output-file tablegen-arg ... |
| 135 | # |
| 136 | # and, if cmake-target-name is provided, creates a custom target for |
| 137 | # executing the custom command depending on output-file. It is |
| 138 | # possible to list more files to depend after DEPENDS. |
| 139 | |
| 140 | parse_arguments( CTG "SOURCE;TARGET;DEPENDS" "" ${ARGN} ) |
| 141 | |
| 142 | if( NOT CTG_SOURCE ) |
| 143 | message(FATAL_ERROR "SOURCE source-file required by clang_tablegen") |
| 144 | endif() |
| 145 | |
| 146 | set( LLVM_TARGET_DEFINITIONS ${CTG_SOURCE} ) |
| 147 | tablegen( ${CTG_DEFAULT_ARGS} ) |
| 148 | |
| 149 | list( GET CTG_DEFAULT_ARGS 0 output_file ) |
| 150 | if( CTG_TARGET ) |
| 151 | add_custom_target( ${CTG_TARGET} DEPENDS ${output_file} ${CTG_DEPENDS} ) |
Oscar Fuentes | a3f787c | 2011-02-20 22:06:44 +0000 | [diff] [blame] | 152 | set_target_properties( ${CTG_TARGET} PROPERTIES FOLDER "Clang tablegenning") |
Oscar Fuentes | c8da1ec | 2011-02-20 22:06:32 +0000 | [diff] [blame] | 153 | endif() |
| 154 | endfunction(clang_tablegen) |
| 155 | |
Oscar Fuentes | d2f4e5e | 2008-10-26 00:56:18 +0000 | [diff] [blame] | 156 | macro(add_clang_library name) |
Oscar Fuentes | 9917401 | 2011-01-03 17:00:02 +0000 | [diff] [blame] | 157 | llvm_process_sources(srcs ${ARGN}) |
Ted Kremenek | bf5de3f | 2009-03-25 20:34:07 +0000 | [diff] [blame] | 158 | if(MSVC_IDE OR XCODE) |
Cedric Venet | fe2f808 | 2008-11-02 16:28:53 +0000 | [diff] [blame] | 159 | string( REGEX MATCHALL "/[^/]+" split_path ${CMAKE_CURRENT_SOURCE_DIR}) |
| 160 | list( GET split_path -1 dir) |
Michael J. Spencer | 5a7f349 | 2010-09-10 21:13:16 +0000 | [diff] [blame] | 161 | file( GLOB_RECURSE headers |
Ted Kremenek | b26bd74 | 2011-02-10 01:03:09 +0000 | [diff] [blame] | 162 | ../../../include/clang/StaticAnalyzer${dir}/*.h |
| 163 | ../../../include/clang/StaticAnalyzer${dir}/*.td |
| 164 | ../../../include/clang/StaticAnalyzer${dir}/*.def |
Douglas Gregor | f5216f2 | 2009-06-17 18:31:02 +0000 | [diff] [blame] | 165 | ../../include/clang${dir}/*.h |
| 166 | ../../include/clang${dir}/*.td |
| 167 | ../../include/clang${dir}/*.def) |
Cedric Venet | fe2f808 | 2008-11-02 16:28:53 +0000 | [diff] [blame] | 168 | set(srcs ${srcs} ${headers}) |
Ted Kremenek | bf5de3f | 2009-03-25 20:34:07 +0000 | [diff] [blame] | 169 | endif(MSVC_IDE OR XCODE) |
Douglas Gregor | eb5dc49 | 2010-06-08 19:23:49 +0000 | [diff] [blame] | 170 | if (MODULE) |
| 171 | set(libkind MODULE) |
| 172 | elseif (SHARED_LIBRARY) |
Douglas Gregor | ac47bc7 | 2009-09-25 06:35:15 +0000 | [diff] [blame] | 173 | set(libkind SHARED) |
| 174 | else() |
| 175 | set(libkind) |
| 176 | endif() |
| 177 | add_library( ${name} ${libkind} ${srcs} ) |
Oscar Fuentes | d2f4e5e | 2008-10-26 00:56:18 +0000 | [diff] [blame] | 178 | if( LLVM_COMMON_DEPENDS ) |
| 179 | add_dependencies( ${name} ${LLVM_COMMON_DEPENDS} ) |
| 180 | endif( LLVM_COMMON_DEPENDS ) |
Oscar Fuentes | fb767c8 | 2011-03-29 20:51:00 +0000 | [diff] [blame] | 181 | |
| 182 | target_link_libraries( ${name} ${LLVM_USED_LIBS} ) |
| 183 | llvm_config( ${name} ${LLVM_LINK_COMPONENTS} ) |
| 184 | target_link_libraries( ${name} ${LLVM_COMMON_LIBS} ) |
| 185 | link_system_libs( ${name} ) |
| 186 | |
Douglas Gregor | a393e9e | 2009-03-16 23:06:59 +0000 | [diff] [blame] | 187 | add_dependencies(${name} ClangDiagnosticCommon) |
Cedric Venet | 1c212a0 | 2008-12-13 11:00:04 +0000 | [diff] [blame] | 188 | if(MSVC) |
| 189 | get_target_property(cflag ${name} COMPILE_FLAGS) |
| 190 | if(NOT cflag) |
| 191 | set(cflag "") |
| 192 | endif(NOT cflag) |
| 193 | set(cflag "${cflag} /Za") |
| 194 | set_target_properties(${name} PROPERTIES COMPILE_FLAGS ${cflag}) |
| 195 | endif(MSVC) |
Oscar Fuentes | d2f4e5e | 2008-10-26 00:56:18 +0000 | [diff] [blame] | 196 | install(TARGETS ${name} |
Oscar Fuentes | bfb06ea | 2009-10-27 19:42:21 +0000 | [diff] [blame] | 197 | LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX} |
Oscar Fuentes | 755f3df | 2009-06-12 02:54:12 +0000 | [diff] [blame] | 198 | ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX}) |
Oscar Fuentes | a3f787c | 2011-02-20 22:06:44 +0000 | [diff] [blame] | 199 | set_target_properties(${name} PROPERTIES FOLDER "Clang libraries") |
Oscar Fuentes | d2f4e5e | 2008-10-26 00:56:18 +0000 | [diff] [blame] | 200 | endmacro(add_clang_library) |
| 201 | |
| 202 | macro(add_clang_executable name) |
Oscar Fuentes | 9917401 | 2011-01-03 17:00:02 +0000 | [diff] [blame] | 203 | add_llvm_executable( ${name} ${ARGN} ) |
Oscar Fuentes | a3f787c | 2011-02-20 22:06:44 +0000 | [diff] [blame] | 204 | set_target_properties(${name} PROPERTIES FOLDER "Clang executables") |
Oscar Fuentes | d2f4e5e | 2008-10-26 00:56:18 +0000 | [diff] [blame] | 205 | endmacro(add_clang_executable) |
| 206 | |
Oscar Fuentes | 617508f | 2011-03-17 19:03:04 +0000 | [diff] [blame] | 207 | include_directories(BEFORE |
Douglas Gregor | a393e9e | 2009-03-16 23:06:59 +0000 | [diff] [blame] | 208 | ${CMAKE_CURRENT_BINARY_DIR}/include |
Oscar Fuentes | 617508f | 2011-03-17 19:03:04 +0000 | [diff] [blame] | 209 | ${CMAKE_CURRENT_SOURCE_DIR}/include |
Oscar Fuentes | d2f4e5e | 2008-10-26 00:56:18 +0000 | [diff] [blame] | 210 | ) |
| 211 | |
Oscar Fuentes | 82cac29 | 2009-10-27 19:59:34 +0000 | [diff] [blame] | 212 | install(DIRECTORY include/ |
| 213 | DESTINATION include |
| 214 | FILES_MATCHING |
| 215 | PATTERN "*.def" |
| 216 | PATTERN "*.h" |
Chris Lattner | bc6ec75 | 2008-11-11 18:39:10 +0000 | [diff] [blame] | 217 | PATTERN ".svn" EXCLUDE |
| 218 | ) |
| 219 | |
Kovarththanan Rajaratnam | ec700a6 | 2010-04-01 14:24:41 +0000 | [diff] [blame] | 220 | install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/include/ |
| 221 | DESTINATION include |
| 222 | FILES_MATCHING |
Chris Lattner | d7d5bb1 | 2010-04-25 04:59:35 +0000 | [diff] [blame] | 223 | PATTERN "CMakeFiles" EXCLUDE |
Kovarththanan Rajaratnam | ec700a6 | 2010-04-01 14:24:41 +0000 | [diff] [blame] | 224 | PATTERN "*.inc" |
| 225 | ) |
| 226 | |
Oscar Fuentes | 2100fe9 | 2011-02-03 22:48:20 +0000 | [diff] [blame] | 227 | add_definitions( -D_GNU_SOURCE -DHAVE_CLANG_CONFIG_H ) |
Oscar Fuentes | d2f4e5e | 2008-10-26 00:56:18 +0000 | [diff] [blame] | 228 | |
Douglas Gregor | 8435bf9 | 2011-02-25 19:24:02 +0000 | [diff] [blame] | 229 | # Clang version information |
| 230 | set(CLANG_EXECUTABLE_VERSION |
| 231 | "${CLANG_VERSION_MAJOR}.${CLANG_VERSION_MINOR}" CACHE STRING |
| 232 | "Version number that will be placed into the clang executable, in the form XX.YY") |
| 233 | set(LIBCLANG_LIBRARY_VERSION |
| 234 | "${CLANG_VERSION_MAJOR}.${CLANG_VERSION_MINOR}" CACHE STRING |
| 235 | "Version number that will be placed into the libclang library , in the form XX.YY") |
| 236 | mark_as_advanced(CLANG_EXECUTABLE_VERSION LIBCLANG_LIBRARY_VERSION) |
| 237 | |
| 238 | |
Daniel Dunbar | 45088e2 | 2009-11-17 09:32:51 +0000 | [diff] [blame] | 239 | option(CLANG_BUILD_EXAMPLES "Build CLANG example programs." OFF) |
| 240 | if(CLANG_BUILD_EXAMPLES) |
| 241 | add_subdirectory(examples) |
| 242 | endif () |
| 243 | |
Douglas Gregor | a393e9e | 2009-03-16 23:06:59 +0000 | [diff] [blame] | 244 | add_subdirectory(include) |
Oscar Fuentes | d2f4e5e | 2008-10-26 00:56:18 +0000 | [diff] [blame] | 245 | add_subdirectory(lib) |
Daniel Dunbar | cbcd98b | 2009-03-24 02:52:57 +0000 | [diff] [blame] | 246 | add_subdirectory(tools) |
Michael J. Spencer | 48263ba | 2010-12-16 03:28:42 +0000 | [diff] [blame] | 247 | add_subdirectory(runtime) |
Oscar Fuentes | d2f4e5e | 2008-10-26 00:56:18 +0000 | [diff] [blame] | 248 | |
| 249 | # TODO: docs. |
Douglas Gregor | 670d6ed | 2011-02-25 00:32:30 +0000 | [diff] [blame] | 250 | add_subdirectory(test) |
Jeffrey Yasskin | 718b01d | 2011-02-15 07:54:28 +0000 | [diff] [blame] | 251 | |
Douglas Gregor | 670d6ed | 2011-02-25 00:32:30 +0000 | [diff] [blame] | 252 | if( LLVM_INCLUDE_TESTS ) |
Douglas Gregor | fd68157 | 2011-02-25 00:12:04 +0000 | [diff] [blame] | 253 | if( NOT CLANG_BUILT_STANDALONE ) |
| 254 | add_subdirectory(unittests) |
| 255 | endif() |
Jeffrey Yasskin | 718b01d | 2011-02-15 07:54:28 +0000 | [diff] [blame] | 256 | endif() |
NAKAMURA Takumi | 792f975 | 2011-02-16 03:07:15 +0000 | [diff] [blame] | 257 | |
| 258 | # Workaround for MSVS10 to avoid the Dialog Hell |
| 259 | # FIXME: This could be removed with future version of CMake. |
| 260 | if( CLANG_BUILT_STANDALONE AND MSVC_VERSION EQUAL 1600 ) |
| 261 | set(CLANG_SLN_FILENAME "${CMAKE_CURRENT_BINARY_DIR}/Clang.sln") |
| 262 | if( EXISTS "${CLANG_SLN_FILENAME}" ) |
| 263 | file(APPEND "${CLANG_SLN_FILENAME}" "\n# This should be regenerated!\n") |
| 264 | endif() |
| 265 | endif() |