From 4860d33525e02d340744deb64f73250d970c24af Mon Sep 17 00:00:00 2001
From: Izuru Yakumo <eternal-servant@yakumo.dev>
Date: Tue, 24 Jun 2025 22:25:15 -0300
Subject: [PATCH 1/2] Various build fixes for BSD systems

Signed-off-by: Izuru Yakumo <eternal-servant@yakumo.dev>
---
 engine/Makefile      | 82 +++++++++++++++++++++++++++++++++-----------
 engine/makeconfig.sh |  4 +++
 2 files changed, 66 insertions(+), 20 deletions(-)

diff --git a/engine/Makefile b/engine/Makefile
index c455f6709..68363e6ce 100644
--- a/engine/Makefile
+++ b/engine/Makefile
@@ -448,6 +448,8 @@ ifeq ($(FTE_TARGET),dos)
 	CXX=i586-pc-msdosdjgpp-g++
 	STRIP=i586-pc-msdosdjgpp-strip
 	CFLAGS+=-DNO_ZLIB
+	FMT=fmt
+	SED=sed
 endif
 
 #if you have an x86, you can get gcc to build binaries using 3 different ABIs, instead of builds for just the default ABI
@@ -457,6 +459,8 @@ ifeq ($(FTE_TARGET),linux32)
 	CXX=g++ -m32
 	STRIP=strip
 	BITS=32
+	FMT=fmt
+	SED=sed
 endif
 ifeq ($(FTE_TARGET),linux_x86)
 	FTE_TARGET=linux
@@ -465,6 +469,8 @@ ifeq ($(FTE_TARGET),linux_x86)
 	CXX=i686-linux-gnu-g++
 	STRIP=i686-linux-gnu-strip
 	BITS=32
+	FMT=fmt
+	SED=sed
 endif
 ifeq ($(FTE_TARGET),linux_amd64)
 	FTE_TARGET=linux
@@ -473,6 +479,8 @@ ifeq ($(FTE_TARGET),linux_amd64)
 	CXX=x86_64-linux-gnu-g++
 	STRIP=x86_64-linux-gnu-strip
 	BITS=64
+	FMT=fmt
+	SED=sed
 endif
 ifeq ($(FTE_TARGET),linux_armhf)
 	#debian's armhf is armv7, but armv6 works on RPI too.
@@ -481,6 +489,8 @@ ifeq ($(FTE_TARGET),linux_armhf)
 	CXX=arm-linux-gnueabihf-g++ -marm -march=armv6 -mfpu=vfp -mfloat-abi=hard
 	STRIP=arm-linux-gnueabihf-strip
 	BITS=armhf
+	FMT=fmt
+	SED=sed
 endif
 ifeq ($(FTE_TARGET),linux_arm64)
 	FTE_TARGET=linux
@@ -489,6 +499,8 @@ ifeq ($(FTE_TARGET),linux_arm64)
 	STRIP=aarch64-linux-gnu-strip
 	BITS=arm64
 	USE_SPEEX=0 #fails to compile due to neon asm, I'm just going to disable it (will still soft-link).
+	FMT=fmt
+	SED=sed
 endif
 ifeq ($(FTE_TARGET),linux_aarch64)
 	FTE_TARGET=linux
@@ -497,6 +509,8 @@ ifeq ($(FTE_TARGET),linux_aarch64)
 	STRIP=aarch64-linux-gnu-strip
 	BITS=aarch64
 	USE_SPEEX=0 #fails to compile due to neon asm, I'm just going to disable it (will still soft-link).
+	FMT=fmt
+	SED=sed
 endif
 ifeq ($(FTE_TARGET),linux_x32)
 	#DO NOT CONFUSE WITH linux_x86. this target is amd64-with-32bit-pointers
@@ -514,6 +528,8 @@ else
 	STRIP=strip
 endif
 	BITS=x32
