XPDEV-MT_CFLAGS	+=	-DXPDEV_THREAD_SAFE
ifneq ($(os),haiku)
 XPDEV_LIBS	+=	-lm
 XPDEV-MT_LIBS	+=	-lm
endif
ifeq ($(os),haiku)
 XPDEV_LIBS	+=	-lnetwork
 XPDEV-MT_LIBS	+=	-lnetwork
endif
ifeq ($(os),netbsd)     # NetBSD
 XPDEV_LIBS	+=	-lossaudio
 XPDEV-MT_LIBS	+=	-lossaudio -lpthread
endif
ifeq ($(os),openbsd)    # OpenBSD
 XPDEV_LIBS	+=	-lossaudio
 XPDEV-MT_LIBS	+=	-lossaudio -lpthread
endif
ifeq ($(os),sunos)	# Solaris
 XPDEV_LIBS	+=	-lsocket
 XPDEV-MT_LIBS	+=	-lpthread -lsocket
endif
ifeq ($(os),darwin)	# Mac OS/X
 XPDEV-MT_LIBS	+=	-lpthread
endif
ifeq ($(os),qnx)	# QNX
 XPDEV-MT_LIBS	+=	-lpthread
endif
ifeq ($(os),linux)	# Linux
 XPDEV-MT_LIBS	+=	-lpthread
endif
ifeq ($(os),win32)	# MinGW32
 XPDEV_LIBS	+=	-lws2_32 -lwinmm -lnetapi32
 XPDEV-MT_LIBS	+=	-lws2_32 -lwinmm -lnetapi32
endif
ifeq ($(shell if [ -c /dev/urandom ] ; then echo YES ; fi),YES)
 XPDEV-MT_CFLAGS	+=	-DHAS_DEV_URANDOM -DURANDOM_DEV=\"/dev/urandom\"
 XPDEV_CFLAGS		+=	-DHAS_DEV_URANDOM -DURANDOM_DEV=\"/dev/urandom\"
endif
ifeq ($(shell if [ -L /dev/urandom ] ; then echo YES ; fi),YES)
 XPDEV-MT_CFLAGS	+=	-DHAS_DEV_URANDOM -DURANDOM_DEV=\"/dev/urandom\"
 XPDEV_CFLAGS		+=	-DHAS_DEV_URANDOM -DURANDOM_DEV=\"/dev/urandom\"
endif
ifeq ($(shell if [ -c /dev/random ] ; then echo YES ; fi),YES)
 XPDEV-MT_CFLAGS	+=	-DHAS_DEV_RANDOM -DRANDOM_DEV=\"/dev/random\"
 XPDEV_CFLAGS		+=	-DHAS_DEV_RANDOM -DRANDOM_DEV=\"/dev/random\"
endif
ifeq ($(shell if [ -f /usr/include/dev/speaker/speaker.h ] ; then echo YES ; fi),YES)
 XPDEV-MT_CFLAGS	+=	-DHAS_DEV_SPEAKER_SPEAKER_H
 XPDEV_CFLAGS		+=	-DHAS_DEV_SPEAKER_SPEAKER_H
endif
ifeq ($(shell if [ -f /usr/include/machine/speaker.h ] ; then echo YES ; fi),YES)
 XPDEV-MT_CFLAGS	+=	-DHAS_MACHINE_SPEAKER_H
 XPDEV_CFLAGS		+=	-DHAS_MACHINE_SPEAKER_H
endif
ifeq ($(shell if [ -f /usr/include/machine/spkr.h ] ; then echo YES ; fi),YES)
 XPDEV-MT_CFLAGS	+=	-DHAS_MACHINE_SPKR_H
 XPDEV_CFLAGS		+=	-DHAS_MACHINE_SPKR_H
endif

# FreeBSD, OpenBSD, NetBSD, Linux, SunOS, and Darwin all have random()/srandom()
ifeq ($(os),freebsd)
 XPDEV-MT_CFLAGS	+=	-DHAS_RANDOM_FUNC -DHAS_SRANDOMDEV_FUNC
 XPDEV_CFLAGS		+=	-DHAS_RANDOM_FUNC -DHAS_SRANDOMDEV_FUNC
