SILENT := 0
DEBUG := 0
LTO := 1
HAVE_OPENGL = 0
HAVE_CHD = 1
HAVE_CDROM = 0

CORE_DIR := .

SPACE :=
SPACE := $(SPACE) $(SPACE)
BACKSLASH :=
BACKSLASH := \$(BACKSLASH)
filter_out1 = $(filter-out $(firstword $1),$1)
filter_out2 = $(call filter_out1,$(call filter_out1,$1))

ifeq ($(platform),)
   platform = unix
   ifeq ($(shell uname -s),)
      platform = win
   else ifneq ($(findstring Darwin,$(shell uname -s)),)
      platform = osx
      arch = intel
      ifeq ($(shell uname -p),powerpc)
         arch = ppc
      endif
   else ifneq ($(findstring MINGW,$(shell uname -s)),)
      platform = win
   endif
else ifneq (,$(findstring armv,$(platform)))
   override platform += unix
endif

ifeq ($(platform), unix)
   ifeq ($(shell uname -m),loongarch64)
      arch = loongarch64
   endif
endif

ifneq ($(platform), osx)
   ifeq ($(findstring Haiku,$(shell uname -s)),)
      PTHREAD_FLAGS = -pthread
   endif
endif

prefix := /usr
libdir := $(prefix)/lib

LIBRETRO_INSTALL_DIR := libretro

NEED_CD = 1
NEED_TREMOR = 1
NEED_BPP = 32
NEED_DEINTERLACER = 1
NEED_THREADING = 1
CORE_DEFINE :=
TARGET_NAME := mednafen_saturn

ifeq ($(HAVE_OPENGL),1)
   TARGET_NAME := mednafen_saturn_hw
endif

GIT_VERSION := " $(shell git rev-parse --short HEAD || echo unknown)"
ifneq ($(GIT_VERSION)," unknown")
   CXXFLAGS += -DGIT_VERSION=\"$(GIT_VERSION)\"
endif

# loongarch
ifeq ($(arch),loongarch64)
   libdir := $(libdir)/loongarch64-linux-gnu
   CXXFLAGS += -D__loongarch64__
endif

# Unix
ifneq (,$(findstring unix,$(platform)))
   # local VFS may mmap FREQUENT_ACCESS files (cdstream/CHD zero-copy)
   FLAGS += -DHAVE_MMAP
   TARGET := $(TARGET_NAME)_libretro.so
   fpic := -fPIC
   SHARED := -shared -Wl,--no-undefined -Wl,--version-script=link.T
   ifneq (,$(findstring Haiku,$(shell uname -s)))
   CXXFLAGS += -fpermissive
   PTHREAD_FLAGS = -lpthread
   endif
   LDFLAGS += $(PTHREAD_FLAGS)
   FLAGS += $(PTHREAD_FLAGS)
   ifeq ($(HAVE_OPENGL),1)
      ifneq (,$(findstring gles,$(platform)))
         GLES = 1
         GL_LIB := -lGLESv2
      else
         GL_LIB := -lGL
      endif
   endif
   ifneq ($(findstring Linux,$(shell uname -s)),)
      HAVE_CDROM = 1
   endif

# OS X
else ifeq ($(platform), osx)
   TARGET := $(TARGET_NAME)_libretro.dylib
   fpic := -fPIC
   SHARED := -dynamiclib
   LDFLAGS += $(PTHREAD_FLAGS)
   FLAGS += $(PTHREAD_FLAGS)
   ifeq ($(arch),ppc)
      ENDIANNESS_DEFINES := -DMSB_FIRST
      OLD_GCC := 1
   endif
   OSXVER = `sw_vers -productVersion | cut -d. -f 2`
   OSX_LT_MAVERICKS = `(( $(OSXVER) <= 9)) && echo "YES"`
   ifeq ($(OSX_LT_MAVERICKS),"YES")
      fpic += -mmacosx-version-min=10.5
   endif
   ifeq ($(HAVE_OPENGL),1)
      GL_LIB := -framework OpenGL
   endif
   ifeq ($(CROSS_COMPILE),1)
	TARGET_RULE   = -target $(LIBRETRO_APPLE_PLATFORM) -isysroot $(LIBRETRO_APPLE_ISYSROOT)
	CFLAGS   += $(TARGET_RULE)
	CPPFLAGS += $(TARGET_RULE)
	CXXFLAGS += $(TARGET_RULE)
	LDFLAGS  += $(TARGET_RULE)
   endif

