Repository navigation
Expand file tree
/
Copy pathMakefile.switch
More file actions
255 lines (224 loc) · 12.6 KB
/
Copy pathMakefile.switch
File metadata and controls
255 lines (224 loc) · 12.6 KB
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
# ============================================================================
# OpenSWOS — Nintendo Switch (NX-NXFP2-a64)
# ============================================================================
#
# make -f Makefile.switch -> bin/SWOS.nss (the linked ELF)
# make -f Makefile.switch clean
#
# Then package it with scripts/build_switch.bat, which runs MakeNso, MakeMeta
# and AuthoringTool. See docs/SWITCH.md.
#
# MUST BE RUN FROM WINDOWS, not WSL: NintendoClang is a Windows executable and
# would be handed Linux paths. scripts/build_switch.sh drives it from WSL
# through Windows interop, the same trick the Cygwin target uses in
# scripts/build_dist.sh.
#
# This target needs two trees that are NOT in this repository and must not be
# committed to it — both are under NDA and available only to registered
# Nintendo developers:
#
# NINTENDO_SDK_ROOT the NintendoSDK (built against 22.2.10)
# SDL_SWITCH_ROOT a checkout of libsdl-org/SDL-switch, licensee-only
#
# Nothing here reproduces SDK source; it describes how this project uses one.
#
# Why a makefile rather than the .vcxproj the SDK expects: the SDK integrates
# with Visual Studio through MSBuild props and targets, and building an NX
# project needs the VSI package. But NintendoClang takes its flags from plain
# config files under Build/ClangConfigs/NX-NXFP2-a64/, so the same compiler
# drives directly and SWOS keeps one build system per target instead of a
# hand-maintained project file that would drift. The flags below are those
# config files reproduced. IF THE SDK IS UPDATED, diff this against
# Build/ClangConfigs/NX-NXFP2-a64/{Compile/C,Link}/Application.cfg.
# ============================================================================
NINTENDO_SDK_ROOT ?= D:/Nintendo/NativeSDK22.2.10/NintendoSDK
SDL_SWITCH_ROOT ?= D:/Projects/GitHub/SDL-switch
NX_TARGET := NX-NXFP2-a64
NX_CLANG := $(NINTENDO_SDK_ROOT)/Compilers/NintendoClang/bin
NX_LIB := $(NINTENDO_SDK_ROOT)/Libraries/$(NX_TARGET)/Develop
CC := $(NX_CLANG)/clang.exe
CXX := $(NX_CLANG)/clang++.exe
AR := $(NX_CLANG)/llvm-ar.exe
BINDIR := bin
TARGET := $(BINDIR)/SWOS.nss
OBJDIR := obj-switch
SRCDIR := src
INCDIR := include
# ---- Sources ---------------------------------------------------------------
# The same src/ as every other target. The Switch is a flavour of the SDL
# build, so nothing is excluded — unlike Makefile.amiga's SDL_ONLY_SRCS.
SOURCES := $(wildcard $(SRCDIR)/*.c) $(wildcard $(SRCDIR)/generated/*.c)
# One C++ TU: the log sink, which exists because NN_LOG is a C++ macro.
SOURCES_CPP := $(wildcard $(SRCDIR)/*.cpp)
OBJECTS := $(patsubst $(SRCDIR)/%.c,$(OBJDIR)/%.o,$(SOURCES)) \
$(patsubst $(SRCDIR)/%.cpp,$(OBJDIR)/%.opp,$(SOURCES_CPP))
DEPS := $(patsubst $(SRCDIR)/%.c,$(OBJDIR)/%.d,$(SOURCES)) \
$(patsubst $(SRCDIR)/%.cpp,$(OBJDIR)/%.d,$(SOURCES_CPP))
# ---- Flags -----------------------------------------------------------------
# Build/ClangConfigs/NX-NXFP2-a64/Compile/C/Application.cfg, verbatim except
# that -x c / -std=gnu17 are dropped: this project is -std=gnu11 and the config
# bakes in a standard the build has to choose for itself.
#
# -fno-common and -fno-short-enums are not cosmetic — the SDK's own libraries
# are built with them and the ABI has to agree.
NX_ARCH := -mcpu=cortex-a57+fp+simd+crypto+crc --target=aarch64-nintendo-nx-elf
NX_COMMON := $(NX_ARCH) -fdata-sections -ffunction-sections -fno-common \
-fno-short-enums -fPIC
# NN_SDK_BUILD_DEVELOP is mandatory: without a build-type macro every SDK
# header stops with "ビルドタイプマクロが定義されていません" — the build type
# macro is not defined.
NX_DEFS := -DNN_SDK_BUILD_DEVELOP \
-DNN_BUILD_CONFIG_SPEC_NX01 \
-DNN_BUILD_CONFIG_OS_HORIZON \
-DSWOS_SWITCH_BUILD=1
# The Amiga "data module" toggles, exactly as the SDL Makefile sets them: the C
# versions of the leagues, tactics, fixtures, palettes and strings replace
# SWOS2.s's data section. NOT optional — without them those files compile to
# almost nothing (tactics_data.o came out at 760 bytes) and the link fails on
# a wall of undefined data symbols like _teamTactics532 and _uefaCup.
DATA_FLAGS := -DCOMPETITION_DATA_C=1 -DTACTICS_DATA_C=1 -DFIXTURE_DATA_C=1 \
-DSIMULATION_DATA_C=1 -DPLAYER_DATA_C=1 -DRENDER_DATA_C=1 \
-DPALETTE_DATA_C=1 -DSTRINGS_DATA_C=1 -DGENERAL_DATA_C=1
# The warning set is the SDL Makefile's, for the same reasons documented there
# (packed Amiga layouts, deliberate BSS-adjacency reads, the tactics extractor's
# braces). Three more are clang-only, and each was measured rather than guessed
# by compiling the whole tree with this compiler:
# -Wno-pointer-to-int-cast display.c stores pointers in the Amiga's 32-bit
# bitplane registers; gcc does not warn, clang does.
# -Wno-comment a handful of pre-existing "/*" inside block
# comments (frontend.c, save_format_json.c).
# -Wno-unknown-pragmas #pragma GCC optimize(...) in four files, which
# clang ignores; the SDL build relies on it, so
# the Switch build is one optimisation short there
# and that is not known to matter.
CFLAGS := $(NX_COMMON) $(NX_DEFS) $(DATA_FLAGS) -O2 -g -std=gnu11 \
-fno-strict-aliasing -fno-builtin -MMD -MP \
-D_FILE_OFFSET_BITS=64 \
-Wall -Wno-unused-function -Wno-unused-variable \
-Wno-unused-but-set-variable -Wno-attributes -Wno-multichar \
-Wno-pointer-sign -Wno-int-conversion -Wno-incompatible-pointer-types \
-Wno-braces -Wno-address-of-packed-member -Wno-strict-aliasing \
-Wno-array-bounds -Wno-unknown-warning-option \
-Wno-pointer-to-int-cast -Wno-comment -Wno-unknown-pragmas \
-include $(INCDIR)/swos_platform.h \
-I$(SRCDIR) -I$(INCDIR) \
-isystem $(SDL_SWITCH_ROOT)/include \
-isystem $(NINTENDO_SDK_ROOT)/Include \
-isystem $(NINTENDO_SDK_ROOT)/TargetSpecificInclude/$(NX_TARGET)
# Escape hatch for one-off defines, the same one Makefile.amiga carries. This
# target has no SWOS_* knobs of its own, so without it a drop built by
# scripts/build_dist.sh would ship every other platform with
# SWOS_TEST_NO_INJURIES_CARDS=1 and the console without it — and a tester
# comparing the two files bugs for non-bugs (see .claude/CLAUDE.md).
# Deliberately NOT added to SDL_CFLAGS/SDL_CXXFLAGS: the fork is third-party
# code and none of the game's toggles mean anything inside it.
CFLAGS += $(EXTRA_CFLAGS)
# Build/ClangConfigs/NX-NXFP2-a64/Link/Application.cfg. -pie and the ldscript
# are what make the output loadable at all.
LDFLAGS := $(NX_ARCH) -nostartfiles -fuse-ld=lld.exe -pie \
-Wl,--pack-dyn-relocs=relr -Wl,--gc-sections \
-Wl,-init=_init -Wl,-fini=_fini \
-Wl,-z,combreloc -Wl,-z,relro -Wl,--enable-new-dtags \
--runtime-libs-root=$(NINTENDO_SDK_ROOT)/Compilers/NintendoClang/lib/NX \
-Wl,-T,$(NINTENDO_SDK_ROOT)/Resources/SpecFiles/Application.aarch64.lp64.ldscript
# ORDER MATTERS. All of these are referenced by nnSdk, which comes after them:
# rocrt.o the startup object. NOT optional — it supplies the
# .rocrt.init section the linker script places first in
# .text, and with it the MOD0 header the loader reads.
# Without it everything links, the ELF looks healthy,
# MakeNso produces a structurally valid NSO — and the
# console refuses to launch it.
# nnApplication.o carries __nnDetailNintendoSdkRuntimeObjectFile.
# libnn_init_memory.a where malloc comes from; the platform has no C runtime
# of its own to fall back on.
NX_SYSLIBS := $(NX_LIB)/rocrt.o \
$(NX_LIB)/nnApplication.o \
$(NX_LIB)/libnn_init_memory.a \
$(NX_LIB)/nnSdk.nss \
$(NX_LIB)/crtend.o
# SDL's NX video backend renders through GL, so the GL loader and the two
# system shared libraries come along. They are also why the packaged code
# directory needs subsdk0/subsdk1 (scripts/build_switch.bat).
NX_GFXLIBS := $(NX_LIB)/libnn_gll.a $(NX_LIB)/opengl.nss $(NX_LIB)/vulkan.nss
# ---- The SDL fork ----------------------------------------------------------
# Built here rather than expected as a prebuilt artifact, so this target needs
# nothing but a checkout. Its own README recommends building it as part of the
# project: it needs no special defines, because NintendoClang predefines
# __NINTENDO__ and __NX, so SDL_platform.h sets __NINTENDO_SWITCH__ and SDL
# picks SDL_config_nintendoswitch.h by itself.
#
# Only these directories belong in an NX build — src/ carries every platform's
# backend. main/nintendoswitch is SDL's nnMain, which calls SDL_main: our
# main(). Set SDL_LIB to point at a prebuilt library instead if you have one.
SDL_NX_DIRS := atomic cpuinfo dynapi events file power render render/opengl \
render/opengles2 render/software sensor sensor/dummy stdlib \
thread thread/pthread timer timer/unix video video/dummy \
video/yuv2rgb audio audio/disk audio/dummy audio/nintendoswitch \
filesystem/nintendoswitch haptic haptic/nintendoswitch \
joystick joystick/nintendoswitch loadso/nintendoswitch \
locale locale/nintendoswitch misc misc/nintendoswitch \
video/nintendoswitch main/nintendoswitch
# EVERY nintendoswitch backend is C++, including main/nintendoswitch, which is
# where nnMain lives — glob .c only and the link fails on `undefined reference:
# nnMain` with nothing to say it was a file-extension problem.
SDL_SOURCES := $(wildcard $(SDL_SWITCH_ROOT)/src/*.c) \
$(foreach d,$(SDL_NX_DIRS),$(wildcard $(SDL_SWITCH_ROOT)/src/$(d)/*.c))
SDL_SOURCES_CPP := $(foreach d,$(SDL_NX_DIRS),$(wildcard $(SDL_SWITCH_ROOT)/src/$(d)/*.cpp))
SDL_OBJECTS := $(patsubst $(SDL_SWITCH_ROOT)/src/%.c,$(OBJDIR)/sdl/%.o,$(SDL_SOURCES)) \
$(patsubst $(SDL_SWITCH_ROOT)/src/%.cpp,$(OBJDIR)/sdl/%.opp,$(SDL_SOURCES_CPP))
SDL_BUILT := $(OBJDIR)/libSDL2_nx.a
SDL_LIB ?= $(SDL_BUILT)
# 2010s C with every platform's backend in one tree; its warnings are not this
# project's to fix, so -w rather than the game's warning set.
SDL_CFLAGS := $(NX_COMMON) $(NX_DEFS) -O2 -std=gnu11 -w -MMD -MP \
-I$(SDL_SWITCH_ROOT)/include \
-isystem $(NINTENDO_SDK_ROOT)/Include \
-isystem $(NINTENDO_SDK_ROOT)/TargetSpecificInclude/$(NX_TARGET)
$(OBJDIR)/sdl/%.o: $(SDL_SWITCH_ROOT)/src/%.c
@mkdir -p $(dir $@)
@echo " CC sdl/$*.c"
@$(CC) $(SDL_CFLAGS) -c $< -o $@
# Build/ClangConfigs/NX-NXFP2-a64/Compile/Cxx/Application.cfg is -std=gnu++20.
# -fno-use-cxa-atexit because nnSdk exports 44 __cxa_* symbols but NOT
# __cxa_atexit, so static destructors registered the usual way have nothing to
# register with; the platform provides plain atexit and expects that.
SDL_CXXFLAGS := $(NX_COMMON) $(NX_DEFS) -O2 -std=gnu++20 -fno-use-cxa-atexit -w \
-MMD -MP -I$(SDL_SWITCH_ROOT)/include \
-isystem $(NINTENDO_SDK_ROOT)/Include \
-isystem $(NINTENDO_SDK_ROOT)/TargetSpecificInclude/$(NX_TARGET)
$(OBJDIR)/sdl/%.opp: $(SDL_SWITCH_ROOT)/src/%.cpp
@mkdir -p $(dir $@)
@echo " CXX sdl/$*.cpp"
@$(CXX) $(SDL_CXXFLAGS) -c $< -o $@
$(SDL_BUILT): $(SDL_OBJECTS)
@echo " AR $@"
@$(AR) rcs $@ $(SDL_OBJECTS)
SDL_DEPS := $(patsubst $(SDL_SWITCH_ROOT)/src/%.c,$(OBJDIR)/sdl/%.d,$(SDL_SOURCES)) \
$(patsubst $(SDL_SWITCH_ROOT)/src/%.cpp,$(OBJDIR)/sdl/%.d,$(SDL_SOURCES_CPP))
.DEFAULT_GOAL := all
.PHONY: all clean
all: $(TARGET)
$(TARGET): $(OBJECTS) $(SDL_LIB) | $(BINDIR)
@echo " LINK $@"
@$(CXX) $(LDFLAGS) -o $@ $(OBJECTS) $(SDL_LIB) $(NX_GFXLIBS) $(NX_SYSLIBS)
$(OBJDIR)/%.o: $(SRCDIR)/%.c
@mkdir -p $(dir $@)
@echo " CC $<"
@$(CC) $(CFLAGS) -c $< -o $@
# Our own C++ (platform_switch.cpp). No -include swos_platform.h: it defines
# the fprintf/printf redirect that this file is the other end of.
$(OBJDIR)/%.opp: $(SRCDIR)/%.cpp
@mkdir -p $(dir $@)
@echo " CXX $<"
@$(CXX) $(NX_COMMON) $(NX_DEFS) -O2 -g -std=gnu++20 -fno-use-cxa-atexit \
-MMD -MP -Wall -I$(SRCDIR) -I$(INCDIR) \
-isystem $(NINTENDO_SDK_ROOT)/Include \
-isystem $(NINTENDO_SDK_ROOT)/TargetSpecificInclude/$(NX_TARGET) \
-c $< -o $@
$(BINDIR):
@mkdir -p $@
clean:
@rm -rf $(OBJDIR) $(TARGET)
@echo " CLEAN switch"
-include $(DEPS)
-include $(SDL_DEPS)