MACRO(checkX265)
	IF(NOT X265_CHECKED)
		OPTION(X265 "" ON)

		MESSAGE(STATUS "Checking for x265")
		MESSAGE(STATUS "*****************")

		IF(X265)
			FIND_HEADER_AND_LIB(_X265 x265.h)
			FIND_HEADER_AND_LIB(_X265_CONFIG x265_config.h)

			IF(_X265_FOUND AND _X265_CONFIG_FOUND)
				FILE(READ ${_X265_INCLUDE_DIR}/x265_config.h X265_CONFIG_H)
				STRING(REGEX MATCH "#define[ ]+X265_BUILD[ ]+([0-9]+)" X265_CONFIG_H "${X265_CONFIG_H}")
				STRING(REGEX REPLACE ".*[ ]([0-9]+).*" "\\1" x265_version "${X265_CONFIG_H}")
				MESSAGE(STATUS "  core version: ${x265_version}")
				
				IF(x265_version LESS 9)
					MESSAGE("WARNING: x265 core version is too old.  At least version 9 is required.")
					SET(X265_FOUND 0)
				ELSE()
                                        IF(NOT WIN32)
                                                SET(DL dl)
                                        ELSE()
                                                IF(NOT MSVC)
                                                        SET(DL stdc++)
                                                ENDIF()
                                        ENDIF()
                                        IF(MSVC)
                                                SET(X265_CFLAGS "-DX265_API_IMPORTS")
                                                ADD_DEFINITIONS(${X265_CFLAGS})
                                        ENDIF()
					FIND_HEADER_AND_LIB(X265 x265.h x265 x265_encoder_open_${x265_version} ${DL})
				ENDIF()
			ELSE()
				MESSAGE(STATUS "  No working version found")
				SET(X265_FOUND 0)
			ENDIF()
                        IF(X265_FOUND)
				MESSAGE(STATUS "  Workable version found")
                                SET(USE_X265 True CACHE BOOL "")
                        ENDIF()

			PRINT_LIBRARY_INFO("x265" X265_FOUND "${X265_INCLUDE_DIR}" "${X265_LIBRARY_DIR}")
		ELSE()
			MESSAGE("${MSG_DISABLE_OPTION}")
		ENDIF()

		SET(X265_CHECKED 1)

		MESSAGE("")
	ENDIF()

	APPEND_SUMMARY_LIST("Video Encoder" "x265" "${USE_X265}")
ENDMACRO()
