-
Notifications
You must be signed in to change notification settings - Fork 51
/
Makefile
executable file
·1874 lines (1550 loc) · 61.4 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Makefile to rebuild SM64 split image
include util.mk
# Default target
default: all
# Preprocessor definitions
DEFINES :=
#==============================================================================#
# Build Options #
#==============================================================================#
# These options can either be set by building with 'make SETTING=value'.
# 'make clean' may be required first.
# Makefile Refactor from Refresh 13 is somewhat adapted but
# has to be carefully to make sure sm64ex and other ports still work.
# There's also some HackerSM64 N64 specific Makefile changes.
# Manual target defines
# Build for original N64
TARGET_N64 ?= 0
# Build and optimize for Raspberry Pi(s)
TARGET_RPI ?= 0
# Build for Emscripten/WebGL
TARGET_WEB ?= 0
# Build for the Wii U
TARGET_WII_U ?= 0
# Build for the 3DS
TARGET_N3DS ?= 0
# Build for Nintendo Switch
TARGET_SWITCH ?= 0
# Compiler to use for N64 (and other targets if required)
# gcc - uses the GNU C Compiler
# clang - uses clang C/C++ frontend for LLVM
COMPILER_TYPE ?= gcc
$(eval $(call validate-option,COMPILER_TYPE,gcc clang))
COMPILER_OPT ?= default
$(eval $(call validate-option,COMPILER_OPT,debug debugmax default fast))
# Automatic target defines
# Build for Windows
WINDOWS_BUILD ?= 0
# Build for Android
TARGET_ANDROID ?= 0
# Makeflag to enable OSX fixes
OSX_BUILD ?= 0
# Specify the target you are building for, TARGET_BITS=0 means native
TARGET_ARCH ?= native
TARGET_BITS ?= 0
# PC Port defines
PC_PORT_DEFINES ?= 0
# PC Port defines from a ported console device
TARGET_PORT_CONSOLE ?= 0
# Various workarounds for weird and/or old toolchains
CPP_ASSEMBLY ?= 0
NO_BZERO_BCOPY ?= 0
NO_LDIV ?= 0
NO_PIE ?= 1
# Use vendor-gnostic GLVND implementation, instead of the old/legacy GLX X11-only implementation.
USE_GLVND ?= 0
# Backend selection
# Renders: GL, GL_LEGACY, D3D11, D3D12, GX2 (forced if the target is Wii U), C3D (forced if the target is 3DS)
RENDER_API ?= GL
# Window managers: SDL1, SDL2, DXGI (forced if D3D11 or D3D12 in RENDER_API), GX2 (forced if the target is Wii U), 3DS (forced if the target is 3DS)
WINDOW_API ?= SDL2
# Audio backends: SDL1, SDL2 (forced if the target is Wii U), 3DS (forced if the target is 3DS)
AUDIO_API ?= SDL2
# Controller backends (can have multiple, space separated): SDL1, SDL2
# WII_U (forced if the target is Wii U), 3DS (forced if the target is 3DS), SWITCH (forced if the target is SWITCH)
CONTROLLER_API ?= SDL2
#==============================================================================#
# Automatic settings for target devices #
#==============================================================================#
# Attempt to detect OS
ifeq ($(OS),Windows_NT)
HOST_OS ?= Windows
else
HOST_OS ?= $(shell uname -s 2>/dev/null || echo Unknown)
# some weird MINGW/Cygwin env that doesn't define $OS
ifneq (,$(findstring MINGW,HOST_OS))
HOST_OS := Windows
endif
endif
OLD_APKSIGNER ?= 0
# Attempt to detect termux android build
ifneq ($(shell which termux-setup-storage 2>/dev/null),)
TARGET_ANDROID := 1
COMPILER_TYPE := clang
ifeq ($(shell dpkg -s apksigner | grep Version | sed "s/Version: //"),0.7-2)
OLD_APKSIGNER := 1
endif
endif
ifeq ($(TARGET_N64),0)
GRUCODE := f3dex2e
PC_PORT_DEFINES := 1
else
GRUCODE ?= f3dzex
NO_LDIV := 1
endif
ifeq ($(TARGET_WII_U),1)
RENDER_API := GX2
WINDOW_API := GX2
AUDIO_API := SDL2
CONTROLLER_API := WII_U
TARGET_PORT_CONSOLE := 1
endif
ifeq ($(TARGET_N3DS),1)
RENDER_API := C3D
WINDOW_API := 3DS
AUDIO_API := 3DS
CONTROLLER_API := 3DS
TARGET_PORT_CONSOLE := 1
endif
ifeq ($(TARGET_SWITCH),1)
RENDER_API := GL
WINDOW_API := SDL2
AUDIO_API := SDL2
CONTROLLER_API := SWITCH
TARGET_PORT_CONSOLE := 1
endif
TOUCH_CONTROLS ?= 0
ifeq ($(TARGET_ANDROID),1)
RENDER_API := GL
WINDOW_API := SDL2
AUDIO_API := SDL2
CONTROLLER_API := SDL2
TOUCH_CONTROLS := 1
endif
# Enforce C preprocessor on these targets
ifeq ($(COMPILER_TYPE),clang)
CPP_ASSEMBLY := 1
endif
ifeq ($(TARGET_PORT_CONSOLE),1)
CPP_ASSEMBLY := 1
endif
ifeq ($(TARGET_WEB),1)
CPP_ASSEMBLY := 1
endif
# Custom Defines
include defines.mk
# MXE overrides
ifeq ($(HOST_OS),Windows)
ifeq ($(TARGET_WEB),0)
ifeq ($(TARGET_PORT_CONSOLE),0)
WINDOWS_BUILD := 1
endif
endif
ifeq ($(CROSS),i686-w64-mingw32.static-)
TARGET_ARCH = i386pe
TARGET_BITS = 32
NO_BZERO_BCOPY := 1
else ifeq ($(CROSS),x86_64-w64-mingw32.static-)
TARGET_ARCH = i386pep
TARGET_BITS = 64
NO_BZERO_BCOPY := 1
endif
endif
ifneq (,$(filter $(RENDER_API),D3D11 D3D12))
ifneq ($(WINDOWS_BUILD),1)
$(error DirectX is only supported on Windows)
endif
ifneq ($(WINDOW_API),DXGI)
$(warning DirectX renderers require DXGI, forcing WINDOW_API value)
WINDOW_API := DXGI
endif
else
ifeq ($(WINDOW_API),DXGI)
$(error DXGI can only be used with DirectX renderers)
endif
endif
# macOS overrides
ifeq ($(HOST_OS),Darwin)
OSX_BUILD := 1
CPP_ASSEMBLY := 1
# Using Homebrew?
ifeq ($(shell which brew >/dev/null 2>&1 && echo y),y)
OSX_GCC_VER = $(shell find `brew --prefix`/bin/gcc* | grep -oE '[[:digit:]]+' | sort -n | uniq | tail -1)
CC := gcc-$(OSX_GCC_VER)
CXX := g++-$(OSX_GCC_VER)
CPP := cpp-$(OSX_GCC_VER)
PLATFORM_CFLAGS := -I $(shell brew --prefix)/include
PLATFORM_LDFLAGS := -L $(shell brew --prefix)/lib
else
# Using MacPorts?
ifeq ($(shell test -d /opt/local/lib && echo y),y)
OSX_GCC_VER = $(shell find /opt/local/bin/gcc* | grep -oE '[[:digit:]]+' | sort -n | uniq | tail -1)
CC := gcc-mp-$(OSX_GCC_VER)
CXX := g++-mp-$(OSX_GCC_VER)
CPP := cpp-mp-$(OSX_GCC_VER)
PLATFORM_CFLAGS := -I /opt/local/include
PLATFORM_LDFLAGS := -L /opt/local/lib
else
$(error No suitable macOS toolchain found, have you installed Homebrew?)
endif
endif
endif
ifneq ($(TARGET_BITS),0)
BITS := -m$(TARGET_BITS)
endif
# Set up WUT for Wii U
ifeq ($(TARGET_WII_U),1)
ifeq ($(strip $(DEVKITPRO)),)
$(error "Please set DEVKITPRO in your environment. export DEVKITPRO=<path to>/devkitpro")
endif
ifeq ($(strip $(DEVKITPPC)),)
$(error "Please set DEVKITPPC in your environment. export DEVKITPPC=<path to>/devkitPro/devkitPPC")
endif
include $(DEVKITPPC)/base_tools
PORTLIBS := $(PORTLIBS_PATH)/wiiu $(PORTLIBS_PATH)/ppc
export PATH := $(PORTLIBS_PATH)/wiiu/bin:$(PORTLIBS_PATH)/ppc/bin:$(PATH)
WUT_ROOT ?= $(DEVKITPRO)/wut
RPXSPECS := -specs=$(WUT_ROOT)/share/wut.specs
MACHDEP = -DESPRESSO -mcpu=750 -meabi -mhard-float
LIBDIRS := $(PORTLIBS) $(WUT_ROOT)
INCLUDE := $(foreach dir,$(LIBDIRS),-I$(dir)/include)
LIBPATHS := $(foreach dir,$(LIBDIRS),-L$(dir)/lib)
endif
# Base settings for EXTERNAL_DATA
ifeq ($(TARGET_PORT_CONSOLE),1)
BASEDIR ?= sm64ex_res
else
BASEDIR ?= res
endif
BASEPACK ?= base.zip
#==============================================================================#
# Main defines #
#==============================================================================#
# VERSION - selects the version of the game to build
# jp - builds the 1996 Japanese version
# us - builds the 1996 North American version
# eu - builds the 1997 PAL version
# sh - builds the 1997 Japanese Shindou version, with rumble pak support
# cn - builds the 2003 Chinese iQue version
VERSION ?= us
$(eval $(call validate-option,VERSION,jp us eu sh cn))
NEW_AUDIO_REV ?= false
ifeq ($(VERSION),jp)
VER_DEFINES += VERSION_JP=1
else ifeq ($(VERSION),us)
VER_DEFINES += VERSION_US=1
else ifeq ($(VERSION),eu)
VER_DEFINES += VERSION_EU=1
else ifeq ($(VERSION),sh)
VER_DEFINES += VERSION_SH=1
NEW_AUDIO_REV := true
else ifeq ($(VERSION),cn)
VER_DEFINES += VERSION_CN=1
NEW_AUDIO_REV := true
endif
DEFINES += $(VER_DEFINES)
TARGET := sm64.$(VERSION).$(GRUCODE)
# GRUCODE - selects which RSP microcode to use.
# f3d_old - First version the Fast3D (Originally in JP - US)
# f3d_new - Second version the Fast3D (Originally in EU - SH)
# f3dex - First version of Fast3D Extended
# f3dex2 - Second (rewritten) version of Fast3D Extended
# l3dex2 - F3DEX2 version that only renders in wireframe
# f3dex2e - Exclusive PC Version of the F3DEX2 microcode
# f3dzex - newer, experimental microcode used in Animal Crossing
# super3d - extremely experimental version of Fast3D lacking many features for speed
$(eval $(call validate-option,GRUCODE,f3d_old f3dex f3dex2 f3dex2e f3dex2pl f3d_new f3dzex super3d l3dex2))
ifeq ($(GRUCODE),f3d_old)
GRU_DEFINES += F3D_OLD=1
else ifeq ($(GRUCODE),f3d_new) # Fast3D 2.0H
GRU_DEFINES += F3D_NEW=1
else ifeq ($(GRUCODE),f3dex) # Fast3DEX
GRU_DEFINES += F3DEX_GBI=1 F3DEX_GBI_SHARED=1
else ifeq ($(GRUCODE),f3dex2) # Fast3DEX2
GRU_DEFINES += F3DEX_GBI_2=1 F3DEX_GBI_SHARED=1
else ifeq ($(GRUCODE),l3dex2) # Line3DEX2
GRU_DEFINES += L3DEX2_GBI=1 L3DEX2_ALONE=1 F3DEX_GBI_2=1 F3DEX_GBI_SHARED=1
else ifeq ($(GRUCODE),f3dex2pl) # Fast3DEX2_PosLight
GRU_DEFINES += F3DEX2PL_GBI=1 F3DEX_GBI_2=1 F3DEX_GBI_SHARED=1
else ifeq ($(GRUCODE),f3dzex) # Fast3DZEX (2.08J / Animal Forest - Dōbutsu no Mori)
GRU_DEFINES += F3DZEX_GBI_2=1 F3DEX_GBI_2=1 F3DEX_GBI_SHARED=1
else ifeq ($(GRUCODE),super3d) # Super3D
$(warning Super3D is experimental. Try at your own risk.)
GRU_DEFINES += SUPER3D_GBI=1 F3D_NEW=1
else ifeq ($(GRUCODE),f3dex2e) # Fast3DEX2 Extended (PC Only)
ifeq ($(TARGET_N64),1)
$(error f3dex2e is only supported on PC Port)
else
GRU_DEFINES += F3DEX_GBI_2E=1
endif
endif
DEFINES += $(GRU_DEFINES)
# Specify target defines
ifeq ($(TARGET_RPI),1) # Define RPi to change SDL2 title & GLES2 hints
DEFINES += TARGET_RPI=1 USE_GLES=1
else ifeq ($(TARGET_ANDROID),1)
DEFINES += TARGET_ANDROID=1 USE_GLES=1
else ifeq ($(OSX_BUILD),1) # Modify GFX & SDL2 for OSX GL
DEFINES += OSX_BUILD=1
else ifeq ($(TARGET_WEB),1)
DEFINES += TARGET_WEB=1 USE_GLES=1
else ifeq ($(TARGET_WII_U),1)
DEFINES += TARGET_WII_U=1
else ifeq ($(TARGET_N3DS),1)
DEFINES += TARGET_N3DS=1
else ifeq ($(TARGET_SWITCH),1)
DEFINES += TARGET_SWITCH=1 USE_GLES=1
endif
# OpenGL defines
ifeq ($(USE_GLES),1) # GLES can be used outside Raspberry Pi, Android or Switch
DEFINES += USE_GLES=1
endif
# Libultra defines
LIBULTRA ?= L
# Libultra number revision (only used on 2.0D)
LIBULTRA_REVISION ?= 0
# LIBULTRA - sets the libultra OS version to use
$(eval $(call validate-option,LIBULTRA,D F H I J K L BB))
# Libultra number revision (only used on 2.0D)
LIBULTRA_REVISION ?= 0
ULTRA_VER_D := 1
ULTRA_VER_E := 2
ULTRA_VER_F := 3
ULTRA_VER_G := 4
ULTRA_VER_H := 5
ULTRA_VER_I := 6
ULTRA_VER_J := 7
ULTRA_VER_K := 8
ULTRA_VER_L := 9
ifeq ($(LIBULTRA),BB)
ULTRA_VER_DEF := LIBULTRA_VERSION=$(ULTRA_VER_L) BBPLAYER
ULTRA_VER_STR := LIBULTRA_STR_VER=\"L\"
else
ULTRA_VER_DEF := LIBULTRA_VERSION=$(ULTRA_VER_$(LIBULTRA)) LIBULTRA_REVISION=$(LIBULTRA_REVISION)
ULTRA_VER_STR := LIBULTRA_STR_VER=\"$(LIBULTRA)\"
endif
DEFINES += $(ULTRA_VER_DEF) $(ULTRA_VER_STR)
# Important defines
# Mandatory defines to ensture correct compilation
DEFINES += NON_MATCHING=1 AVOID_UB=1
# Check for no bzero/bcopy workaround option
ifeq ($(NO_BZERO_BCOPY),1)
DEFINES += NO_BZERO_BCOPY=1
endif
# Use internal ldiv()/lldiv()
ifeq ($(NO_LDIV),1)
DEFINES += NO_LDIV=1
endif
# Set no-pie if compiler supports it
ifeq ($(NO_PIE),1)
NO_PIE_DEF := -no-pie
endif
ifeq ($(TARGET_N64),1)
DEFINES += TARGET_N64=1 _FINALROM=1
endif
#==============================================================================#
# Universal Dependencies #
#==============================================================================#
PYTHON := python3
TOOLS_DIR = tools
# (This is a bit hacky, but a lot of rules implicitly depend
# on tools and assets, and we use directory globs further down
# in the makefile that we want should cover assets.)
ifeq ($(filter clean distclean print-%,$(MAKECMDGOALS)),)
# Make sure assets exist
NOEXTRACT ?= 0
ifeq ($(NOEXTRACT),0)
DUMMY != $(PYTHON) extract_assets.py $(VERSION) >&2 || echo FAIL
ifeq ($(DUMMY),FAIL)
$(error Failed to extract assets from US ROM)
endif
ifneq (,$(wildcard baserom.jp.z64))
DUMMY != $(PYTHON) extract_assets.py jp >&2 || echo FAIL
ifeq ($(DUMMY),FAIL)
$(error Failed to extract assets from JP ROM)
endif
endif
ifneq (,$(wildcard baserom.eu.z64))
DUMMY != $(PYTHON) extract_assets.py eu >&2 || echo FAIL
ifeq ($(DUMMY),FAIL)
$(error Failed to extract assets from EU ROM)
endif
endif
ifneq (,$(wildcard baserom.sh.z64))
DUMMY != $(PYTHON) extract_assets.py sh >&2 || echo FAIL
ifeq ($(DUMMY),FAIL)
$(error Failed to extract assets from SH ROM)
endif
endif
endif
# Make tools if out of date
$(info Building tools...)
ifeq ($(TARGET_PORT_CONSOLE),0)
DUMMY != CC=$(CC) CXX=$(CXX) $(MAKE) -s -C $(TOOLS_DIR) >&2 || echo FAIL
else
DUMMY != $(MAKE) -s -C $(TOOLS_DIR) >&2 || echo FAIL
endif
ifeq ($(DUMMY),FAIL)
$(error Failed to build tools)
endif
$(info Building game...)
endif
#==============================================================================#
# Optimization flags #
#==============================================================================#
ifeq ($(TARGET_N64),1)
ifeq ($(COMPILER_TYPE),gcc)
MIPSISET := -mips3
OPT_FLAGS := -Ofast
else ifeq ($(COMPILER_TYPE),clang)
# clang doesn't support ABI 'o32' for 'mips3'
MIPSISET := -mips2
OPT_FLAGS := -Ofast
endif
else
ifeq ($(COMPILER_OPT),default)
OPT_FLAGS := -O2
else ifeq ($(COMPILER_OPT),debug)
OPT_FLAGS := -g
else ifeq ($(COMPILER_OPT),debugmax)
OPT_FLAGS := -O2 -g3
else ifeq ($(COMPILER_OPT),fast)
OPT_FLAGS := -Ofast
endif
# Set BITS (32/64) to compile for
OPT_FLAGS += $(BITS)
endif
ifeq ($(TARGET_WEB),1)
OPT_FLAGS := -O2 -g4 --source-map-base http://localhost:8080/
endif
ifeq ($(TARGET_RPI),1)
OPT_FLAGS := -O3 -march=native -mtune=native
endif
#==============================================================================#
# Target Executable and Sources #
#==============================================================================#
# BUILD_DIR is location where all build artifacts are placed
BUILD_DIR_BASE := build
TARGET_NAME :=
ifeq ($(TARGET_N64),1)
BUILD_DIR := $(BUILD_DIR_BASE)/$(VERSION)
EXE := $(BUILD_DIR)/$(TARGET)
ROM := $(BUILD_DIR)/$(TARGET).z64
TARGET_NAME := Nintendo 64
else ifeq ($(TARGET_WEB),1)
BUILD_DIR := $(BUILD_DIR_BASE)/$(VERSION)_web
EXE := $(BUILD_DIR)/$(TARGET).html
TARGET_NAME := Website
else ifeq ($(TARGET_WII_U),1)
BUILD_DIR := $(BUILD_DIR_BASE)/$(VERSION)_wiiu
EXE := $(BUILD_DIR)/$(TARGET).rpx
TARGET_NAME := Nintendo Wii U
else ifeq ($(TARGET_N3DS),1)
BUILD_DIR := $(BUILD_DIR_BASE)/$(VERSION)_3ds
EXE := $(BUILD_DIR)/$(TARGET).3dsx
CIA := $(BUILD_DIR)/$(TARGET).cia
TARGET_NAME := Nintendo 3DS
else ifeq ($(TARGET_SWITCH),1)
BUILD_DIR := $(BUILD_DIR_BASE)/$(VERSION)_nx
EXE := $(BUILD_DIR)/$(TARGET).nro
TARGET_NAME := Nintendo Switch
else ifeq ($(TARGET_ANDROID),1)
BUILD_DIR := $(BUILD_DIR_BASE)/$(VERSION)_android
EXE := $(BUILD_DIR)/libmain.so
APK := $(BUILD_DIR)/$(TARGET).unsigned.apk
APK_SIGNED := $(BUILD_DIR)/$(TARGET).apk
TARGET_NAME := Android
else ifeq ($(WINDOWS_BUILD),1) # us_win (to be renamed)
BUILD_DIR := $(BUILD_DIR_BASE)/$(VERSION)_pc
EXE := $(BUILD_DIR)/$(TARGET).exe
TARGET_NAME := Windows
else # Linux/Unix builds/binary namer
BUILD_DIR := $(BUILD_DIR_BASE)/$(VERSION)_pc
ifeq ($(TARGET_RPI),1) # us_rpi (to be renamed)
EXE := $(BUILD_DIR)/$(TARGET).arm
else # us_tux (to be renamed)
EXE := $(BUILD_DIR)/$(TARGET)
endif
ifeq ($(OSX_BUILD),1)
TARGET_NAME := macOS
else ifeq ($(HOST_OS),Haiku)
TARGET_NAME := Haiku OS
else ifeq ($(TARGET_RPI),1)
TARGET_NAME := Raspberry Pi
else
TARGET_NAME := Linux-Unix system
endif
endif
ELF := $(BUILD_DIR)/$(TARGET).elf
LD_SCRIPT := sm64.ld
CHARMAP := charmap.txt
CHARMAP_DEBUG := charmap.debug.txt
MIO0_DIR := $(BUILD_DIR)/bin
SOUND_BIN_DIR := $(BUILD_DIR)/sound
TEXTURE_DIR := textures
ACTOR_DIR := actors
LEVEL_DIRS := $(patsubst levels/%,%,$(dir $(wildcard levels/*/header.h)))
# Directories containing source files
SRC_DIRS := src src/engine src/game src/audio src/menu src/buffers src/extras actors levels bin bin/$(VERSION) data assets sound
BOOT_DIR := src/boot
ifeq ($(TARGET_N64),1)
SRC_DIRS += asm lib src/extras/n64
else
# Specify target folders
PLATFORM_DIR := platform
SRC_DIRS += src/pc src/pc/audio src/pc/controller src/pc/crash src/pc/fs src/pc/fs/packtypes src/pc/gfx
ifeq ($(WINDOWS_BUILD),1)
PLATFORM_DIR := $(PLATFORM_DIR)/win
else ifeq ($(TARGET_N3DS),1)
PLATFORM_DIR := $(PLATFORM_DIR)/3ds
else ifeq ($(TARGET_SWITCH),1)
PLATFORM_DIR := $(PLATFORM_DIR)/switch
else ifeq ($(TARGET_ANDROID),1)
PLATFORM_DIR := $(PLATFORM_DIR)/android
endif
SRC_DIRS += $(PLATFORM_DIR)
endif
ifeq ($(TARGET_PORT_CONSOLE),0)
ifeq ($(DISCORDRPC),1)
SRC_DIRS += src/pc/discord
endif
endif
BIN_DIRS := bin bin/$(VERSION)
ifeq ($(LIBULTRA),BB)
ULTRA_SRC_DIRS := $(shell find lib/ultra -type d)
else
ULTRA_SRC_DIRS := $(shell find lib/ultra -type d -not -path "lib/ultra/bb/*")
endif
ULTRA_BIN_DIRS := lib/bin
LIBGCC_SRC_DIRS := lib/gcc
ifeq ($(GODDARD_MFACE),1)
GODDARD_SRC_DIRS := src/goddard src/goddard/dynlists
endif
# Whether to hide commands or not
VERBOSE ?= 0
ifeq ($(VERBOSE),0)
V := @
endif
# Whether to colorize build messages
COLOR ?= 1
# File dependencies and variables for specific files
include Makefile.split
# Source code files
LEVEL_C_FILES := $(wildcard levels/*/leveldata.c) $(wildcard levels/*/script.c) $(wildcard levels/*/geo.c)
ifeq ($(RM2C),1)
LEVEL_C_FILES += $(wildcard levels/*/custom.leveldata.c) $(wildcard levels/*/custom.script.c) $(wildcard levels/*/custom.geo.c)
endif
C_FILES := $(foreach dir,$(SRC_DIRS),$(wildcard $(dir)/*.c)) $(LEVEL_C_FILES)
CXX_FILES := $(foreach dir,$(SRC_DIRS),$(wildcard $(dir)/*.cpp))
S_FILES := $(foreach dir,$(SRC_DIRS),$(wildcard $(dir)/*.s))
ifeq ($(TARGET_N64),1)
C_FILES += $(foreach dir,$(BOOT_DIR),$(wildcard $(dir)/*.c))
S_FILES += $(foreach dir,$(BOOT_DIR),$(wildcard $(dir)/*.s))
ULTRA_C_FILES := $(foreach dir,$(ULTRA_SRC_DIRS),$(wildcard $(dir)/*.c))
ULTRA_S_FILES := $(foreach dir,$(ULTRA_SRC_DIRS),$(wildcard $(dir)/*.s))
LIBGCC_C_FILES := $(foreach dir,$(LIBGCC_SRC_DIRS),$(wildcard $(dir)/*.c))
endif
ifeq ($(GODDARD_MFACE),1)
GODDARD_C_FILES := $(foreach dir,$(GODDARD_SRC_DIRS),$(wildcard $(dir)/*.c))
endif
ifeq ($(WINDOWS_BUILD),1)
RC_FILES := $(foreach dir,$(SRC_DIRS),$(wildcard $(dir)/*.rc))
endif
GENERATED_C_FILES := $(BUILD_DIR)/assets/mario_anim_data.c $(BUILD_DIR)/assets/demo_data.c
ifneq ($(TARGET_N64),1)
GENERATED_C_FILES += $(addprefix $(BUILD_DIR)/bin/,$(addsuffix _skybox.c,$(notdir $(basename $(wildcard textures/skyboxes/*.png)))))
ULTRA_C_FILES := \
audio/bnkf.c \
gu/lookatref.c \
gu/mtxutil.c \
gu/normalize.c \
gu/ortho.c \
gu/perspective.c \
gu/rotate.c \
gu/scale.c \
gu/translate.c \
libc/ldiv.c
ULTRA_C_FILES := $(addprefix lib/ultra/,$(ULTRA_C_FILES))
C_FILES += $(addprefix src/boot/,memory.c)
endif
# "If we're not N64, use the above"
SOUND_BANK_FILES := $(wildcard sound/sound_banks/*.json)
ifeq ($(VERSION),cn)
SOUND_SEQUENCE_DIRS := sound/sequences sound/sequences/sh
else
SOUND_SEQUENCE_DIRS := sound/sequences sound/sequences/$(VERSION)
endif
# all .m64 files in SOUND_SEQUENCE_DIRS, plus all .m64 files that are generated from .s files in SOUND_SEQUENCE_DIRS
SOUND_SEQUENCE_FILES := \
$(foreach dir,$(SOUND_SEQUENCE_DIRS),\
$(wildcard $(dir)/*.m64) \
$(foreach file,$(wildcard $(dir)/*.s),$(BUILD_DIR)/$(file:.s=.m64)) \
)
SOUND_SAMPLE_DIRS := $(wildcard sound/samples/*)
SOUND_SAMPLE_AIFFS := $(foreach dir,$(SOUND_SAMPLE_DIRS),$(wildcard $(dir)/*.aiff))
SOUND_SAMPLE_TABLES := $(foreach file,$(SOUND_SAMPLE_AIFFS),$(BUILD_DIR)/$(file:.aiff=.table))
SOUND_SAMPLE_AIFCS := $(foreach file,$(SOUND_SAMPLE_AIFFS),$(BUILD_DIR)/$(file:.aiff=.aifc))
# Object files
O_FILES := $(foreach file,$(C_FILES),$(BUILD_DIR)/$(file:.c=.o)) \
$(foreach file,$(CXX_FILES),$(BUILD_DIR)/$(file:.cpp=.o)) \
$(foreach file,$(S_FILES),$(BUILD_DIR)/$(file:.s=.o)) \
$(foreach file,$(GENERATED_C_FILES),$(file:.c=.o))
ifeq ($(WINDOWS_BUILD),1)
O_FILES += $(foreach file,$(RC_FILES),$(BUILD_DIR)/$(file:.rc=.o))
endif
ULTRA_O_FILES := $(foreach file,$(ULTRA_S_FILES),$(BUILD_DIR)/$(file:.s=.o)) \
$(foreach file,$(ULTRA_C_FILES),$(BUILD_DIR)/$(file:.c=.o))
ifeq ($(GODDARD_MFACE),1)
GODDARD_O_FILES := $(foreach file,$(GODDARD_C_FILES),$(BUILD_DIR)/$(file:.c=.o))
endif
ifeq ($(TARGET_N64),1)
LIBGCC_O_FILES := $(foreach file,$(LIBGCC_C_FILES),$(BUILD_DIR)/$(file:.c=.o))
endif
RPC_LIBS :=
ifeq ($(TARGET_PORT_CONSOLE),0)
ifeq ($(DISCORDRPC),1)
ifeq ($(WINDOWS_BUILD),1)
RPC_LIBS := lib/discord/libdiscord-rpc.dll
else ifeq ($(OSX_BUILD),1)
# needs testing
RPC_LIBS := lib/discord/libdiscord-rpc.dylib
else
RPC_LIBS := lib/discord/libdiscord-rpc.so
endif
endif
endif
# Automatic dependency files
DEP_FILES := $(O_FILES:.o=.d) $(ULTRA_O_FILES:.o=.d) $(GODDARD_O_FILES:.o=.d) $(BUILD_DIR)/$(LD_SCRIPT).d
ifeq ($(TARGET_N64),1)
DEP_FILES += $(LIBGCC_O_FILES:.o=.d)
endif
#==============================================================================#
# Compiler Options #
#==============================================================================#
INCLUDE_DIRS := include $(BUILD_DIR) $(BUILD_DIR)/include src .
ifeq ($(TARGET_N64),1)
INCLUDE_DIRS += include/gcc
endif
ifeq ($(TARGET_ANDROID),1)
INCLUDE_DIRS += $(PLATFORM_DIR)/SDL/include
endif
ifeq ($(TARGET_SWITCH),1)
ifeq ($(strip $(DEVKITPRO)),)
$(error "Please set DEVKITPRO in your environment. export DEVKITPRO=<path to>/devkitpro")
endif
export PATH := $(DEVKITPRO)/devkitA64/bin:$(PATH)
include $(DEVKITPRO)/devkitA64/base_tools
NXPATH := $(DEVKITPRO)/portlibs/switch/bin
PORTLIBS ?= $(DEVKITPRO)/portlibs/switch
LIBNX ?= $(DEVKITPRO)/libnx
CROSS ?= aarch64-none-elf-
SDLCROSS := $(NXPATH)/
CC := $(CROSS)gcc
CXX := $(CROSS)g++
STRIP := $(CROSS)strip
NXARCH := -march=armv8-a+crc+crypto -mtune=cortex-a57 -mtp=soft -fPIE
INCLUDE_DIRS += $(LIBNX)/include $(PORTLIBS)/include
NX_APP_TITLE := Super Mario 64
NX_APP_AUTHOR := Nintendo - Port by Vatuu, fgsfdsfgs and KiritoDev
NX_APP_VERSION := ver_$(VERSION)
NX_APP_ICON := $(PLATFORM_DIR)/logo.jpg
NACP_FILE := $(BUILD_DIR)/$(PLATFORM_DIR)/sm64.nacp
endif
C_DEFINES := $(foreach d,$(DEFINES),-D$(d))
DEF_INC_CFLAGS := $(foreach i,$(INCLUDE_DIRS),-I $(i)) $(C_DEFINES)
# Set C Preprocessor flags
ifeq ($(COMPILER_TYPE),gcc)
CPPFLAGS := -P -Wno-trigraphs
else ifeq ($(COMPILER_TYPE),clang)
CPPFLAGS := -E -P -x c -Wno-trigraphs
endif
CPPFLAGS += $(DEF_INC_CFLAGS) $(CUSTOM_C_DEFINES)
ASMDEFINES := $(VER_DEFINES) $(GRU_DEFINES) $(ULTRA_VER_DEF)
# 3DS Minimap flags
ifeq ($(TARGET_N3DS),1)
MINIMAP := $(PLATFORM_DIR)/minimap
MINIMAP_C := $(wildcard $(MINIMAP)/*.c)
MINIMAP_O := $(foreach file,$(MINIMAP_C),$(BUILD_DIR)/$(file:.c=.o))
MINIMAP_TEXTURES := $(MINIMAP)/textures
MINIMAP_PNG := $(wildcard $(MINIMAP_TEXTURES)/*.png)
MINIMAP_T3S := $(foreach file,$(MINIMAP_PNG),$(BUILD_DIR)/$(file:.png=.t3s))
MINIMAP_T3X := $(foreach file,$(MINIMAP_T3S),$(file:.t3s=.t3x))
MINIMAP_T3X_O := $(foreach file,$(MINIMAP_T3X),$(file:.t3x=.t3x.o))
MINIMAP_T3X_HEADERS := $(foreach file,$(MINIMAP_PNG),$(BUILD_DIR)/$(file:.png=_t3x.h))
endif
ifeq ($(TARGET_N64),1)
# detect prefix for MIPS toolchain
ifneq ($(call find-command,mips64-elf-ld),)
CROSS := mips64-elf-
else ifneq ($(call find-command,mips-n64-ld),)
CROSS := mips-n64-
else ifneq ($(call find-command,mips64-ld),)
CROSS := mips64-
else ifneq ($(call find-command,mips-linux-gnu-ld),)
CROSS := mips-linux-gnu-
else ifneq ($(call find-command,mips64-linux-gnu-ld),)
CROSS := mips64-linux-gnu-
else ifneq ($(call find-command,mips64-none-elf-ld),)
CROSS := mips64-none-elf-
else ifneq ($(call find-command,mips-ld),)
CROSS := mips-
else
$(error Unable to detect a suitable MIPS toolchain installed)
endif
# change the compiler to gcc, to use the default, install the gcc-mips-linux-gnu package
ifeq ($(COMPILER_TYPE),gcc)
CC := $(CROSS)gcc
CPP := cpp
else ifeq ($(COMPILER_TYPE),clang)
CC := clang
CPP := clang
endif
# use GNU binutils for assembler, linker, archiver, and object tools
AS := $(CROSS)as
LD := $(CROSS)ld
AR := $(CROSS)ar
OBJDUMP := $(CROSS)objdump
OBJCOPY := $(CROSS)objcopy
# Check code syntax with host compiler
CFLAGS := -G 0 -nostdinc $(MIPSISET) $(DEF_INC_CFLAGS)
ifeq ($(COMPILER_TYPE),gcc)
CFLAGS += -mno-shared -march=vr4300 -mfix4300 -mabi=32 -mhard-float -mdivide-breaks -fno-stack-protector -fno-common -fno-zero-initialized-in-bss -fno-PIC -mno-abicalls -fno-strict-aliasing -fno-inline-functions -ffreestanding -fwrapv -Wall -Wextra
CFLAGS += -Wno-missing-braces
else ifeq ($(COMPILER_TYPE),clang)
CFLAGS += -mfpxx -target mips -mabi=32 -mhard-float -fomit-frame-pointer -fno-stack-protector -fno-common -I include -I src/ -I $(BUILD_DIR)/include -fno-PIC -mno-abicalls -fno-strict-aliasing -fno-inline-functions -ffreestanding -fwrapv -Wall -Wextra
CFLAGS += -Wno-missing-braces
else
CFLAGS += -non_shared -Wab,-r4300_mul -Xcpluscomm -Xfullwarn -signed -32
endif
ifeq ($(CPP_ASSEMBLY),1)
ASFLAGS := -march=vr4300 -mabi=32 $(foreach i,$(INCLUDE_DIRS),-I$(i)) $(foreach d,$(ASMDEFINES),--defsym $(d))
else
ASMFLAGS := -G 0 $(DEF_INC_CFLAGS) -w -nostdinc -c -march=vr4300 -mfix4300 -mno-abicalls -DMIPSEB -D_LANGUAGE_ASSEMBLY -D_MIPS_SIM=1 -D_MIPS_SZLONG=32
endif
RSPASMFLAGS := $(foreach d,$(ASMDEFINES),-definelabel $(subst =, ,$(d)))
OBJCOPYFLAGS := --pad-to=0x800000 --gap-fill=0xFF
SYMBOL_LINKING_FLAGS := --no-check-sections $(addprefix -R ,$(SEG_FILES))
LDFLAGS := -T $(BUILD_DIR)/$(LD_SCRIPT) -Map $(BUILD_DIR)/sm64.$(VERSION).map $(SYMBOL_LINKING_FLAGS)
CFLAGS += $(CUSTOM_C_DEFINES)
ASMFLAGS += $(CUSTOM_C_DEFINES)
else # TARGET_N64
ifeq ($(TARGET_WII_U),1)
LD := $(CXX)
CPP := powerpc-eabi-cpp
OBJDUMP := powerpc-eabi-objdump
SDLCONFIG :=
else ifeq ($(TARGET_N3DS),1)
CPP := $(DEVKITARM)/bin/arm-none-eabi-cpp
OBJDUMP := $(DEVKITARM)/bin/arm-none-eabi-objdump
OBJCOPY := $(DEVKITARM)/bin/arm-none-eabi-objcopy
AS := $(DEVKITARM)/bin/arm-none-eabi-as
CC := $(DEVKITARM)/bin/arm-none-eabi-gcc
CXX := $(DEVKITARM)/bin/arm-none-eabi-g++
LD := $(CXX)
SDLCONFIG :=
SMDH_TITLE ?= Super Mario 64
SMDH_DESCRIPTION ?= Super Mario 64 3DS Port
SMDH_AUTHOR ?= Nintendo - port by Fnouwt (Gericom) and mkst
SMDH_ICON := $(PLATFORM_DIR)/icon.smdh
else
# for some reason sdl-config in dka64 is not prefixed, while pkg-config is
SDLCROSS ?= $(CROSS)
AS ?= $(CROSS)as
ifeq ($(OSX_BUILD),1) # As in, not using macOS
AS := /usr/bin/as
endif
ifneq ($(TARGET_WEB),1) # As in, not-web PC port
CC ?= $(CROSS)gcc
CXX ?= $(CROSS)g++
else
CC := emcc
CXX := emcc
endif
AS_MINGW := i686-w64-mingw32-as
LD := $(CXX)
ifeq ($(DISCORDRPC),1)
LD := $(CXX)
else ifeq ($(WINDOWS_BUILD),1)
ifeq ($(CROSS),i686-w64-mingw32.static-) # fixes compilation in MXE on Linux and WSL
LD := $(CC)
else ifeq ($(CROSS),x86_64-w64-mingw32.static-)
LD := $(CC)
else
LD := $(CXX)
endif
endif
ifeq ($(WINDOWS_BUILD),1) # fixes compilation in MXE on Linux and WSL
CPP := cpp
OBJCOPY := objcopy
OBJDUMP := $(CROSS)objdump
else ifeq ($(OSX_BUILD),1)
OBJCOPY := i686-w64-mingw32-objcopy
OBJDUMP := i686-w64-mingw32-objdump
else ifeq ($(TARGET_ANDROID),1) # Termux has clang
ifeq ($(COMPILER_TYPE),clang)
CPP := clang
else
CPP := cpp
endif
OBJCOPY := $(CROSS)objcopy
OBJDUMP := $(CROSS)objdump
else # Linux & other builds
ifeq ($(COMPILER_TYPE),clang)
CPP := clang
else
CPP := $(CROSS)cpp
endif
OBJCOPY := $(CROSS)objcopy
OBJDUMP := $(CROSS)objdump
endif
SDLCONFIG := $(SDLCROSS)sdl2-config
WINDRES := $(CROSS)windres
endif
# configure backend flags
BACKEND_CFLAGS := -DRAPI_$(RENDER_API)=1 -DWAPI_$(WINDOW_API)=1 -DAAPI_$(AUDIO_API)=1
# can have multiple controller APIs
BACKEND_CFLAGS += $(foreach capi,$(CONTROLLER_API),-DCAPI_$(capi)=1)
BACKEND_LDFLAGS :=
SDL1_USED := 0
SDL2_USED := 0
# for now, it's either SDL+GL, DXGI+DirectX, GX2 or C3D so choose based on WAPI
ifeq ($(WINDOW_API),DXGI)
DXBITS := `cat $(ENDIAN_BITWIDTH) | tr ' ' '\n' | tail -1`
ifeq ($(RENDER_API),D3D12)
BACKEND_CFLAGS += -Iinclude/dxsdk
endif
BACKEND_LDFLAGS += -ld3dcompiler -ldxgi -ldxguid
BACKEND_LDFLAGS += -lsetupapi -ldinput8 -luser32 -lgdi32 -limm32 -lole32 -loleaut32 -lshell32 -lwinmm -lversion -luuid -static
else ifeq ($(findstring SDL,$(WINDOW_API)),SDL)
ifeq ($(WINDOWS_BUILD),1)
BACKEND_LDFLAGS += -lglew32 -lglu32 -lopengl32
else ifeq ($(TARGET_ANDROID),1)
BACKEND_LDFLAGS += -lGLESv2
else ifeq ($(TARGET_RPI),1)
BACKEND_LDFLAGS += -lGLESv2
else ifeq ($(TARGET_SWITCH),1)
BACKEND_LDFLAGS += -lGLESv2
else ifeq ($(USE_GLES),1)
BACKEND_LDFLAGS += -lGLESv2
else ifeq ($(OSX_BUILD),1)
BACKEND_LDFLAGS += -framework OpenGL $(shell pkg-config --libs glew)
else ifeq ($(USE_GLVND),1)
BACKEND_LDFLAGS += -lOpenGL
else
BACKEND_LDFLAGS += -lGL
endif
else ifeq ($(WINDOW_API),GX2)
BACKEND_LDFLAGS += -lSDL2 -lwut
else ifeq ($(WINDOW_API),C3D)
BACKEND_LDFLAGS +=
endif