# From llvm-lua
# - Find libev
# Find the native LLVM includes and library
#
#  LLVM_INCLUDE_DIR - where to find ev.h, etc.
#  LLVM_FOUND       - True if libev found.

IF (LLVM_INCLUDE_DIR)
  SET(LLVM_FOUND TRUE)
ENDIF (LLVM_INCLUDE_DIR)

FIND_PROGRAM(LLVM_CONFIG_EXECUTABLE NAMES llvm-config DOC "llvm-config executable")
MARK_AS_ADVANCED(LLVM_CONFIG_EXECUTABLE)

FIND_PROGRAM(LLVM_CLANG_EXECUTABLE NAMES clang DOC "clang executable")
MARK_AS_ADVANCED(LLVM_CLANG_EXECUTABLE)

if (LLVM_CLANG_EXECUTABLE)
    MESSAGE(STATUS "Found clang at: ${LLVM_CLANG_EXECUTABLE}")
endif(LLVM_CLANG_EXECUTABLE)

IF (LLVM_CONFIG_EXECUTABLE)
  SET (LLVM_FOUND TRUE)
ELSE (LLVM_CONFIG_EXECUTABLE)
  SET (LLVM_FOUND FALSE)
ENDIF (LLVM_CONFIG_EXECUTABLE)

IF(LLVM_FOUND)
  EXECUTE_PROCESS(
    COMMAND ${LLVM_CONFIG_EXECUTABLE} --cppflags
    OUTPUT_VARIABLE LLVM_CFLAGS
    OUTPUT_STRIP_TRAILING_WHITESPACE
    )

  EXECUTE_PROCESS(
    COMMAND ${LLVM_CONFIG_EXECUTABLE} --ldflags
    OUTPUT_VARIABLE LLVM_LFLAGS
    OUTPUT_STRIP_TRAILING_WHITESPACE
    )

  EXECUTE_PROCESS(
    COMMAND ${LLVM_CONFIG_EXECUTABLE} --libs core ipo bitwriter bitreader asmparser scalaropts transformutils
    OUTPUT_VARIABLE LLVM_LIBS
    OUTPUT_STRIP_TRAILING_WHITESPACE
    )

  EXECUTE_PROCESS(
    COMMAND ${LLVM_CONFIG_EXECUTABLE} --includedir
    OUTPUT_VARIABLE LLVM_INCLUDE_DIR
    OUTPUT_STRIP_TRAILING_WHITESPACE
    )

  EXECUTE_PROCESS(
    COMMAND ${LLVM_CONFIG_EXECUTABLE} --libdir
    OUTPUT_VARIABLE LLVM_LIBRARIES
    OUTPUT_STRIP_TRAILING_WHITESPACE
    )

  GET_FILENAME_COMPONENT(LLVM_BINARY_DIR ${LLVM_CONFIG_EXECUTABLE} PATH)

  IF (NOT LLVM_FIND_QUIETLY)
    MESSAGE(STATUS "Found LLVM: ${LLVM_LIBRARIES}")
  ENDIF (NOT LLVM_FIND_QUIETLY)

ELSE(LLVM_FOUND)
  IF (LLVM_FIND_REQUIRED)
      MESSAGE(FATAL_ERROR "Could not find LLVM!")
  ENDIF(LLVM_FIND_REQUIRED)
ENDIF(LLVM_FOUND)