ifeq ($(HAVE_GL_CONTEXT),)
   HAVE_GL_CONTEXT=0

   ifeq ($(HAVE_OPENGL), 1)
      HAVE_GL_CONTEXT=1
   endif

   ifeq ($(HAVE_GLES), 1)
      HAVE_GL_CONTEXT=1
   endif

   ifeq ($(HAVE_GLES3), 1)
      HAVE_GL_CONTEXT=1
   endif
endif

ifeq ($(HAVE_LIBRETRODB),)
   HAVE_LIBRETRODB = 1
endif

ifeq ($(HAVE_LIBRETRODB), 1)
	DEFINES += -DHAVE_LIBRETRODB
endif

ifeq ($(HAVE_FBO), 1)
   DEFINES += -DHAVE_FBO
endif

ifeq ($(HAVE_DYLIB), 1)
   DEFINES += -DHAVE_DYLIB
endif

ifeq ($(SCALER_NO_SIMD), 1)
   DEFINES += -DSCALER_NO_SIMD
endif


ifeq ($(HAVE_PRESERVE_DYLIB),1)
   DEFINES += -DNO_DLCLOSE
endif

ifeq ($(GL_DEBUG), 1)
   CFLAGS += -DGL_DEBUG
   CXXFLAGS += -DGL_DEBUG
endif

ifeq ($(HAVE_HARD_FLOAT), 1)
   DEFINES += -mfloat-abi=hard
endif

ifeq ($(TDM_GCC),)
   LDCXXFLAGS += -static-libstdc++
endif

ifeq ($(HAVE_FILE_LOGGER), 1)
   CFLAGS += -DHAVE_FILE_LOGGER
endif

CFLAGS += -I./libretro-common/include

# Switches


# System

ifneq ($(findstring BSD,$(OS)),)
   BSD_LOCAL_INC += -I/usr/local/include
endif

ifneq ($(findstring Darwin,$(OS)),)
   OSX := 1
   LIBS += -framework AppKit
else
   OSX := 0
endif

ifneq ($(findstring Linux,$(OS)),)
   LIBS += -lrt
   OBJ += input/drivers/linuxraw_input.o \
			 input/common/linux_common.o \
          input/drivers_joypad/linuxraw_joypad.o \
          frontend/drivers/platform_linux.o
endif

ifeq ($(findstring Haiku,$(OS)),)
   LIBS += -lm
   DEBUG_FLAG = -g
else
   LIBS += -lroot -lnetwork
   # stable and nightly haiku builds are stuck on gdb 6.x but we use gcc4
   DEBUG_FLAG = -gdwarf-2
endif

# Git

GIT_VERSION := $(shell git rev-parse --short HEAD 2>/dev/null)
ifneq ($(GIT_VERSION),)
   DEFINES += -DHAVE_GIT_VERSION -DGIT_VERSION=$(GIT_VERSION)
   OBJ += git_version.o
endif

# General object files