# iOS
else ifneq (,$(findstring ios,$(platform)))
   TARGET := $(TARGET_NAME)_libretro_ios.dylib
   fpic := -fPIC
   SHARED := -dynamiclib
   LDFLAGS += $(PTHREAD_FLAGS)
   FLAGS += $(PTHREAD_FLAGS)  -DHAVE_UNISTD_H
   ifeq ($(IOSSDK),)
      IOSSDK := $(shell xcrun -sdk iphoneos -show-sdk-path)
   endif
   ifeq ($(HAVE_OPENGL),1)
      GL_LIB := -framework OpenGLES
   endif
   ifeq ($(platform), ios-arm64)
     CC = cc -arch arm64 -isysroot $(IOSSDK)
     CXX = c++ -arch arm64 -isysroot $(IOSSDK)
   else
     CC = cc -arch armv7 -isysroot $(IOSSDK)
     CXX = c++ -arch armv7 -isysroot $(IOSSDK)
   endif
   IPHONEMINVER :=
   ifeq ($(platform),$(filter $(platform),ios9 ios-arm64))
      IPHONEMINVER = -miphoneos-version-min=8.0
   else
      IPHONEMINVER = -miphoneos-version-min=5.0
   endif
   LDFLAGS += $(IPHONEMINVER)
   FLAGS += $(IPHONEMINVER)
   CC += $(IPHONEMINVER)
   CXX += $(IPHONEMINVER)

# tvOS
else ifeq ($(platform), tvos-arm64)
   TARGET := $(TARGET_NAME)_libretro_tvos.dylib
   fpic := -fPIC
   SHARED := -dynamiclib
   FLAGS += -DHAVE_UNISTD_H

ifeq ($(IOSSDK),)
   IOSSDK := $(shell xcodebuild -version -sdk appletvos Path)
endif
   CC = clang -arch arm64 -isysroot $(IOSSDK)
   CXX = clang++ -arch arm64 -isysroot $(IOSSDK)
   MINVER = -mappletvos-version-min=11.0
   LDFLAGS += $(MINVER)
   FLAGS += $(MINVER)
   CC += $(MINVER)
   CXX += $(MINVER)

# QNX
else ifeq ($(platform), qnx)
   TARGET := $(TARGET_NAME)_libretro_$(platform).so
   fpic := -fPIC
   SHARED := -lcpp -lm -shared -Wl,--no-undefined -Wl,--version-script=link.T
   #LDFLAGS += $(PTHREAD_FLAGS)
   #FLAGS += $(PTHREAD_FLAGS)
   CC = qcc -Vgcc_ntoarmv7le
   CXX = QCC -Vgcc_ntoarmv7le_cpp
   AR = QCC -Vgcc_ntoarmv7le
   FLAGS += -D__BLACKBERRY_QNX__ -marm -mcpu=cortex-a9 -mfpu=neon -mfloat-abi=softfp
   ifeq ($(HAVE_OPENGL),1)
      GL_LIB := -lGLESv2
   endif

# Lightweight PS3 Homebrew SDK
else ifeq ($(platform), psl1ght)
   TARGET := $(TARGET_NAME)_libretro_$(platform).a
   CC = $(PS3DEV)/ppu/bin/ppu-gcc$(EXE_EXT)
   CXX = $(PS3DEV)/ppu/bin/ppu-g++$(EXE_EXT)
   AR = $(PS3DEV)/ppu/bin/ppu-ar$(EXE_EXT)
   ENDIANNESS_DEFINES := -DMSB_FIRST
   STATIC_LINKING = 1
   # frontend + core share one libretro-common; no hybrid there
   FLAGS += -DSTATIC_LINKING

# PSP
else ifeq ($(platform), psp1)
   TARGET := $(TARGET_NAME)_libretro_$(platform).a
   CC = psp-gcc$(EXE_EXT)
   CXX = psp-g++$(EXE_EXT)
   AR = psp-ar$(EXE_EXT)
   FLAGS += -DPSP -G0
   STATIC_LINKING = 1
   # frontend + core share one libretro-common; no hybrid there
   FLAGS += -DSTATIC_LINKING
   EXTRA_INCLUDES := -I$(shell psp-config --pspsdk-path)/include