endif
ifeq ($(os),openbsd)
 XPDEV-MT_CFLAGS	+=	-DHAS_RANDOM_FUNC -DHAS_SRANDOMDEV_FUNC
 XPDEV_CFLAGS		+=	-DHAS_RANDOM_FUNC -DHAS_SRANDOMDEV_FUNC
endif
ifeq ($(os),netbsd)
 XPDEV-MT_CFLAGS	+=	-DHAS_RANDOM_FUNC
 XPDEV_CFLAGS		+=	-DHAS_RANDOM_FUNC
endif
ifeq ($(os),linux)
 XPDEV-MT_CFLAGS	+=	-DHAS_RANDOM_FUNC
 XPDEV_CFLAGS		+=	-DHAS_RANDOM_FUNC
endif
ifeq ($(os),sunos)
 XPDEV-MT_CFLAGS	+=	-DHAS_RANDOM_FUNC
 XPDEV_CFLAGS		+=	-DHAS_RANDOM_FUNC
endif
ifeq ($(os),darwin)
 XPDEV-MT_CFLAGS	+=	-DHAS_RANDOM_FUNC
 XPDEV_CFLAGS		+=	-DHAS_RANDOM_FUNC
endif

# macOS needs to use the standard snprintf()... use it on FreeBSD as well for testing.
ifeq ($(os),darwin)
 XPDEV_CFLAGS	+=	-DUSE_SNPRINTF
 XPDEV-MT_CFLAGS+=	-DUSE_SNPRINTF
endif
ifeq ($(os),freebsd)
 XPDEV_CFLAGS	+=	-DUSE_SNPRINTF
 XPDEV-MT_CFLAGS+=	-DUSE_SNPRINTF
endif

# Find SDL headers!
ifdef USE_SDL_AUDIO
 ifdef SDL_CONFIG
  ifeq ($(shell ${SDL_CONFIG} --cflags > /dev/null 2>&1 && echo YES),YES)
   WITH_SDL_AUDIO	:=	1
  endif
 else
  ifeq ($(os),darwin)
   ifeq ($(shell if [ -d /Library/Frameworks/SDL.framework ] ; then echo YES ; fi),YES)
    SDL_FRAMEWORK_PATH	?=	/Library/Frameworks
   endif
   ifdef SDL_FRAMEWORK_PATH
    WITH_SDL_AUDIO	:=	1
   endif
  else
   ifeq ($(shell sdl12-config --cflags > /dev/null 2>&1 && echo YES),YES)
    SDL_CONFIG := sdl12-config
    WITH_SDL_AUDIO	:=	1
   else
    ifeq ($(shell sdl-config --cflags > /dev/null 2>&1 && echo YES),YES)
     SDL_CONFIG := sdl-config
     WITH_SDL_AUDIO	:=	1
    else
     ifeq ($(shell sdl11-config --cflags > /dev/null 2>&1 && echo YES),YES)
      SDL_CONFIG := sdl11-config
      WITH_SDL_AUDIO	:=	1
     endif
    endif
   endif
  endif
 endif
endif

ifdef STATIC
 STATIC_SDL := true
endif