+	FMT=fmt
+	SED=sed
 endif
 ifeq ($(FTE_TARGET),linux64)
 	FTE_TARGET=linux
@@ -521,6 +537,8 @@ ifeq ($(FTE_TARGET),linux64)
 	CXX=g++ -m64
 	STRIP=strip
 	BITS=64
+	FMT=fmt
+	SED=sed
 endif
 ifeq ($(FTE_TARGET),web)
 	CC=emcc
@@ -532,6 +550,19 @@ ifeq ($(FTE_TARGET),cygwin)
 endif
 ifeq ($(shell uname -s),OpenBSD)
 	PKGCONFIG=pkg-config
+	FMT=gfmt
+	SED=gsed
+endif
+ifeq ($(shell uname -s),NetBSD)
+	PKGCONFIG=pkg-config
+	BASELDFLAGS=-lm
+	FMT=gfmt
+	SED=gsed
+endif
+ifeq ($(shell uname -s),FreeBSD)
+	PKGCONFIG=pkg-config
+	FMT=gfmt
+	SED=gsed
 endif
 
 ifeq ($(FTE_TARGET),) 	#user didn't specify prefered target
@@ -1002,20 +1033,20 @@ SERVERLIBFLAGS=$(COMMONLIBFLAGS)
 CLIENTLDDEPS=$(COMMONLDDEPS) $(LIBOPUS_LDFLAGS) $(LIBSPEEX_LDFLAGS) $(OGGVORBISLDFLAGS)
 SERVERLDDEPS=$(COMMONLDDEPS)
 ifeq (1,$(USE_OPUS))
-    LIBOPUS_STATIC=-DOPUS_STATIC
-    LIBOPUS_LDFLAGS=-lopus
+    LIBOPUS_STATIC=-DOPUS_STATIC $(shell $(PKGCONFIG) --cflags opus)
+    LIBOPUS_LDFLAGS=$(shell $(PKGCONFIG) --libs opus)
     ALL_CFLAGS+=-I/usr/include/opus
     MAKELIBS+=libs-$(ARCH)/libopus.a
 endif
 ifeq (1,$(USE_SPEEX))
-    LIBSPEEX_STATIC=-DSPEEX_STATIC
-    LIBSPEEX_LDFLAGS=-lspeex -lspeexdsp
+    LIBSPEEX_STATIC=-DSPEEX_STATIC $(shell $(PKGCONFIG) --cflags speex speexdsp)
+    LIBSPEEX_LDFLAGS=$(shell $(PKGCONFIG) --libs speex speexdsp)
     MAKELIBS+=libs-$(ARCH)/libspeex.a libs-$(ARCH)/libspeexdsp.a
 endif
 
 ifeq (1,$(USE_VORBISFILE))
-    OGGVORBISFILE_STATIC=-DLIBVORBISFILE_STATIC
-    OGGVORBISLDFLAGS ?= -lvorbisfile -lvorbis -logg
+    OGGVORBISFILE_STATIC=-DLIBVORBISFILE_STATIC $(shell $(PKGCONFIG) --cflags vorbisfile vorbis ogg)
+    OGGVORBISLDFLAGS ?= $(shell $(PKGCONFIG) --libs vorbisfile vorbis ogg)
 else
     OGGVORBISLDFLAGS=
     OGGVORBISFILE_STATIC=
@@ -1043,12 +1074,12 @@ endif
 ALL_CFLAGS+=$(FREETYPE_CFLAGS)
 
 ifeq (1,$(LINK_PNG))
-    CLIENTLIBFLAGS+=-DLIBPNG_STATIC
-    CLIENTLDDEPS+=-lpng
+    CLIENTLIBFLAGS+=-DLIBPNG_STATIC $(shell $(PKGCONFIG) --cflags libpng)
+    CLIENTLDDEPS+=$(shell $(PKGCONFIG) --libs libpng)
 endif
 ifeq (1,$(LINK_JPEG))
