####
# Find Clang compiler and tools to use
####
if(LLVMLIB_FRONTEND AND NOT CROSS_COMPILE AND NOT SPIRV_CLANG_FOUND)
	if(NOT CLANG_FOUND)
		find_program(CLANG_FOUND clang NAMES clang clang-3.9 clang-4.0 clang-5.0 clang-6.0 clang-7 clang-8 clang-9 clang-10 clang-11)
	endif()
	if (NOT OPT_FOUND)
		find_program(OPT_FOUND opt NAMES opt opt-3.9 opt-4.0 opt-5.0 opt-6.0 opt-7 opt-8 opt-9 opt-10 opt-11)
	endif()
	if (NOT LLVM_DIS_FOUND)
		find_program(LLVM_DIS_FOUND llvm-dis NAMES llvm-dis llvm-dis-3.9 llvm-dis-4.0 llvm-dis-5.0 llvm-dis-6.0 llvm-dis-7 llvm-dis-8 llvm-dis-9 llvm-dis-10 llvm-dis-11)
	endif()
	if (NOT LLVM_AS_FOUND)
		find_program(LLVM_AS_FOUND llvm-as NAMES llvm-as llvm-as-3.9 llvm-as-4.0 llvm-as-5.0 llvm-as-6.0 llvm-as-7 llvm-as-8 llvm-as-9 llvm-as-10 llvm-as-11)
	endif()
	if(NOT LLVM_LINK_FOUND)
		find_program(LLVM_LINK_FOUND llvm-link NAMES llvm-link llvm-link-3.9 llvm-link-4.0 llvm-link-5.0 llvm-link-6.0 llvm-link-7 llvm-link-8 llvm-link-9 llvm-link-10 llvm-link-11)
	endif()
	if(CLANG_FOUND)
		message(STATUS "CLang compiler found: " ${CLANG_FOUND})
		EXECUTE_PROCESS( COMMAND ${CLANG_FOUND} --version OUTPUT_VARIABLE clang_full_version_string )
		string (REGEX REPLACE ".*clang version ([0-9]+\\.[0-9]+).*" "\\1" CLANG_VERSION_STRING ${clang_full_version_string})
	else()
		message(WARNING "No CLang compiler found!")
	endif()
	if(OPT_FOUND)
		message(STATUS "Clang opt found: " ${OPT_FOUND})
	endif()
	if(LLVM_DIS_FOUND)
		message(STATUS "LLVM-dis found: " ${LLVM_DIS_FOUND})
	endif()
	if(LLVM_AS_FOUND)
		message(STATUS "LLVM-as found: " ${LLVM_AS_FOUND})
	endif()
	if(LLVM_LINK_FOUND)
		message(STATUS "LLVM-link found: " ${LLVM_LINK_FOUND})
	endif()
endif()

####
# Find libClang when front-end is enabled
####
if(CLANG_LIBRARY)
	if(NOT LIBCLANG_LIBRARIES)
		find_library(LIBCLANG_LIBRARY_FRONTEND NAMES clangFrontend libclangFrontend)
		find_library(LIBCLANG_LIBRARY_CODEGEN NAMES clangCodeGen libclangCodeGen)

		if(LIBCLANG_LIBRARY_FRONTEND AND LIBCLANG_LIBRARY_CODEGEN)
			set(LIBCLANG_LIBRARIES ${LIBCLANG_LIBRARY_FRONTEND} ${LIBCLANG_LIBRARY_CODEGEN})
		else()
			# Try shared libclang-cpp.so
			find_library(LIBCLANG_LIBRARIES NAMES clang-cpp libclang-cpp)
		endif()
	endif()
	if(NOT LIBCLANG_INCLUDE_PATH)
		find_path(LIBCLANG_INCLUDE_PATH clang-c/Index.h)
	endif()
	if(LIBCLANG_LIBRARIES AND LIBCLANG_INCLUDE_PATH)
		message(STATUS "Using LibClang libraries: ${LIBCLANG_LIBRARIES}")
		message(STATUS "Using LibClang headers: ${LIBCLANG_INCLUDE_PATH}")
		set(VC4C_ENABLE_LIBCLANG ON)
	else()
		message(WARNING "LibClang enabled, but no libClang libraries were found")
		set(VC4C_ENABLE_LIBCLANG OFF)
	endif()
endif()