Skip to content

Commit

Permalink
cmake: Look for clang at configure time
Browse files Browse the repository at this point in the history
  • Loading branch information
iamsergio committed Aug 31, 2015
1 parent a0e629f commit 03d0928
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 1 deletion.
6 changes: 5 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
cmake_minimum_required (VERSION 2.6)
project (ClangLazy)

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_SOURCE_DIR}/cmake)
find_package(Clang 3.6 MODULE REQUIRED)
include_directories(${CLANG_INCLUDE_DIRS})

set( CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin )
set( CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib )
set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib )

add_definitions (-D__STDC_LIMIT_MACROS -D__STDC_CONSTANT_MACROS)
add_definitions (-D_GNU_SOURCE -DHAVE_CLANG_CONFIG_H)

set (LLVM_INSTALL_PREFIX /data/prefix/)
set (LLVM_INSTALL_PREFIX ${CMAKE_INSTALL_PREFIX})

set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -fPIC -fno-common -Woverloaded-virtual -Wcast-qual -fno-strict-aliasing -pedantic -Wno-long-long -Wall -W -Wno-unused-parameter -Wwrite-strings -fno-exceptions -fno-rtti")
set (CMAKE_MODULE_LINKER_FLAGS "-Wl,-flat_namespace -Wl,-undefined -Wl,suppress")
Expand Down
108 changes: 108 additions & 0 deletions cmake/FindClang.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
# Detect Clang libraries
#
# Defines the following variables:
# CLANG_FOUND - True if Clang was found
# CLANG_INCLUDE_DIR - Where to find Clang includes
# CLANG_LIBRARY_DIR - Where to find Clang libraries
#
# CLANG_CLANG_LIB - LibClang library
# CLANG_CLANGFRONTEND_LIB - Clang Frontend Library
# CLANG_CLANGDRIVER_LIB - Clang Driver Library
# ...
#
# Uses the same include and library paths detected by FindLLVM.cmake
#
# See http://clang.llvm.org/docs/InternalsManual.html for full list of libraries

#=============================================================================
# Copyright 2014 Kevin Funk <[email protected]>
#
# Distributed under the OSI-approved BSD License (the "License");
# see accompanying file Copyright.txt for details.
#
# This software is distributed WITHOUT ANY WARRANTY; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the License for more information.

#=============================================================================

if(${Clang_FIND_REQUIRED})
set(LLVM_FIND_TYPE REQUIRED)
else()
set(LLVM_FIND_TYPE OPTIONAL)
endif()

find_package(LLVM CONFIG ${LLVM_FIND_TYPE} QUIET)
# if(NOT LLVM_FOUND)
# find_package(LLVM 3.4 CONFIG ${LLVM_FIND_TYPE} QUIET)
# endif()
# if(NOT LLVM_FOUND)
# find_package(LLVM 3.5 CONFIG ${LLVM_FIND_TYPE} QUIET)
# endif()
# if(NOT LLVM_FOUND)
# find_package(LLVM 3.6 CONFIG ${LLVM_FIND_TYPE} QUIET)
# endif()

set(Clang_FOUND FALSE)

if(NOT TARGET llvm-config)
message(STATUS "LLVM was not found -> cannot find clang")
return()
endif()

set(CLANG_LIBRARY_DIR ${LLVM_LIBRARY_DIRS})
set(CLANG_INCLUDE_DIR ${LLVM_INCLUDE_DIRS})

macro(FIND_AND_ADD_CLANG_LIB _libname_)
string(TOUPPER ${_libname_} _prettylibname_)
find_library(CLANG_${_prettylibname_}_LIB NAMES "clang${_libname_}" HINTS ${LLVM_LIBRARY_DIRS})
if(CLANG_${_prettylibname_}_LIB)
add_library(Clang::${_libname_} UNKNOWN IMPORTED)
set_property(TARGET Clang::${_libname_} PROPERTY IMPORTED_LOCATION "${CLANG_${_prettylibname_}_LIB}")
set_property(TARGET Clang::${_libname_} PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${CLANG_INCLUDE_DIR})

set(CLANG_LIBS ${CLANG_LIBS} ${CLANG_${_prettylibname_}_LIB})
endif()
endmacro(FIND_AND_ADD_CLANG_LIB)

FIND_AND_ADD_CLANG_LIB(Frontend)
FIND_AND_ADD_CLANG_LIB(Driver)
FIND_AND_ADD_CLANG_LIB(CodeGen)
FIND_AND_ADD_CLANG_LIB(Sema)
FIND_AND_ADD_CLANG_LIB(Checker)
FIND_AND_ADD_CLANG_LIB(Analysis)
FIND_AND_ADD_CLANG_LIB(Rewrite)
FIND_AND_ADD_CLANG_LIB(AST)
FIND_AND_ADD_CLANG_LIB(Parse)
FIND_AND_ADD_CLANG_LIB(Lex)
FIND_AND_ADD_CLANG_LIB(Basic)
FIND_AND_ADD_CLANG_LIB(ARCMigrate)
FIND_AND_ADD_CLANG_LIB(Edit)
FIND_AND_ADD_CLANG_LIB(FrontendTool)
FIND_AND_ADD_CLANG_LIB(Serialization)
FIND_AND_ADD_CLANG_LIB(Tooling)
FIND_AND_ADD_CLANG_LIB(ToolingCore)
FIND_AND_ADD_CLANG_LIB(StaticAnalyzerCheckers)
FIND_AND_ADD_CLANG_LIB(StaticAnalyzerCore)
FIND_AND_ADD_CLANG_LIB(StaticAnalyzerFrontend)
FIND_AND_ADD_CLANG_LIB(RewriteCore)
FIND_AND_ADD_CLANG_LIB(ASTMatchers)

if(CLANG_LIBS)
set(Clang_FOUND TRUE)
set(Clang_VERSION ${LLVM_VERSION})

else()
message(STATUS "Could not find any Clang libraries in ${LLVM_LIBRARY_DIR}")
endif()

if(Clang_FOUND)
message(STATUS "Found Clang (LLVM version: ${LLVM_VERSION})")
message(STATUS " Include dirs: ${CLANG_INCLUDE_DIR}")
message(STATUS " Library dir: ${CLANG_LIBRARY_DIR}")
message(STATUS " Libraries: ${CLANG_LIBS}")
else()
if(Clang_FIND_REQUIRED)
message(FATAL_ERROR "Could NOT find Clang")
endif()
endif()

0 comments on commit 03d0928

Please sign in to comment.