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 endif()
     70 
     71 set(CLANG_RESOURCE_DIR "" CACHE STRING
     72   "Relative directory from the Clang binary to its resource files.")
     73 
     74 set(C_INCLUDE_DIRS "" CACHE STRING
     75   "Colon separated list of directories clang will search for headers.")
     76 
     77 set(GCC_INSTALL_PREFIX "" CACHE PATH "Directory where gcc is installed." )
     78 set(DEFAULT_SYSROOT "" CACHE PATH
     79   "Default <path> to all compiler invocations for --sysroot=<path>." )
     80 
     81 set(CLANG_VENDOR "" CACHE STRING
     82   "Vendor-specific text for showing with version information.")
     83 
     84 if( CLANG_VENDOR )
     85   add_definitions( -DCLANG_VENDOR="${CLANG_VENDOR} " )
     86 endif()
     87 
     88 set(CLANG_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
     89 set(CLANG_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
     90 
     91 if( CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR AND NOT MSVC_IDE )
     92   message(FATAL_ERROR "In-source builds are not allowed. CMake would overwrite "
     93 "the makefiles distributed with LLVM. Please create a directory and run cmake "
     94 "from there, passing the path to this source directory as the last argument. "
     95 "This process created the file `CMakeCache.txt' and the directory "
     96 "`CMakeFiles'. Please delete them.")
     97 endif()
     98 
     99 if( NOT CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR )
    100   file(GLOB_RECURSE
    101     tablegenned_files_on_include_dir
    102     "${CLANG_SOURCE_DIR}/include/clang/*.inc")
    103   if( tablegenned_files_on_include_dir )
    104     message(FATAL_ERROR "Apparently there is a previous in-source build, "
    105 "probably as the result of running `configure' and `make' on "
    106 "${CLANG_SOURCE_DIR}. This may cause problems. The suspicious files are:\n"
    107 "${tablegenned_files_on_include_dir}\nPlease clean the source directory.")
    108   endif()
    109 endif()
    110 
    111 # Compute the Clang version from the LLVM version.
    112 string(REGEX MATCH "[0-9]+\\.[0-9]+(\\.[0-9]+)?" CLANG_VERSION
    113   ${PACKAGE_VERSION})
    114 message(STATUS "Clang version: ${CLANG_VERSION}")
    115 
    116 string(REGEX REPLACE "([0-9]+)\\.[0-9]+(\\.[0-9]+)?" "\\1" CLANG_VERSION_MAJOR
    117   ${CLANG_VERSION})
    118 string(REGEX REPLACE "[0-9]+\\.([0-9]+)(\\.[0-9]+)?" "\\1" CLANG_VERSION_MINOR
    119   ${CLANG_VERSION})
    120 if (${CLANG_VERSION} MATCHES "[0-9]+\\.[0-9]+\\.[0-9]+")
    121   set(CLANG_HAS_VERSION_PATCHLEVEL 1)
    122   string(REGEX REPLACE "[0-9]+\\.[0-9]+\\.([0-9]+)" "\\1" CLANG_VERSION_PATCHLEVEL
    123     ${CLANG_VERSION})
    124 else()
    125   set(CLANG_HAS_VERSION_PATCHLEVEL 0)
    126 endif()
    127 
    128 # Configure the Version.inc file.
    129 configure_file(
    130   ${CMAKE_CURRENT_SOURCE_DIR}/include/clang/Basic/Version.inc.in
    131   ${CMAKE_CURRENT_BINARY_DIR}/include/clang/Basic/Version.inc)
    132 
    133 # Add appropriate flags for GCC
    134 if (LLVM_COMPILER_IS_GCC_COMPATIBLE)
    135   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")
    136 endif ()
    137 
    138 if (APPLE)
    139   set(CMAKE_MODULE_LINKER_FLAGS "-Wl,-flat_namespace -Wl,-undefined -Wl,suppress")
    140 endif ()
    141 
    142 # libxml2 is an optional dependency, required only to run validation
    143 # tests on XML output.
    144 find_package(LibXml2)
    145 
    146 configure_file(
    147   ${CLANG_SOURCE_DIR}/include/clang/Config/config.h.cmake
    148   ${CLANG_BINARY_DIR}/include/clang/Config/config.h)
    149 
    150 include(LLVMParseArguments)
    151 
    152 function(clang_tablegen)
    153   # Syntax:
    154   # clang_tablegen output-file [tablegen-arg ...] SOURCE source-file
    155   # [[TARGET cmake-target-name] [DEPENDS extra-dependency ...]]
    156   #
    157   # Generates a custom command for invoking tblgen as
    158   #
    159   # tblgen source-file -o=output-file tablegen-arg ...
    160   #
    161   # and, if cmake-target-name is provided, creates a custom target for
    162   # executing the custom command depending on output-file. It is
    163   # possible to list more files to depend after DEPENDS.
    164 
    165   parse_arguments( CTG "SOURCE;TARGET;DEPENDS" "" ${ARGN} )
    166 
    167   if( NOT CTG_SOURCE )
    168     message(FATAL_ERROR "SOURCE source-file required by clang_tablegen")
    169   endif()
    170 
    171   set( LLVM_TARGET_DEFINITIONS ${CTG_SOURCE} )
    172   tablegen( CLANG ${CTG_DEFAULT_ARGS} )
    173 
    174   list( GET CTG_DEFAULT_ARGS 0 output_file )
    175   if( CTG_TARGET )
    176     add_custom_target( ${CTG_TARGET} DEPENDS ${output_file} ${CTG_DEPENDS} )
    177     set_target_properties( ${CTG_TARGET} PROPERTIES FOLDER "Clang tablegenning")
    178   endif()
    179 endfunction(clang_tablegen)
    180 
    181 macro(add_clang_library name)
    182   llvm_process_sources(srcs ${ARGN})
    183   if(MSVC_IDE OR XCODE)
    184     string( REGEX MATCHALL "/[^/]+" split_path ${CMAKE_CURRENT_SOURCE_DIR})
    185     list( GET split_path -1 dir)
    186     file( GLOB_RECURSE headers
    187       ../../../include/clang/StaticAnalyzer${dir}/*.h
    188       ../../../include/clang/StaticAnalyzer${dir}/*.td
    189       ../../../include/clang/StaticAnalyzer${dir}/*.def
    190       ../../include/clang${dir}/*.h
    191       ../../include/clang${dir}/*.td
    192       ../../include/clang${dir}/*.def)
    193     set(srcs ${srcs} ${headers})
    194   endif(MSVC_IDE OR XCODE)
    195   if (MODULE)
    196     set(libkind MODULE)
    197   elseif (SHARED_LIBRARY)
    198     set(libkind SHARED)
    199   else()
    200     set(libkind)
    201   endif()
    202   add_library( ${name} ${libkind} ${srcs} )
    203   if( LLVM_COMMON_DEPENDS )
    204     add_dependencies( ${name} ${LLVM_COMMON_DEPENDS} )
    205   endif( LLVM_COMMON_DEPENDS )
    206 
    207   llvm_config( ${name} ${LLVM_LINK_COMPONENTS} )
    208   target_link_libraries( ${name} ${LLVM_COMMON_LIBS} )
    209   link_system_libs( ${name} )
    210 
    211   install(TARGETS ${name}
    212     LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX}
    213     ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX}
    214     RUNTIME DESTINATION bin)
    215   set_target_properties(${name} PROPERTIES FOLDER "Clang libraries")
    216 endmacro(add_clang_library)
    217 
    218 macro(add_clang_executable name)
    219   add_llvm_executable( ${name} ${ARGN} )
    220   set_target_properties(${name} PROPERTIES FOLDER "Clang executables")
    221 endmacro(add_clang_executable)
    222 
    223 include_directories(BEFORE
    224   ${CMAKE_CURRENT_BINARY_DIR}/include
    225   ${CMAKE_CURRENT_SOURCE_DIR}/include
    226   )
    227 
    228 install(DIRECTORY include/
    229   DESTINATION include
    230   FILES_MATCHING
    231   PATTERN "*.def"
    232   PATTERN "*.h"
    233   PATTERN "config.h" EXCLUDE
    234   PATTERN ".svn" EXCLUDE
    235   )
    236 
    237 install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/include/
    238   DESTINATION include
    239   FILES_MATCHING
    240   PATTERN "CMakeFiles" EXCLUDE
    241   PATTERN "*.inc"
    242   )
    243 
    244 add_definitions( -D_GNU_SOURCE )
    245 
    246 # Clang version information
    247 set(CLANG_EXECUTABLE_VERSION
    248      "${CLANG_VERSION_MAJOR}.${CLANG_VERSION_MINOR}" CACHE STRING
    249     "Version number that will be placed into the clang executable, in the form XX.YY")
    250 set(LIBCLANG_LIBRARY_VERSION
    251      "${CLANG_VERSION_MAJOR}.${CLANG_VERSION_MINOR}" CACHE STRING
    252     "Version number that will be placed into the libclang library , in the form XX.YY")
    253 mark_as_advanced(CLANG_EXECUTABLE_VERSION LIBCLANG_LIBRARY_VERSION)
    254 
    255 add_subdirectory(utils/TableGen)
    256 
    257 add_subdirectory(include)
    258 add_subdirectory(lib)
    259 add_subdirectory(tools)
    260 add_subdirectory(runtime)
    261 
    262 option(CLANG_BUILD_EXAMPLES "Build CLANG example programs by default." OFF)
    263 add_subdirectory(examples)
    264 
    265 # TODO: docs.
    266 add_subdirectory(test)
    267 
    268 if( LLVM_INCLUDE_TESTS )
    269   if( NOT CLANG_BUILT_STANDALONE )
    270     add_subdirectory(unittests)
    271   endif()
    272 endif()
    273 
    274 # Workaround for MSVS10 to avoid the Dialog Hell
    275 # FIXME: This could be removed with future version of CMake.
    276 if( CLANG_BUILT_STANDALONE AND MSVC_VERSION EQUAL 1600 )
    277   set(CLANG_SLN_FILENAME "${CMAKE_CURRENT_BINARY_DIR}/Clang.sln")
    278   if( EXISTS "${CLANG_SLN_FILENAME}" )
    279     file(APPEND "${CLANG_SLN_FILENAME}" "\n# This should be regenerated!\n")
    280   endif()
    281 endif()
    282 
    283 set(BUG_REPORT_URL "http://llvm.org/bugs/" CACHE STRING
    284   "Default URL where bug reports are to be submitted.")
    285 
    286