summaryrefslogtreecommitdiff
path: root/libs.c
diff options
context:
space:
mode:
authorJakob Kaivo <jkk@ung.org>2022-09-08 13:39:47 -0400
committerJakob Kaivo <jkk@ung.org>2022-09-08 13:39:47 -0400
commitdb5e9fa3254dada609947ed054b52a77ebc42051 (patch)
treea320c2b63b1dbc3faf75b39b90ff2b209e0d2fdb /libs.c
parentfd528d27b11f1d85dbf6afcd65b6bb4140e56070 (diff)
handle -L and -l somewhat intelligently
Diffstat (limited to 'libs.c')
-rw-r--r--libs.c39
1 files changed, 39 insertions, 0 deletions
diff --git a/libs.c b/libs.c
new file mode 100644
index 0000000..e9ad7ae
--- /dev/null
+++ b/libs.c
@@ -0,0 +1,39 @@
+#define _XOPEN_SOURCE 700
+#include <errno.h>
+#include <libgen.h>
+#include <limits.h>
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <unistd.h>
+
+#include "cc.h"
+#include "libs.h"
+
+static char **libdirs = NULL;
+static size_t nlibdirs = 0;
+
+void addlibdir(char *path, int commandline)
+{
+ libdirs = realloc(libdirs, (nlibdirs + 1) * sizeof(*libdirs));
+ if (!libdirs) {
+ error(NULL, commandline, "out of memory adding '%s' to library search path\n", path);
+ }
+ libdirs[nlibdirs++] = path;
+}
+
+char *findlib(const char *lib)
+{
+ char path[PATH_MAX];
+ for (size_t i = 0; i < nlibdirs; i++) {
+ snprintf(path, sizeof(path), "-l%s/lib%s.a", libdirs[i], lib);
+ printf("checking %s\n", path + 2);
+ if (access(path + 2, F_OK) == 0) {
+ printf("\tOK\n");
+ return strdup(path);
+ }
+ }
+ error(NULL, 1, "could not find library -l%s\n", lib);
+ return NULL;
+}