OBJ += frontend/frontend.o \
       frontend/frontend_driver.o \
       frontend/drivers/platform_null.o \
       ui/ui_companion_driver.o \
       ui/drivers/ui_null.o \
       libretro_version_1.o \
       retroarch.o \
		 input/input_keyboard.o \
       system.o \
       command_event.o \
       msg_hash.o \
       intl/msg_hash_de.o \
       intl/msg_hash_eo.o \
       intl/msg_hash_es.o \
       intl/msg_hash_fr.o \
       intl/msg_hash_it.o \
       intl/msg_hash_nl.o \
       intl/msg_hash_pl.o \
       intl/msg_hash_pt.o \
       intl/msg_hash_us.o \
       runloop.o \
       runloop_data.o \
       tasks/tasks.o \
       tasks/task_file_transfer.o \
       content.o \
		 libretro-common/encodings/encoding_utf.o \
       libretro-common/file/file_list.o \
       libretro-common/file/dir_list.o \
       libretro-common/file/retro_dirent.o \
       libretro-common/file/retro_file.o \
       libretro-common/file/retro_stat.o \
       libretro-common/string/string_list.o \
       libretro-common/string/stdstring.o \
       libretro-common/memmap/memalign.o \
       dir_list_special.o \
       string_list_special.o \
       file_ops.o \
       libretro-common/file/nbio/nbio_stdio.o \
       libretro-common/file/file_path.o \
       file_path_special.o \
       libretro-common/hash/rhash.o \
       audio/audio_driver.o \
       input/input_driver.o \
       input/input_hid_driver.o \
       gfx/video_common.o \
       gfx/video_driver.o \
       camera/camera_driver.o \
       location/location_driver.o \
       driver.o \
       configuration.o \
       libretro-common/dynamic/dylib.o \
       dynamic.o \
       cores/dynamic_dummy.o \
       libretro-common/queues/message_queue.o \
       rewind.o \
       gfx/drivers_font_renderer/bitmapfont.o \
       input/input_autodetect.o \
       input/input_joypad_driver.o \
       input/input_config.o \
       input/input_keymaps.o \
       input/input_remapping.o \
       tasks/task_overlay.o \
       input/input_overlay.o \
       patch.o \
       libretro-common/queues/fifo_buffer.o \
       core_options.o \
       libretro-common/compat/compat_fnmatch.o \
		 libretro-common/compat/compat_posix_string.o \
       cheats.o \
       core_info.o \
       libretro-common/file/config_file.o \
       libretro-common/file/config_file_userdata.o \
       screenshot.o \
       libretro-common/gfx/scaler/scaler.o \
       gfx/drivers_shader/shader_null.o \
       gfx/video_shader_driver.o \
       gfx/video_shader_parse.o \
       libretro-common/gfx/scaler/pixconv.o \
       libretro-common/gfx/scaler/scaler_int.o \
       libretro-common/gfx/scaler/scaler_filter.o \
       gfx/font_driver.o \
       gfx/font_renderer_driver.o \
       gfx/video_filter.o \
       audio/audio_resampler_driver.o \
       audio/audio_dsp_filter.o \
       audio/drivers_resampler/sinc.o \
       audio/drivers_resampler/nearest.o \
       audio/drivers_resampler/cc_resampler.o \
       location/drivers/nulllocation.o \
       camera/drivers/nullcamera.o \
       gfx/drivers/nullgfx.o \
       audio/drivers/nullaudio.o \
       input/drivers/nullinput.o \
       input/drivers_hid/null_hid.o \
       input/drivers_joypad/null_joypad.o \
       input/drivers_joypad/hid_joypad.o \
       playlist.o \
       movie.o \
       record/record_driver.o \
       record/drivers/record_null.o \
       performance.o \
		 verbosity.o

ifneq ($(HAVE_GETOPT_LONG), 1)
OBJ += libretro-common/compat/compat_getopt.o
endif

ifneq ($(HAVE_STRCASESTR), 1)
OBJ += libretro-common/compat/compat_strcasestr.o
endif

ifneq ($(HAVE_STRL), 1)
OBJ += libretro-common/compat/compat_strl.o
endif

OBJ += gfx/image/image.o

ifeq ($(HAVE_IMAGEVIEWER), 1)
DEFINES += -DHAVE_IMAGEVIEWER
OBJ += cores/image_core.o
endif

# Qt

ifeq ($(HAVE_QT), 1)
OBJ += ui/drivers/ui_qt.o
# TODO/FIXME - figure out which libraries we need to link against
LIBS += -lQt5Quick -lQt5Widgets -lQt5Gui -lQt5Qml -lQt5Network -lQt5Core -lglu32 -lopengl32 -L./ui/drivers/qt/wrapper/build/release -lwrapper
endif

# LibretroDB

ifeq ($(HAVE_LIBRETRODB), 1)
OBJ += libretro-db/bintree.o \
       libretro-db/libretrodb.o \
       libretro-db/query.o \
       libretro-db/rmsgpack.o \
       libretro-db/rmsgpack_dom.o \
       database_info.o \
       tasks/task_database.o \
       tasks/task_database_cue.o
endif

# Miscellaneous

ifeq ($(HAVE_STDIN_CMD), 1)
   DEFINES += -DHAVE_COMMAND -DHAVE_STDIN_CMD
endif

ifeq ($(HAVE_PYTHON), 1)
   DEFINES += $(PYTHON_CFLAGS) -Wno-unused-parameter
   LIBS += $(PYTHON_LIBS)
   OBJ += gfx/video_state_python.o
endif