# Vita
else ifeq ($(platform), vita)
   TARGET := $(TARGET_NAME)_libretro_$(platform).a
   CC = arm-vita-eabi-gcc$(EXE_EXT)
   CXX = arm-vita-eabi-g++$(EXE_EXT)
   AR = arm-vita-eabi-ar$(EXE_EXT)
   FLAGS += -DVITA
   STATIC_LINKING = 1
   # frontend + core share one libretro-common; no hybrid there
   FLAGS += -DSTATIC_LINKING

# Xbox 360
else ifeq ($(platform), xenon)
   TARGET := $(TARGET_NAME)_libretro_xenon360.a
   CC = xenon-gcc$(EXE_EXT)
   CXX = xenon-g++$(EXE_EXT)
   AR = xenon-ar$(EXE_EXT)
   ENDIANNESS_DEFINES += -D__LIBXENON__ -m32 -D__ppc__ -DMSB_FIRST 
   LIBS := $(PTHREAD_FLAGS)
   STATIC_LINKING = 1
   # frontend + core share one libretro-common; no hybrid there
   FLAGS += -DSTATIC_LINKING

# Nintendo Game Cube / Nintendo Wii
else ifneq (,$(filter $(platform),ngc wii))
   ifeq ($(platform), ngc)
      TARGET := $(TARGET_NAME)_libretro_$(platform).a
      ENDIANNESS_DEFINES += -DHW_DOL
   else ifeq ($(platform), wii)
      TARGET := $(TARGET_NAME)_libretro_$(platform).a
      ENDIANNESS_DEFINES += -DHW_RVL
   endif
   ENDIANNESS_DEFINES += -DGEKKO -mrvl -mcpu=750 -meabi -mhard-float -DMSB_FIRST 
   CC = $(DEVKITPPC)/bin/powerpc-eabi-gcc$(EXE_EXT)
   CXX = $(DEVKITPPC)/bin/powerpc-eabi-g++$(EXE_EXT)
   AR = $(DEVKITPPC)/bin/powerpc-eabi-ar$(EXE_EXT)
   EXTRA_INCLUDES := -I$(DEVKITPRO)/libogc/include
   STATIC_LINKING = 1
   # frontend + core share one libretro-common; no hybrid there
   FLAGS += -DSTATIC_LINKING

# GCW0
else ifeq ($(platform), gcw0)
   TARGET := $(TARGET_NAME)_libretro.so
   CC = /opt/gcw0-toolchain/usr/bin/mipsel-linux-gcc
   CXX = /opt/gcw0-toolchain/usr/bin/mipsel-linux-g++
   AR = /opt/gcw0-toolchain/usr/bin/mipsel-linux-ar
   fpic := -fPIC
   SHARED := -shared -Wl,--no-undefined -Wl,--version-script=link.T
   LDFLAGS += $(PTHREAD_FLAGS)
   FLAGS += $(PTHREAD_FLAGS)
   FLAGS += -ffast-math -march=mips32 -mtune=mips32r2 -mhard-float
   GLES = 1
   GL_LIB := -lGLESv2

# Emscripten
else ifeq ($(platform), emscripten)
   TARGET := $(TARGET_NAME)_libretro_$(platform).bc
   FLAGS += -pthread -msimd128 -ftree-vectorize
   M68K_SPLIT_SWITCH = 1
   STATIC_LINKING = 1
   # frontend + core share one libretro-common; no hybrid there
   FLAGS += -DSTATIC_LINKING

ifeq ($(HAVE_OPENGL),1)
	ifneq (,$(findstring gles,$(platform)))
		GLES = 1
		GL_LIB := -lGLESv2
	else
		GL_LIB := -lGL
	endif
endif