ifdef WITH_SDL_AUDIO
 ifdef SDL_CONFIG
  ifeq ($(shell ${SDL_CONFIG} --cflags > /dev/null 2>&1 && echo YES),YES)
   XPDEV-MT_CFLAGS	+=	-DWITH_SDL_AUDIO
   XPDEV-MT_CFLAGS	+=	$(shell $(SDL_CONFIG) --cflags)
   XPDEV_CFLAGS	+=	-DWITH_SDL_AUDIO
   XPDEV_CFLAGS	+=	$(shell $(SDL_CONFIG) --cflags)
   ifdef STATIC_SDL
    XPDEV-MT_LIBS	+=	$(shell $(SDL_CONFIG) --static-libs)
    XPDEV-MT_CFLAGS	+=	-DSTATIC_SDL
    XPDEV-LIBS	+=	$(shell $(SDL_CONFIG) --static-libs)
    XPDEV-CFLAGS	+=	-DSTATIC_SDL
   else
    ifeq ($(os),darwin)
     XPDEV-MT_LIBS		+=  $(UL_PRE)dl$(UL_SUF)
     XPDEV_LIBS		+=  $(UL_PRE)dl$(UL_SUF)
    endif
    ifeq ($(os),linux)
     XPDEV-MT_LIBS		+=  $(UL_PRE)dl$(UL_SUF)
     XPDEV_LIBS		+=  $(UL_PRE)dl$(UL_SUF)
    endif
    ifeq ($(os),sunos)
     XPDEV-MT_LIBS		+=  $(UL_PRE)dl$(UL_SUF)
     XPDEV_LIBS		+=  $(UL_PRE)dl$(UL_SUF)
    endif
   endif
  endif
 else
  ifeq ($(os),darwin)
   XPDEV-MT_CFLAGS	+=	-DWITH_SDL_AUDIO -DSTATIC_SDL
   XPDEV-MT_CFLAGS	+=	-I${SDL_FRAMEWORK_PATH}/SDL.framework/Headers
   XPDEV-MT_LIBS	+=	-F${SDL_FRAMEWORK_PATH} -framework SDL -framework Cocoa -framework Carbon -I${SDL_FRAMEWORK_PATH}/SDL.framework/Headers
   XPDEV_CFLAGS	+=	-DWITH_SDL_AUDIO -DSTATIC_SDL
   XPDEV_CFLAGS	+=	-I${SDL_FRAMEWORK_PATH}/SDL.framework/Headers
   XPDEV_LIBS	+=	-F${SDL_FRAMEWORK_PATH} -framework SDL -framework Cocoa -framework Carbon -I${SDL_FRAMEWORK_PATH}/SDL.framework/Headers
   XPDEV-MT_LDFLAGS += -Wl,-rpath,@executable_path/../Frameworks
  endif
 endif
endif

X_PATH ?=      $(shell if [ -f /usr/X11R6/include/X11/Xlib.h ]; then echo /usr/X11R6 ; \
				elif [ -f /usr/local/include/X11/Xlib.h ] ; then echo /usr/local ; \
				elif [ -f /usr/X11R7/include/X11/Xlib.h ] ; then echo /usr/X11R7 ; \
				elif [ -f /usr/X11/include/X11/Xlib.h ] ; then echo /usr/X11 ; \
				elif [ -f /usr/X/include/X11/Xlib.h ] ; then echo /usr/X ; \
				elif [ -f /usr/include/X11/Xlib.h ] ; then echo /usr ; \
				elif [ -f /opt/X11/include/Xlib.h ] ; then echo /opt/X11 ; \
				else echo 0 ; fi)

ifeq ($(os),sunos)
 X_PATH	?=	/usr/X
else
 X_PATH ?=	/usr/X11R6
endif
X_HEADERS	?=	$(X_PATH)$(DIRSEP)include
X_PRESENT	= $(shell if [ ! \( -f $(X_HEADERS)$(DIRSEP)X11$(DIRSEP)Xlib.h -a -f $(X_HEADERS)$(DIRSEP)X11$(DIRSEP)Xutil.h -a -f $(X_HEADERS)$(DIRSEP)X11$(DIRSEP)keysym.h -a -f $(X_HEADERS)$(DIRSEP)X11$(DIRSEP)Xatom.h \) ] ; then echo NO ; else echo YES ; fi )
ifeq ($(X_PRESENT),NO)
 NO_X=1
else
 ifeq ($(os),darwin)	# No X11 for OSX
  ifndef HAS_DLOPEN
   NO_X=1
  endif
 endif
 XPDEV-MT_CFLAGS	+=	-I$(X_HEADERS)
 XPDEV_CFLAGS	+=	-I$(X_HEADERS)
endif

ifndef WITHOUT_PORTAUDIO
 PORTAUDIO_PATH ?=      $(shell if [ -f /usr/local/include/portaudio.h ]; then echo /usr/local ; elif [ -f /usr/include/portaudio.h ] ; then echo /usr ; else echo NOTFOUND ; fi)

 ifneq ($(PORTAUDIO_PATH),NOTFOUND)
  XPDEV_CFLAGS += -DWITH_PORTAUDIO
  ifeq ($(os),linux)
   XPDEV-MT_LIBS += -ldl
   XPDEV_LIBS += -ldl
  endif
 endif
endif
ifeq ($(shell if [ -f /usr/include/alsa/asoundlib.h ] ; then echo YES ; fi),YES)
 XPDEV-MT_LIBS += -ldl
 XPDEV_LIBS += -ldl
endif