ifeq ($(HAVE_EMSCRIPTEN), 1)
OBJ += frontend/drivers/platform_emscripten.o \
       input/drivers/rwebinput_input.o \
       audio/drivers/rwebaudio.o \
       camera/drivers/rwebcam.o
endif

# Audio
#
ifeq ($(HAVE_COREAUDIO), 1)
   OBJ += audio/drivers/coreaudio.o
   LIBS += -framework CoreServices -framework CoreAudio -framework AudioUnit
endif

ifeq ($(HAVE_CORETEXT), 1)
   OBJ += gfx/drivers_font_renderer/coretext.o
endif

ifeq ($(HAVE_OSS), 1)
   OBJ += audio/drivers/oss.o
endif

ifeq ($(HAVE_OSS_BSD), 1)
   OBJ += audio/drivers/oss.o
endif

ifeq ($(HAVE_ALSA), 1)
   OBJ += audio/drivers/alsa.o \
			 audio/drivers/alsathread.o
   LIBS += $(ALSA_LIBS)
   DEFINES += $(ALSA_CFLAGS)
endif

ifeq ($(HAVE_ROAR), 1)
   OBJ += audio/drivers/roar.o
   LIBS += $(ROAR_LIBS)
   DEFINES += $(ROAR_CFLAGS)
endif

ifeq ($(HAVE_AL), 1)
   OBJ += audio/drivers/openal.o
   ifeq ($(OSX),1)
      LIBS += -framework OpenAL
   else
      LIBS += -lopenal
   endif
endif

ifneq ($(findstring Darwin,$(OS)),)
   OBJ += frontend/drivers/platform_darwin.o
endif

ifeq ($(HAVE_JACK),1)
   OBJ += audio/drivers/jack.o
   LIBS += $(JACK_LIBS)
   DEFINES += $(JACK_CFLAGS)
endif

ifeq ($(HAVE_PULSE), 1)
   OBJ += audio/drivers/pulse.o
   LIBS += $(PULSE_LIBS)
   DEFINES += $(PULSE_CFLAGS)
endif

ifeq ($(HAVE_OSS_LIB), 1)
   LIBS += -lossaudio
endif

ifeq ($(HAVE_RSOUND), 1)
   OBJ += audio/drivers/rsound.o
   DEFINES += $(RSOUND_CFLAGS)
   LIBS += $(RSOUND_LIBS)
endif

ifeq ($(HAVE_DSOUND), 1)
   OBJ += audio/drivers/dsound.o
   DEFINES += -DHAVE_DSOUND
   LIBS += -ldxguid -ldsound
endif

ifeq ($(HAVE_XAUDIO), 1)
   OBJ += audio/drivers/xaudio.o
   DEFINES += -DHAVE_XAUDIO
   LIBS += -lole32
endif

# Audio Resamplers

ifeq ($(HAVE_NEON),1)
   OBJ += audio/drivers_resampler/sinc_neon.o \
          audio/drivers_resampler/cc_resampler_neon.o
   # When compiled without this, tries to attempt to compile sinc lerp,
   # which will error out
	#
	# TODO - we need to make quality levels of sinc
	# runtime toggleable instead of going by compile-time
	# defines
   DEFINES += -DSINC_LOWER_QUALITY
endif

OBJ += audio/audio_utils.o
ifeq ($(HAVE_NEON),1)
   OBJ += audio/audio_utils_neon.o
endif

# XMB and MaterialUI are always enabled if supported and not explicitly disabled
ifeq ($(HAVE_RGUI)$(HAVE_GL_CONTEXT), 11)
   ifeq ($(HAVE_ZARCH),)
      HAVE_ZARCH = 1
   endif

   ifeq ($(HAVE_MATERIALUI),)
      HAVE_MATERIALUI = 1
   endif

   ifeq ($(HAVE_XMB),)
      HAVE_XMB = 1
   endif
else
   HAVE_ZARCH = 0
   HAVE_MATERIALUI  = 0
   HAVE_XMB   = 0
endif

ifeq ($(HAVE_RGUI), 1)
   OBJ += menu/drivers/rgui.o
   DEFINES += -DHAVE_MENU -DHAVE_RGUI
   HAVE_MENU_COMMON = 1