# Windows MSVC 2017 all architectures
else ifneq (,$(findstring windows_msvc2017,$(platform)))

    NO_GCC := 1
    FLAGS += -DNOMINMAX -D__PIC__

	PlatformSuffix = $(subst windows_msvc2017_,,$(platform))
	ifneq (,$(findstring desktop,$(PlatformSuffix)))
		WinPartition = desktop
		MSVC2017CompileFlags = -DWINAPI_FAMILY=WINAPI_FAMILY_DESKTOP_APP -FS
		LDFLAGS += -MANIFEST -LTCG:incremental -NXCOMPAT -DYNAMICBASE -DEBUG -OPT:REF -INCREMENTAL:NO -SUBSYSTEM:WINDOWS -MANIFESTUAC:"level='asInvoker' uiAccess='false'" -OPT:ICF -ERRORREPORT:PROMPT -NOLOGO -TLBID:1
		LIBS += kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib
	else ifneq (,$(findstring uwp,$(PlatformSuffix)))
		WinPartition = uwp
		MSVC2017CompileFlags = -DWINAPI_FAMILY=WINAPI_FAMILY_APP -DWINDLL -D_UNICODE -DUNICODE -DWRL_NO_DEFAULT_LIB -FS
		LDFLAGS += -APPCONTAINER -NXCOMPAT -DYNAMICBASE -MANIFEST:NO -LTCG -OPT:REF -SUBSYSTEM:CONSOLE -MANIFESTUAC:NO -OPT:ICF -ERRORREPORT:PROMPT -NOLOGO -TLBID:1 -DEBUG:FULL -WINMD:NO
		LIBS += WindowsApp.lib
	endif

	TargetArchMoniker = $(subst $(WinPartition)_,,$(PlatformSuffix))

    ifneq (,$(findstring x64,$(TargetArchMoniker)))
		MSVC2017CompileFlags += -D__x86_64__
	endif
    ifneq (,$(findstring x86,$(TargetArchMoniker)))
		MSVC2017CompileFlags += -D__i386__
	endif
    ifneq (,$(findstring arm,$(TargetArchMoniker)))
		MSVC2017CompileFlags += -D__arm__
	endif

	CFLAGS += $(MSVC2017CompileFlags)
	CXXFLAGS += $(MSVC2017CompileFlags) -EHsc

	CC  = cl.exe
	CXX = cl.exe
	LD = link.exe

	reg_query = $(call filter_out2,$(subst $2,,$(shell reg query "$2" -v "$1" 2>nul)))
	fix_path = $(subst $(SPACE),\ ,$(subst \,/,$1))

	ProgramFiles86w := $(shell cmd /c "echo %PROGRAMFILES(x86)%")
	ProgramFiles86 := $(shell cygpath "$(ProgramFiles86w)")

	WindowsSdkDir ?= $(call reg_query,InstallationFolder,HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Microsoft SDKs\Windows\v10.0)
	WindowsSdkDir ?= $(call reg_query,InstallationFolder,HKEY_CURRENT_USER\SOFTWARE\Wow6432Node\Microsoft\Microsoft SDKs\Windows\v10.0)
	WindowsSdkDir ?= $(call reg_query,InstallationFolder,HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SDKs\Windows\v10.0)
	WindowsSdkDir ?= $(call reg_query,InstallationFolder,HKEY_CURRENT_USER\SOFTWARE\Microsoft\Microsoft SDKs\Windows\v10.0)
	WindowsSdkDir := $(WindowsSdkDir)

	WindowsSDKVersion ?= $(firstword $(foreach folder,$(subst $(subst \,/,$(WindowsSdkDir)Include/),,$(wildcard $(call fix_path,$(WindowsSdkDir)Include\*))),$(if $(wildcard $(call fix_path,$(WindowsSdkDir)Include/$(folder)/um/Windows.h)),$(folder),)))$(BACKSLASH)
	WindowsSDKVersion := $(WindowsSDKVersion)

	VsInstallBuildTools = $(ProgramFiles86)/Microsoft Visual Studio/2017/BuildTools
	VsInstallEnterprise = $(ProgramFiles86)/Microsoft Visual Studio/2017/Enterprise
	VsInstallProfessional = $(ProgramFiles86)/Microsoft Visual Studio/2017/Professional
	VsInstallCommunity = $(ProgramFiles86)/Microsoft Visual Studio/2017/Community

	VsInstallRoot ?= $(shell if [ -d "$(VsInstallBuildTools)" ]; then echo "$(VsInstallBuildTools)"; fi)
	ifeq ($(VsInstallRoot), )
		VsInstallRoot = $(shell if [ -d "$(VsInstallEnterprise)" ]; then echo "$(VsInstallEnterprise)"; fi)
	endif
	ifeq ($(VsInstallRoot), )
		VsInstallRoot = $(shell if [ -d "$(VsInstallProfessional)" ]; then echo "$(VsInstallProfessional)"; fi)
	endif
	ifeq ($(VsInstallRoot), )
		VsInstallRoot = $(shell if [ -d "$(VsInstallCommunity)" ]; then echo "$(VsInstallCommunity)"; fi)
	endif
	VsInstallRoot := $(VsInstallRoot)

	VcCompilerToolsVer := $(shell cat "$(VsInstallRoot)/VC/Auxiliary/Build/Microsoft.VCToolsVersion.default.txt" | grep -o '[0-9\.]*')
	VcCompilerToolsDir := $(VsInstallRoot)/VC/Tools/MSVC/$(VcCompilerToolsVer)

	WindowsSDKSharedIncludeDir := $(shell cygpath -w "$(WindowsSdkDir)\Include\$(WindowsSDKVersion)\shared")
	WindowsSDKUCRTIncludeDir := $(shell cygpath -w "$(WindowsSdkDir)\Include\$(WindowsSDKVersion)\ucrt")
	WindowsSDKUMIncludeDir := $(shell cygpath -w "$(WindowsSdkDir)\Include\$(WindowsSDKVersion)\um")
	WindowsSDKUCRTLibDir := $(shell cygpath -w "$(WindowsSdkDir)\Lib\$(WindowsSDKVersion)\ucrt\$(TargetArchMoniker)")
	WindowsSDKUMLibDir := $(shell cygpath -w "$(WindowsSdkDir)\Lib\$(WindowsSDKVersion)\um\$(TargetArchMoniker)")

	# For some reason the HostX86 compiler doesn't like compiling for x64
	# ("no such file" opening a shared library), and vice-versa.
	# Work around it for now by using the strictly x86 compiler for x86, and x64 for x64.
	# NOTE: What about ARM?
	ifneq (,$(findstring x64,$(TargetArchMoniker)))
		VCCompilerToolsBinDir := $(VcCompilerToolsDir)\bin\HostX64
	else
		VCCompilerToolsBinDir := $(VcCompilerToolsDir)\bin\HostX86
	endif

	PATH := $(shell IFS=$$'\n'; cygpath "$(VCCompilerToolsBinDir)/$(TargetArchMoniker)"):$(PATH)
	PATH := $(PATH):$(shell IFS=$$'\n'; cygpath "$(VsInstallRoot)/Common7/IDE")
	INCLUDE := $(shell IFS=$$'\n'; cygpath -w "$(VcCompilerToolsDir)/include")
	LIB := $(shell IFS=$$'\n'; cygpath -w "$(VcCompilerToolsDir)/lib/$(TargetArchMoniker)")

	export INCLUDE := $(INCLUDE);$(WindowsSDKSharedIncludeDir);$(WindowsSDKUCRTIncludeDir);$(WindowsSDKUMIncludeDir)
	export LIB := $(LIB);$(WindowsSDKUCRTLibDir);$(WindowsSDKUMLibDir)
	TARGET := $(TARGET_NAME)_libretro.dll
	LDFLAGS += -DLL

