1DEBUG = 0
2HAVE_NETWORK = 0
3VIDEO_RGB565 = 1
4
5SPACE :=
6SPACE := $(SPACE) $(SPACE)
7BACKSLASH :=
8BACKSLASH := \$(BACKSLASH)
9filter_out1 = $(filter-out $(firstword $1),$1)
10filter_out2 = $(call filter_out1,$(call filter_out1,$1))
11unixpath = $(subst \,/,$1)
12unixcygpath = /$(subst :,,$(call unixpath,$1))
13
14# system platform
15ifeq ($(platform),)
16   platform = unix
17   ifeq ($(shell uname -s),)
18      platform = win
19   else ifneq ($(findstring MINGW,$(shell uname -s)),)
20      platform = win
21   else ifneq ($(findstring Darwin,$(shell uname -s)),)
22      platform = osx
23      arch = intel
24      ifeq ($(shell uname -p),arm64)
25	arch = arm
26      endif
27      ifeq ($(shell uname -p),powerpc)
28	arch = ppc
29      endif
30   else ifneq ($(findstring win,$(shell uname -s)),)
31      platform = win
32   endif
33else ifneq (,$(findstring armv,$(platform)))
34    ifeq (,$(findstring classic_,$(platform)))
35        override platform += unix
36    endif
37else ifneq (,$(findstring rpi,$(platform)))
38   override platform += unix
39endif
40
41# system platform
42system_platform = unix
43ifeq ($(shell uname -a),)
44	EXE_EXT = .exe
45	system_platform = win
46else ifneq ($(findstring Darwin,$(shell uname -a)),)
47	system_platform = osx
48	arch = intel
49	ifeq ($(shell uname -p),powerpc)
50		arch = ppc
51	endif
52else ifneq ($(findstring MINGW,$(shell uname -a)),)
53	system_platform = win
54endif
55
56TARGET_NAME := gambatte
57
58version_script = libgambatte/libretro/link.T
59core_installdir := $(prefix)/lib
60
61# Unix
62ifneq (,$(findstring unix,$(platform)))
63   TARGET := $(TARGET_NAME)_libretro.so
64   fpic := -fPIC
65   SHARED := -shared -Wl,-version-script=$(version_script)
66   HAVE_NETWORK=1
67   ifneq (,$(findstring Haiku,$(shell uname -s)))
68   LDFLAGS += -lnetwork -lroot
69   endif
70
71   # Raspberry Pi
72   ifneq (,$(findstring rpi,$(platform)))
73      CFLAGS += -fomit-frame-pointer -ffast-math
74      CXXFLAGS += $(CFLAGS)
75      ifneq (,$(findstring rpi1,$(platform)))
76         CFLAGS += -march=armv6j -mfpu=vfp -mfloat-abi=hard -marm
77      else ifneq (,$(findstring rpi2,$(platform)))
78         CFLAGS += -mcpu=cortex-a7 -mfpu=neon-vfpv4 -mfloat-abi=hard -marm
79      else ifneq (,$(findstring rpi3,$(platform)))
80         CFLAGS += -mcpu=cortex-a53 -mfpu=neon-fp-armv8 -mfloat-abi=hard -marm
81      else ifneq (,$(findstring rpi4,$(platform)))
82         CFLAGS += -mcpu=cortex-a72 -mtune=cortex-a72
83      endif
84   endif
85
86# Classic Platforms ####################
87# Platform affix = classic_<ISA>_<µARCH>
88# Help at https://modmyclassic.com/comp
89
90# (armv7 a7, hard point, neon based) ###
91# NESC, SNESC, C64 mini
92else ifeq ($(platform), classic_armv7_a7)
93	TARGET := $(TARGET_NAME)_libretro.so
94	fpic := -fPIC
95    SHARED := -shared -Wl,--version-script=$(version_script)  -Wl,--no-undefined -fPIC
96	CFLAGS += -Ofast \
97	-flto=4 -fwhole-program -fuse-linker-plugin \
98	-fdata-sections -ffunction-sections -Wl,--gc-sections \
99	-fno-stack-protector -fno-ident -fomit-frame-pointer \
100	-falign-functions=1 -falign-jumps=1 -falign-loops=1 \
101	-fno-unwind-tables -fno-asynchronous-unwind-tables -fno-unroll-loops \
102	-fmerge-all-constants -fno-math-errno \
103	-marm -mtune=cortex-a7 -mfpu=neon-vfpv4 -mfloat-abi=hard
104	CXXFLAGS += $(CFLAGS)
105    HAVE_NETWORK=1
106	ifeq ($(shell echo `$(CC) -dumpversion` "< 4.9" | bc -l), 1)
107	  CFLAGS += -march=armv7-a
108	else
109	  CFLAGS += -march=armv7ve
110	  # If gcc is 5.0 or later
111	  ifeq ($(shell echo `$(CC) -dumpversion` ">= 5" | bc -l), 1)
112	    LDFLAGS += -static-libgcc -static-libstdc++
113	  endif
114	endif
115#######################################
116
117# (armv8 a35, hard point, neon based) ###
118# PlayStation Classic
119else ifeq ($(platform), classic_armv8_a35)
120	TARGET := $(TARGET_NAME)_libretro.so
121	fpic := -fPIC
122	SHARED := -shared -Wl,--version-script=$(version_script)  -Wl,--no-undefined -fPIC
123	CFLAGS += -Ofast \
124	-fuse-linker-plugin \
125	-fno-stack-protector -fno-ident -fomit-frame-pointer \
126	-fmerge-all-constants -ffast-math -funroll-all-loops \
127	-marm -mcpu=cortex-a35 -mfpu=neon-fp-armv8 -mfloat-abi=hard
128	CXXFLAGS += $(CFLAGS)
129	HAVE_NETWORK=1
130	LDFLAGS += -marm -mcpu=cortex-a35 -mfpu=neon-fp-armv8 -mfloat-abi=hard -Ofast -flto -fuse-linker-plugin
131#######################################
132
133# OS X
134else ifeq ($(platform), osx)
135   TARGET := $(TARGET_NAME)_libretro.dylib
136   fpic := -fPIC
137   SHARED := -dynamiclib
138	OSXVER = $(shell sw_vers -productVersion | cut -d. -f 2)
139   OSX_LT_MAVERICKS = `(( $(OSXVER) <= 9)) && echo "YES"`
140   ifeq ($(OSX_LT_MAVERICKS),YES)
141   	   fpic += -mmacosx-version-min=10.1
142   endif
143   ifeq ($(CROSS_COMPILE),1)
144		TARGET_RULE   = -target $(LIBRETRO_APPLE_PLATFORM) -isysroot $(LIBRETRO_APPLE_ISYSROOT)
145		CFLAGS   += $(TARGET_RULE)
146		CPPFLAGS += $(TARGET_RULE)
147		CXXFLAGS += $(TARGET_RULE)
148		LDFLAGS  += $(TARGET_RULE)
149   endif
150
151   ifndef ($(NOUNIVERSAL))
152      CFLAGS += $(ARCHFLAGS)
153      CXXFLAGS += $(ARCHFLAGS)
154      LDFLAGS += $(ARCHFLAGS)
155   endif
156   HAVE_NETWORK=1
157ifeq ($(arch),ppc)
158	CFLAGS += -DHAVE_NO_LANGEXTRA
159	CXXFLAGS += -DHAVE_NO_LANGEXTRA
160endif
161
162# iOS
163else ifneq (,$(findstring ios,$(platform)))
164   TARGET := $(TARGET_NAME)_libretro_ios.dylib
165   fpic := -fPIC
166   SHARED := -dynamiclib
167   LDFLAGS += -stdlib=libc++
168   CXXFLAGS += $(LDFLAGS)
169   ifeq ($(IOSSDK),)
170      IOSSDK := $(shell xcodebuild -version -sdk iphoneos Path)
171   endif
172   ifeq ($(platform),ios-arm64)
173      CC = cc -arch arm64 -isysroot $(IOSSDK)
174      CXX = c++ -arch arm64 -isysroot $(IOSSDK)
175   else
176      CC = cc -arch armv7 -isysroot $(IOSSDK)
177      CXX = c++ -arch armv7 -isysroot $(IOSSDK)
178   endif
179   ifeq ($(platform),$(filter $(platform),ios9 ios-arm64))
180      CC += -miphoneos-version-min=8.0
181      CXX += -miphoneos-version-min=8.0
182      PLATFORM_DEFINES := -miphoneos-version-min=8.0
183   else
184      CC += -miphoneos-version-min=5.0
185      CXX += -miphoneos-version-min=5.0
186      PLATFORM_DEFINES := -miphoneos-version-min=5.0
187   endif
188   HAVE_NETWORK=1
189
190# tvOS
191else ifeq ($(platform), tvos-arm64)
192   TARGET := $(TARGET_NAME)_libretro_tvos.dylib
193   fpic := -fPIC
194   SHARED := -dynamiclib
195   ifeq ($(IOSSDK),)
196      IOSSDK := $(shell xcodebuild -version -sdk appletvos Path)
197   endif
198   HAVE_NETWORK=1
199
200# Theos
201else ifeq ($(platform), theos_ios)
202   DEPLOYMENT_IOSVERSION = 5.0
203   TARGET = iphone:latest:$(DEPLOYMENT_IOSVERSION)
204   ARCHS = armv7 armv7s
205   TARGET_IPHONEOS_DEPLOYMENT_VERSION=$(DEPLOYMENT_IOSVERSION)
206   THEOS_BUILD_DIR := objs
207   include $(THEOS)/makefiles/common.mk
208   LIBRARY_NAME = $(TARGET_NAME)_libretro_ios
209   HAVE_NETWORK=1
210
211# QNX
212else ifeq ($(platform), qnx)
213   TARGET := $(TARGET_NAME)_libretro_qnx.so
214   fpic := -fPIC
215   SHARED := -lcpp -lm -shared -Wl,-version-script=$(version_script)
216   CC = qcc -Vgcc_ntoarmv7le
217   CXX = QCC -Vgcc_ntoarmv7le_cpp
218   AR = QCC -Vgcc_ntoarmv7le
219   PLATFORM_DEFINES := -D__BLACKBERRY_QNX__ -fexceptions -marm -mcpu=cortex-a9 -mfpu=neon -mfloat-abi=softfp
220
221   # Lightweight PS3 Homebrew SDK
222   ifneq (,$(findstring psl1ght,$(platform)))
223      CC = $(PS3DEV)/ppu/bin/ppu-gcc$(EXE_EXT)
224      CXX = $(PS3DEV)/ppu/bin/ppu-g++$(EXE_EXT)
225      AR = $(PS3DEV)/ppu/bin/ppu-ar$(EXE_EXT)
226   endif
227
228# PS2
229else ifeq ($(platform), ps2)
230   TARGET := $(TARGET_NAME)_libretro_$(platform).a
231   CC = mips64r5900el-ps2-elf-gcc$(EXE_EXT)
232   CXX = mips64r5900el-ps2-elf-g++$(EXE_EXT)
233   AR = mips64r5900el-ps2-elf-ar$(EXE_EXT)
234   PLATFORM_DEFINES := -DPS2 -DCC_RESAMPLER -DCC_RESAMPLER_NO_HIGHPASS -DVIDEO_ABGR1555
235   CFLAGS += -G0
236   CXXFLAGS += -G0
237   STATIC_LINKING = 1
238   VIDEO_RGB565 = 0
239
240
241# PSP
242else ifeq ($(platform), psp1)
243   TARGET := $(TARGET_NAME)_libretro_$(platform).a
244   CC = psp-gcc$(EXE_EXT)
245   CXX = psp-g++$(EXE_EXT)
246   AR = psp-ar$(EXE_EXT)
247   PLATFORM_DEFINES := -DPSP -DCC_RESAMPLER -DCC_RESAMPLER_NO_HIGHPASS
248   CFLAGS += -G0
249   CXXFLAGS += -G0
250   STATIC_LINKING = 1
251
252# Vita
253else ifeq ($(platform), vita)
254   TARGET := $(TARGET_NAME)_libretro_$(platform).a
255   CC = arm-vita-eabi-gcc$(EXE_EXT)
256   CXX = arm-vita-eabi-g++$(EXE_EXT)
257   AR = arm-vita-eabi-ar$(EXE_EXT)
258   PLATFORM_DEFINES := -DCC_RESAMPLER -DCC_RESAMPLER_NO_HIGHPASS
259   CFLAGS += -DVITA
260   CXXFLAGS += -DVITA
261   STATIC_LINKING = 1
262
263# CTR(3DS)
264else ifeq ($(platform), ctr)
265   TARGET := $(TARGET_NAME)_libretro_$(platform).a
266   CC = $(DEVKITARM)/bin/arm-none-eabi-gcc$(EXE_EXT)
267   CXX = $(DEVKITARM)/bin/arm-none-eabi-g++$(EXE_EXT)
268   AR = $(DEVKITARM)/bin/arm-none-eabi-ar$(EXE_EXT)
269   PLATFORM_DEFINES := -DARM11 -D_3DS -DCC_RESAMPLER -DCC_RESAMPLER_NO_HIGHPASS
270   CFLAGS += -march=armv6k -mtune=mpcore -mfloat-abi=hard
271   CFLAGS += -Wall -mword-relocations
272   CFLAGS += -fomit-frame-pointer -ffast-math
273   CXXFLAGS += $(CFLAGS)
274   STATIC_LINKING = 1
275
276# DOS
277else ifeq ($(platform), dos)
278   TARGET := $(TARGET_NAME)_libretro_$(platform).a
279   CC = i686-pc-msdosdjgpp-gcc
280   CXX = i686-pc-msdosdjgpp-g++
281   AR = i686-pc-msdosdjgpp-ar
282   CFLAGS += -march=i386
283   CFLAGS += -Wall
284   CFLAGS += -fomit-frame-pointer -ffast-math
285   CXXFLAGS += $(CFLAGS)
286   STATIC_LINKING = 1
287
288# Xbox 360
289else ifeq ($(platform), xenon)
290   TARGET := $(TARGET_NAME)_libretro_xenon360.a
291   CC = xenon-gcc$(EXE_EXT)
292   CXX = xenon-g++$(EXE_EXT)
293   AR = xenon-ar$(EXE_EXT)
294   PLATFORM_DEFINES := -D__LIBXENON__
295   STATIC_LINKING = 1
296
297# Nintendo Game Cube / Wii / WiiU
298else ifneq (,$(filter $(platform), ngc wii wiiu))
299   TARGET := $(TARGET_NAME)_libretro_$(platform).a
300   CC = $(DEVKITPPC)/bin/powerpc-eabi-gcc$(EXE_EXT)
301   CXX = $(DEVKITPPC)/bin/powerpc-eabi-g++$(EXE_EXT)
302   AR = $(DEVKITPPC)/bin/powerpc-eabi-ar$(EXE_EXT)
303   PLATFORM_DEFINES += -DGEKKO -mcpu=750 -meabi -mhard-float
304   PLATFORM_DEFINES += -U__INT32_TYPE__ -U __UINT32_TYPE__ -D__INT32_TYPE__=int
305   STATIC_LINKING = 1
306
307   # Nintendo WiiU
308   ifneq (,$(findstring wiiu,$(platform)))
309      PLATFORM_DEFINES += -DWIIU -DHW_RVL
310
311   # Nintendo Wii
312   else ifneq (,$(findstring wii,$(platform)))
313      PLATFORM_DEFINES += -DHW_RVL -mrvl
314
315   # Nintendo Game Cube
316   else ifneq (,$(findstring ngc,$(platform)))
317      PLATFORM_DEFINES += -DHW_DOL -mrvl
318   endif
319
320# Nintendo Switch (libtransistor)
321else ifeq ($(platform), switch)
322	EXT=a
323        TARGET := $(TARGET_NAME)_libretro_$(platform).$(EXT)
324        include $(LIBTRANSISTOR_HOME)/libtransistor.mk
325        STATIC_LINKING=1
326
327# Nintendo Switch (libnx)
328else ifeq ($(platform), libnx)
329    include $(DEVKITPRO)/libnx/switch_rules
330    EXT=a
331    TARGET := $(TARGET_NAME)_libretro_$(platform).$(EXT)
332    DEFINES := -DSWITCH=1 -U__linux__ -U__linux -DRARCH_INTERNAL
333    CFLAGS  :=  $(DEFINES) -g \
334                -O2 \
335            -fPIE -I$(LIBNX)/include/ -ffunction-sections -fdata-sections -ftls-model=local-exec -Wl,--allow-multiple-definition -specs=$(LIBNX)/switch.specs
336    CFLAGS += $(INCDIRS)
337    CFLAGS  += $(INCLUDE)  -D__SWITCH__ -DHAVE_LIBNX
338    CXXFLAGS += $(ASFLAGS) $(CFLAGS) -fno-rtti -fno-exceptions -std=gnu++11
339    CFLAGS += -std=gnu11
340    STATIC_LINKING = 1
341
342# emscripten
343else ifeq ($(platform), emscripten)
344   TARGET := $(TARGET_NAME)_libretro_$(platform).bc
345   STATIC_LINKING = 1
346
347# GCW0
348else ifeq ($(platform), gcw0)
349   TARGET := $(TARGET_NAME)_libretro.so
350   CC = /opt/gcw0-toolchain/usr/bin/mipsel-linux-gcc
351   CXX = /opt/gcw0-toolchain/usr/bin/mipsel-linux-g++
352   AR = /opt/gcw0-toolchain/usr/bin/mipsel-linux-ar
353   fpic := -fPIC
354   SHARED := -shared -Wl,-version-script=$(version_script)
355   PLATFORM_DEFINES := -DCC_RESAMPLER -DCC_RESAMPLER_NO_HIGHPASS
356   CFLAGS += -fomit-frame-pointer -ffast-math -march=mips32 -mtune=mips32r2 -mhard-float
357   CXXFLAGS += $(CFLAGS)
358
359# Windows MSVC 2003 Xbox 1
360else ifeq ($(platform), xbox1_msvc2003)
361TARGET := $(TARGET_NAME)_libretro_xdk1.lib
362
363CC  = CL.exe
364CXX  = CL.exe
365LD   = lib.exe
366
367export INCLUDE := $(INCLUDE);$(XDK)/xbox/include
368export LIB := $(XDK)/xbox/lib
369PATH := $(call unixcygpath,$(XDK)/xbox/bin/vc71):$(PATH)
370
371PSS_STYLE :=2
372CFLAGS   += -D_XBOX -D_XBOX1
373CXXFLAGS += -D_XBOX -D_XBOX1
374STATIC_LINKING=1
375HAS_GCC := 0
376# Windows MSVC 2010 Xbox 360
377else ifeq ($(platform), xbox360_msvc2010)
378TARGET := $(TARGET_NAME)_libretro_xdk360.lib
379MSVCBINDIRPREFIX = $(XEDK)/bin/win32
380CC  = "$(MSVCBINDIRPREFIX)/cl.exe"
381CXX  = "$(MSVCBINDIRPREFIX)/cl.exe"
382LD   = "$(MSVCBINDIRPREFIX)/lib.exe"
383
384export INCLUDE := $(XEDK)/include/xbox
385export LIB := $(XEDK)/lib/xbox
386PSS_STYLE :=2
387CFLAGS   += -D_XBOX -D_XBOX360
388CXXFLAGS += -D_XBOX -D_XBOX360
389STATIC_LINKING=1
390HAS_GCC := 0
391
392# Windows MSVC 2017 all architectures
393else ifneq (,$(findstring windows_msvc2017,$(platform)))
394
395	PlatformSuffix = $(subst windows_msvc2017_,,$(platform))
396	ifneq (,$(findstring desktop,$(PlatformSuffix)))
397		WinPartition = desktop
398        MSVC2017CompileFlags = -DWINAPI_FAMILY=WINAPI_FAMILY_DESKTOP_APP
399		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
400		LIBS += kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib
401	else ifneq (,$(findstring uwp,$(PlatformSuffix)))
402		WinPartition = uwp
403		MSVC2017CompileFlags = -DWINAPI_FAMILY=WINAPI_FAMILY_APP -D_WINDLL -D_UNICODE -DUNICODE -D__WRL_NO_DEFAULT_LIB__ -EHsc
404		LDFLAGS += -APPCONTAINER -NXCOMPAT -DYNAMICBASE -MANIFEST:NO -LTCG -OPT:REF -SUBSYSTEM:CONSOLE -MANIFESTUAC:NO -OPT:ICF -ERRORREPORT:PROMPT -NOLOGO -TLBID:1 -DEBUG:FULL -WINMD:NO
405		LIBS += WindowsApp.lib
406	endif
407
408	CFLAGS += $(MSVC2017CompileFlags)
409    CXXFLAGS += $(MSVC2017CompileFlags)
410
411	TargetArchMoniker = $(subst $(WinPartition)_,,$(PlatformSuffix))
412
413	CC  = cl.exe
414	CXX = cl.exe
415
416	reg_query = $(call filter_out2,$(subst $2,,$(shell reg query "$2" -v "$1" 2>nul)))
417	fix_path = $(subst $(SPACE),\ ,$(subst \,/,$1))
418
419	ProgramFiles86w := $(shell cmd //c "echo %PROGRAMFILES(x86)%")
420	ProgramFiles86 := $(shell cygpath "$(ProgramFiles86w)")
421
422	WindowsSdkDir ?= $(call reg_query,InstallationFolder,HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Microsoft SDKs\Windows\v10.0)
423	WindowsSdkDir ?= $(call reg_query,InstallationFolder,HKEY_CURRENT_USER\SOFTWARE\Wow6432Node\Microsoft\Microsoft SDKs\Windows\v10.0)
424	WindowsSdkDir ?= $(call reg_query,InstallationFolder,HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SDKs\Windows\v10.0)
425	WindowsSdkDir ?= $(call reg_query,InstallationFolder,HKEY_CURRENT_USER\SOFTWARE\Microsoft\Microsoft SDKs\Windows\v10.0)
426	WindowsSdkDir := $(WindowsSdkDir)
427
428	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)
429	WindowsSDKVersion := $(WindowsSDKVersion)
430
431	VsInstallBuildTools = $(ProgramFiles86)/Microsoft Visual Studio/2017/BuildTools
432	VsInstallEnterprise = $(ProgramFiles86)/Microsoft Visual Studio/2017/Enterprise
433	VsInstallProfessional = $(ProgramFiles86)/Microsoft Visual Studio/2017/Professional
434	VsInstallCommunity = $(ProgramFiles86)/Microsoft Visual Studio/2017/Community
435
436	VsInstallRoot ?= $(shell if [ -d "$(VsInstallBuildTools)" ]; then echo "$(VsInstallBuildTools)"; fi)
437	ifeq ($(VsInstallRoot), )
438		VsInstallRoot = $(shell if [ -d "$(VsInstallEnterprise)" ]; then echo "$(VsInstallEnterprise)"; fi)
439	endif
440	ifeq ($(VsInstallRoot), )
441		VsInstallRoot = $(shell if [ -d "$(VsInstallProfessional)" ]; then echo "$(VsInstallProfessional)"; fi)
442	endif
443	ifeq ($(VsInstallRoot), )
444		VsInstallRoot = $(shell if [ -d "$(VsInstallCommunity)" ]; then echo "$(VsInstallCommunity)"; fi)
445	endif
446	VsInstallRoot := $(VsInstallRoot)
447
448	VcCompilerToolsVer := $(shell cat "$(VsInstallRoot)/VC/Auxiliary/Build/Microsoft.VCToolsVersion.default.txt" | grep -o '[0-9\.]*')
449	VcCompilerToolsDir := $(VsInstallRoot)/VC/Tools/MSVC/$(VcCompilerToolsVer)
450
451	WindowsSDKSharedIncludeDir := $(shell cygpath -w "$(WindowsSdkDir)\Include\$(WindowsSDKVersion)\shared")
452	WindowsSDKUCRTIncludeDir := $(shell cygpath -w "$(WindowsSdkDir)\Include\$(WindowsSDKVersion)\ucrt")
453	WindowsSDKUMIncludeDir := $(shell cygpath -w "$(WindowsSdkDir)\Include\$(WindowsSDKVersion)\um")
454	WindowsSDKUCRTLibDir := $(shell cygpath -w "$(WindowsSdkDir)\Lib\$(WindowsSDKVersion)\ucrt\$(TargetArchMoniker)")
455	WindowsSDKUMLibDir := $(shell cygpath -w "$(WindowsSdkDir)\Lib\$(WindowsSDKVersion)\um\$(TargetArchMoniker)")
456
457	# For some reason the HostX86 compiler doesn't like compiling for x64
458	# ("no such file" opening a shared library), and vice-versa.
459	# Work around it for now by using the strictly x86 compiler for x86, and x64 for x64.
460	# NOTE: What about ARM?
461	ifneq (,$(findstring x64,$(TargetArchMoniker)))
462		VCCompilerToolsBinDir := $(VcCompilerToolsDir)\bin\HostX64
463	else
464		VCCompilerToolsBinDir := $(VcCompilerToolsDir)\bin\HostX86
465	endif
466
467	PATH := $(shell IFS=$$'\n'; cygpath "$(VCCompilerToolsBinDir)/$(TargetArchMoniker)"):$(PATH)
468	PATH := $(PATH):$(shell IFS=$$'\n'; cygpath "$(VsInstallRoot)/Common7/IDE")
469	INCLUDE := $(shell IFS=$$'\n'; cygpath -w "$(VcCompilerToolsDir)/include")
470	LIB := $(shell IFS=$$'\n'; cygpath -w "$(VcCompilerToolsDir)/lib/$(TargetArchMoniker)")
471	ifneq (,$(findstring uwp,$(PlatformSuffix)))
472		LIB := $(shell IFS=$$'\n'; cygpath -w "$(LIB)/store")
473	endif
474
475	export INCLUDE := $(INCLUDE);$(WindowsSDKSharedIncludeDir);$(WindowsSDKUCRTIncludeDir);$(WindowsSDKUMIncludeDir)
476	export LIB := $(LIB);$(WindowsSDKUCRTLibDir);$(WindowsSDKUMLibDir)
477	TARGET := $(TARGET_NAME)_libretro.dll
478	PSS_STYLE :=2
479	LDFLAGS += -DLL
480
481# Windows MSVC 2010 x64
482else ifeq ($(platform), windows_msvc2010_x64)
483	CC  = cl.exe
484	CXX = cl.exe
485
486PATH := $(shell IFS=$$'\n'; cygpath "$(VS100COMNTOOLS)../../VC/bin/amd64"):$(PATH)
487PATH := $(PATH):$(shell IFS=$$'\n'; cygpath "$(VS100COMNTOOLS)../IDE")
488LIB := $(shell IFS=$$'\n'; cygpath "$(VS100COMNTOOLS)../../VC/lib/amd64")
489INCLUDE := $(shell IFS=$$'\n'; cygpath "$(VS100COMNTOOLS)../../VC/include")
490
491WindowsSdkDir := $(shell reg query "HKLM\SOFTWARE\Microsoft\Microsoft SDKs\Windows\v7.1A" -v "InstallationFolder" | grep -o '[A-Z]:\\.*')
492WindowsSdkDir ?= $(shell reg query "HKLM\SOFTWARE\Microsoft\Microsoft SDKs\Windows\v7.0A" -v "InstallationFolder" | grep -o '[A-Z]:\\.*')
493
494WindowsSDKIncludeDir := $(shell cygpath -w "$(WindowsSdkDir)\Include")
495WindowsSDKGlIncludeDir := $(shell cygpath -w "$(WindowsSdkDir)\Include\gl")
496WindowsSDKLibDir := $(shell cygpath -w "$(WindowsSdkDir)\Lib\x64")
497
498INCFLAGS_PLATFORM = -I"$(WindowsSDKIncludeDir)"
499export INCLUDE := $(INCLUDE);$(WindowsSDKIncludeDir);$(WindowsSDKGlIncludeDir)
500export LIB := $(LIB);$(WindowsSDKLibDir)
501TARGET := $(TARGET_NAME)_libretro.dll
502PSS_STYLE :=2
503LDFLAGS += -DLL
504# Windows MSVC 2010 x86
505else ifeq ($(platform), windows_msvc2010_x86)
506	CC  = cl.exe
507	CXX = cl.exe
508
509PATH := $(shell IFS=$$'\n'; cygpath "$(VS100COMNTOOLS)../../VC/bin"):$(PATH)
510PATH := $(PATH):$(shell IFS=$$'\n'; cygpath "$(VS100COMNTOOLS)../IDE")
511LIB := $(shell IFS=$$'\n'; cygpath -w "$(VS100COMNTOOLS)../../VC/lib")
512INCLUDE := $(shell IFS=$$'\n'; cygpath "$(VS100COMNTOOLS)../../VC/include")
513
514WindowsSdkDir := $(shell reg query "HKLM\SOFTWARE\Microsoft\Microsoft SDKs\Windows\v7.1A" -v "InstallationFolder" | grep -o '[A-Z]:\\.*')
515WindowsSdkDir ?= $(shell reg query "HKLM\SOFTWARE\Microsoft\Microsoft SDKs\Windows\v7.0A" -v "InstallationFolder" | grep -o '[A-Z]:\\.*')
516
517WindowsSDKIncludeDir := $(shell cygpath -w "$(WindowsSdkDir)\Include")
518WindowsSDKGlIncludeDir := $(shell cygpath -w "$(WindowsSdkDir)\Include\gl")
519WindowsSDKLibDir := $(shell cygpath -w "$(WindowsSdkDir)\Lib")
520
521INCFLAGS_PLATFORM = -I"$(WindowsSDKIncludeDir)"
522export INCLUDE := $(INCLUDE);$(WindowsSDKIncludeDir);$(WindowsSDKGlIncludeDir)
523export LIB := $(LIB);$(WindowsSDKLibDir)
524TARGET := $(TARGET_NAME)_libretro.dll
525PSS_STYLE :=2
526LDFLAGS += -DLL
527
528# Windows MSVC 2005 x86
529else ifeq ($(platform), windows_msvc2005_x86)
530	CC  = cl.exe
531	CXX = cl.exe
532
533PATH := $(shell IFS=$$'\n'; cygpath "$(VS80COMNTOOLS)../../VC/bin"):$(PATH)
534PATH := $(PATH):$(shell IFS=$$'\n'; cygpath "$(VS80COMNTOOLS)../IDE")
535INCLUDE := $(shell IFS=$$'\n'; cygpath "$(VS80COMNTOOLS)../../VC/include")
536LIB := $(shell IFS=$$'\n'; cygpath -w "$(VS80COMNTOOLS)../../VC/lib")
537BIN := $(shell IFS=$$'\n'; cygpath "$(VS80COMNTOOLS)../../VC/bin")
538
539WindowsSdkDir := $(shell reg query "HKLM\SOFTWARE\Microsoft\MicrosoftSDK\InstalledSDKs\8F9E5EF3-A9A5-491B-A889-C58EFFECE8B3" -v "Install Dir" | grep -o '[A-Z]:\\.*')
540
541WindowsSDKIncludeDir := $(shell cygpath -w "$(WindowsSdkDir)\Include")
542WindowsSDKAtlIncludeDir := $(shell cygpath -w "$(WindowsSdkDir)\Include\atl")
543WindowsSDKCrtIncludeDir := $(shell cygpath -w "$(WindowsSdkDir)\Include\crt")
544WindowsSDKGlIncludeDir := $(shell cygpath -w "$(WindowsSdkDir)\Include\gl")
545WindowsSDKMfcIncludeDir := $(shell cygpath -w "$(WindowsSdkDir)\Include\mfc")
546WindowsSDKLibDir := $(shell cygpath -w "$(WindowsSdkDir)\Lib")
547
548export INCLUDE := $(INCLUDE);$(WindowsSDKIncludeDir);$(WindowsSDKAtlIncludeDir);$(WindowsSDKCrtIncludeDir);$(WindowsSDKGlIncludeDir);$(WindowsSDKMfcIncludeDir);libgambatte/libretro-common/include/compat/msvc
549export LIB := $(LIB);$(WindowsSDKLibDir)
550TARGET := $(TARGET_NAME)_libretro.dll
551PSS_STYLE :=2
552LDFLAGS += -DLL
553LIBS =
554# Windows MSVC 2003 x86
555else ifeq ($(platform), windows_msvc2003_x86)
556	CC  = cl.exe
557	CXX = cl.exe
558
559PATH := $(shell IFS=$$'\n'; cygpath "$(VS71COMNTOOLS)../../Vc7/bin"):$(PATH)
560PATH := $(PATH):$(shell IFS=$$'\n'; cygpath "$(VS71COMNTOOLS)../IDE")
561INCLUDE := $(shell IFS=$$'\n'; cygpath -w "$(VS71COMNTOOLS)../../Vc7/include")
562LIB := $(shell IFS=$$'\n'; cygpath -w "$(VS71COMNTOOLS)../../Vc7/lib")
563BIN := $(shell IFS=$$'\n'; cygpath "$(VS71COMNTOOLS)../../Vc7/bin")
564
565WindowsSdkDir := $(INETSDK)
566
567export INCLUDE := $(INCLUDE);$(INETSDK)/Include;libgambatte/libretro-common/include/compat/msvc
568export LIB := $(LIB);$(WindowsSdkDir);$(INETSDK)/Lib
569TARGET := $(TARGET_NAME)_libretro.dll
570PSS_STYLE :=2
571LDFLAGS += -DLL
572LIBS =
573# Windows
574else
575   TARGET := $(TARGET_NAME)_libretro.dll
576   HAVE_NETWORK=1
577   CC ?= gcc
578   CXX ?= g++
579   SHARED := -shared -static-libgcc -static-libstdc++ -Wl,-no-undefined -Wl,-version-script=$(version_script)
580   LDFLAGS += -lws2_32
581endif
582
583ifeq ($(DEBUG), 1)
584ifneq (,$(findstring msvc,$(platform)))
585   CFLAGS += -MTd
586   CXXFLAGS += -MTd
587   LDFLAGS += -DEBUG
588	CFLAGS += -Od -Zi -D_DEBUG
589	CXXFLAGS += -Od -Zi -D_DEBUG
590else
591	CFLAGS += -O0 -g
592	CXXFLAGS += -O0 -g
593endif
594   CFLAGS   += -DDEBUG
595   CXXFLAGS += -DDEBUG
596else
597ifneq (,$(findstring msvc,$(platform)))
598   CFLAGS   += -MT
599   CXXFLAGS += -MT
600endif
601   CFLAGS   += -O2 -DNDEBUG
602   CXXFLAGS += -O2 -DNDEBUG
603endif
604
605ifeq (,$(findstring msvc,$(platform)))
606CXXFLAGS += -fno-exceptions -fno-rtti
607CXXFLAGS += -std=c++98
608else
609CFLAGS += -D_CRT_SECURE_NO_DEPRECATE
610CXXFLAGS += -D_CRT_SECURE_NO_DEPRECATE
611endif
612
613CFLAGS   += -DHAVE_STDINT_H
614CXXFLAGS += -DHAVE_STDINT_H
615
616ifneq ($(SANITIZER),)
617   CFLAGS   += -fsanitize=$(SANITIZER)
618   CXXFLAGS += -fsanitize=$(SANITIZER)
619   LDFLAGS  += -fsanitize=$(SANITIZER)
620endif
621
622CORE_DIR := libgambatte/src
623
624include Makefile.common
625
626OBJECTS := $(SOURCES_CXX:.cpp=.o) $(SOURCES_C:.c=.o)
627
628DEFINES := -D__LIBRETRO__ $(PLATFORM_DEFINES) -DHAVE_STDINT_H -DHAVE_INTTYPES_H
629
630ifeq ($(VIDEO_RGB565), 1)
631   DEFINES += -DVIDEO_RGB565
632endif
633
634ifeq ($(HAVE_NETWORK), 1)
635   DEFINES += -DHAVE_NETWORK
636endif
637
638CFLAGS   += $(fpic) $(DEFINES)
639CXXFLAGS += $(fpic) $(DEFINES)
640
641OBJOUT   = -o
642LINKOUT  = -o
643
644ifneq (,$(findstring msvc,$(platform)))
645	OBJOUT = -Fo
646	LINKOUT = -out:
647ifeq ($(STATIC_LINKING),1)
648	LD ?= lib.exe
649	STATIC_LINKING=0
650else
651	LD = link.exe
652endif
653else
654	LD = $(CXX)
655endif
656
657INCFLAGS += $(INCFLAGS_PLATFORM)
658
659ifeq ($(platform), theos_ios)
660COMMON_FLAGS := -DIOS $(COMMON_DEFINES) $(INCFLAGS) -I$(THEOS_INCLUDE_PATH) -Wno-error
661$(LIBRARY_NAME)_CFLAGS += $(CFLAGS) $(COMMON_FLAGS)
662$(LIBRARY_NAME)_CXXFLAGS += $(CXXFLAGS) $(COMMON_FLAGS)
663${LIBRARY_NAME}_FILES = $(SOURCES_CXX) $(SOURCES_C)
664include $(THEOS_MAKE_PATH)/library.mk
665else
666all: $(TARGET)
667
668ifeq ($(platform), osx)
669ifndef ($(NOUNIVERSAL))
670   CFLAGS += $(ARCHFLAGS)
671   CXXFLAGS += $(ARCHFLAGS)
672   LFLAGS += $(ARCHFLAGS)
673endif
674endif
675
676$(TARGET): $(OBJECTS)
677ifeq ($(STATIC_LINKING), 1)
678ifneq (,$(findstring msvc,$(platform)))
679	$(LD) $(LINKOUT)$@ $(OBJECTS)
680else
681	$(AR) rcs $@ $(OBJECTS)
682endif
683else
684	$(LD) $(fpic) $(SHARED) $(INCLUDES) $(LFLAGS) $(LINKOUT)$@ $(OBJECTS) $(LIBS) $(LDFLAGS)
685endif
686
687CFLAGS   += $(INCFLAGS)
688CXXFLAGS += $(INCFLAGS)
689
690%.o: %.cpp
691	$(CXX) $(CXXFLAGS) -c $(OBJOUT)$@ $<
692
693%.o: %.c
694	$(CC) $(CFLAGS) -c $(OBJOUT)$@ $<
695
696clean:
697	rm -f $(OBJECTS) $(TARGET)
698
699.PHONY: clean
700endif
701
702install: $(TARGET)
703	install -D -m 755 $(TARGET) $(DESTDIR)$(core_installdir)/$(TARGET)
704
705uninstall:
706	rm $(DESTDIR)$(core_installdir)/$(TARGET)
707