ifeq ($(HAVE_MATERIALUI), 1)
   OBJ += menu/drivers/materialui.o
   DEFINES += -DHAVE_MATERIALUI
endif
ifeq ($(HAVE_ZARCH), 1)
   OBJ += menu/drivers/zarch.o
   DEFINES += -DHAVE_ZARCH
endif
ifeq ($(HAVE_XMB), 1)
   OBJ += menu/drivers/xmb.o
   DEFINES += -DHAVE_XMB
endif
endif

ifeq ($(HAVE_LAKKA), 1)
   DEFINES += -DHAVE_LAKKA
endif

ifeq ($(HAVE_MENU_COMMON), 1)
   ifeq ($(PSEUDOLOC), 1)
      PSEUDO_NS := pseudo
   else
      PSEUDO_NS :=
   endif
   OBJ += menu/menu.o \
          menu/menu_hash.o \
			 menu/menu_driver.o \
          menu/intl/menu_hash_de.o \
          menu/intl/menu_hash_es.o \
          menu/intl/menu_hash_eo.o \
          menu/intl/menu_hash_fr.o \
          menu/intl/menu_hash_it.o \
          menu/intl/menu_hash_nl.o \
          menu/intl/menu_hash_pl.o \
          menu/intl/menu_hash_pt.o \
          menu/intl/menu_hash_us$(PSEUDO_NS).o \
          menu/menu_input.o \
          menu/menu_entry.o \
          menu/menu_entries.o \
          menu/menu_navigation.o  \
          menu/menu_setting.o \
          menu/menu_shader.o \
          menu/menu_cbs.o \
          menu/cbs/menu_cbs_ok.o \
          menu/cbs/menu_cbs_cancel.o \
          menu/cbs/menu_cbs_select.o \
          menu/cbs/menu_cbs_start.o \
          menu/cbs/menu_cbs_info.o \
          menu/cbs/menu_cbs_refresh.o \
          menu/cbs/menu_cbs_left.o \
          menu/cbs/menu_cbs_right.o \
          menu/cbs/menu_cbs_deferred_push.o \
          menu/cbs/menu_cbs_scan.o \
          menu/cbs/menu_cbs_get_value.o \
          menu/cbs/menu_cbs_title.o \
          menu/cbs/menu_cbs_up.o \
          menu/cbs/menu_cbs_down.o \
          menu/cbs/menu_cbs_contentlist_switch.o \
          menu/menu_display.o \
          menu/menu_displaylist.o \
          menu/menu_animation.o \
          menu/drivers_display/menu_display_null.o \
          menu/drivers/menu_generic.o \
          menu/drivers/null.o
endif

ifeq ($(UTF8), 1)
   DEFINES += -DHAVE_UTF8
endif

ifeq ($(HAVE_STB_FONT), 1)
   OBJ += gfx/drivers_font_renderer/stb.o
endif

ifeq ($(HAVE_FREETYPE), 1)
   OBJ += gfx/drivers_font_renderer/freetype.o
   LIBS += $(FREETYPE_LIBS)
   DEFINES += $(FREETYPE_CFLAGS)
endif

ifeq ($(HAVE_THREADS), 1)
   OBJ += autosave.o \
			 libretro-common/rthreads/rthreads.o \
			 libretro-common/rthreads/rsemaphore.o  \
			 libretro-common/rthreads/async_job.o \
			 gfx/video_thread_wrapper.o \
			 audio/audio_thread_wrapper.o
   DEFINES += -DHAVE_THREADS
   ifeq ($(findstring Haiku,$(OS)),)
      LIBS += -lpthread
   endif
endif

ifeq ($(HAVE_COMMAND), 1)
   OBJ += command.o
endif

ifeq ($(HAVE_NETWORK_GAMEPAD), 1)
   OBJ += remote.o
endif

#Input

ifeq ($(HAVE_WAYLAND), 1)
 #OBJ += input/drivers/wayland.o
 DEFINES += $(WAYLAND_CFLAGS)
 LIBS += $(WAYLAND_LIBS)
endif

ifeq ($(HAVE_DINPUT), 1)
   LIBS += -ldinput8 -ldxguid -lole32
   DEFINES += -DHAVE_DINPUT
   OBJ += input/drivers/dinput.o \
          input/drivers_joypad/dinput_joypad.o
endif