-    CLIENTLIBFLAGS+=-DLIBJPEG_STATIC
-    CLIENTLDDEPS+=-ljpeg
+    CLIENTLIBFLAGS+=-DLIBJPEG_STATIC $(shell $(PKGCONFIG) --cflags libjpeg)
+    CLIENTLDDEPS+=$(shell $(PKGCONFIG) --libs libjpeg)
 endif
 ifeq (1,$(LINK_ZLIB))
     CLIENTLIBFLAGS+=-DZLIB_STATIC
@@ -1524,6 +1555,13 @@ ifeq ($(FTE_TARGET),bsd)
 		VK_CFLAGS+= -DFREETYPE_STATIC
 		GL_CFLAGS+= -DFREETYPE_STATIC
 	endif
+	ifeq ($(shell uname -s),NetBSD)
+		GL_CFLAGS=$(GLCFLAGS) -I/usr/X11R7/include -I/usr/X11R7/include/freetype2 -I/usr/pkg/include -DMULTITHREAD -DAUDIO_OSS
+		GL_LDFLAGS+= -lossaudio -lfreetype -Wl,-R/usr/X11R7/lib -L/usr/X11R7/lib -lX11
+		M_CFLAGS=${GL_CFLAGS}
+		M_LDFLAGS+=${GL_LDFLAGS}
+		VK_LDFLAGS+=${GL_LDFLAGS}
+	endif
 
 	ifeq (,$(findstring NO_ZLIB,$(CFLAGS)))
 		SV_LDFLAGS+= -lz
@@ -1886,12 +1924,16 @@ $(OUT_DIR)/fteqcc.o : fteqcc.rc
 #	sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
 #	rm -f $@.$$$$
 
+# XXX: Pattern recipe did not update peer target
+# In a future version of GNU Make, this will cause an error
+# and abort.
+# ~Izuru Yakumo
 $(OUT_DIR)/%.o $(OUT_DIR)/%.d : %.c
 ifneq ($(DEPCC),)
 	@-set -e; rm -f $@.d; \
 	 $(DEPCC) -MM $(ALL_CFLAGS) $< > $@.d.$$$$; \
-	 sed 's,\($*\)\.o[ :]*,$@ $@.d : ,g' < $@.d.$$$$ > $@.d; \
-	 sed -e 's/.*://' -e 's/\\$$//' < $@.d.$$$$ | fmt -1 | sed -e 's/^ *//' -e 's/$$/:/' >> $@.d; \
+	 ${SED} 's,\($*\)\.o[ :]*,$@ $@.d : ,g' < $@.d.$$$$ > $@.d; \
+	 ${SED} -e 's/.*://' -e 's/\\$$//' < $@.d.$$$$ | ${FMT} -1 | sed -e 's/^ *//' -e 's/$$/:/' >> $@.d; \
 	 rm -f $@.d.$$$$
 endif
 	$(DO_CC) -I$(OUT_DIR)
@@ -1900,8 +1942,8 @@ $(OUT_DIR)/%.o $(OUT_DIR)/%.d : %.cpp
 ifneq ($(DEPCXX),)
 	@-set -e; rm -f $@.d; \
 	$(DEPCXX) -MM $(ALL_CXXFLAGS) $< > $@.d.$$$$; \
-	sed 's,\($*\)\.o[ :]*,$@ $@.d : ,g' < $@.d.$$$$ > $@.d; \
-	sed -e 's/.*://' -e 's/\\$$//' < $@.d.$$$$ | fmt -1 | sed -e 's/^ *//' -e 's/$$/:/' >> $@.d; \
+	${SED} 's,\($*\)\.o[ :]*,$@ $@.d : ,g' < $@.d.$$$$ > $@.d; \
+	${SED} -e 's/.*://' -e 's/\\$$//' < $@.d.$$$$ | ${FMT} -1 | sed -e 's/^ *//' -e 's/$$/:/' >> $@.d; \
 	rm -f $@.d.$$$$
 endif
 	$(DO_CXX) -I$(OUT_DIR)
