Skip to content

Commit 312acae

Browse files
template application from libnx switch-examples
1 parent bf99210 commit 312acae

File tree

2 files changed

+266
-0
lines changed

2 files changed

+266
-0
lines changed

Makefile

Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
#---------------------------------------------------------------------------------
2+
.SUFFIXES:
3+
#---------------------------------------------------------------------------------
4+
5+
ifeq ($(strip $(DEVKITPRO)),)
6+
$(error "Please set DEVKITPRO in your environment. export DEVKITPRO=<path to>/devkitpro")
7+
endif
8+
9+
TOPDIR ?= $(CURDIR)
10+
include $(DEVKITPRO)/libnx/switch_rules
11+
12+
#---------------------------------------------------------------------------------
13+
# TARGET is the name of the output
14+
# BUILD is the directory where object files & intermediate files will be placed
15+
# SOURCES is a list of directories containing source code
16+
# DATA is a list of directories containing data files
17+
# INCLUDES is a list of directories containing header files
18+
# ROMFS is the directory containing data to be added to RomFS, relative to the Makefile (Optional)
19+
#
20+
# NO_ICON: if set to anything, do not use icon.
21+
# NO_NACP: if set to anything, no .nacp file is generated.
22+
# APP_TITLE is the name of the app stored in the .nacp file (Optional)
23+
# APP_AUTHOR is the author of the app stored in the .nacp file (Optional)
24+
# APP_VERSION is the version of the app stored in the .nacp file (Optional)
25+
# APP_TITLEID is the titleID of the app stored in the .nacp file (Optional)
26+
# ICON is the filename of the icon (.jpg), relative to the project folder.
27+
# If not set, it attempts to use one of the following (in this order):
28+
# - <Project name>.jpg
29+
# - icon.jpg
30+
# - <libnx folder>/default_icon.jpg
31+
#
32+
# CONFIG_JSON is the filename of the NPDM config file (.json), relative to the project folder.
33+
# If not set, it attempts to use one of the following (in this order):
34+
# - <Project name>.json
35+
# - config.json
36+
# If a JSON file is provided or autodetected, an ExeFS PFS0 (.nsp) is built instead
37+
# of a homebrew executable (.nro). This is intended to be used for sysmodules.
38+
# NACP building is skipped as well.
39+
#---------------------------------------------------------------------------------
40+
TARGET := $(notdir $(CURDIR))
41+
BUILD := build
42+
SOURCES := source
43+
DATA := data
44+
INCLUDES := include
45+
#ROMFS := romfs
46+
47+
#---------------------------------------------------------------------------------
48+
# options for code generation
49+
#---------------------------------------------------------------------------------
50+
ARCH := -march=armv8-a -mtune=cortex-a57 -mtp=soft -fPIE
51+
52+
CFLAGS := -g -Wall -O2 -ffunction-sections \
53+
$(ARCH) $(DEFINES)
54+
55+
CFLAGS += $(INCLUDE) -D__SWITCH__
56+
57+
CXXFLAGS := $(CFLAGS) -fno-rtti -fno-exceptions
58+
59+
ASFLAGS := -g $(ARCH)
60+
LDFLAGS = -specs=$(DEVKITPRO)/libnx/switch.specs -g $(ARCH) -Wl,-Map,$(notdir $*.map)
61+
62+
LIBS := -lnx
63+
64+
#---------------------------------------------------------------------------------
65+
# list of directories containing libraries, this must be the top level containing
66+
# include and lib
67+
#---------------------------------------------------------------------------------
68+
LIBDIRS := $(PORTLIBS) $(LIBNX)
69+
70+
71+
#---------------------------------------------------------------------------------
72+
# no real need to edit anything past this point unless you need to add additional
73+
# rules for different file extensions
74+
#---------------------------------------------------------------------------------
75+
ifneq ($(BUILD),$(notdir $(CURDIR)))
76+
#---------------------------------------------------------------------------------
77+
78+
export OUTPUT := $(CURDIR)/$(TARGET)
79+
export TOPDIR := $(CURDIR)
80+
81+
export VPATH := $(foreach dir,$(SOURCES),$(CURDIR)/$(dir)) \
82+
$(foreach dir,$(DATA),$(CURDIR)/$(dir))
83+
84+
export DEPSDIR := $(CURDIR)/$(BUILD)
85+
86+
CFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c)))
87+
CPPFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.cpp)))
88+
SFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.s)))
89+
BINFILES := $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.*)))
90+
91+
#---------------------------------------------------------------------------------
92+
# use CXX for linking C++ projects, CC for standard C
93+
#---------------------------------------------------------------------------------
94+
ifeq ($(strip $(CPPFILES)),)
95+
#---------------------------------------------------------------------------------
96+
export LD := $(CC)
97+
#---------------------------------------------------------------------------------
98+
else
99+
#---------------------------------------------------------------------------------
100+
export LD := $(CXX)
101+
#---------------------------------------------------------------------------------
102+
endif
103+
#---------------------------------------------------------------------------------
104+
105+
export OFILES_BIN := $(addsuffix .o,$(BINFILES))
106+
export OFILES_SRC := $(CPPFILES:.cpp=.o) $(CFILES:.c=.o) $(SFILES:.s=.o)
107+
export OFILES := $(OFILES_BIN) $(OFILES_SRC)
108+
export HFILES_BIN := $(addsuffix .h,$(subst .,_,$(BINFILES)))
109+
110+
export INCLUDE := $(foreach dir,$(INCLUDES),-I$(CURDIR)/$(dir)) \
111+
$(foreach dir,$(LIBDIRS),-I$(dir)/include) \
112+
-I$(CURDIR)/$(BUILD)
113+
114+
export LIBPATHS := $(foreach dir,$(LIBDIRS),-L$(dir)/lib)
115+
116+
ifeq ($(strip $(CONFIG_JSON)),)
117+
jsons := $(wildcard *.json)
118+
ifneq (,$(findstring $(TARGET).json,$(jsons)))
119+
export APP_JSON := $(TOPDIR)/$(TARGET).json
120+
else
121+
ifneq (,$(findstring config.json,$(jsons)))
122+
export APP_JSON := $(TOPDIR)/config.json
123+
endif
124+
endif
125+
else
126+
export APP_JSON := $(TOPDIR)/$(CONFIG_JSON)
127+
endif
128+
129+
ifeq ($(strip $(ICON)),)
130+
icons := $(wildcard *.jpg)
131+
ifneq (,$(findstring $(TARGET).jpg,$(icons)))
132+
export APP_ICON := $(TOPDIR)/$(TARGET).jpg
133+
else
134+
ifneq (,$(findstring icon.jpg,$(icons)))
135+
export APP_ICON := $(TOPDIR)/icon.jpg
136+
endif
137+
endif
138+
else
139+
export APP_ICON := $(TOPDIR)/$(ICON)
140+
endif
141+
142+
ifeq ($(strip $(NO_ICON)),)
143+
export NROFLAGS += --icon=$(APP_ICON)
144+
endif
145+
146+
ifeq ($(strip $(NO_NACP)),)
147+
export NROFLAGS += --nacp=$(CURDIR)/$(TARGET).nacp
148+
endif
149+
150+
ifneq ($(APP_TITLEID),)
151+
export NACPFLAGS += --titleid=$(APP_TITLEID)
152+
endif
153+
154+
ifneq ($(ROMFS),)
155+
export NROFLAGS += --romfsdir=$(CURDIR)/$(ROMFS)
156+
endif
157+
158+
.PHONY: $(BUILD) clean all
159+
160+
#---------------------------------------------------------------------------------
161+
all: $(BUILD)
162+
163+
$(BUILD):
164+
@[ -d $@ ] || mkdir -p $@
165+
@$(MAKE) --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile
166+
167+
#---------------------------------------------------------------------------------
168+
clean:
169+
@echo clean ...
170+
ifeq ($(strip $(APP_JSON)),)
171+
@rm -fr $(BUILD) $(TARGET).nro $(TARGET).nacp $(TARGET).elf
172+
else
173+
@rm -fr $(BUILD) $(TARGET).nsp $(TARGET).nso $(TARGET).npdm $(TARGET).elf
174+
endif
175+
176+
177+
#---------------------------------------------------------------------------------
178+
else
179+
.PHONY: all
180+
181+
DEPENDS := $(OFILES:.o=.d)
182+
183+
#---------------------------------------------------------------------------------
184+
# main targets
185+
#---------------------------------------------------------------------------------
186+
ifeq ($(strip $(APP_JSON)),)
187+
188+
all : $(OUTPUT).nro
189+
190+
ifeq ($(strip $(NO_NACP)),)
191+
$(OUTPUT).nro : $(OUTPUT).elf $(OUTPUT).nacp
192+
else
193+
$(OUTPUT).nro : $(OUTPUT).elf
194+
endif
195+
196+
else
197+
198+
all : $(OUTPUT).nsp
199+
200+
$(OUTPUT).nsp : $(OUTPUT).nso $(OUTPUT).npdm
201+
202+
$(OUTPUT).nso : $(OUTPUT).elf
203+
204+
endif
205+
206+
$(OUTPUT).elf : $(OFILES)
207+
208+
$(OFILES_SRC) : $(HFILES_BIN)
209+
210+
#---------------------------------------------------------------------------------
211+
# you need a rule like this for each extension you use as binary data
212+
#---------------------------------------------------------------------------------
213+
%.bin.o %_bin.h : %.bin
214+
#---------------------------------------------------------------------------------
215+
@echo $(notdir $<)
216+
@$(bin2o)
217+
218+
-include $(DEPENDS)
219+
220+
#---------------------------------------------------------------------------------------
221+
endif
222+
#---------------------------------------------------------------------------------------