ifeq ($(HAVE_XINPUT), 1)
   DEFINES += -DHAVE_XINPUT -DHAVE_BUILTIN_AUTOCONFIG
   OBJ += input/drivers_joypad/xinput_joypad.o \
          input/autoconf/builtin_win.o
endif

ifeq ($(HAVE_X11), 1)
   OBJ += input/common/input_x11_common.o \
			 input/drivers/x11_input.o \
          gfx/common/x11_common.o \
          input/drivers_keyboard/keyboard_event_x11.o

   LIBS += $(X11_LIBS) $(XEXT_LIBS) $(XF86VM_LIBS) $(XINERAMA_LIBS)
   DEFINES += $(X11_CFLAGS) $(XEXT_CFLAGS) $(XF86VM_CFLAGS) $(XINERAMA_CFLAGS)
endif

ifeq ($(HAVE_XKBCOMMON), 1)
   DEFINES += $(XKBCOMMON_CFLAGS)
	OBJ += input/drivers_keyboard/keyboard_event_xkb.o
   LIBS += $(XKBCOMMON_LIBS)
endif

ifeq ($(HAVE_UDEV), 1)
   DEFINES += $(UDEV_CFLAGS)
   LIBS += $(UDEV_LIBS)
   OBJ += input/drivers/udev_input.o \
			 input/drivers_keyboard/keyboard_event_udev.o \
          input/drivers_joypad/udev_joypad.o
endif

ifeq ($(HAVE_LIBUSB), 1)
	DEFINES += -DHAVE_LIBUSB
	OBJ += input/drivers_hid/libusb_hid.o
	LIBS += -lusb-1.0
	HAVE_HID = 1
endif

ifeq ($(HAVE_IOHIDMANAGER), 1)
	DEFINES += -DHAVE_IOHIDMANAGER
	OBJ += input/drivers_hid/iohidmanager_hid.o
	HAVE_HID = 1
	LIBS += -framework IOKit
endif

ifeq ($(HAVE_CORELOCATION), 1)
	DEFINES += -DHAVE_CORELOCATION
	LIBS += -framework CoreLocation
endif

ifeq ($(HAVE_HID), 1)
	DEFINES += -DHAVE_HID
	OBJ += input/connect/joypad_connection.o \
               input/connect/connect_ps3.o \
               input/connect/connect_ps4.o \
               input/connect/connect_wii.o
endif

ifeq ($(HAVE_PARPORT), 1)
   OBJ += input/drivers_joypad/parport_joypad.o
endif

# Companion UI

ifneq ($(findstring Win32,$(OS)),)
OBJ += ui/drivers/ui_win32.o
endif

# Video

OBJ += gfx/video_context_driver.o \
       gfx/drivers_context/gfx_null_ctx.o \
       gfx/video_state_tracker.o \
		 libretro-common/gfx/math/matrix_4x4.o \
		 libretro-common/gfx/math/matrix_3x3.o

ifneq ($(findstring Win32,$(OS)),)
OBJ += gfx/video_texture.o
else
OBJ += gfx/video_texture_c.o
endif


