]> Trent Huber's Code - xpmview.git/commitdiff
Adding option to link raylib as dynamic; Linux portability considerations
authorTrent Huber <trentmhuber@gmail.com>
Wed, 16 Apr 2025 22:37:04 +0000 (18:37 -0400)
committerTrent Huber <trentmhuber@gmail.com>
Wed, 16 Apr 2025 22:37:04 +0000 (18:37 -0400)
build.c
build.h
external/build.c
external/cbs
src/build.c
src/colors/build.c
src/font/build.c
src/main.c
src/xpm.c

diff --git a/build.c b/build.c
index 8e0adbb811226faa0362077e933d29215c763ae6..fc92b2b851c31c402bf7ed489c2abbb9a2767081 100644 (file)
--- a/build.c
+++ b/build.c
@@ -1,5 +1,3 @@
-#include "build.h"
-
 #include "external/cbs/cbs.c"
 
 int main(void) {
diff --git a/build.h b/build.h
index 6eedea174f29879ef350cf681411701882c63328..4cd39a7dfab00bc28076f421e3d497934005ec85 100644 (file)
--- a/build.h
+++ b/build.h
@@ -1,27 +1,29 @@
-#define BIN "bin/"
-#define EXTERNAL "external/"
-#define SRC "src/"
+// #define RLDYNAMIC
 
-#define CBSSRC EXTERNAL "cbs/"
-#define CBS CBSSRC "cbs.c"
+#ifdef RLDYNAMIC
+#define RLEXT DYEXT
+#else
+#define RLEXT ".a"
+#endif
 
-#define RLROOT EXTERNAL "raylib/"
+#define SRC ROOT "src/"
+
+#define RLROOT ROOT "external/raylib/"
 #define RLSRC RLROOT "src/"
+#define RLHDR RLSRC "raylib"
+#define RLLIB RLROOT "raylib" RLEXT
 
-#define RLSRCS(ROOT) \
-   ROOT RLSRC "raudio", ROOT RLSRC "rmodels", \
-   ROOT RLSRC "rshapes", ROOT RLSRC "rtext", \
-   ROOT RLSRC "rtextures", ROOT RLSRC "utils", \
-   ROOT RLSRC "rcore", ROOT RLSRC "rglfw"
-#define RLCFPLAT "-DPLATFORM_DESKTOP"
-#define RLGLFWINC RLSRC "external/glfw/include"
-#define RLCFOBJC "-x", "objective-c"
+#define CFGLOBALS "-Wall", "-Wextra", "-Wpedantic"
+#define CFRAYLIB "-I" RLSRC
+#define CFSRC "-I" SRC
 
-#define RLHDR RLSRC "raylib"
-#define RLLIB RLROOT "raylib.a"
-#define RLLFLAGS \
+#ifdef __APPLE__
+#define LFRAYLIB \
    "-framework", "Cocoa", \
    "-framework", "CoreVideo", \
    "-framework", "GLUT", \
    "-framework", "IOKit", \
    "-framework", "OpenGL"
+#else
+#define LFRAYLIB "-lm"
+#endif
index 34521d19446a527759ad44de951e7917b1a1228b..b42568d7948ab413ac6f692ccab032f6e47ebfcc 100644 (file)
@@ -1,24 +1,46 @@
+#define ROOT "../"
 #include "../build.h"
 
-#define ROOT "../"
+#define RLSRCS \
+   RLSRC "raudio", RLSRC "rmodels", \
+   RLSRC "rshapes", RLSRC "rtext", \
+   RLSRC "rtextures", RLSRC "utils", \
+   RLSRC "rcore", RLSRC "rglfw"
+
+#define CFRLPLAT "-DPLATFORM_DESKTOP"
+#define CFRLGLFW "-I" RLSRC "external/glfw/include"
+#ifdef __APPLE__
+#define CFRLX "-x", "objective-c"
+#else
+#define CFRLX "-D_GLFW_X11"
+#endif
+
+#ifdef RLDYNAMIC
+#define RLTYPE 'd'
+#else
+#define RLTYPE 's'
+#endif
 
 #include "cbs/cbs.c"
 
 int main(void) {
    char **src;
-   int i;
+   size_t i;
 
    build(NULL);
 
-   src = (char *[]){RLSRCS(ROOT), NULL};
-   cflags = (char *[]){RLCFPLAT, NULL};
+   src = (char *[]){RLSRCS, NULL};
+   cflags = (char *[]){CFRLPLAT, NULL};
    for (i = 0; i < 6; ++i) compile(src[i], NULL);
-   cflags = (char *[]){RLCFPLAT, "-I" ROOT RLGLFWINC, NULL};
+   cflags = (char *[]){CFRLPLAT, CFRLGLFW, NULL};
    compile(src[6], NULL);
-   cflags = (char *[]){RLCFPLAT, "-I" ROOT RLGLFWINC, RLCFOBJC, NULL};
+   cflags = (char *[]){CFRLPLAT, CFRLGLFW, CFRLX, NULL};
    compile(src[7], NULL);
 
-   load('s', ROOT RLLIB, RLSRCS(ROOT), NULL);
+#ifdef RLDYNAMIC
+   lflags = (char *[]){LFRAYLIB, NULL};
+#endif
+   load(RLTYPE, RLLIB, RLSRCS, NULL);
 
    return 0;
 }
index 8560714dd531fdcd3265ebe38e916aa07ed03768..08b7229355a54a4339931411629bcb9ea48b5926 160000 (submodule)
@@ -1 +1 @@
-Subproject commit 8560714dd531fdcd3265ebe38e916aa07ed03768
+Subproject commit 08b7229355a54a4339931411629bcb9ea48b5926
index 1259734d9c3970b98c28baa7fd8e92bd89ad674b..618ee8e67e105369211533cbe35523d77d587000 100644 (file)
@@ -1,26 +1,33 @@
+#define ROOT "../"
 #include "../build.h"
 
-#define ROOT "../"
+#define BIN ROOT "bin/"
+#define CBS ROOT "external/cbs/"
+
+#define CFCBS "-I" CBS
 
 #include "../external/cbs/cbs.c"
 
 int main(void) {
    build(NULL);
 
+   cflags = (char *[]){CFGLOBALS, NULL};
    compile("error", NULL);
 
    build("colors/");
    build("font/");
 
-   cflags = (char *[]){"-I" ROOT RLSRC, NULL};
-   compile("main", "error", ROOT RLHDR, "xpm", "font.c", NULL);
-   cflags = NULL;
+   cflags = (char *[]){CFGLOBALS, CFRAYLIB, NULL};
+   compile("main", "error", RLHDR, "xpm", "font.c", NULL);
+   cflags = (char *[]){CFGLOBALS, NULL};
    compile("options", "error", NULL);
-   cflags = (char *[]){"-I" ROOT RLSRC, "-I" ROOT CBSSRC, NULL};
-   compile("xpm", "error", ROOT RLHDR, "xpm", ROOT CBS, "colors.c", NULL);
+   cflags = (char *[]){CFGLOBALS, CFRAYLIB, CFCBS, NULL};
+   compile("xpm", "error", RLHDR, "xpm", CBS "cbs.c", "colors.c", NULL);
 
-   lflags = (char *[]){RLLFLAGS, NULL};
-   load('x', ROOT BIN "simplexpm", "error", "main", "options", "xpm", ROOT RLLIB, NULL);
+#ifndef RLDYNAMIC
+   lflags = (char *[]){LFRAYLIB, NULL};
+#endif
+   load('x', BIN "simplexpm", "error", "main", "options", "xpm", RLLIB, NULL);
 
    return 0;
 }
index b25fdb0fe75a54a7fb0b3fe2c522ddeafaeba8bf..2e5c8b45c7496ab3e52865ee475e9ed820c8da9d 100644 (file)
@@ -1,18 +1,17 @@
-#include "../../build.h"
-
 #define ROOT "../../"
+#include "../../build.h"
 
 #include "../../external/cbs/cbs.c"
 
 int main(void) {
    build(NULL);
 
-   cflags = (char *[]){"-I" ROOT SRC, NULL};
-   compile("gencolors", ROOT SRC "error", NULL);
-   load('x', "gencolors", ROOT SRC "error", "gencolors", NULL);
+   cflags = (char *[]){CFGLOBALS, CFSRC, NULL};
+   compile("gencolors", SRC "error", NULL);
+   load('x', "gencolors", SRC "error", "gencolors", NULL);
 
-   if (modified(ROOT SRC "colors.c", "gencolors.c")
-       || modified(ROOT SRC "colors.c", "rgb.txt"))
+   if (modified(SRC "colors.c", "gencolors.c")
+       || modified(SRC "colors.c", "rgb.txt"))
        run("gencolors", (char *[]){"./gencolors", NULL}, "execution", "gencolors");
 
    return 0;
index 7f1cd3aa6b51c01b7eeee91a85a5666659dacc23..ac5e31a1648708f2da7098bce0fe6b5c3ba9173c 100644 (file)
@@ -1,18 +1,20 @@
-#include "../../build.h"
-
 #define ROOT "../../"
+#include "../../build.h"
 
 #include "../../external/cbs/cbs.c"
 
 int main(void) {
    build(NULL);
 
-   cflags = (char *[]){"-I" ROOT SRC, "-I" ROOT RLSRC, NULL};
-   compile("genfont", ROOT SRC "error", ROOT RLHDR, NULL);
-   lflags = (char *[]){RLLFLAGS, NULL};
-   load('x', "genfont", ROOT SRC "error", "genfont", ROOT RLLIB, NULL);
+   cflags = (char *[]){CFGLOBALS, CFRAYLIB, CFSRC, NULL};
+   compile("genfont", SRC "error", RLHDR, NULL);
+
+#ifndef RLDYNAMIC
+   lflags = (char *[]){LFRAYLIB, NULL};
+#endif
+   load('x', "genfont", SRC "error", "genfont", RLLIB, NULL);
 
-   if (modified(ROOT SRC "font.c", "genfont.c") || modified(ROOT SRC "font.c", "font.ttf"))
+   if (modified(SRC "font.c", "genfont.c") || modified(SRC "font.c", "font.ttf"))
        run("genfont", (char *[]){"./genfont", NULL}, "execution", "genfont");
 
    return 0;
index a9ab7562bf884eae8c2bea2467ec4b04934be1cc..87f6f2c150c83cbdd5735b756422e4c4cd984299 100644 (file)
@@ -18,13 +18,14 @@ extern bool isGpuReady;
 #include "font.c"
 
 int main(int argc, char **argv) {
-   int debug, mode, width, height, len;
+   int debug, mode;
    char *xpm, *welcome, *error;
    Image image;
    Texture2D texture;
    Font font;
    FilePathList files;
    KeyboardKey key;
+   size_t len, width, height;
    float scale;
    Vector2 pos, dim;
 
index b1893773d9014169a64268223870ce62e67f47e9..de5a5aef6a797acba27bf91fadd12efac27487c9 100644 (file)
--- a/src/xpm.c
+++ b/src/xpm.c
@@ -1,9 +1,5 @@
 #include <dlfcn.h>
 #include <fcntl.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <sys/errno.h>
 #include <sys/mman.h>
 
 #include "error.h"
 #include "cbs.c"
 #include "colors.c"
 
+static char *strnsub(char *str, char *sub, size_t l) {
+   size_t subl;
+
+   subl = strlen(sub);
+   while (l-- >= subl) if (strncmp(str++, sub, subl) == 0) return str - 1;
+   return NULL;
+}
+
 static int space(char c) {
    return c == ' ' || c == '\t';
 }
 
-static void *zalloc(size_t size) {
-   void *result;
+static void *zalloc(size_t s) {
+   void *r;
 
-   if ((result  = calloc(1, size))) return result;
+   if ((r  = calloc(1, s))) return r;
 
-   xpmerror("Out of memory");
+   xpmerror("Memory allocation");
    exit(EXIT_FAILURE);
 }
 
-static char *arrname(char *p, size_t len) {
-   int step;
-   char *start, *result;
+static char *arrname(char *p, size_t l) {
+   size_t step;
+   char *start, *r;
 
-   for (; (step = 5); len = len > step ? len - step : 0, p += step) {
-       if (len == 0) return NULL;
+   for (; (step = 5); l = l > step ? l - step : 0, p += step) {
+       if (l == 0) return NULL;
        if (space(*p) || *p == '*') step = 1;
-       else if (strncmp(p, "const", step) != 0
+       else if (strncmp(p, "const", 5) != 0
                 || (!space(*(p + step)) && *(p + step) != '*'))
            break;
    }
 
    start = p;
-   for (; !space(*p) && *p != '['; ++p, --len) if (len == 0) return NULL;
-   len = p - start;
-   result = zalloc(len + 1);
-   strncpy(result, start, len);
-   result[len] = '\0';
+   for (; !space(*p) && *p != '['; ++p, --l) if (l == 0) return NULL;
+   l = p - start;
+   r = zalloc(l + 1);
+   strncpy(r, start, l);
 
-   return result;
+   return r;
 }
 
-static int writeall(int fd, char *buf, size_t size) {
-   ssize_t n;
+static int writeall(int fd, char *buf, size_t n) {
+   ssize_t w;
 
-   while (size > 0) {
-       if ((n = write(fd, buf, size)) == -1) return 0;
-       buf += n;
-       size -= n;
+   while (n > 0) {
+       if ((w = write(fd, buf, n)) == -1) return 0;
+       buf += w;
+       n -= w;
    }
-   if (size < 0) return 0;
    return 1;
 }
 
 static int key2mode(char **strp) {
-   int result;
+   int r;
 
    switch (*(*strp)++) {
    case 'm':
-       result = MODEM;
+       r = MODEM;
        break;
    case 's':
-       result = SYMBOLIC;
+       r = SYMBOLIC;
        break;
    case 'g':
        if (**strp == '4') {
            ++*strp;
-           result = MODEG4;
+           r = MODEG4;
            break;
        }
-       result = MODEG;
+       r = MODEG;
        break;
    case 'c':
-       result = MODEC;
+       r = MODEC;
        break;
    default:
        xpmerror("Unknown key `%c'", *(*strp - 1));
-       result = NUMMODES;
+       r = NUMMODES;
    }
 
    while (space(**strp)) ++*strp;
 
-   return result;
+   return r;
 }
 
-static int lendian(void) {
-   int i;
+static char lendian(void) {
+   size_t i;
 
    i = 1;
    return *(char *)&i;
 }
 
 static unsigned int str2color(char **strp) {
-   unsigned int i, result, value;
-   size_t len;
+   size_t i, l;
+   unsigned int r;
    char *name;
 
-   len = i = 0;
+   l = i = 0;
 
    // RGB
-   if (**strp == '#') result = strtol(++*strp, strp, 16);
+   if (**strp == '#') r = strtol(++*strp, strp, 16);
 
    // Color names
    else for (; i < numcolors; ++i) {
        name = colors[i].name;
-       value = colors[i].value;
-       len = strlen(name);
-       if (strncmp(name, *strp, len) == 0
-           && (i == 0 || *(*strp + len) == '\0' || space(*(*strp + len)))) {
-           result = value;
+       l = strlen(name);
+       if (strncmp(name, *strp, l) == 0
+           && (i == 0 || *(*strp + l) == '\0' || space(*(*strp + l)))) {
+           r = colors[i].value;
            break;
        }
    }
@@ -124,30 +125,25 @@ static unsigned int str2color(char **strp) {
        return 0;
    }
 
-   if (result > 0xffffff || result < 0) {
-       xpmerror("`0x%06x' is not a valid RGB color", result);
+   if (r > 0xffffff || r < 0) {
+       xpmerror("`0x%06x' is not a valid RGB color", r);
        return 0;
    }
-   if (lendian())
-       result = (result >> 16 & 0xff)
-                | (result & 0xff00)
-                | (result & 0xff) << 16;
-   if (strncmp(name, "None", 4) != 0)
-       result |= 0xff000000;
-
-   *strp += len;
+   if (lendian()) r = (r >> 16 & 0xff) | (r & 0xff00) | (r & 0xff) << 16;
+   if (strcmp(name, "None") != 0) r |= 0xff000000;
+
+   *strp += l;
    while (space(**strp)) ++*strp;
 
-   return result;
+   return r;
 }
 
 static Image parse(char **data, long *sizep) {
    Image result;
    char *p, *chars, **pp;
-   long width, height, ncolors, cpp;
+   long width, height, ncolors, cpp, l;
    unsigned int *colors, color, *pixels;
    int i, mode, j, k, m;
-   size_t len;
 
    // Values
    result = (Image){0};
@@ -166,7 +162,7 @@ static Image parse(char **data, long *sizep) {
    colors = zalloc(NUMMODES * ncolors * sizeof*colors);
    for (i = 0; i < ncolors; ++i) {
        p = data[1 + i];
-       strncpy(&chars[i * cpp], p, cpp);
+       strncpy(chars + i * cpp, p, cpp);
        p += cpp;
        while (space(*p)) ++p;
        while (*p) switch ((mode = key2mode(&p))) {
@@ -185,18 +181,18 @@ static Image parse(char **data, long *sizep) {
    // Pixels
    pixels = zalloc(NUMMODES * height * width * sizeof*pixels);
    j = width;
-   len = 0;
+   l = 0;
    for (i = 0, pp = &data[1 + ncolors];
-        i < height && j == width && len == 0;
+        i < height && j == width && l == 0;
         ++i, ++pp)
-       for (j = 0, p = *pp, len = strlen(p);
-            j < width && len > 0;
-            ++j, p += cpp, len = len > cpp ? len - cpp : 0)
+       for (j = 0, p = *pp, l = strlen(p);
+            j < width && l > 0;
+            ++j, p += cpp, l -= cpp)
            for (k = 0; k < ncolors; ++k)
-               if (strncmp(p, &chars[k * cpp], cpp) == 0)
+               if (strncmp(p, chars + k * cpp, cpp) == 0)
                    for (m = 0; m < NUMMODES; ++m)
                        pixels[m * width * height + i * width + j] = colors[m * ncolors + k];
-   if (j != width || len > 0) {
+   if (j != width || l != 0) {
        xpmerror("Actual image width too narrow");
        goto free;
    }
@@ -220,7 +216,7 @@ static Image process(char *xpm) {
    Image result;
    int xpmfd, srcfd, e, cpid, status;
    struct stat xstat;
-   size_t len, offset;
+   size_t l, offset;
    char *map, *p, *a, **data;
    void *d;
    long *sizep;
@@ -235,18 +231,18 @@ static Image process(char *xpm) {
        xpmerror("Unable to stat `%s'", xpm);
        goto close;
    }
-   len = xstat.st_size;
-   if ((map = mmap(NULL, len, PROT_READ, MAP_PRIVATE, xpmfd, 0)) == MAP_FAILED) {
+   l = xstat.st_size;
+   if ((map = mmap(NULL, l, PROT_READ, MAP_PRIVATE, xpmfd, 0)) == MAP_FAILED) {
        xpmerror("Unable to map `%s' to memory", xpm);
        goto close;
    }
 
-   if ((p = strnstr(map, "char", len)) == NULL) { // Skip "static" keyword
+   if ((p = strnsub(map, "char", l)) == NULL) { // Skip "static" keyword
        xpmerror("`%s' improperly formatted", xpm);
        goto munmap;
    }
    offset = p - map;
-   if ((a = arrname(p + 4, len - offset - 4)) == NULL) {
+   if ((a = arrname(p + 4, l - offset - 4)) == NULL) {
        xpmerror("`%s' improperly formatted", xpm);
        goto munmap;
    }
@@ -255,7 +251,7 @@ static Image process(char *xpm) {
        xpmerror("Unable to open `/tmp/xpm.c'");
        goto munmap;
    }
-   e = !writeall(srcfd, p, len - offset)
+   e = !writeall(srcfd, p, l - offset)
        || dprintf(srcfd, "\n\nlong size = sizeof %s / sizeof*%s;\n", a, a) < 0;
    if (close(srcfd) == -1) {
        xpmerror("Unable to close `/tmp/xpm.c'");
@@ -282,7 +278,8 @@ static Image process(char *xpm) {
        goto munmap;
    }
    if ((data = (char **)dlsym(d, a)) == NULL) {
-       xpmerror("Unable to load image data from `/tmp/libxpm.dylib': `%s'", dlerror());
+       xpmerror("Unable to load image data from `/tmp/libxpm.dylib': `%s'",
+                dlerror());
        goto dlclose;
    }
    if ((sizep = (long *)dlsym(d, "size")) == NULL) {
@@ -298,12 +295,10 @@ dlclose:
        xpmerror("Unable to unload `/tmp/libxpm.dylib': %s", dlerror());
 
 munmap:
-   if (munmap(map, len) == -1)
-       xpmerror("Unable to unmap `%s' from memory", xpm);
+   if (munmap(map, l) == -1) xpmerror("Unable to unmap `%s' from memory", xpm);
 
 close:
-   if (close(xpmfd) == -1)
-       xpmerror("Unable to close `%s'", xpm);
+   if (close(xpmfd) == -1) xpmerror("Unable to close `%s'", xpm);
 
    return result;
 }