# Windows
else
   TARGET := $(TARGET_NAME)_libretro.dll
   CC ?= gcc
   CXX ?= g++
   SHARED := -shared -Wl,--no-undefined -Wl,--version-script=link.T
   LDFLAGS += -static-libgcc -static-libstdc++ -lwinmm
   ifeq ($(HAVE_OPENGL),1)
      GL_LIB := -lopengl32
   endif
   HAVE_CDROM = 1
   LTO = 0
endif

include Makefile.common

WARNINGS := -Wall \
   -Wno-sign-compare \
   -Wno-unused-variable \
   -Wno-unused-function \
   -Wno-uninitialized \
   -Wno-strict-aliasing

ifeq ($(NO_GCC),1)
   WARNINGS :=
endif

OBJECTS := $(SOURCES_CXX:.cpp=.o) $(SOURCES_C:.c=.o)
DEPS    := $(OBJECTS:.o=.d)

all: $(TARGET)

ifeq ($(DEBUG),0)
   # -DNDEBUG: compile out all assert() calls.  The Makefile previously
   # shipped release builds without it, which left ~85 assertions live in
   # the binary -- each one a runtime compare + conditional jump to an
   # abort path with format-string call setup.  None of the assert
   # expressions in this codebase have side effects (audited; the only
   # function call inside any assert is GetSR() in m68k.c, which is a
   # pure read of CCR | SRHB), so dropping them at NDEBUG is safe.
   # Saves a measurable per-file footprint: -61 KiB on vdp2_render.c
   # alone (asserts in the inner render loops compiled to substantial
   # dead code under -O2), -2.7 KiB on cdb.c (17 CDB state-machine
   # invariants), -2.9 KiB on ss.c, and small wins across the rest.
   FLAGS += -O2 -DNDEBUG
else
   FLAGS += -O0 -g
endif