ifeq ($(HAVE_GL_CONTEXT), 1)
   DEFINES += -DHAVE_OPENGL -DHAVE_GLSL
   OBJ += gfx/drivers/gl.o \
          gfx/common/gl_common.o \
          gfx/drivers_font/gl_raster_font.o \
          libretro-common/glsym/rglgen.o
	
   ifeq ($(HAVE_MENU_COMMON), 1)
		OBJ += menu/drivers_display/menu_display_gl.o
	endif

   ifeq ($(HAVE_KMS), 1)
	   HAVE_AND_WILL_USE_DRM = 1
      OBJ += gfx/drivers_context/drm_egl_ctx.o
      DEFINES += $(GBM_CFLAGS) $(DRM_CFLAGS) $(EGL_CFLAGS)
      LIBS += $(GBM_LIBS) $(DRM_LIBS) $(EGL_LIBS)
   endif

   ifeq ($(HAVE_VIDEOCORE), 1)
      OBJ += gfx/drivers_context/vc_egl_ctx.o
      DEFINES += $(EGL_CFLAGS)
      LIBS += $(EGL_LIBS)
   endif

   ifeq ($(HAVE_EMSCRIPTEN), 1)
      OBJ += gfx/drivers_context/emscriptenegl_ctx.o
   endif

   ifeq ($(HAVE_MALI_FBDEV), 1)
      OBJ += gfx/drivers_context/mali_fbdev_ctx.o
      DEFINES += $(EGL_CFLAGS)
      LIBS += $(EGL_LIBS)
   endif

   ifeq ($(HAVE_VIVANTE_FBDEV), 1)
      OBJ += gfx/drivers_context/vivante_fbdev_ctx.o
      DEFINES += $(EGL_CFLAGS)
      LIBS += $(EGL_LIBS)
   endif

   ifeq ($(HAVE_X11), 1)
      ifeq ($(HAVE_EGL), 1)
         OBJ += gfx/drivers_context/xegl_ctx.o
         DEFINES += $(EGL_CFLAGS)
         LIBS += $(EGL_LIBS)
      endif
      ifneq ($(HAVE_GLES), 1)
			OBJ += gfx/drivers_context/glx_ctx.o
		endif
   endif

   ifeq ($(HAVE_WAYLAND), 1)
	   ifeq ($(HAVE_EGL), 1)
	     OBJ += gfx/drivers_context/wayland_ctx.o
      endif
   endif

   ifeq ($(HAVE_GLES), 1)
      LIBS += $(GLES_LIBS)
      DEFINES += $(GLES_CFLAGS) -DHAVE_OPENGLES -DHAVE_OPENGLES2
      ifeq ($(HAVE_GLES3), 1)
         DEFINES += -DHAVE_OPENGLES3
      endif
      OBJ += libretro-common/glsym/glsym_es2.o
   else
      DEFINES += -DHAVE_GL_SYNC
      OBJ += libretro-common/glsym/glsym_gl.o
		GL_LIBS := -lGL
      ifeq ($(OSX), 1)
         GL_LIBS := -framework OpenGL
         OBJ += gfx/drivers_context/cgl_ctx.o
      else ifneq ($(findstring Win32,$(OS)),)
         GL_LIBS := -lopengl32 -lgdi32 -lcomdlg32 -lcomctl32
         OBJ += gfx/drivers_context/wgl_ctx.o
      endif
		LIBS += $(GL_LIBS)
   endif

   OBJ += gfx/drivers_shader/shader_glsl.o
   DEFINES += -DHAVE_GLSL
endif

ifeq ($(HAVE_EGL), 1)
	OBJ += gfx/common/egl_common.o
endif

ifeq ($(HAVE_SDL2), 1)
   HAVE_SDL=0
endif

ifeq ($(HAVE_SDL), 1)
   OBJ += gfx/drivers/sdl_gfx.o \
          input/drivers/sdl_input.o \
          input/drivers_joypad/sdl_joypad.o \
          audio/drivers/sdl_audio.o

   ifeq ($(HAVE_GL_CONTEXT), 1)
     OBJ += gfx/drivers_context/sdl_gl_ctx.o
   endif

   DEFINES += $(SDL_CFLAGS) $(BSD_LOCAL_INC)
   LIBS += $(SDL_LIBS)
endif

ifeq ($(HAVE_SDL2), 1)
   OBJ += gfx/drivers/sdl2_gfx.o \
          input/drivers/sdl_input.o \
          input/drivers_joypad/sdl_joypad.o \
          audio/drivers/sdl_audio.o

   ifeq ($(HAVE_GL_CONTEXT), 1)
     OBJ += gfx/drivers_context/sdl_gl_ctx.o
   endif

   DEFINES += $(SDL2_CFLAGS) $(BSD_LOCAL_INC)
   LIBS += $(SDL2_LIBS)
   HAVE_SDL = 0
endif

ifeq ($(HAVE_OMAP), 1)
   OBJ += gfx/drivers/omap_gfx.o
endif

ifeq ($(HAVE_EXYNOS), 1)
   OBJ += gfx/drivers/exynos_gfx.o \
          memory/neon/memcpy-neon.o
   LIBS += $(DRM_LIBS) $(EXYNOS_LIBS)
   DEFINES += $(DRM_CFLAGS) $(EXYNOS_CFLAGS)
	HAVE_AND_WILL_USE_DRM = 1
endif

