find_program(LLVM_CONFIG_EXECUTABLE llvm-config)

if(NOT LLVM_CONFIG_EXECUTABLE AND LLVM_FIND_REQUIRED)
	message(FATAL_ERROR "Could NOT find llvm-config executable")
endif()

if(NOT LLVM_FIND_QUIETLY)
	message(STATUS "Found llvm-config at: ${LLVM_CONFIG_EXECUTABLE}")
endif()

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

if(NOT LLVM_FIND_QUIETLY)
	message(STATUS "LLVM version: ${LLVM_VERSION}")
endif()

string(REGEX REPLACE "^([0-9]+)\\.([0-9]+).*" "\\1" LLVM_VERSION_MAJOR ${LLVM_VERSION})
string(REGEX REPLACE "^([0-9]+)\\.([0-9]+).*" "\\2" LLVM_VERSION_MINOR ${LLVM_VERSION})

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

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

execute_process(COMMAND ${LLVM_CONFIG_EXECUTABLE} --cppflags
	OUTPUT_VARIABLE LLVM_CPPFLAGS OUTPUT_STRIP_TRAILING_WHITESPACE
)

execute_process(COMMAND ${LLVM_CONFIG_EXECUTABLE} --cflags
	OUTPUT_VARIABLE LLVM_CFLAGS OUTPUT_STRIP_TRAILING_WHITESPACE
)

execute_process(COMMAND ${LLVM_CONFIG_EXECUTABLE} --cxxflags
	OUTPUT_VARIABLE LLVM_CXXFLAGS OUTPUT_STRIP_TRAILING_WHITESPACE
)

execute_process(COMMAND ${LLVM_CONFIG_EXECUTABLE} --ldflags
	OUTPUT_VARIABLE LLVM_LDFLAGS OUTPUT_STRIP_TRAILING_WHITESPACE
)

execute_process(COMMAND ${LLVM_CONFIG_EXECUTABLE} --libs ${LLVM_FIND_COMPONENTS}
	OUTPUT_VARIABLE LLVM_LIBS OUTPUT_STRIP_TRAILING_WHITESPACE
)

set(LLVM_FOUND TRUE)
