# Configure these CMake Variables from llvm-config executable.
# 
#    LLVM_LIBS
#    LLVM_DEFINITIONS
#    LLVM_FOUND
#    LLVM_VERSION
#    LLVM_INCLUDE_DIRS
#    LLVM_LIBRARY_DIRS

FUNCTION(loadLLVMConfig LLVM_CONFIG_EXE NEED_LLVM_LIB_LIST)
  set(NEED_LLVM_LIB ${${NEED_LLVM_LIB_LIST}})
  # Make sure there is a LLVM_CONFIG
  if(NOT DEFINED LLVM_CONFIG_EXE)
    message(FATAL_ERROR "LLVM_CONFIG should exists here. it is a bug."
			" please contact authors.")
  endif()


  execute_process(COMMAND ${LLVM_CONFIG_EXE} --includedir
		OUTPUT_VARIABLE LLVM_INCLUDE_DIRS
		OUTPUT_STRIP_TRAILING_WHITESPACE)

  execute_process(COMMAND ${LLVM_CONFIG_EXE} --libdir
		OUTPUT_VARIABLE LLVM_LIBRARY_DIRS
		OUTPUT_STRIP_TRAILING_WHITESPACE)

  execute_process(COMMAND ${LLVM_CONFIG_EXE} --version
		OUTPUT_VARIABLE LLVM_VERSION
		OUTPUT_STRIP_TRAILING_WHITESPACE)

  execute_process(COMMAND ${LLVM_CONFIG_EXE} --libs ${NEED_LLVM_LIB}
		OUTPUT_VARIABLE LLVM_LIBS
		OUTPUT_STRIP_TRAILING_WHITESPACE)

  STRING(REPLACE " -l" ";" LLVM_LIBS "${LLVM_LIBS}")
  STRING(REPLACE "-l" "" LLVM_LIBS "${LLVM_LIBS}")
  LIST(APPEND LLVM_LIBS "dl" "pthread" "ffi")

  SET(LLVM_DEFINITIONS "-D__STDC_LIMIT_MACROS" "-D__STDC_CONSTANT_MACROS")
  SET(LLVM_FOUND TRUE)

  set(LLVM_FOUND ${LLVM_FOUND} PARENT_SCOPE)
  set(LLVM_LIBS ${LLVM_LIBS} PARENT_SCOPE)
  set(LLVM_DEFINITIONS ${LLVM_DEFINITIONS} PARENT_SCOPE)
  set(LLVM_VERSION ${LLVM_VERSION} PARENT_SCOPE)
  set(LLVM_INCLUDE_DIRS ${LLVM_INCLUDE_DIRS} PARENT_SCOPE)
  set(LLVM_LIBRARY_DIRS ${LLVM_LIBRARY_DIRS} PARENT_SCOPE)
ENDFUNCTION(loadLLVMConfig)
