Skip to content

Commit 6da03ae

Browse files
committed
set up service and log
1 parent 637b271 commit 6da03ae

File tree

5 files changed

+249
-1
lines changed

5 files changed

+249
-1
lines changed

Makefile

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
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+
include $(DEVKITPRO)/libnx/switch_rules
10+
11+
#---------------------------------------------------------------------------------
12+
# TARGET is the name of the output
13+
# SOURCES is a list of directories containing source code
14+
# DATA is a list of directories containing data files
15+
# INCLUDES is a list of directories containing header files
16+
#---------------------------------------------------------------------------------
17+
TARGET := eiffel
18+
SOURCES := src
19+
DATA := data
20+
INCLUDES := include
21+
22+
#---------------------------------------------------------------------------------
23+
# options for code generation
24+
#---------------------------------------------------------------------------------
25+
ARCH := -march=armv8-a+crc+crypto -mtune=cortex-a57 -mtp=soft -fPIC -ftls-model=local-exec
26+
27+
CFLAGS := -g -Wall -Werror \
28+
-ffunction-sections \
29+
-fdata-sections \
30+
$(ARCH) \
31+
$(BUILD_CFLAGS)
32+
33+
CFLAGS += $(INCLUDE)
34+
35+
CXXFLAGS := $(CFLAGS) -fno-rtti -fno-exceptions
36+
37+
ASFLAGS := -g $(ARCH)
38+
39+
#---------------------------------------------------------------------------------
40+
# list of directories containing libraries, this must be the top level containing
41+
# include and lib
42+
#---------------------------------------------------------------------------------
43+
LIBDIRS := $(PORTLIBS) $(LIBNX)
44+
45+
#---------------------------------------------------------------------------------
46+
# no real need to edit anything past this point unless you need to add additional
47+
# rules for different file extensions
48+
#---------------------------------------------------------------------------------
49+
ifneq ($(BUILD),$(notdir $(CURDIR)))
50+
#---------------------------------------------------------------------------------
51+
52+
export VPATH := $(foreach dir,$(SOURCES),$(CURDIR)/$(dir)) \
53+
$(foreach dir,$(DATA),$(CURDIR)/$(dir))
54+
55+
CFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c)))
56+
CPPFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.cpp)))
57+
SFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.s)))
58+
BINFILES := $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.*)))
59+
60+
#---------------------------------------------------------------------------------
61+
# use CXX for linking C++ projects, CC for standard C
62+
#---------------------------------------------------------------------------------
63+
ifeq ($(strip $(CPPFILES)),)
64+
#---------------------------------------------------------------------------------
65+
export LD := $(CC)
66+
#---------------------------------------------------------------------------------
67+
else
68+
#---------------------------------------------------------------------------------
69+
export LD := $(CXX)
70+
#---------------------------------------------------------------------------------
71+
endif
72+
#---------------------------------------------------------------------------------
73+
74+
export OFILES_BIN := $(addsuffix .o,$(BINFILES))
75+
export OFILES_SRC := $(CPPFILES:.cpp=.o) $(CFILES:.c=.o) $(SFILES:.s=.o)
76+
export OFILES := $(OFILES_BIN) $(OFILES_SRC)
77+
export HFILES := $(addsuffix .h,$(subst .,_,$(BINFILES)))
78+
79+
export INCLUDE := $(foreach dir,$(INCLUDES),-I$(CURDIR)/$(dir)) \
80+
$(foreach dir,$(LIBDIRS),-I$(dir)/include) \
81+
-I$(CURDIR)/$(BUILD)
82+
83+
.PHONY: clean all
84+
85+
#---------------------------------------------------------------------------------
86+
all: lib/lib$(TARGET).a lib/lib$(TARGET)d.a
87+
88+
lib:
89+
@[ -d $@ ] || mkdir -p $@
90+
91+
release:
92+
@[ -d $@ ] || mkdir -p $@
93+
94+
debug:
95+
@[ -d $@ ] || mkdir -p $@
96+
97+
lib/lib$(TARGET).a : lib release $(SOURCES) $(INCLUDES)
98+
@$(MAKE) BUILD=release OUTPUT=$(CURDIR)/$@ \
99+
BUILD_CFLAGS="-DNDEBUG=1 -O2" \
100+
DEPSDIR=$(CURDIR)/release \
101+
--no-print-directory -C release \
102+
-f $(CURDIR)/Makefile
103+
104+
lib/lib$(TARGET)d.a : lib debug $(SOURCES) $(INCLUDES)
105+
@$(MAKE) BUILD=debug OUTPUT=$(CURDIR)/$@ \
106+
BUILD_CFLAGS="-DDEBUG=1 -Og" \
107+
DEPSDIR=$(CURDIR)/debug \
108+
--no-print-directory -C debug \
109+
-f $(CURDIR)/Makefile
110+
111+
dist-bin: all
112+
@tar --exclude=*~ -cjf lib$(TARGET).tar.bz2 include lib
113+
114+
dist-src:
115+
@tar --exclude=*~ -cjf lib$(TARGET)-src.tar.bz2 include source Makefile
116+
117+
dist: dist-src dist-bin
118+
119+
#---------------------------------------------------------------------------------
120+
clean:
121+
@echo clean ...
122+
@rm -fr release debug lib *.bz2
123+
124+
#---------------------------------------------------------------------------------
125+
else
126+
127+
DEPENDS := $(OFILES:.o=.d)
128+
129+
#---------------------------------------------------------------------------------
130+
# main targets
131+
#---------------------------------------------------------------------------------
132+
$(OUTPUT) : $(OFILES)
133+
134+
$(OFILES_SRC) : $(HFILES)
135+
136+
#---------------------------------------------------------------------------------
137+
%_bin.h %.bin.o : %.bin
138+
#---------------------------------------------------------------------------------
139+
@echo $(notdir $<)
140+
@$(bin2o)
141+
142+
143+
-include $(DEPENDS)
144+
145+
#---------------------------------------------------------------------------------------
146+
endif
147+
#---------------------------------------------------------------------------------------
148+

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
# libeiffle
1+
# libeiffel

