Home | History | Annotate | Download | only in clang
      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/llvm-tblgen${CMAKE_EXECUTABLE_SUFFIX}" )
     23     # Looking for bin/Debug/llvm-tblgen is a complete hack. How can we get
     24     # around this?
     25     if( NOT EXISTS "${CLANG_PATH_TO_LLVM_BUILD}/bin/Debug/llvm-tblgen${CMAKE_EXECUTABLE_SUFFIX}" )
     26       message(FATAL_ERROR "Please set CLANG_PATH_TO_LLVM_BUILD to a directory containing a LLVM build.")
     27     endif()
     28   endif()
     29 
     30   list(APPEND CMAKE_MODULE_PATH "${CLANG_PATH_TO_LLVM_BUILD}/share/llvm/cmake")
     31 
     32   get_filename_component(PATH_TO_LLVM_BUILD ${CLANG_PATH_TO_LLVM_BUILD}
     33     ABSOLUTE)
     34 
     35   include(AddLLVM)
     36   include(TableGen)
     37   include("${CLANG_PATH_TO_LLVM_BUILD}/share/llvm/cmake/LLVMConfig.cmake")
     38   include(HandleLLVMOptions)
     39 
     40   set(PACKAGE_VERSION "${LLVM_PACKAGE_VERSION}")
     41 
     42   set(LLVM_MAIN_INCLUDE_DIR "${LLVM_MAIN_SRC_DIR}/include")
     43   set(LLVM_BINARY_DIR ${CMAKE_BINARY_DIR})
     44 
     45   set(CMAKE_INCLUDE_CURRENT_DIR ON)
     46   include_directories("${PATH_TO_LLVM_BUILD}/include" "${LLVM_MAIN_INCLUDE_DIR}")
     47   link_directories("${PATH_TO_LLVM_BUILD}/lib")
     48 
     49   if( EXISTS "${CLANG_PATH_TO_LLVM_BUILD}/bin/llvm-tblgen${CMAKE_EXECUTABLE_SUFFIX}" )
     50     set(LLVM_TABLEGEN_EXE "${PATH_TO_LLVM_BUILD}/bin/llvm-tblgen${CMAKE_EXECUTABLE_SUFFIX}")
     51   else()
     52     # FIXME: This is an utter hack.
     53     set(LLVM_TABLEGEN_EXE "${PATH_TO_LLVM_BUILD}/bin/Debug/llvm-tblgen${CMAKE_EXECUTABLE_SUFFIX}")
     54   endif()
     55 
     56   # Define the default arguments to use with 'lit', and an option for the user
     57   # to override.
     58   set(LIT_ARGS_DEFAULT "-sv")
     59   if (MSVC OR XCODE)
     60     set(LIT_ARGS_DEFAULT "${LIT_ARGS_DEFAULT} --no-progress-bar")
     61   endif()
     62   set(LLVM_LIT_ARGS "${LIT_ARGS_DEFAULT}" CACHE STRING "Default options for lit")
     63 
     64   set( CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin )
     65   set( CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib )
     66   set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib )
     67 
     68   set( CLANG_BUILT_STANDALONE 1 )
     69 
     70   find_package(LibXml2)
     71   if (LIBXML2_FOUND)
     72     set(CLANG_HAVE_LIBXML 1)
     73   endif ()
     74 endif()
     75 
     76 set(CLANG_RESOURCE_DIR "" CACHE STRING
     77   "Relative directory from the Clang binary to its resource files.")
     78 
     79 set(C_INCLUDE_DIRS "" CACHE STRING
     80   "Colon separated list of directories clang will search for headers.")
     81 
     82 set(GCC_INSTALL_PREFIX "" CACHE PATH "Directory where gcc is installed." )
     83 set(DEFAULT_SYSROOT "" CACHE PATH
     84   "Default <path> to all compiler invocations for --sysroot=<path>." )
     85 
     86 set(CLANG_VENDOR "" CACHE STRING
     87   "Vendor-specific text for showing with version information.")
     88 
     89 if( CLANG_VENDOR )
     90   add_definitions( -DCLANG_VENDOR="${CLANG_VENDOR} " )
     91 endif()
     92 
     93 set(CLANG_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
     94 set(CLANG_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
     95 
     96 if( CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR AND NOT MSVC_IDE )
     97   message(FATAL_ERROR "In-source builds are not allowed. CMake would overwrite "
     98 "the makefiles distributed with LLVM. Please create a directory and run cmake "
     99 "from there, passing the path to this source directory as the last argument. "
    100 "This process created the file `CMakeCache.txt' and the directory "
    101 "`CMakeFiles'. Please delete them.")
    102 endif()
    103 
    104 if( NOT CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR )
    105   file(GLOB_RECURSE
    106     tablegenned_files_on_include_dir
    107     "${CLANG_SOURCE_DIR}/include/clang/*.inc")
    108   if( tablegenned_files_on_include_dir )
    109     message(FATAL_ERROR "Apparently there is a previous in-source build, "
    110 "probably as the result of running `configure' and `make' on "
    111 "${CLANG_SOURCE_DIR}. This may cause problems. The suspicious files are:\n"
    112 "${tablegenned_files_on_include_dir}\nPlease clean the source directory.")
    113   endif()
    114 endif()
    115 
    116 # Compute the Clang version from the LLVM version.
    117 string(REGEX MATCH "[0-9]+\\.[0-9]+(\\.[0-9]+)?" CLANG_VERSION
    118   ${PACKAGE_VERSION})
    119 message(STATUS "Clang version: ${CLANG_VERSION}")
    120 
    121 string(REGEX REPLACE "([0-9]+)\\.[0-9]+(\\.[0-9]+)?" "\\1" CLANG_VERSION_MAJOR
    122   ${CLANG_VERSION})
    123 string(REGEX REPLACE "[0-9]+\\.([0-9]+)(\\.[0-9]+)?" "\\1" CLANG_VERSION_MINOR
    124   ${CLANG_VERSION})
    125 if (${CLANG_VERSION} MATCHES "[0-9]+\\.[0-9]+\\.[0-9]+")
    126   set(CLANG_HAS_VERSION_PATCHLEVEL 1)
    127   string(REGEX REPLACE "[0-9]+\\.[0-9]+\\.([0-9]+)" "\\1" CLANG_VERSION_PATCHLEVEL
    128     ${CLANG_VERSION})
    129 else()
    130   set(CLANG_HAS_VERSION_PATCHLEVEL 0)
    131 endif()
    132 
    133 # Configure the Version.inc file.
    134 configure_file(
    135   ${CMAKE_CURRENT_SOURCE_DIR}/include/clang/Basic/Version.inc.in
    136   ${CMAKE_CURRENT_BINARY_DIR}/include/clang/Basic/Version.inc)
    137 
    138 # Add appropriate flags for GCC
    139 if (LLVM_COMPILER_IS_GCC_COMPATIBLE)
    140   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-common -Woverloaded-virtual -Wcast-qual -fno-strict-aliasing")
    141 
    142   # Enable -pedantic for Clang even if it's not enabled for LLVM.
    143   if (NOT LLVM_ENABLE_PEDANTIC)
    144     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic -Wno-long-long")
    145   endif ()
    146 
    147   check_cxx_compiler_flag("-Werror -Wnested-anon-types" CXX_SUPPORTS_NO_NESTED_ANON_TYPES_FLAG)
    148   if( CXX_SUPPORTS_NO_NESTED_ANON_TYPES_FLAG )
    149     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-nested-anon-types" )
    150   endif()
    151 endif ()
    152 
    153 if (APPLE)
    154   set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -Wl,-flat_namespace -Wl,-undefined -Wl,suppress")
    155 endif ()
    156 
    157 configure_file(
    158   ${CLANG_SOURCE_DIR}/include/clang/Config/config.h.cmake
    159   ${CLANG_BINARY_DIR}/include/clang/Config/config.h)
    160 
    161 include(LLVMParseArguments)
    162 
    163 function(clang_tablegen)
    164   # Syntax:
    165   # clang_tablegen output-file [tablegen-arg ...] SOURCE source-file
    166   # [[TARGET cmake-target-name] [DEPENDS extra-dependency ...]]
    167   #
    168   # Generates a custom command for invoking tblgen as
    169   #
    170   # tblgen source-file -o=output-file tablegen-arg ...
    171   #
    172   # and, if cmake-target-name is provided, creates a custom target for
    173   # executing the custom command depending on output-file. It is
    174   # possible to list more files to depend after DEPENDS.
    175 
    176   parse_arguments( CTG "SOURCE;TARGET;DEPENDS" "" ${ARGN} )
    177 
    178   if( NOT CTG_SOURCE )
    179     message(FATAL_ERROR "SOURCE source-file required by clang_tablegen")
    180   endif()
    181 
    182   set( LLVM_TARGET_DEFINITIONS ${CTG_SOURCE} )
    183   tablegen( CLANG ${CTG_DEFAULT_ARGS} )
    184 
    185   list( GET CTG_DEFAULT_ARGS 0 output_file )
    186   if( CTG_TARGET )
    187     add_custom_target( ${CTG_TARGET} DEPENDS ${output_file} ${CTG_DEPENDS} )
    188     set_target_properties( ${CTG_TARGET} PROPERTIES FOLDER "Clang tablegenning")
    189   endif()
    190 endfunction(clang_tablegen)
    191 
    192 macro(add_clang_library name)
    193   llvm_process_sources(srcs ${ARGN})
    194   if(MSVC_IDE OR XCODE)
    195     # Add public headers
    196     file(RELATIVE_PATH lib_path
    197       ${CLANG_SOURCE_DIR}/lib/
    198       ${CMAKE_CURRENT_SOURCE_DIR}
    199     )
    200     if(NOT lib_path MATCHES "^[.][.]")
    201       file( GLOB_RECURSE headers
    202         ${CLANG_SOURCE_DIR}/include/clang/${lib_path}/*.h
    203         ${CLANG_SOURCE_DIR}/include/clang/${lib_path}/*.def
    204       )
    205       set_source_files_properties(${headers} PROPERTIES HEADER_FILE_ONLY ON)
    206 
    207       file( GLOB_RECURSE tds
    208         ${CLANG_SOURCE_DIR}/include/clang/${lib_path}/*.td
    209       )
    210       source_group("TableGen descriptions" FILES ${tds})
    211       set_source_files_properties(${tds}} PROPERTIES HEADER_FILE_ONLY ON)
    212 
    213       set(srcs ${srcs} ${headers} ${tds})
    214     endif()
    215   endif(MSVC_IDE OR XCODE)
    216   if (MODULE)
    217     set(libkind MODULE)
    218   elseif (SHARED_LIBRARY)
    219     set(libkind SHARED)
    220   else()
    221     set(libkind)
    222   endif()
    223   add_library( ${name} ${libkind} ${srcs} )
    224   if( LLVM_COMMON_DEPENDS )
    225     add_dependencies( ${name} ${LLVM_COMMON_DEPENDS} )
    226   endif( LLVM_COMMON_DEPENDS )
    227 
    228   llvm_config( ${name} ${LLVM_LINK_COMPONENTS} )
    229   target_link_libraries( ${name} ${LLVM_COMMON_LIBS} )
    230   link_system_libs( ${name} )
    231 
    232   install(TARGETS ${name}
    233     LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX}
    234     ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX}
    235     RUNTIME DESTINATION bin)
    236   set_target_properties(${name} PROPERTIES FOLDER "Clang libraries")
    237 endmacro(add_clang_library)
    238 
    239 macro(add_clang_executable name)
    240   add_llvm_executable( ${name} ${ARGN} )
    241   set_target_properties(${name} PROPERTIES FOLDER "Clang executables")
    242 endmacro(add_clang_executable)
    243 
    244 include_directories(BEFORE
    245   ${CMAKE_CURRENT_BINARY_DIR}/include
    246   ${CMAKE_CURRENT_SOURCE_DIR}/include
    247   )
    248 
    249 install(DIRECTORY include/
    250   DESTINATION include
    251   FILES_MATCHING
    252   PATTERN "*.def"
    253   PATTERN "*.h"
    254   PATTERN "config.h" EXCLUDE
    255   PATTERN ".svn" EXCLUDE
    256   )
    257 
    258 install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/include/
    259   DESTINATION include
    260   FILES_MATCHING
    261   PATTERN "CMakeFiles" EXCLUDE
    262   PATTERN "*.inc"
    263   )
    264 
    265 add_definitions( -D_GNU_SOURCE )
    266 
    267 option(CLANG_ENABLE_ARCMT "Enable ARCMT by default." ON)
    268 option(CLANG_ENABLE_REWRITER "Enable rewriter by default." ON)
    269 option(CLANG_ENABLE_STATIC_ANALYZER "Enable static analyzer by default." ON)
    270 
    271 if (NOT CLANG_ENABLE_REWRITER AND CLANG_ENABLE_ARCMT)
    272   message(FATAL_ERROR "Cannot disable rewriter while enabling ARCMT")
    273 endif()
    274 
    275 if (NOT CLANG_ENABLE_REWRITER AND CLANG_ENABLE_STATIC_ANALYZER)
    276   message(FATAL_ERROR "Cannot disable rewriter while enabling static analyzer")
    277 endif()
    278 
    279 if(CLANG_ENABLE_ARCMT)
    280   add_definitions(-DCLANG_ENABLE_ARCMT)
    281 endif()
    282 if(CLANG_ENABLE_REWRITER)
    283   add_definitions(-DCLANG_ENABLE_REWRITER)
    284 endif()
    285 if(CLANG_ENABLE_STATIC_ANALYZER)
    286   add_definitions(-DCLANG_ENABLE_STATIC_ANALYZER)
    287 endif()
    288 
    289 # Clang version information
    290 set(CLANG_EXECUTABLE_VERSION
    291      "${CLANG_VERSION_MAJOR}.${CLANG_VERSION_MINOR}" CACHE STRING
    292     "Version number that will be placed into the clang executable, in the form XX.YY")
    293 set(LIBCLANG_LIBRARY_VERSION
    294      "${CLANG_VERSION_MAJOR}.${CLANG_VERSION_MINOR}" CACHE STRING
    295     "Version number that will be placed into the libclang library , in the form XX.YY")
    296 mark_as_advanced(CLANG_EXECUTABLE_VERSION LIBCLANG_LIBRARY_VERSION)
    297 
    298 add_subdirectory(utils/TableGen)
    299 
    300 add_subdirectory(include)
    301 add_subdirectory(lib)
    302 add_subdirectory(tools)
    303 add_subdirectory(runtime)
    304 
    305 option(CLANG_BUILD_EXAMPLES "Build CLANG example programs by default." OFF)
    306 add_subdirectory(examples)
    307 
    308 option(CLANG_INCLUDE_TESTS
    309        "Generate build targets for the Clang unit tests."
    310        ${LLVM_INCLUDE_TESTS})
    311 
    312 # TODO: docs.
    313 add_subdirectory(test)
    314 
    315 if( CLANG_INCLUDE_TESTS )
    316   add_subdirectory(unittests)
    317 endif()
    318 
    319 # Workaround for MSVS10 to avoid the Dialog Hell
    320 # FIXME: This could be removed with future version of CMake.
    321 if( CLANG_BUILT_STANDALONE AND MSVC_VERSION EQUAL 1600 )
    322   set(CLANG_SLN_FILENAME "${CMAKE_CURRENT_BINARY_DIR}/Clang.sln")
    323   if( EXISTS "${CLANG_SLN_FILENAME}" )
    324     file(APPEND "${CLANG_SLN_FILENAME}" "\n# This should be regenerated!\n")
    325   endif()
    326 endif()
    327 
    328 set(BUG_REPORT_URL "http://llvm.org/bugs/" CACHE STRING
    329   "Default URL where bug reports are to be submitted.")
    330