Initial commit.

This commit is contained in:
2016-02-18 14:53:30 +01:00
commit 8e93ca7a95
2215 changed files with 341269 additions and 0 deletions

View File

@@ -0,0 +1,95 @@
#
# Get the directory where the libclang headers reside.
#
# If found the following variable will be set:
# - LIBCLANG_BUILTIN_HEADERS_DIR
#
set(CHECK_LIBCLANG_BUILTIN_HEADERS_DIR_CHECKER_CODE_IN
${CMAKE_CURRENT_LIST_DIR}/LibClangDiagnosticsChecker.cpp)
function(check_libclang_builtin_headers_dir)
if (LIBCLANG_BUILTIN_HEADERS_DIR)
return() # already in cache
endif()
message(STATUS "Detecting libclang builtin headers directory")
find_package (LibClang REQUIRED)
set(checker_code
${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/LibClangDiagnosticsChecker.cpp)
set(checked_file
"${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/check-libclang-stddef.cpp")
configure_file(${CHECK_LIBCLANG_BUILTIN_HEADERS_DIR_CHECKER_CODE_IN}
${checker_code} COPYONLY)
file(WRITE "${checked_file}" "#include <stddef.h>\n")
foreach (version ${LIBCLANG_KNOWN_LLVM_VERSIONS} .)
list(APPEND builtin_include_dir_suffixes "${version}/include")
endforeach()
# Paths stolen from Rip-Rip/clang_complete#getBuiltinHeaderPath()
find_path(CHECK_LIBCLANG_BUILTIN_HEADERS_STDDEF_DIR stddef.h
NO_DEFAULT_PATH
# the default path, favor this one over the other, in case a specific
# libclang has been chosen.
HINTS "${LIBCLANG_LIBRARY_DIR}/../lib/clang"
# other, distribution specific, paths
PATHS
"${LIBCLANG_LIBRARY_DIR}/../clang" # Gentoo
"${LIBCLANG_LIBRARY_DIR}/clang" # openSUSE, Windows
"${LIBCLANG_LIBRARY_DIR}/" # Google
"/usr/lib64/clang" # x86_64 (openSUSE, Fedora)
"/usr/lib/clang"
PATH_SUFFIXES ${builtin_include_dir_suffixes}
)
if (CHECK_LIBCLANG_BUILTIN_HEADERS_STDDEF_DIR)
# On Windows the paths weren't escaped correctly, similar to:
# http://public.kitware.com/pipermail/cmake/2006-February/008473.html
list(APPEND run_args -isystem \"${CHECK_LIBCLANG_BUILTIN_HEADERS_STDDEF_DIR}\")
endif()
list(APPEND run_args ${checked_file})
try_run(
CHECK_LIBCLANG_BUILTIN_HEADERS_DIR_NUM_DIAGNOSTICS
CHECK_LIBCLANG_BUILTIN_HEADERS_COMPILE_RESULT
${CMAKE_BINARY_DIR}
${checker_code}
CMAKE_FLAGS
"-DINCLUDE_DIRECTORIES:STRING=${LIBCLANG_INCLUDE_DIRS}"
"-DLINK_LIBRARIES:STRING=${LIBCLANG_LIBRARIES}"
COMPILE_OUTPUT_VARIABLE compile_output
RUN_OUTPUT_VARIABLE run_output
ARGS ${run_args}
)
if (NOT CHECK_LIBCLANG_BUILTIN_HEADERS_COMPILE_RESULT)
set(CHECK_LIBCLANG_BUILTIN_HEADERS_DIR_NUM_DIAGNOSTICS 1)
endif()
if (CHECK_LIBCLANG_BUILTIN_HEADERS_DIR_NUM_DIAGNOSTICS EQUAL 0)
message(STATUS "Detecting libclang builtin headers directory -- success")
if (CHECK_LIBCLANG_BUILTIN_HEADERS_STDDEF_DIR)
set(LIBCLANG_BUILTIN_HEADERS_DIR "${CHECK_LIBCLANG_BUILTIN_HEADERS_STDDEF_DIR}"
CACHE INTERNAL "libclang builtin headers directory.")
endif()
else()
message(STATUS "Detecting libclang builtin headers directory -- fail")
if (NOT CHECK_LIBCLANG_BUILTIN_HEADERS_COMPILE_RESULT)
message(WARNING "CheckLibClangBuiltinHeadersDir: failed to compile checker, please report.
Compile output:
${compile_output}
")
else()
message(WARNING "CheckLibClangBuiltinHeadersDir: unsupported configuration, please report.
Check with args: ${run_args}
Check output:
${run_output}
")
endif()
endif()
endfunction()

View File

@@ -0,0 +1,47 @@
/*
This program takes some forward its command line arguments to libclang and
returns the number of diagnostics that occured during the parsing.
It is used during CMake generation to adjust the default parameters to
libclang.
*/
#include <clang-c/Index.h>
#include <stdio.h>
int main(int argc, const char *argv[]) {
for (int i = 1; i < argc; ++i) {
fprintf(stdout, "argv[%d]: %s\n", i, argv[i]);
}
CXIndex Idx = clang_createIndex(0, 0);
CXTranslationUnit TU = clang_parseTranslationUnit(
Idx, NULL, &argv[1], argc - 1, 0, 0, CXTranslationUnit_None);
int NumDiagnostics;
if (TU == NULL) {
NumDiagnostics = 1;
fprintf(stderr, "failed to create translation unit!\n");
} else {
int i;
NumDiagnostics = clang_getNumDiagnostics(TU);
for (i = 0; i < NumDiagnostics; ++i) {
CXDiagnostic Diag = clang_getDiagnostic(TU, i);
CXString DiagStr =
clang_formatDiagnostic(Diag, clang_defaultDiagnosticDisplayOptions());
fprintf(stderr, "%s\n", clang_getCString(DiagStr));
clang_disposeString(DiagStr);
clang_disposeDiagnostic(Diag);
}
clang_disposeTranslationUnit(TU);
}
clang_disposeIndex(Idx);
return NumDiagnostics;
}

View File

@@ -0,0 +1,92 @@
#
# Try to find libclang
#
# Once done this will define:
# - LIBCLANG_FOUND
# System has libclang.
# - LIBCLANG_INCLUDE_DIRS
# The libclang include directories.
# - LIBCLANG_LIBRARIES
# The libraries needed to use libclang.
# - LIBCLANG_LIBRARY_DIR
# The path to the directory containing libclang.
# - LIBCLANG_KNOWN_LLVM_VERSIONS
# Known LLVM release numbers.
# most recent versions come first
# http://llvm.org/apt/
set(LIBCLANG_KNOWN_LLVM_VERSIONS 3.9.0 3.9
3.8.0 3.8
3.7.1 3.7.0 3.7
3.6.2 3.6.1 3.6.0 3.6
3.5.2 3.5.1 3.5.0 3.5
3.4.2 3.4.1 3.4
3.3
3.2
3.1)
set(libclang_llvm_header_search_paths)
set(libclang_llvm_lib_search_paths
# LLVM Fedora
/usr/lib/llvm
)
foreach (version ${LIBCLANG_KNOWN_LLVM_VERSIONS})
string(REPLACE "." "" undotted_version "${version}")
list(APPEND libclang_llvm_header_search_paths
# LLVM Debian/Ubuntu nightly packages: http://llvm.org/apt/
"/usr/lib/llvm-${version}/include/"
# LLVM MacPorts
"/opt/local/libexec/llvm-${version}/include"
# LLVM Homebrew
"/usr/local/Cellar/llvm/${version}/include"
# LLVM Homebrew/versions
"/usr/local/lib/llvm-${version}/include"
# FreeBSD ports versions
"/usr/local/llvm${undotted_version}/include"
)
list(APPEND libclang_llvm_lib_search_paths
# LLVM Debian/Ubuntu nightly packages: http://llvm.org/apt/
"/usr/lib/llvm-${version}/lib/"
# LLVM MacPorts
"/opt/local/libexec/llvm-${version}/lib"
# LLVM Homebrew
"/usr/local/Cellar/llvm/${version}/lib"
# LLVM Homebrew/versions
"/usr/local/lib/llvm-${version}/lib"
# FreeBSD ports versions
"/usr/local/llvm${undotted_version}/lib"
)
endforeach()
find_path(LIBCLANG_INCLUDE_DIR clang-c/Index.h
PATHS ${libclang_llvm_header_search_paths}
PATH_SUFFIXES LLVM/include #Windows package from http://llvm.org/releases/
DOC "The path to the directory that contains clang-c/Index.h")
# On Windows with MSVC, the import library uses the ".imp" file extension
# instead of the comon ".lib"
if (MSVC)
find_file(LIBCLANG_LIBRARY libclang.imp
PATH_SUFFIXES LLVM/lib
DOC "The file that corresponds to the libclang library.")
endif()
find_library(LIBCLANG_LIBRARY NAMES libclang.imp libclang clang
PATHS ${libclang_llvm_lib_search_paths}
PATH_SUFFIXES LLVM/lib #Windows package from http://llvm.org/releases/
DOC "The file that corresponds to the libclang library.")
get_filename_component(LIBCLANG_LIBRARY_DIR ${LIBCLANG_LIBRARY} PATH)
set(LIBCLANG_LIBRARIES ${LIBCLANG_LIBRARY})
set(LIBCLANG_INCLUDE_DIRS ${LIBCLANG_INCLUDE_DIR})
include(FindPackageHandleStandardArgs)
# handle the QUIETLY and REQUIRED arguments and set LIBCLANG_FOUND to TRUE if
# all listed variables are TRUE
find_package_handle_standard_args(LibClang DEFAULT_MSG
LIBCLANG_LIBRARY LIBCLANG_INCLUDE_DIR)
mark_as_advanced(LIBCLANG_INCLUDE_DIR LIBCLANG_LIBRARY)

View File

@@ -0,0 +1,139 @@
include(CheckCXXCompilerFlag)
include(CheckCXXSourceCompiles)
#
# check_for_in_source_build()
#
function(check_for_in_source_build)
if (CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR AND NOT MSVC_IDE)
message(FATAL_ERROR "In-source builds are not allowed.
Please create a build/ directory and run cmake from there, passing
the path to this source directory as the last argument. This process
created the file `CMakeCache.txt' and the directory `CMakeFiles'.
Please delete them.
")
endif()
endfunction()
#
# release_as_default_build_type()
#
function(release_as_default_build_type)
# Set a default build type if none was specified for build systems with a
# unique configuration type (i.e: Make/Ninja builds)
if (NOT CMAKE_CONFIGURATION_TYPES AND NOT CMAKE_BUILD_TYPE)
message(STATUS "Setting build type to 'Release' as none was specified")
set (CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build." FORCE)
set_property (CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS
"Debug" "Release" "MinSizeRel" "RelWithDebInfo")
endif()
endfunction()
#
# add_compile_options_(<opts...>)
#
# Adds options to the compiler command line for sources in the current directory
# and below.
#
# note: add_compile_options() backport, which first appeared in CMake 2.8.12
function(add_compile_options_)
# possible to check with:
# if (${CMAKE_VERSION} VERSION_LESS "2.8.12")
if (COMMAND add_compile_options)
add_compile_options(${ARGN})
else()
add_definitions(${ARGN})
endif()
endfunction()
#
# enable_colored_diagnotics()
#
# Setup the flag to enable colored diagnostics if any.
#
# For now this option is enforced only for Ninja builds, where compiler output
# is redirected to pipes.
#
# Clang has '-fcolor-diagnostics' for a long time now. Since GCC 4.9, a similar
# flag has been added '-fdiagnostics-color' (somehow they managed to use another
# syntax than Clang's one...). Recent version of Clang will support both as they
# added support for GCC's -fdiagnostics-color.
#
function(enable_colored_diagnotics)
if (${CMAKE_GENERATOR} MATCHES "Ninja")
# Clang
check_cxx_compiler_flag("-fcolor-diagnostics" HAS_FCOLOR_DIAGNOSTICS_FLAG)
if (HAS_FCOLOR_DIAGNOSTICS_FLAG)
add_compile_options_(-fcolor-diagnostics)
else() # GCC (and Clang for compatibility with GCC)
check_cxx_compiler_flag("-fdiagnostics-color" HAS_DIAGNOSTICS_FCOLOR_FLAG)
if (HAS_DIAGNOSTICS_FCOLOR_FLAG)
add_compile_options_(-fdiagnostics-color)
endif()
endif()
endif()
endfunction()
#
# check_cxx11_options()
#
# Throws a FATAL_ERROR if C++11 isn't available otherwise sets:
# - CXX11_COMPILE_OPTIONS
# - CXX11_LINK_OPTIONS
#
function(check_cxx11_options)
if (CXX11_COMPILE_OPTIONS)
return() # already in cache
endif()
if (CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
check_cxx_compiler_flag("-std=c++11" HAS_STDCXX11)
if (HAS_STDCXX11)
set(compile_options -std=c++11)
else()
check_cxx_compiler_flag("-std=c++0x" HAS_STDCXX0X)
if (HAS_STDCXX0X)
set(compile_options -std=c++0x)
endif()
endif()
# Check whether or not the system library provides proper C++11 support, if
# not we try to link specifically against libc++.
#
# This seems useful for Mac OS X builds, see:
# http://cplusplusmusings.wordpress.com/2012/07/05/clang-and-standard-libraries-on-mac-os-x/
set(CMAKE_REQUIRED_FLAGS ${compile_options})
check_cxx_source_compiles("#include <random>
int main() {
std::random_device rd;
std::default_random_engine e(rd());
std::uniform_int_distribution<int> dist(0, 15);
return dist(e);
}
"
HAS_CXX11_STDLIB)
if (NOT HAS_CXX11_STDLIB)
check_cxx_compiler_flag("-stdlib=libc++" HAS_LIBCXX)
if (HAS_LIBCXX)
list(APPEND compile_options -stdlib=libc++)
list(APPEND link_options -stdlib=libc++)
else()
message(FATAL_ERROR "Standard library doesn't support C++11!")
endif()
endif()
endif()
if (compile_options)
message(STATUS "C++11 compiler option(s): ${compile_options}")
endif()
set(CXX11_COMPILE_OPTIONS ${compile_options} CACHE INTERNAL
"C++11 compile options, if any")
set(CXX11_LINK_OPTIONS ${link_options} CACHE INTERNAL
"C++11 link options, if any")
endfunction()