@@ -1910,8 +1952,8 @@ $(OUT_DIR)/%.o $(OUT_DIR)/%.d : %.cxx
 ifneq ($(DEPCXX),)
 	@-set -e; rm -f $@.d; \
 	$(DEPCXX) -MM $(ALL_CXXFLAGS) $< > $@.d.$$$$; \
-	sed 's,\($*\)\.o[ :]*,$@ $@.d : ,g' < $@.d.$$$$ > $@.d; \
-	sed -e 's/.*://' -e 's/\\$$//' < $@.d.$$$$ | fmt -1 | sed -e 's/^ *//' -e 's/$$/:/' >> $@.d; \
+	${SED} 's,\($*\)\.o[ :]*,$@ $@.d : ,g' < $@.d.$$$$ > $@.d; \
+        ${SED} -e 's/.*://' -e 's/\\$$//' < $@.d.$$$$ | ${FMT} -1 | sed -e 's/^ *//' -e 's/$$/:/' >> $@.d; \
 	rm -f $@.d.$$$$
 endif
 	$(DO_CXX) -I$(OUT_DIR)
@@ -1920,8 +1962,8 @@ $(OUT_DIR)/%.oo $(OUT_DIR)/%.d : %.c
 ifneq ($(DEPCC),)
 	@-set -e; rm -f $@.d; \
 	 $(DEPCC) -MM $(ALL_CFLAGS) $< > $@.d.$$$$; \
-	 sed 's,\($*\)\.oo[ :]*,$@ $@.d : ,g' < $@.d.$$$$ > $@.d; \
-	 sed -e 's/.*://' -e 's/\\$$//' < $@.d.$$$$ | fmt -1 | sed -e 's/^ *//' -e 's/$$/:/' >> $@.d; \
+	 ${SED} 's,\($*\)\.oo[ :]*,$@ $@.d : ,g' < $@.d.$$$$ > $@.d; \
+	 ${SED} -e 's/.*://' -e 's/\\$$//' < $@.d.$$$$ | ${FMT} -1 | sed -e 's/^ *//' -e 's/$$/:/' >> $@.d; \
 	 rm -f $@.d.$$$$
 endif
 	$(DO_CC) -I$(OUT_DIR)
@@ -1929,8 +1971,8 @@ endif
 $(OUT_DIR)/%.mo $(OUT_DIR)/%.d : %.m
 	@-set -e; rm -f $@.d; \
 	 $(DEPCC) -MM $(ALL_CFLAGS) $< > $@.d.$$$$; \
-	 sed 's,\($*\)\.mo[ :]*,$@ $@.d : ,g' < $@.d.$$$$ > $@.d; \
-	 sed -e 's/.*://' -e 's/\\$$//' < $@.d.$$$$ | fmt -1 | sed -e 's/^ *//' -e 's/$$/:/' >> $@.d; \
+	 ${SED} 's,\($*\)\.mo[ :]*,$@ $@.d : ,g' < $@.d.$$$$ > $@.d; \
+	 ${SED} -e 's/.*://' -e 's/\\$$//' < $@.d.$$$$ | ${FMT} -1 | sed -e 's/^ *//' -e 's/$$/:/' >> $@.d; \
 	 rm -f $@.d.$$$$
 	$(DO_CC) -I$(OUT_DIR)
 
diff --git a/engine/makeconfig.sh b/engine/makeconfig.sh
index 7c271b655..1f3016301 100644
--- a/engine/makeconfig.sh
+++ b/engine/makeconfig.sh
@@ -48,6 +48,10 @@ querylibrary()
 		query "$1" "$2"
 		return
 	fi
+	if [ -f /usr/pkg/include/$3 ]; then
+		query "$1" "$2"
+		return
+	fi
 #they don't have it, force no.
 	echo "$1 n"
 	echo "//#define $2" >> config.h
-- 
2.48.1

