### emu2149
add_subdirectory (emu2149)
set (EMU2149_INCLUDE_DIRS ${EMU2149_INCLUDE_DIRS} PARENT_SCOPE)
set (EMU2149_COMPILE_OPTIONS ${EMU2149_CFLAGS_OTHER} PARENT_SCOPE)
set (EMU2149_LIBRARIES ${EMU2149_LIBRARIES} PARENT_SCOPE)
set (EMU2149_LIBRARY_DIRS ${EMU2149_LIBRARY_DIRS} PARENT_SCOPE)
set (EMU2149_LINK_OPTIONS ${EMU2149_LDFLAGS_OTHER} PARENT_SCOPE)
set (EMU2149_LDFLAGS_LEGACY ${EMU2149_LDFLAGS} PARENT_SCOPE)

include (FindPkgConfig OPTIONAL)

### RtAudio
option (SYSTEM_RTAUDIO "Use system-installed RtAudio instead of vendored one" OFF)

if (SYSTEM_RTAUDIO)
	if (PKG_CONFIG_FOUND)
		pkg_check_modules (RTAUDIO rtaudio>=5.1)
	endif (PKG_CONFIG_FOUND)

	if (NOT RTAUDIO_FOUND)
		find_library (RTAUDIO_MANUAL_LIBRARY rtaudio)
		find_path (RTAUDIO_MANUAL_INCLUDE_DIRS NAMES "RtAudio.h" PATH_SUFFIXES "include/rtaudio")

		if (RTAUDIO_MANUAL_LIBRARY AND RTAUDIO_MANUAL_INCLUDE_DIRS)
			include (CheckCXXSymbolExists REQUIRED)
			set (CMAKE_REQUIRED_INCLUDES ${RTAUDIO_MANUAL_INCLUDE_DIRS})
			set (CMAKE_REQUIRED_LIBRARIES ${RTAUDIO_MANUAL_LIBRARY})
			check_cxx_symbol_exists (RtAudio::getApiDisplayName "RtAudio.h" RTAUDIO_MANUAL_SUFFICIENT)

			if (RTAUDIO_MANUAL_SUFFICIENT)
				set (RTAUDIO_INCLUDE_DIRS ${RTAUDIO_MANUAL_INCLUDE_DIRS})
				set (RTAUDIO_LIBRARIES ${RTAUDIO_MANUAL_LIBRARY})
				set (RTAUDIO_LDFLAGS ${RTAUDIO_LIBRARIES})
			else()
				message (FATAL_ERROR
					"System-installed RtAudio requested but no compatible version found (symbol 'RtAudio::getApiDisplayName' missing)"
				)
			endif()
		else()
			message (FATAL_ERROR "System-installed RtAudio requested but not found")
		endif()
	endif (NOT RTAUDIO_FOUND)
else()
	add_subdirectory (RtAudio)
endif()
set (RTAUDIO_INCLUDE_DIRS ${RTAUDIO_INCLUDE_DIRS} PARENT_SCOPE)
set (RTAUDIO_COMPILE_OPTIONS ${RTAUDIO_CFLAGS_OTHER} PARENT_SCOPE)
set (RTAUDIO_LIBRARIES ${RTAUDIO_LIBRARIES} PARENT_SCOPE)
set (RTAUDIO_LIBRARY_DIRS ${RTAUDIO_LIBRARY_DIRS} PARENT_SCOPE)
set (RTAUDIO_LINK_OPTIONS ${RTAUDIO_LDFLAGS_OTHER} PARENT_SCOPE)
set (RTAUDIO_LDFLAGS_LEGACY ${RTAUDIO_LDFLAGS} PARENT_SCOPE)

### RtMidi
option(SYSTEM_RTMIDI "Use system-installed RtMidi instead of vendored one" OFF)

if (SYSTEM_RTMIDI)
	if (PKG_CONFIG_FOUND)
		pkg_check_modules (RTMIDI rtmidi>=4.0)
	endif (PKG_CONFIG_FOUND)

	if (NOT RTMIDI_FOUND)
		find_library (RTMIDI_MANUAL_LIBRARY rtmidi)
		find_path (RTMIDI_MANUAL_INCLUDE_DIRS NAMES "RtMidi.h" PATH_SUFFIXES "include/rtmidi")

		if (RTAUDIO_MANUAL_LIBRARY AND RTAUDIO_MANUAL_INCLUDE_DIRS)
			set (RTMIDI_INCLUDE_DIRS ${RTMIDI_MANUAL_INCLUDE_DIRS})
			set (RTMIDI_LIBRARIES ${RTMIDI_MANUAL_LIBRARY})
			set (RTMIDI_LDFLAGS ${RTMIDI_LIBRARIES})
		else()
			message (FATAL_ERROR "System-installed RtMidi requested but not found")
		endif()
	endif (NOT RTMIDI_FOUND)
else()
	add_subdirectory(RtMidi)
endif()
set (RTMIDI_INCLUDE_DIRS ${RTMIDI_INCLUDE_DIRS} PARENT_SCOPE)
set (RTMIDI_COMPILE_OPTIONS ${RTMIDI_CFLAGS_OTHER} PARENT_SCOPE)
set (RTMIDI_LIBRARIES ${RTMIDI_LIBRARIES} PARENT_SCOPE)
set (RTMIDI_LIBRARY_DIRS ${RTMIDI_LIBRARY_DIRS} PARENT_SCOPE)
set (RTMIDI_LINK_OPTIONS ${RTMIDI_LDFLAGS_OTHER} PARENT_SCOPE)
set (RTMIDI_LDFLAGS_LEGACY ${RTMIDI_LDFLAGS} PARENT_SCOPE)