ifeq ($(LTO),1)
   # -flto=auto and -fipa-pta are GCC-only spellings. clang (used on all
   # Apple targets, and on some Linux/BSD setups) rejects -fipa-pta as
   # an unknown argument, and older clangs (including Apple clang from
   # Xcode <= 12) reject -flto=auto with "unsupported argument 'auto'".
   # Clang from LLVM 13+ ignores -flto=auto as a GCC compat alias, but
   # we have to support the older Apple SDKs the iOS CI runners ship.
   # Use plain -flto on clang (full LTO, same effective behavior as
   # -flto=auto on newer clangs), and the full GCC spelling otherwise.
   CC_IS_CLANG := $(shell $(firstword $(CC)) --version 2>/dev/null | grep -ic clang)
   ifeq ($(CC_IS_CLANG),0)
      FLAGS   += -flto=auto -fipa-pta
      LDFLAGS += -flto=auto -fipa-pta -O2
   else
      FLAGS   += -flto
      LDFLAGS += -flto -O2
   endif
endif

LDFLAGS += $(fpic) $(SHARED)

FLAGS += $(fpic) $(INCFLAGS) $(ENDIANNESS_DEFINES) $(WARNINGS) $(CORE_DEFINE) -DSTDC_HEADERS -D__STDC_LIMIT_MACROS -D__LIBRETRO__ -D_LOW_ACCURACY_ $(EXTRA_INCLUDES) $(SOUND_DEFINE) -D__STDC_CONSTANT_MACROS

# The codebase type-puns hardware memory through a handful of raw
# (uint16_t*) casts (SH-2 instruction prefetch, cart ROM read paths).
# WARNINGS already carries -Wno-strict-aliasing, but that only hides
# the diagnostic - at -O2 the optimizer still does type-based alias
# analysis unless -fno-strict-aliasing is also set.  Pair the two so
# the optimizer assumption matches the silenced warning.
FLAGS += -fno-strict-aliasing

CXXFLAGS += $(FLAGS)
CFLAGS   += $(FLAGS)

ifeq (,$(findstring msvc,$(platform)))
    CXXFLAGS += -std=c++11
    CFLAGS   += -MMD -MP
    CXXFLAGS += -MMD -MP
endif

# DSP JIT (SCU + SCSP MPROG): on by default.  Source-level guards keep
# it a no-op on non-aarch64 builds, and the beetle_saturn_jit_scu /
# beetle_saturn_jit_scsp libretro options gate dispatch at runtime.
# Pass WANT_JIT=0 to drop it at compile time.
WANT_JIT ?= 1
ifeq ($(WANT_JIT), 1)
    CFLAGS   += -DWANT_JIT
    CXXFLAGS += -DWANT_JIT
endif
# DSP JIT perf jitdump emitter: writes /tmp/jit-<pid>.dump so `perf
# inject --jit` can resolve [JIT] samples to per-slot symbols.
# Diagnostic only, and the dump grows unbounded over long runs.
# Requires WANT_JIT=1.
ifeq ($(WANT_DSP_JIT_PERF_DUMP), 1)
    CFLAGS += -DWANT_DSP_JIT_PERF_DUMP
endif

OBJOUT   = -o
LINKOUT  = -o 

ifneq (,$(findstring msvc,$(platform)))
	OBJOUT = -Fo
	LINKOUT = -out:
	LD = link.exe
else
	LD = $(CXX)
endif

$(TARGET): $(OBJECTS)
ifeq ($(STATIC_LINKING), 1)
	$(AR) rcs $@ $(OBJECTS)
else
	$(LD) $(LINKOUT)$@ $^ $(LDFLAGS) $(GL_LIB) $(LIBS)
endif

%.o: %.cpp
	@if [ $(SILENT) -ne 1 ]; then\
		$(if $@, $(shell echo echo CXX $<),);\
	fi
	$(CXX) -c $(OBJOUT)$@ $< $(CXXFLAGS)

%.o: %.c
	@if [ $(SILENT) -ne 1 ]; then\
		$(if $@, $(shell echo echo CC $<),);\
	fi
	$(CC) -c $(OBJOUT)$@ $< $(CFLAGS)

clean:
	rm -f $(TARGET) $(OBJECTS) $(DEPS)

install:
	install -D -m 755 $(TARGET) $(DESTDIR)$(libdir)/$(LIBRETRO_INSTALL_DIR)/$(TARGET)

uninstall:
	rm $(DESTDIR)$(libdir)/$(LIBRETRO_INSTALL_DIR)/$(TARGET)

.PHONY: clean install uninstall

-include $(DEPS)
