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