include/eiffel.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#pragma once
2+
3+
#include <switch.h>
4+
5+
#define EIFFEL_SERVICE_NAME "efl"
6+
7+
typedef enum {
8+
EFL_CMD_LOG = 0,
9+
} EiffelCommandId;
10+
11+
typedef enum {
12+
EFL_LOG_LEVEL_INFO = 0,
13+
EFL_LOG_LEVEL_WARNING = 1,
14+
EFL_LOG_LEVEL_ERROR = 2,
15+
} EiffelLogLevel;
16+
17+
Result eiffelInitialize();
18+
void eiffelExit();
19+
20+
Result eiffelLog(const char* moduleName, EiffelLogLevel level, const char* logContent);

src/eiffel.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include "eiffel.h"
2+
3+
#include <string.h>
4+
5+
#include "service_guard.h"
6+
7+
static Service g_eiffelSrv;
8+
9+
NX_GENERATE_SERVICE_GUARD(eiffel);
10+
11+
Result _eiffelInitialize() { return smGetService(&g_eiffelSrv, EIFFEL_SERVICE_NAME); }
12+
13+
void _eiffelCleanup(void) { serviceClose(&g_eiffelSrv); }
14+
15+
Result eiffelLog(const char* moduleName, EiffelLogLevel level, const char* logContent) {
16+
return serviceDispatchIn(&g_eiffelSrv, EFL_CMD_LOG, level,
17+
.buffer_attrs =
18+
{
19+
SfBufferAttr_HipcMapAlias | SfBufferAttr_In,
20+
SfBufferAttr_HipcMapAlias | SfBufferAttr_In,
21+
},
22+
.buffers = {
23+
{moduleName, strlen(moduleName) + 1},
24+
{logContent, strlen(logContent) + 1},
25+
});
26+
}

src/service_guard.h

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright 2017-2020 libnx Authors
2+
3+
// Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby
4+
// granted, provided that the above copyright notice and this permission notice appear in all copies.
5+
6+
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
7+
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
8+
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
9+
// AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
10+
// PERFORMANCE OF THIS SOFTWARE.
11+
12+
#pragma once
13+
#include <switch.h>
14+
15+
typedef struct ServiceGuard {
16+
Mutex mutex;
17+
u32 refCount;
18+
} ServiceGuard;
19+
20+
NX_INLINE bool serviceGuardBeginInit(ServiceGuard* g) {
21+
mutexLock(&g->mutex);
22+
return (g->refCount++) == 0;
23+
}
24+
25+
NX_INLINE Result serviceGuardEndInit(ServiceGuard* g, Result rc, void (*cleanupFunc)(void)) {
26+
if (R_FAILED(rc)) {
27+
cleanupFunc();
28+
--g->refCount;
29+
}
30+
mutexUnlock(&g->mutex);
31+
return rc;
32+
}
33+
34+
NX_INLINE void serviceGuardExit(ServiceGuard* g, void (*cleanupFunc)(void)) {
35+
mutexLock(&g->mutex);
36+
if (g->refCount && (--g->refCount) == 0) cleanupFunc();
37+
mutexUnlock(&g->mutex);
38+
}
39+
40+
#define NX_GENERATE_SERVICE_GUARD_PARAMS(name, _paramdecl, _parampass) \
41+
\
42+
static ServiceGuard g_##name##Guard; \
43+
NX_INLINE Result _##name##Initialize _paramdecl; \
44+
static void _##name##Cleanup(void); \
45+
\
46+
Result name##Initialize _paramdecl { \
47+
Result rc = 0; \
48+
if (serviceGuardBeginInit(&g_##name##Guard)) rc = _##name##Initialize _parampass; \
49+
return serviceGuardEndInit(&g_##name##Guard, rc, _##name##Cleanup); \
50+
} \
51+
\
52+
void name##Exit(void) { serviceGuardExit(&g_##name##Guard, _##name##Cleanup); }
53+
54+
#define NX_GENERATE_SERVICE_GUARD(name) NX_GENERATE_SERVICE_GUARD_PARAMS(name, (void), ())

0 commit comments

Comments
 (0)