1cmake_minimum_required(VERSION 2.8.8) 2 3# FIXME: It may be removed when we use 2.8.12. 4if(CMAKE_VERSION VERSION_LESS 2.8.12) 5 # Invalidate a couple of keywords. 6 set(cmake_2_8_12_INTERFACE) 7 set(cmake_2_8_12_PRIVATE) 8else() 9 # Use ${cmake_2_8_12_KEYWORD} intead of KEYWORD in target_link_libraries(). 10 set(cmake_2_8_12_INTERFACE INTERFACE) 11 set(cmake_2_8_12_PRIVATE PRIVATE) 12 if(POLICY CMP0022) 13 cmake_policy(SET CMP0022 NEW) # automatic when 2.8.12 is required 14 endif() 15endif() 16 17# If we are not building as a part of LLVM, build Clang as an 18# standalone project, using LLVM as an external library: 19if( CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR ) 20 project(Clang) 21 22 # Rely on llvm-config. 23 set(CONFIG_OUTPUT) 24 find_program(LLVM_CONFIG "llvm-config") 25 if(LLVM_CONFIG) 26 message(STATUS "Found LLVM_CONFIG as ${LLVM_CONFIG}") 27 set(CONFIG_COMMAND ${LLVM_CONFIG} 28 "--assertion-mode" 29 "--bindir" 30 "--libdir" 31 "--includedir" 32 "--prefix" 33 "--src-root") 34 execute_process( 35 COMMAND ${CONFIG_COMMAND} 36 RESULT_VARIABLE HAD_ERROR 37 OUTPUT_VARIABLE CONFIG_OUTPUT 38 ) 39 if(NOT HAD_ERROR) 40 string(REGEX REPLACE 41 "[ \t]*[\r\n]+[ \t]*" ";" 42 CONFIG_OUTPUT ${CONFIG_OUTPUT}) 43 else() 44 string(REPLACE ";" " " CONFIG_COMMAND_STR "${CONFIG_COMMAND}") 45 message(STATUS "${CONFIG_COMMAND_STR}") 46 message(FATAL_ERROR "llvm-config failed with status ${HAD_ERROR}") 47 endif() 48 else() 49 message(FATAL_ERROR "llvm-config not found -- ${LLVM_CONFIG}") 50 endif() 51 52 list(GET CONFIG_OUTPUT 0 ENABLE_ASSERTIONS) 53 list(GET CONFIG_OUTPUT 1 TOOLS_BINARY_DIR) 54 list(GET CONFIG_OUTPUT 2 LIBRARY_DIR) 55 list(GET CONFIG_OUTPUT 3 INCLUDE_DIR) 56 list(GET CONFIG_OUTPUT 4 LLVM_OBJ_ROOT) 57 list(GET CONFIG_OUTPUT 5 MAIN_SRC_DIR) 58 59 if(NOT MSVC_IDE) 60 set(LLVM_ENABLE_ASSERTIONS ${ENABLE_ASSERTIONS} 61 CACHE BOOL "Enable assertions") 62 # Assertions should follow llvm-config's. 63 mark_as_advanced(LLVM_ENABLE_ASSERTIONS) 64 endif() 65 66 set(LLVM_TOOLS_BINARY_DIR ${TOOLS_BINARY_DIR} CACHE PATH "Path to llvm/bin") 67 set(LLVM_LIBRARY_DIR ${LIBRARY_DIR} CACHE PATH "Path to llvm/lib") 68 set(LLVM_MAIN_INCLUDE_DIR ${INCLUDE_DIR} CACHE PATH "Path to llvm/include") 69 set(LLVM_BINARY_DIR ${LLVM_OBJ_ROOT} CACHE PATH "Path to LLVM build tree") 70 set(LLVM_MAIN_SRC_DIR ${MAIN_SRC_DIR} CACHE PATH "Path to LLVM source tree") 71 72 find_program(LLVM_TABLEGEN_EXE "llvm-tblgen" ${LLVM_TOOLS_BINARY_DIR} 73 NO_DEFAULT_PATH) 74 75 set(LLVM_CMAKE_PATH "${LLVM_BINARY_DIR}/share/llvm/cmake") 76 set(LLVMCONFIG_FILE "${LLVM_CMAKE_PATH}/LLVMConfig.cmake") 77 if(EXISTS ${LLVMCONFIG_FILE}) 78 list(APPEND CMAKE_MODULE_PATH "${LLVM_CMAKE_PATH}") 79 include(${LLVMCONFIG_FILE}) 80 else() 81 message(FATAL_ERROR "Not found: ${LLVMCONFIG_FILE}") 82 endif() 83 84 # They are used as destination of target generators. 85 set(LLVM_RUNTIME_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/bin) 86 set(LLVM_LIBRARY_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/lib) 87 88 option(LLVM_INSTALL_TOOLCHAIN_ONLY 89 "Only include toolchain files in the 'install' target." OFF) 90 91 option(LLVM_FORCE_USE_OLD_HOST_TOOLCHAIN 92 "Set to ON to force using an old, unsupported host toolchain." OFF) 93 94 include(AddLLVM) 95 include(TableGen) 96 include(HandleLLVMOptions) 97 98 set(PACKAGE_VERSION "${LLVM_PACKAGE_VERSION}") 99 100 if (NOT DEFINED LLVM_INCLUDE_TESTS) 101 set(LLVM_INCLUDE_TESTS ON) 102 endif() 103 104 include_directories("${LLVM_BINARY_DIR}/include" "${LLVM_MAIN_INCLUDE_DIR}") 105 link_directories("${LLVM_LIBRARY_DIR}") 106 107 set( CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin ) 108 set( CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib ) 109 set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib ) 110 111 if(LLVM_INCLUDE_TESTS) 112 # Check prebuilt llvm/utils. 113 if(EXISTS ${LLVM_TOOLS_BINARY_DIR}/FileCheck${CMAKE_EXECUTABLE_SUFFIX} 114 AND EXISTS ${LLVM_TOOLS_BINARY_DIR}/count${CMAKE_EXECUTABLE_SUFFIX} 115 AND EXISTS ${LLVM_TOOLS_BINARY_DIR}/not${CMAKE_EXECUTABLE_SUFFIX}) 116 set(LLVM_UTILS_PROVIDED ON) 117 endif() 118 119 if(EXISTS ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py) 120 set(LLVM_LIT ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py) 121 if(NOT LLVM_UTILS_PROVIDED) 122 add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/FileCheck utils/FileCheck) 123 add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/count utils/count) 124 add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/not utils/not) 125 set(LLVM_UTILS_PROVIDED ON) 126 set(CLANG_TEST_DEPS FileCheck count not) 127 endif() 128 set(UNITTEST_DIR ${LLVM_MAIN_SRC_DIR}/utils/unittest) 129 if(EXISTS ${UNITTEST_DIR}/googletest/include/gtest/gtest.h 130 AND NOT EXISTS ${LLVM_LIBRARY_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}gtest${CMAKE_STATIC_LIBRARY_SUFFIX} 131 AND EXISTS ${UNITTEST_DIR}/CMakeLists.txt) 132 add_subdirectory(${UNITTEST_DIR} utils/unittest) 133 endif() 134 else() 135 # Seek installed Lit. 136 find_program(LLVM_LIT "lit.py" ${LLVM_MAIN_SRC_DIR}/utils/lit 137 DOC "Path to lit.py") 138 endif() 139 140 if(LLVM_LIT) 141 # Define the default arguments to use with 'lit', and an option for the user 142 # to override. 143 set(LIT_ARGS_DEFAULT "-sv") 144 if (MSVC OR XCODE) 145 set(LIT_ARGS_DEFAULT "${LIT_ARGS_DEFAULT} --no-progress-bar") 146 endif() 147 set(LLVM_LIT_ARGS "${LIT_ARGS_DEFAULT}" CACHE STRING "Default options for lit") 148 149 # On Win32 hosts, provide an option to specify the path to the GnuWin32 tools. 150 if( WIN32 AND NOT CYGWIN ) 151 set(LLVM_LIT_TOOLS_DIR "" CACHE PATH "Path to GnuWin32 tools") 152 endif() 153 else() 154 set(LLVM_INCLUDE_TESTS OFF) 155 endif() 156 endif() 157 158 set( CLANG_BUILT_STANDALONE 1 ) 159 set(BACKEND_PACKAGE_STRING "LLVM ${LLVM_PACKAGE_VERSION}") 160else() 161 set(BACKEND_PACKAGE_STRING "${PACKAGE_STRING}") 162endif() 163 164find_package(LibXml2) 165if (LIBXML2_FOUND) 166 set(CLANG_HAVE_LIBXML 1) 167endif() 168 169set(CLANG_RESOURCE_DIR "" CACHE STRING 170 "Relative directory from the Clang binary to its resource files.") 171 172set(C_INCLUDE_DIRS "" CACHE STRING 173 "Colon separated list of directories clang will search for headers.") 174 175set(GCC_INSTALL_PREFIX "" CACHE PATH "Directory where gcc is installed." ) 176set(DEFAULT_SYSROOT "" CACHE PATH 177 "Default <path> to all compiler invocations for --sysroot=<path>." ) 178 179set(CLANG_VENDOR "" CACHE STRING 180 "Vendor-specific text for showing with version information.") 181 182if( CLANG_VENDOR ) 183 add_definitions( -DCLANG_VENDOR="${CLANG_VENDOR} " ) 184endif() 185 186set(CLANG_REPOSITORY_STRING "" CACHE STRING 187 "Vendor-specific text for showing the repository the source is taken from.") 188 189if(CLANG_REPOSITORY_STRING) 190 add_definitions(-DCLANG_REPOSITORY_STRING="${CLANG_REPOSITORY_STRING}") 191endif() 192 193set(CLANG_VENDOR_UTI "org.llvm.clang" CACHE STRING 194 "Vendor-specific uti.") 195 196set(CLANG_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}) 197set(CLANG_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}) 198 199if( CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR AND NOT MSVC_IDE ) 200 message(FATAL_ERROR "In-source builds are not allowed. CMake would overwrite " 201"the makefiles distributed with LLVM. Please create a directory and run cmake " 202"from there, passing the path to this source directory as the last argument. " 203"This process created the file `CMakeCache.txt' and the directory " 204"`CMakeFiles'. Please delete them.") 205endif() 206 207if( NOT CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR ) 208 file(GLOB_RECURSE 209 tablegenned_files_on_include_dir 210 "${CLANG_SOURCE_DIR}/include/clang/*.inc") 211 if( tablegenned_files_on_include_dir ) 212 message(FATAL_ERROR "Apparently there is a previous in-source build, " 213"probably as the result of running `configure' and `make' on " 214"${CLANG_SOURCE_DIR}. This may cause problems. The suspicious files are:\n" 215"${tablegenned_files_on_include_dir}\nPlease clean the source directory.") 216 endif() 217endif() 218 219# Compute the Clang version from the LLVM version. 220string(REGEX MATCH "[0-9]+\\.[0-9]+(\\.[0-9]+)?" CLANG_VERSION 221 ${PACKAGE_VERSION}) 222message(STATUS "Clang version: ${CLANG_VERSION}") 223 224string(REGEX REPLACE "([0-9]+)\\.[0-9]+(\\.[0-9]+)?" "\\1" CLANG_VERSION_MAJOR 225 ${CLANG_VERSION}) 226string(REGEX REPLACE "[0-9]+\\.([0-9]+)(\\.[0-9]+)?" "\\1" CLANG_VERSION_MINOR 227 ${CLANG_VERSION}) 228if (${CLANG_VERSION} MATCHES "[0-9]+\\.[0-9]+\\.[0-9]+") 229 set(CLANG_HAS_VERSION_PATCHLEVEL 1) 230 string(REGEX REPLACE "[0-9]+\\.[0-9]+\\.([0-9]+)" "\\1" CLANG_VERSION_PATCHLEVEL 231 ${CLANG_VERSION}) 232else() 233 set(CLANG_HAS_VERSION_PATCHLEVEL 0) 234endif() 235 236# Configure the Version.inc file. 237configure_file( 238 ${CMAKE_CURRENT_SOURCE_DIR}/include/clang/Basic/Version.inc.in 239 ${CMAKE_CURRENT_BINARY_DIR}/include/clang/Basic/Version.inc) 240 241# Add appropriate flags for GCC 242if (LLVM_COMPILER_IS_GCC_COMPATIBLE) 243 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-common -Woverloaded-virtual -Wcast-qual -fno-strict-aliasing") 244 245 # Enable -pedantic for Clang even if it's not enabled for LLVM. 246 if (NOT LLVM_ENABLE_PEDANTIC) 247 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic -Wno-long-long") 248 endif () 249 250 check_cxx_compiler_flag("-Werror -Wnested-anon-types" CXX_SUPPORTS_NO_NESTED_ANON_TYPES_FLAG) 251 if( CXX_SUPPORTS_NO_NESTED_ANON_TYPES_FLAG ) 252 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-nested-anon-types" ) 253 endif() 254endif () 255 256configure_file( 257 ${CLANG_SOURCE_DIR}/include/clang/Config/config.h.cmake 258 ${CLANG_BINARY_DIR}/include/clang/Config/config.h) 259 260include(CMakeParseArguments) 261 262function(clang_tablegen) 263 # Syntax: 264 # clang_tablegen output-file [tablegen-arg ...] SOURCE source-file 265 # [[TARGET cmake-target-name] [DEPENDS extra-dependency ...]] 266 # 267 # Generates a custom command for invoking tblgen as 268 # 269 # tblgen source-file -o=output-file tablegen-arg ... 270 # 271 # and, if cmake-target-name is provided, creates a custom target for 272 # executing the custom command depending on output-file. It is 273 # possible to list more files to depend after DEPENDS. 274 275 cmake_parse_arguments(CTG "" "SOURCE;TARGET" "" ${ARGN}) 276 277 if( NOT CTG_SOURCE ) 278 message(FATAL_ERROR "SOURCE source-file required by clang_tablegen") 279 endif() 280 281 set( LLVM_TARGET_DEFINITIONS ${CTG_SOURCE} ) 282 tablegen(CLANG ${CTG_UNPARSED_ARGUMENTS}) 283 284 if(CTG_TARGET) 285 add_public_tablegen_target(${CTG_TARGET}) 286 set_target_properties( ${CTG_TARGET} PROPERTIES FOLDER "Clang tablegenning") 287 set_property(GLOBAL APPEND PROPERTY CLANG_TABLEGEN_TARGETS ${CTG_TARGET}) 288 endif() 289endfunction(clang_tablegen) 290 291macro(add_clang_library name) 292 cmake_parse_arguments(ARG 293 "" 294 "" 295 "ADDITIONAL_HEADERS" 296 ${ARGN}) 297 set(srcs) 298 if(MSVC_IDE OR XCODE) 299 # Add public headers 300 file(RELATIVE_PATH lib_path 301 ${CLANG_SOURCE_DIR}/lib/ 302 ${CMAKE_CURRENT_SOURCE_DIR} 303 ) 304 if(NOT lib_path MATCHES "^[.][.]") 305 file( GLOB_RECURSE headers 306 ${CLANG_SOURCE_DIR}/include/clang/${lib_path}/*.h 307 ${CLANG_SOURCE_DIR}/include/clang/${lib_path}/*.def 308 ) 309 set_source_files_properties(${headers} PROPERTIES HEADER_FILE_ONLY ON) 310 311 file( GLOB_RECURSE tds 312 ${CLANG_SOURCE_DIR}/include/clang/${lib_path}/*.td 313 ) 314 source_group("TableGen descriptions" FILES ${tds}) 315 set_source_files_properties(${tds}} PROPERTIES HEADER_FILE_ONLY ON) 316 317 if(headers OR tds) 318 set(srcs ${headers} ${tds}) 319 endif() 320 endif() 321 endif(MSVC_IDE OR XCODE) 322 if(srcs OR ARG_ADDITIONAL_HEADERS) 323 set(srcs 324 ADDITIONAL_HEADERS 325 ${srcs} 326 ${ARG_ADDITIONAL_HEADERS} # It may contain unparsed unknown args. 327 ) 328 endif() 329 llvm_add_library(${name} ${ARG_UNPARSED_ARGUMENTS} ${srcs}) 330 331 if(TARGET ${name}) 332 target_link_libraries(${name} ${cmake_2_8_12_INTERFACE} ${LLVM_COMMON_LIBS}) 333 334 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY OR ${name} STREQUAL "libclang") 335 install(TARGETS ${name} 336 LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX} 337 ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX} 338 RUNTIME DESTINATION bin) 339 endif() 340 else() 341 # Add empty "phony" target 342 add_custom_target(${name}) 343 endif() 344 345 set_target_properties(${name} PROPERTIES FOLDER "Clang libraries") 346endmacro(add_clang_library) 347 348macro(add_clang_executable name) 349 add_llvm_executable( ${name} ${ARGN} ) 350 set_target_properties(${name} PROPERTIES FOLDER "Clang executables") 351endmacro(add_clang_executable) 352 353set(CMAKE_INCLUDE_CURRENT_DIR ON) 354 355include_directories(BEFORE 356 ${CMAKE_CURRENT_BINARY_DIR}/include 357 ${CMAKE_CURRENT_SOURCE_DIR}/include 358 ) 359 360if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY) 361 install(DIRECTORY include/clang include/clang-c 362 DESTINATION include 363 FILES_MATCHING 364 PATTERN "*.def" 365 PATTERN "*.h" 366 PATTERN "config.h" EXCLUDE 367 PATTERN ".svn" EXCLUDE 368 ) 369 370 install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/include/clang 371 DESTINATION include 372 FILES_MATCHING 373 PATTERN "CMakeFiles" EXCLUDE 374 PATTERN "*.inc" 375 ) 376endif() 377 378install(DIRECTORY include/clang-c 379 DESTINATION include 380 FILES_MATCHING 381 PATTERN "*.h" 382 PATTERN ".svn" EXCLUDE 383 ) 384 385add_definitions( -D_GNU_SOURCE ) 386 387option(CLANG_ENABLE_ARCMT "Build ARCMT." ON) 388option(CLANG_ENABLE_REWRITER "Build rewriter." ON) 389option(CLANG_ENABLE_STATIC_ANALYZER "Build static analyzer." ON) 390 391if (NOT CLANG_ENABLE_REWRITER AND CLANG_ENABLE_ARCMT) 392 message(FATAL_ERROR "Cannot disable rewriter while enabling ARCMT") 393endif() 394 395if (NOT CLANG_ENABLE_REWRITER AND CLANG_ENABLE_STATIC_ANALYZER) 396 message(FATAL_ERROR "Cannot disable rewriter while enabling static analyzer") 397endif() 398 399if (NOT CLANG_ENABLE_STATIC_ANALYZER AND CLANG_ENABLE_ARCMT) 400 message(FATAL_ERROR "Cannot disable static analyzer while enabling ARCMT") 401endif() 402 403if(CLANG_ENABLE_ARCMT) 404 add_definitions(-DCLANG_ENABLE_ARCMT) 405endif() 406if(CLANG_ENABLE_REWRITER) 407 add_definitions(-DCLANG_ENABLE_REWRITER) 408endif() 409if(CLANG_ENABLE_STATIC_ANALYZER) 410 add_definitions(-DCLANG_ENABLE_STATIC_ANALYZER) 411endif() 412 413# Clang version information 414set(CLANG_EXECUTABLE_VERSION 415 "${CLANG_VERSION_MAJOR}.${CLANG_VERSION_MINOR}" CACHE STRING 416 "Version number that will be placed into the clang executable, in the form XX.YY") 417set(LIBCLANG_LIBRARY_VERSION 418 "${CLANG_VERSION_MAJOR}.${CLANG_VERSION_MINOR}" CACHE STRING 419 "Version number that will be placed into the libclang library , in the form XX.YY") 420mark_as_advanced(CLANG_EXECUTABLE_VERSION LIBCLANG_LIBRARY_VERSION) 421 422add_subdirectory(utils/TableGen) 423 424add_subdirectory(include) 425 426# All targets below may depend on all tablegen'd files. 427get_property(CLANG_TABLEGEN_TARGETS GLOBAL PROPERTY CLANG_TABLEGEN_TARGETS) 428list(APPEND LLVM_COMMON_DEPENDS ${CLANG_TABLEGEN_TARGETS}) 429 430add_subdirectory(lib) 431add_subdirectory(tools) 432add_subdirectory(runtime) 433 434option(CLANG_BUILD_EXAMPLES "Build CLANG example programs by default." OFF) 435if (CLANG_BUILD_EXAMPLES) 436 set(ENABLE_CLANG_EXAMPLES "1") 437else() 438 set(ENABLE_CLANG_EXAMPLES "0") 439endif() 440add_subdirectory(examples) 441 442option(CLANG_INCLUDE_TESTS 443 "Generate build targets for the Clang unit tests." 444 ${LLVM_INCLUDE_TESTS}) 445 446if( CLANG_INCLUDE_TESTS ) 447 if(EXISTS ${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest/include/gtest/gtest.h) 448 add_subdirectory(unittests) 449 list(APPEND CLANG_TEST_DEPS ClangUnitTests) 450 list(APPEND CLANG_TEST_PARAMS 451 clang_unit_site_config=${CMAKE_CURRENT_BINARY_DIR}/test/Unit/lit.site.cfg 452 ) 453 endif() 454 add_subdirectory(test) 455 456 if(CLANG_BUILT_STANDALONE) 457 # Add a global check rule now that all subdirectories have been traversed 458 # and we know the total set of lit testsuites. 459 get_property(LLVM_LIT_TESTSUITES GLOBAL PROPERTY LLVM_LIT_TESTSUITES) 460 get_property(LLVM_LIT_PARAMS GLOBAL PROPERTY LLVM_LIT_PARAMS) 461 get_property(LLVM_LIT_DEPENDS GLOBAL PROPERTY LLVM_LIT_DEPENDS) 462 get_property(LLVM_LIT_EXTRA_ARGS GLOBAL PROPERTY LLVM_LIT_EXTRA_ARGS) 463 add_lit_target(check-all 464 "Running all regression tests" 465 ${LLVM_LIT_TESTSUITES} 466 PARAMS ${LLVM_LIT_PARAMS} 467 DEPENDS ${LLVM_LIT_DEPENDS} 468 ARGS ${LLVM_LIT_EXTRA_ARGS} 469 ) 470 endif() 471endif() 472 473option(CLANG_INCLUDE_DOCS "Generate build targets for the Clang docs." 474 ${LLVM_INCLUDE_DOCS}) 475if( CLANG_INCLUDE_DOCS ) 476 add_subdirectory(docs) 477endif() 478 479set(CLANG_ORDER_FILE "" CACHE FILEPATH 480 "Order file to use when compiling clang in order to improve startup time.") 481