source/main.c

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Include the most common headers from the C standard library
2+
#include <stdio.h>
3+
#include <stdlib.h>
4+
#include <string.h>
5+
6+
// Include the main libnx system header, for Switch development
7+
#include <switch.h>
8+
9+
// Main program entrypoint
10+
int main(int argc, char* argv[])
11+
{
12+
// This example uses a text console, as a simple way to output text to the screen.
13+
// If you want to write a software-rendered graphics application,
14+
// take a look at the graphics/simplegfx example, which uses the libnx Framebuffer API instead.
15+
// If on the other hand you want to write an OpenGL based application,
16+
// take a look at the graphics/opengl set of examples, which uses EGL instead.
17+
consoleInit(NULL);
18+
19+
// Other initialization goes here. As a demonstration, we print hello world.
20+
printf("Hello World!\n");
21+
22+
// Main loop
23+
while (appletMainLoop())
24+
{
25+
// Scan all the inputs. This should be done once for each frame
26+
hidScanInput();
27+
28+
// hidKeysDown returns information about which buttons have been
29+
// just pressed in this frame compared to the previous one
30+
u64 kDown = hidKeysDown(CONTROLLER_P1_AUTO);
31+
32+
if (kDown & KEY_PLUS)
33+
break; // break in order to return to hbmenu
34+
35+
// Your code goes here
36+
37+
// Update the console, sending a new frame to the display
38+
consoleUpdate(NULL);
39+
}
40+
41+
// Deinitialize and clean up resources used by the console (important!)
42+
consoleExit(NULL);
43+
return 0;
44+
}

0 commit comments

Comments
 (0)