ifeq ($(HAVE_AND_WILL_USE_DRM), 1)
	OBJ += gfx/common/drm_common.o
endif

ifeq ($(HAVE_DISPMANX), 1)
   OBJ += gfx/drivers/dispmanx_gfx.o
	HAVE_VIDEOCORE = 1
   LIBS += $(DISPMANX_LIBS)
   DEFINES += $(DISPMANX_CFLAGS)
endif

ifeq ($(HAVE_SUNXI), 1)
   OBJ += gfx/drivers/sunxi_gfx.o \
          gfx/include/pixman/pixman-arm-neon-asm.o
endif

ifeq ($(HAVE_VG), 1)
   OBJ += gfx/drivers/vg.o
   DEFINES += $(VG_CFLAGS)
   LIBS += $(VG_LIBS)
endif

ifeq ($(HAVE_XVIDEO), 1)
   OBJ += gfx/drivers/xvideo.o
   LIBS += $(XVIDEO_LIBS)
   DEFINES += $(XVIDEO_CFLAGS)
endif

ifeq ($(HAVE_XSHM), 1)
   OBJ += gfx/drivers/xshm.o
   LIBS += $(XSHM_LIBS)
   DEFINES += $(XSHM_CFLAGS)
endif

ifeq ($(HAVE_CG), 1)
	DEFINES += -DHAVE_CG
   OBJ += gfx/drivers_shader/shader_gl_cg.o
   LIBS += $(CG_LIBS)
endif

ifeq ($(HAVE_D3D9), 1)
   OBJ += gfx/d3d/d3d.o \
          gfx/d3d/render_chain_null.o \
          gfx/d3d/render_chain_driver.o \
          gfx/common/d3d_common.o \
          gfx/drivers_font/d3d_w32_font.o \
          gfx/drivers_context/d3d_ctx.o
   DEFINES += -DHAVE_D3D -DHAVE_D3D9
   LIBS += -ld3d9 -ld3dx9 -ldxguid

   ifeq ($(HAVE_MENU_COMMON), 1)
		OBJ += menu/drivers_display/menu_display_d3d.o
	endif

   ifeq ($(HAVE_CG), 1)
      LIBS += -lcgD3D9
      OBJ  += gfx/d3d/render_chain_cg.o
   endif
endif

#ifeq ($(HAVE_LIBXML2), 1)
   #LIBS += $(LIBXML2_LIBS)
   #DEFINES += $(LIBXML2_CFLAGS)
#else
   #OBJ += libretro-common/formats/xml/rxml.o
#endif

# Compression/Archive

ifeq ($(HAVE_7ZIP),1)
   CFLAGS  += -I./deps/7zip
   HAVE_COMPRESSION = 1
   DEFINES += -DHAVE_7ZIP
   7ZOBJ = deps/7zip/7zIn.o \
           deps/7zip/7zAlloc.o \
           deps/7zip/Bra86.o \
           deps/7zip/7zFile.o \
           deps/7zip/7zStream.o \
           deps/7zip/7zBuf2.o \
           deps/7zip/LzmaDec.o \
           deps/7zip/7zCrcOpt.o \
           deps/7zip/Bra.o \
           deps/7zip/7zDec.o \
           deps/7zip/Bcj2.o \
           deps/7zip/7zCrc.o \
           deps/7zip/Lzma2Dec.o \
   deps/7zip/7zBuf.o
   OBJ += $(7ZOBJ)
endif


ifeq ($(HAVE_ZLIB), 1)
   OBJ += libretro-common/file/file_extract.o \
          tasks/task_decompress.o
   OBJ += $(ZLIB_OBJS)
   DEFINES += -DHAVE_ZLIB
	HAVE_RPNG = 1
   HAVE_COMPRESSION = 1
   ifeq ($(WANT_ZLIB), 1)
      DEFINES += -DWANT_ZLIB
   else
      LIBS += -lz
		HAVE_ZLIB_DEFLATE = 1
   endif
endif

ifdef HAVE_RPNG
	DEFINES += -DHAVE_RPNG
endif

ifeq ($(HAVE_RPNG), 1)
   OBJ += libretro-common/formats/png/rpng.o \
          libretro-common/formats/png/rpng_encode.o
endif
OBJ += libretro-common/formats/bmp/rbmp_encode.o \
       libretro-common/formats/tga/rtga.o \
       libretro-common/formats/json/jsonsax.o

