From fe554ee4aa208a52d80431ed1c1d741956f131e8 Mon Sep 17 00:00:00 2001 From: Dennis Date: Mon, 1 Jun 2020 16:23:21 +0200 Subject: [PATCH 1/4] Fix own path detection on FreeBSD --- src/main.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/main.c b/src/main.c index 1c6872f9..a9009390 100644 --- a/src/main.c +++ b/src/main.c @@ -9,6 +9,8 @@ #include #elif __APPLE__ #include +#elif __FreeBSD__ + #include #endif @@ -26,7 +28,7 @@ static double get_scale(void) { } -static void get_exe_filename(char *buf, int sz) { +static void get_exe_filename(const char *self, char *buf, int sz) { #if _WIN32 int len = GetModuleFileName(NULL, buf, sz - 1); buf[len] = '\0'; @@ -38,6 +40,8 @@ static void get_exe_filename(char *buf, int sz) { #elif __APPLE__ unsigned size = sz; _NSGetExecutablePath(buf, &size); +#elif __FreeBSD__ + realpath(self, buf); #else strcpy(buf, "./lite"); #endif @@ -111,8 +115,13 @@ int main(int argc, char **argv) { lua_pushnumber(L, get_scale()); lua_setglobal(L, "SCALE"); +#ifdef __FreeBSD__ + char exename[PATH_MAX]; +#else char exename[2048]; - get_exe_filename(exename, sizeof(exename)); +#endif + + get_exe_filename(argv[0], exename, sizeof(exename)); lua_pushstring(L, exename); lua_setglobal(L, "EXEFILE"); From 19a165cac71bbcd2123b49413246062962f2e689 Mon Sep 17 00:00:00 2001 From: Dennis Date: Mon, 1 Jun 2020 16:26:01 +0200 Subject: [PATCH 2/4] Use cc instead of gcc and stop assuming bash's path clang and gcc both expose cc, so that should be used. bash doesn't have to be installed at /bin/bash, even on certain Linux distributions --- build.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/build.sh b/build.sh index 308ebf94..b7e5689c 100755 --- a/build.sh +++ b/build.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash cflags="-Wall -O3 -g -std=gnu11 -fno-strict-aliasing -Isrc" lflags="-lSDL2 -lm" @@ -14,7 +14,7 @@ if [[ $* == *windows* ]]; then else platform="unix" outfile="lite" - compiler="gcc" + compiler="cc" cflags="$cflags -DLUA_USE_POSIX" lflags="$lflags -o $outfile" fi From 5d800b396457ee71c62015ade06d28ebf4fd70ba Mon Sep 17 00:00:00 2001 From: Dennis Date: Mon, 1 Jun 2020 16:32:52 +0200 Subject: [PATCH 3/4] Inject SDL2 specific flags into variables via pkgconf when available SDL2 is only available at a non-default path on some systems. pkgconf, when installed, may help in those cases --- build.sh | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/build.sh b/build.sh index b7e5689c..f05e23de 100755 --- a/build.sh +++ b/build.sh @@ -1,14 +1,14 @@ #!/usr/bin/env bash cflags="-Wall -O3 -g -std=gnu11 -fno-strict-aliasing -Isrc" -lflags="-lSDL2 -lm" +lflags="-lm" if [[ $* == *windows* ]]; then platform="windows" outfile="lite.exe" compiler="x86_64-w64-mingw32-gcc" cflags="$cflags -DLUA_USE_POPEN -Iwinlib/SDL2-2.0.10/x86_64-w64-mingw32/include" - lflags="$lflags -Lwinlib/SDL2-2.0.10/x86_64-w64-mingw32/lib" + lflags="$lflags -lSDL2 -Lwinlib/SDL2-2.0.10/x86_64-w64-mingw32/lib" lflags="-lmingw32 -lSDL2main $lflags -mwindows -o $outfile res.res" x86_64-w64-mingw32-windres res.rc -O coff -o res.res else @@ -17,6 +17,12 @@ else compiler="cc" cflags="$cflags -DLUA_USE_POSIX" lflags="$lflags -o $outfile" + if command -v pkgconf >/dev/null; then + cflags="$cflags $(pkgconf --cflags --silence-errors sdl2)" + lflags="$lflags $(pkgconf --libs --silence-errors sdl2)" + else + lflags="$lflags -lSDL2" + fi fi if command -v ccache >/dev/null; then From 4f898083d16eefadd15df43a4703dc7b166167cf Mon Sep 17 00:00:00 2001 From: Dennis Date: Sun, 9 May 2021 14:11:01 +0200 Subject: [PATCH 4/4] Add support for path detection when using symlinks or lite is in $PATH It turns out using argv[0] is a bad idea. This uses sysctl() to retrieve its own path --- src/main.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/main.c b/src/main.c index a9009390..53c08cdd 100644 --- a/src/main.c +++ b/src/main.c @@ -10,7 +10,8 @@ #elif __APPLE__ #include #elif __FreeBSD__ - #include + #include + #include #endif @@ -28,7 +29,7 @@ static double get_scale(void) { } -static void get_exe_filename(const char *self, char *buf, int sz) { +static void get_exe_filename(char *buf, int sz) { #if _WIN32 int len = GetModuleFileName(NULL, buf, sz - 1); buf[len] = '\0'; @@ -41,7 +42,16 @@ static void get_exe_filename(const char *self, char *buf, int sz) { unsigned size = sz; _NSGetExecutablePath(buf, &size); #elif __FreeBSD__ - realpath(self, buf); + int items[] = { + CTL_KERN, + KERN_PROC, + KERN_PROC_PATHNAME, + getpid() + }; + + size_t len; + (void) sysctl(items, 4, buf, &len, NULL, 0); + buf[len] = '\0'; #else strcpy(buf, "./lite"); #endif @@ -121,7 +131,7 @@ int main(int argc, char **argv) { char exename[2048]; #endif - get_exe_filename(argv[0], exename, sizeof(exename)); + get_exe_filename(exename, sizeof(exename)); lua_pushstring(L, exename); lua_setglobal(L, "EXEFILE");