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
ifdef win		# MinGW32
 XPDEV_LIBS	+=	-lws2_32 -lwinmm -lnetapi32 -liphlpapi
 XPDEV-MT_LIBS	+=	-lws2_32 -lwinmm -lnetapi32 -liphlpapi
endif
ifndef win
 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
 XPDEV-MT_CFLAGS	+=	-DSOUNDCARD_H_IN=$(shell if [ -f /usr/include/sys/soundcard.h ] ; then echo 1 ; elif [ -f /usr/include/soundcard.h ] ; then echo 2 ; elif [ -f /usr/include/linux/soundcard.h ] ; then echo 3 ; else echo 0 ; fi)
 XPDEV_CFLAGS	+=	-DSOUNDCARD_H_IN=$(shell if [ -f /usr/include/sys/soundcard.h ] ; then echo 1 ; elif [ -f /usr/include/soundcard.h ] ; then echo 2 ; elif [ -f /usr/include/linux/soundcard.h ] ; then echo 3 ; else echo 0 ; fi)
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 command -v ${SDL_CONFIG} > /dev/null && if [ `${SDL_CONFIG} --version | sed -E 's/\..*$$//'` -ge 2 ] ; then echo 'YES' ; fi),YES)
   WITH_SDL_AUDIO	:=	1
  endif
 else
  ifeq ($(shell command -v sdl2-config > /dev/null && sdl2-config --cflags > /dev/null 2>&1 && echo YES),YES)
   SDL_CONFIG := sdl2-config
   WITH_SDL_AUDIO	:=	1
  else
   ifeq ($(shell command -v sdl-config > /dev/null && if [ `sdl-config --version | sed -E 's/\..*$$//'` -ge 2 ] ; then echo 'YES' ; fi),YES)
    SDL_CONFIG := sdl-config
    WITH_SDL_AUDIO	:=	1
   endif
  endif
  ifndef WITH_SDL
   ifeq ($(os),darwin)
    ifeq ($(shell if [ -d /Library/Frameworks/SDL2.framework ] ; then echo YES ; fi),YES)
     SDL_FRAMEWORK_PATH ?= /Library/Frameworks
     CFLAGS += -F${SDL_FRAMEWORK_PATH}
    endif
    ifdef SDL_FRAMEWORK_PATH
     WITH_SDL_AUDIO	:=	1
     CIOLIB-MT_LDFLAGS += -Wl,-rpath,@executable_path/../Frameworks
     CIOLIB-MT_LDFLAGS += -Wl,-rpath,${SDL_FRAMEWORK_PATH}
    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_CFLAGS	+=	-DWITH_SDL_AUDIO -DSTATIC_SDL
   ifdef SDL_FRAMEWORK_PATH
    XPDEV-MT_CFLAGS	+=	-I${SDL_FRAMEWORK_PATH}/SDL2.framework/Headers
    XPDEV-MT_LIBS	+=	-F${SDL_FRAMEWORK_PATH} -framework SDL2 -I${SDL_FRAMEWORK_PATH}/SDL2.framework/Headers
    XPDEV_CFLAGS	+=	-I${SDL_FRAMEWORK_PATH}/SDL2.framework/Headers
    XPDEV_LIBS	+=	-F${SDL_FRAMEWORK_PATH} -framework SDL2 -I${SDL_FRAMEWORK_PATH}/SDL2.framework/Headers
    XPDEV-MT_LDFLAGS += -Wl,-rpath,@executable_path/../Frameworks
   endif
  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

ifndef win
 ifndef WITHOUT_ALSA_SOUND
  ifeq ($(shell command -v pkg-config > /dev/null && pkg-config --exists alsa && echo 'YES'),YES)
   XPDEV-MT_CFLAGS += -DUSE_ALSA_SOUND `pkg-config alsa --cflags`
   ifeq ($(os),linux)
    XPDEV-MT_LIBS += -ldl
   endif
  endif
 endif
endif

ifndef NO_PULSEAUDIO
 ifeq ($(shell command -v pkg-config > /dev/null && pkg-config --exists libpulse-simple && echo 'YES'),YES)
  XPDEV-CFLAGS += -DWITH_PULSEAUDIO `pkg-config libpulse-simple --cflags`
  XPDEV-MT_CFLAGS += -DWITH_PULSEAUDIO `pkg-config libpulse-simple --cflags`
  ifeq ($(os),linux)
   XPDEV-MT_LIBS += -ldl
   XPDEV_LIBS += -ldl
  endif
 endif
endif