ifdef HAVE_COMPRESSION
   DEFINES += -DHAVE_COMPRESSION
endif

ifeq ($(WANT_ZLIB),1)
   OBJ += deps/zlib/adler32.o \
          deps/zlib/compress.o \
          deps/zlib/crc32.o \
          deps/zlib/deflate.o \
          deps/zlib/gzclose.o \
          deps/zlib/gzlib.o \
          deps/zlib/gzread.o \
          deps/zlib/gzwrite.o \
          deps/zlib/inffast.o \
          deps/zlib/inflate.o \
          deps/zlib/inftrees.o \
          deps/zlib/trees.o \
          deps/zlib/uncompr.o \
          deps/zlib/zutil.o \
          deps/zlib/ioapi.o \
          deps/zlib/unzip.o
else
ifeq ($(HAVE_ZLIB),1)
ZLIB_OBJS = deps/zlib/unzip.o \
            deps/zlib/ioapi.o
OBJ += $(ZLIB_OBJS)
HAVE_ZLIB_DEFLATE = 1
endif
endif

ifeq ($(HAVE_ZLIB_DEFLATE),1)
DEFINES += -DHAVE_ZLIB_DEFLATE
endif

# Camera

ifeq ($(HAVE_V4L2),1)
   OBJ += camera/drivers/video4linux2.o
   DEFINES += -DHAVE_V4L2
endif

# Things that depend on network availability

ifeq ($(HAVE_NETWORKING), 1)
   DEFINES += -DHAVE_NETWORKING
   OBJ += libretro-common/net/net_compat.o \
			 libretro-common/net/net_http.o \
			 net_http_special.o \
			 tasks/task_http.o

   ifneq ($(findstring Win32,$(OS)),)
      LIBS += -lws2_32
   endif

   # Netplay

   ifeq ($(HAVE_NETPLAY), 1)
      DEFINES += -DHAVE_NETPLAY -DHAVE_NETWORK_CMD -DHAVE_NETWORK_GAMEPAD
      OBJ += netplay.o
   endif

   # Retro Achievements (also depends on threads)
   
   ifeq ($(HAVE_CHEEVOS), 1)
      ifeq ($(HAVE_THREADS), 1)
         DEFINES += -DHAVE_CHEEVOS
         OBJ += cheevos.o libretro-common/utils/md5.o
      endif
   endif
endif

ifneq ($(findstring Win32,$(OS)),)
   OBJ += media/rarch.o \
          input/drivers_keyboard/keyboard_event_win32.o \
          gfx/common/win32_common.o \
          frontend/drivers/platform_win32.o
endif

ifeq ($(HAVE_AVFOUNDATION), 1)
ifeq ($(HAVE_COCOA), 1)
   DEFINES += -DHAVE_AVFOUNDATION
   LIBS += -framework AVFoundation
   LIBS += -framework CoreVideo
   LIBS += -framework CoreMedia
endif
endif

# Record

ifeq ($(HAVE_FFMPEG), 1)
   OBJ += record/drivers/record_ffmpeg.o \
          cores/libretro-ffmpeg/ffmpeg_core.o
   LIBS += $(AVCODEC_LIBS) $(AVFORMAT_LIBS) $(AVUTIL_LIBS) $(SWSCALE_LIBS) $(SWRESAMPLE_LIBS) $(FFMPEG_LIBS)
   DEFINES += $(AVCODEC_CFLAGS) $(AVFORMAT_CFLAGS) $(AVUTIL_CFLAGS) $(SWSCALE_CFLAGS) $(SWRESAMPLE_CFLAGS)
	DEFINES += -DHAVE_FFMPEG -Iffmpeg
endif

ifeq ($(HAVE_COMPRESSION), 1)
   DEFINES += -DHAVE_COMPRESSION
endif

ifeq ($(HAVE_COCOA),1)
	DEFINES += -DHAVE_MAIN
	OBJ += input/drivers/cocoa_input.o \
               input/drivers_keyboard/keyboard_event_apple.o \
               ui/drivers/ui_cocoa.o \
               ui/drivers/cocoa/cocoa_common.o \
               gfx/drivers_context/cocoa_gl_ctx.o
endif
