Skip to content

Commit

Permalink
Modifies FindLLVM.cmake to switch between versions in an easy way.
Browse files Browse the repository at this point in the history
Issue: #59
  • Loading branch information
kpp committed Feb 16, 2015
1 parent 95076fb commit 6359f61
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 34 deletions.
9 changes: 4 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ cmake_minimum_required(VERSION 2.8.4)

set (CMAKE_USER_MAKE_RULES_OVERRIDE "${CMAKE_SOURCE_DIR}/cmake/variables.cmake")
set (CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
set (LLVM_PACKAGE_VERSION 3.3)

project(llst-project)

find_package(Threads REQUIRED QUIET)
find_package(READLINE)
find_package(TINFO)
find_package(LLVM 3.3 EXACT)
find_package(LLVM ${LLVM_PACKAGE_VERSION} EXACT)
find_package(POD2MAN)
find_package(GZIP REQUIRED)

Expand All @@ -19,17 +20,15 @@ option(USE_POD2MAN "Should we use pod2man to build the documentation (we will cr
if (USE_LLVM)
if (LLVM_FOUND)
message(STATUS "Using LLVM ${LLVM_VERSION}")
set (CMAKE_C_FLAGS "${LLVM_C_FLAGS} ${CMAKE_C_FLAGS}")
set (CMAKE_CXX_FLAGS "${LLVM_CXX_FLAGS} ${CMAKE_CXX_FLAGS}")
set (CMAKE_EXE_LINKER_FLAGS "${LLVM_LD_FLAGS} ${CMAKE_EXE_LINKER_FLAGS}")

# LLVM generates loads of warnings...
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unused-parameter")

set (LLVM_LIBS_TO_LINK ${LLVM_LIBS})
add_definitions(-DLLVM)
add_definitions(-DLLVM ${LLVM_CPP_FLAGS})
else()
message(FATAL_ERROR "\nInstall llvm-3.3-dev:i386 and try again.")
message(FATAL_ERROR "\nInstall llvm-${LLVM_PACKAGE_VERSION}-dev:i386 and try again.")
endif()
else()
message(STATUS "LLVM is disabled")
Expand Down
114 changes: 85 additions & 29 deletions cmake/FindLLVM.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,40 @@
# Export variables:
# LLVM_FOUND
# LLVM_CONFIG_EXE
# LLVM_CPP_FLAGS
# LLVM_CXX_FLAGS
# LLVM_C_FLAGS
# LLVM_LD_FLAGS
# LLVM_INSTALL_PREFIX
# LLVM_VERSION
# LLVM_LIBS
# LLVM_LIBFILES
# LLVM_INCLUDE_DIR

include(CheckIncludeFileCXX)
include(CheckCXXSourceCompiles)
include(CMakePushCheckState)
include(FindPackageHandleStandardArgs)

if(LLVM_FOUND AND PREVIOUS_FOUND_VERSION EQUAL LLVM_FIND_VERSION)
# No need to find it again.
return()
else()
unset(LLVM_FOUND CACHE)
unset(LLVM_PREVIOUS_FOUND_VERSION CACHE)
unset(LLVM_CONFIG_EXE CACHE)
unset(LLVM_LIBS_INSTALLED CACHE)
unset(LLVM_HEADERS_INSTALLED CACHE)
unset(LLVM_COMPILED_AND_LINKED CACHE)
endif()

message(STATUS "Looking for LLVM ${LLVM_FIND_VERSION}")

macro(get_llvm_config_var args out_var)
if (NOT LLVM_CONFIG_EXE)
unset(${out_var})
return()
endif()
execute_process(
COMMAND ${LLVM_CONFIG_EXE} ${args}
OUTPUT_VARIABLE ${out_var}
Expand All @@ -31,50 +51,86 @@ macro(get_llvm_config_var args out_var)
endif()
endmacro()

macro(check_llvm_libs out_var)
if (${out_var})
return() # Result is cached
endif()

set (libs_exist YES)

string(REPLACE " " ";" LIBS_LIST ${LLVM_LIBFILES})
if (NOT LIBS_LIST)
set (libs_exist NOTFOUND)
endif()

foreach(lib ${LIBS_LIST})
if (NOT EXISTS ${lib})
message(STATUS "File ${lib} is missing")
set (libs_exist NOTFOUND)
break()
endif()
endforeach()

set (${out_var} "${libs_exist}" CACHE INTERNAL "")
endmacro()

macro(check_llvm_header header out_var)
cmake_push_check_state()
CMAKE_PUSH_CHECK_STATE()
set(CMAKE_REQUIRED_FLAGS "${LLVM_CXX_FLAGS}")
CHECK_INCLUDE_FILE_CXX("${header}" ${out_var})
cmake_pop_check_state()
CMAKE_POP_CHECK_STATE()
endmacro()

macro(check_llvm_source_compiles code out_var)
cmake_push_check_state()
set(CMAKE_REQUIRED_FLAGS "${LLVM_CXX_FLAGS} ${LLVM_LD_FLAGS}")
set(CMAKE_REQUIRED_LIBRARIES ${LLVM_LIBS} dl pthread)
CMAKE_PUSH_CHECK_STATE()
set(CMAKE_REQUIRED_FLAGS ${LLVM_CXX_FLAGS})
set(CMAKE_REQUIRED_LIBRARIES ${LLVM_LIBS} ${LLVM_LD_FLAGS})
CHECK_CXX_SOURCE_COMPILES("${code}" ${out_var})
cmake_pop_check_state()
CMAKE_POP_CHECK_STATE()
endmacro()

set(LLVM_CONFIG_NAMES "llvm-config-${LLVM_FIND_VERSION}" llvm-config)
foreach(version ${LLVM_ADDITIONAL_VERSIONS} 2.8 2.9 3.0 3.1 3.2 3.3 3.4 3.5)
list(APPEND LLVM_CONFIG_NAMES "llvm-config-${version}")
endforeach()
if (NOT LLVM_FIND_VERSION_EXACT)
foreach(version 3.1 3.2 3.3 3.4 3.5)
list(APPEND LLVM_CONFIG_NAMES "llvm-config-${version}")
endforeach()
endif()

find_program(LLVM_CONFIG_EXE NAMES ${LLVM_CONFIG_NAMES} DOC "Full path to llvm-config")
unset(LLVM_CONFIG_NAMES)

if (LLVM_CONFIG_EXE)
get_llvm_config_var(--cxxflags LLVM_CXX_FLAGS)
get_llvm_config_var(--cflags LLVM_C_FLAGS)
get_llvm_config_var(--ldflags LLVM_LD_FLAGS)
get_llvm_config_var(--prefix LLVM_INSTALL_PREFIX)
get_llvm_config_var(--version LLVM_VERSION)
get_llvm_config_var(--libs LLVM_LIBS)
get_llvm_config_var(--includedir LLVM_INCLUDE_DIR)

# Header 'Pass.h' locates in 'include/llvm/' directory since 1.9 till 3.4 release
check_llvm_header("llvm/Pass.h" LLVM_PASS_H)
check_llvm_source_compiles("#include <llvm/LinkAllPasses.h> \n int main(){ return 0; }" LLVM_PASSES_LINKED)

if (NOT LLVM_PASS_H OR NOT LLVM_PASSES_LINKED)
message(STATUS "See ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log for details")
endif()
mark_as_advanced(LLVM_CONFIG_EXE)
if (NOT LLVM_CONFIG_EXE)
message(STATUS "Could NOT find llvm-config (tried ${LLVM_CONFIG_NAMES})")
endif()

get_llvm_config_var(--cppflags LLVM_CPP_FLAGS)
get_llvm_config_var(--cxxflags LLVM_CXX_FLAGS)
get_llvm_config_var(--cflags LLVM_C_FLAGS)
get_llvm_config_var(--ldflags LLVM_LD_FLAGS)
get_llvm_config_var(--prefix LLVM_INSTALL_PREFIX)
get_llvm_config_var(--version LLVM_VERSION)
get_llvm_config_var(--libs LLVM_LIBS)
get_llvm_config_var(--libfiles LLVM_LIBFILES)
get_llvm_config_var(--includedir LLVM_INCLUDE_DIR)

check_llvm_libs(LLVM_LIBS_INSTALLED)
check_llvm_header("llvm/Support/TargetSelect.h" LLVM_HEADERS_INSTALLED)
check_llvm_source_compiles("#include <llvm/Support/TargetSelect.h> \n int main(){ return 0; }" LLVM_COMPILED_AND_LINKED)

if (LLVM_HEADERS_INSTALLED AND NOT LLVM_LIBS_INSTALLED)
message(STATUS "Only header files installed in the package")
elseif (LLVM_LIBS_INSTALLED AND NOT LLVM_HEADERS_INSTALLED)
message(STATUS "Libs installed while header files are missing")
elseif (LLVM_HEADERS_INSTALLED AND LLVM_LIBS_INSTALLED AND NOT LLVM_COMPILED_AND_LINKED)
message(STATUS "Libs and headers are installed, but during test compilation something went wrong. See ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log for details")
endif()

FIND_PACKAGE_HANDLE_STANDARD_ARGS( LLVM
REQUIRED_VARS LLVM_CONFIG_EXE LLVM_PASS_H LLVM_PASSES_LINKED
FOUND_VAR LLVM_FOUND
REQUIRED_VARS LLVM_CONFIG_EXE LLVM_LIBS_INSTALLED LLVM_HEADERS_INSTALLED LLVM_COMPILED_AND_LINKED
VERSION_VAR LLVM_VERSION
)

mark_as_advanced(LLVM_CONFIG_EXE)
set(LLVM_FOUND ${LLVM_FOUND} CACHE INTERNAL "LLVM_FOUND")
if (LLVM_FOUND)
set(PREVIOUS_FOUND_VERSION ${LLVM_VERSION} CACHE INTERNAL "The version of LLVM found by previous call find_package")
endif()

0 comments on commit 6359f61

Please sign in to comment.