Skip to content

Commit 3f71ae1

Browse files
demo/sdl3_renderer: Rewrite SDL3 Makefile to support Emscripten (#894)
* fix(demo/sdl3_renderer): Makefile should now support Win + Emsdk Fixed several issues that would prevent this demo from being compilable on Windows with Emscripten: (1) there was a typo preventing proper resolution of BINEXT, (2) in case of using Emscripten, Windows was taking a precedense due to OS always being defined, (3) BINEXT now has a special case for Emscipten, (4) pkg-config provided by w64devkit has no --path flag, so we retry with --exists too, (5) compiler invocation was missing system flags, which could break on some MSYS2 runtimes, (6) compiler invocation now uses all kinds of C/CPP/LD flags so you can easily inject something like --shell-file now, and probably more... This was done in order to help with implementing and testing for: #888 * refactor(demo/sdl3_renderer): consistent syntax and flow in Makefile Use ${} for variable expansion and $() for invoking functions. Use $(or ...) for all branches instead of ifeq(...) blocks. Hopefully, this will make it easier to read and understand. * fix(demo/sdl3_renderer): append user provided C/CPP/LD flags in Makefile Before it was overwriting those flags entirely, meaning that it could delete some required ones (e.g. from pkg-config). Now it will append them correctly, so your flags will take priority where it matters.
1 parent a329721 commit 3f71ae1

1 file changed

Lines changed: 42 additions & 27 deletions

File tree

demo/sdl3_renderer/Makefile

Lines changed: 42 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,56 @@
11
# this Makefile is specific to GNU Make and GCC'compatible compilers
22

3-
PKG_CONFIG ?= $(shell command -v pkg-config)
4-
ifeq (,$(PKG_CONFIG))
5-
$(error missing pkg-config utility!)
6-
endif
3+
PKG_CONFIG := $(or\
4+
$(shell pkg-config --version >/dev/null 2>/dev/null && echo pkg-config),\
5+
$(shell command -v pkg-config 2>/dev/null),\
6+
$(error missing pkg-config utility!))
77

8-
PKG_SDL3 ?= sdl3
9-
ifeq (,$(shell $(PKG_CONFIG) --path $(PKG_SDL3)))
10-
$(error $(PKG_CONFIG) could not find: $(PKG_SDL3))
11-
endif
8+
PKG_SDL3 := $(or\
9+
$(and $(shell ${PKG_CONFIG} sdl3 --path 2>/dev/null),sdl3),\
10+
$(shell ${PKG_CONFIG} sdl3 --exists 2>/dev/null && echo sdl3),\
11+
$(error pkg-config was unable to find: sdl3))
1212

13-
OS ?= $(shell uname -s)
14-
BINEXT-Windows_NT = .exe
15-
BINEXT ?= $(BINEXIT-$(OS))
13+
OSNAME := $(or\
14+
$(and ${EMSCRIPTEN}, Emscripten),\
15+
${OS},\
16+
$(shell uname -s),\
17+
$(error could not detect OSNAME))
1618

17-
TEMPDIR ?= ./bin
18-
BIN ?= $(TEMPDIR)/demo$(BINEXT)
19+
binext_Windows_NT := .exe
20+
binext_Emscripten := .html
21+
BINEXT := ${binext_${OSNAME}}
1922

20-
CFLAGS += -std=c89 -Wall -Wextra -Wpedantic
21-
CFLAGS += -O2
22-
#CFLAGS += -O0 -g
23-
#CFLAGS += -fsanitize=address
24-
#CFLAGS += -fsanitize=undefined
25-
CFLAGS += $(shell $(PKG_CONFIG) $(PKG_SDL3) --cflags)
23+
TEMPDIR := ./bin
24+
BIN := ${TEMPDIR}/demo${BINEXT}
2625

27-
LIBS += -lm
28-
LIBS += $(shell $(PKG_CONFIG) $(PKG_SDL3) --libs)
26+
cflags += -std=c89 -Wall -Wextra -Wpedantic
27+
cflags += -O2
28+
#cflags += -O0 -g
29+
#cflags += -fsanitize=address
30+
#cflags += -fsanitize=undefined
31+
cflags += ${CFLAGS}
2932

30-
DEP ?= $(BIN).d
33+
cppflags += $(shell ${PKG_CONFIG} ${PKG_SDL3} --cflags --keep-system-cflags)
34+
cppflags += ${CPPFLAGS}
3135

32-
SRC = main.c
36+
ldflags += $(shell ${PKG_CONFIG} ${PKG_SDL3} --libs-only-L --libs-only-other --keep-system-libs)
37+
ldflags += ${LDFLAGS}
3338

34-
$(BIN):
39+
ldlibs += $(shell ${PKG_CONFIG} ${PKG_SDL3} --libs-only-l --keep-system-libs)
40+
ldlibs += -lm
41+
ldlibs += ${LDLIBS}
42+
# HACK: this one is for compatibility with other demos
43+
ldlibs += ${LIBS}
44+
45+
DEP := ${TEMPDIR}/$(notdir ${BIN}).d
46+
47+
SRC := main.c
48+
49+
${BIN}:
3550
mkdir -p $(dir $@)
36-
$(CC) $(SRC) -o $@ -MD -MF $(DEP) $(CFLAGS) $(LIBS)
51+
${CC} ${SRC} -o $@ -MD -MF ${DEP} ${cppflags} ${ldflags} ${ldlibs} ${cflags}
3752

38-
$(BIN): $(SRC) ./Makefile ./nuklear_sdl3_renderer.h ./../../nuklear.h
53+
${BIN}: ${SRC}
3954

40-
-include $(DEP)
55+
-include ${DEP}
4156

0 commit comments

Comments
 (0)