summaryrefslogtreecommitdiff
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
parentfd528d27b11f1d85dbf6afcd65b6bb4140e56070 (diff)
handle -L and -l somewhat intelligently
-rw-r--r--Makefile6
-rw-r--r--cc.c52
-rw-r--r--libs.c39
-rw-r--r--libs.h7
4 files changed, 92 insertions, 12 deletions
diff --git a/Makefile b/Makefile
index e675b7e..194bc2d 100644
--- a/Makefile
+++ b/Makefile
@@ -42,6 +42,12 @@ $(OBJDIR)/cpp.o: $(SRCDIR)/cpp.c
@mkdir -p $(@D)
$(CC) $(CFLAGS) -o $@ -c $(SRCDIR)/cpp.c
+$(BINDIR)/cc: $(OBJDIR)/libs.o
+$(OBJDIR)/libs.o: $(SRCDIR)/cc.h
+$(OBJDIR)/libs.o: $(SRCDIR)/libs.c
+ @mkdir -p $(@D)
+ $(CC) $(CFLAGS) -o $@ -c $(SRCDIR)/libs.c
+
$(BINDIR)/cc:
@mkdir -p $(@D)
$(LD) $(LDFLAGS) -o $@ $(OBJDIR)/*.o $(LDLIBS)
diff --git a/cc.c b/cc.c
index 71e179b..409ec2f 100644
--- a/cc.c
+++ b/cc.c
@@ -1,12 +1,15 @@
#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"
#include "cpp.h"
#include "link.h"
#include "version.h"
@@ -87,11 +90,14 @@ static int getoptarg(char *argv[], int i, char **arg)
int main(int argc, char *argv[])
{
enum { PREPROCESSED, ASSEMBLY, OBJECT, BINARY } output = BINARY;
- char *progname = basename(argv[0]);
char *output_path = "a.out";
+ char *inputs[argc];
+ int ninputs = 0;
struct macro *predef = do_predefs(argv[0]);
+ addlibdir("/lib/x86_64-linux-gnu", 0);
+
for (int i = 1; i < argc; i++) {
if (!strcmp(argv[i], "-c")) {
output = OBJECT;
@@ -124,9 +130,10 @@ int main(int argc, char *argv[])
//include(path);
} else if (!strncmp(argv[i], "-L", 2)) {
+ /* TODO: add to inputs[] */
char *path = NULL;
i += getoptarg(argv, i, &path);
- //libpath(path);
+ addlibdir(path, 1);
} else if (!strncmp(argv[i], "-O", 2)) {
/* optimize */
@@ -137,21 +144,42 @@ int main(int argc, char *argv[])
//undef(macro);
} else if (!strncmp(argv[i], "-l", 2)) {
+ /* TODO: add to inputs[] */
char *lib = NULL;
i += getoptarg(argv, i, &lib);
- //addobj(lib);
+ inputs[ninputs++] = findlib(lib);
} else if (argv[i][0] == '-') {
- fprintf(stderr, "%s: unknown option %s\n", progname, argv[i]);
- /* invalid option */
+ error(NULL, 1, "unknown options '%s'\n", argv[i]);
+
} else {
- switch (output) {
- case PREPROCESSED:
- preprocess(argv[i], output_path, predef);
- break;
- default:
- break;
- }
+ inputs[ninputs++] = argv[i];
}
}
+
+ for (int i = 0; i < ninputs; i++) {
+ if (!strcmp(inputs[i], "")) {
+ continue;
+ }
+
+ fprintf(stderr, "%s\n", inputs[i]);
+
+ //preprocess
+ //compile
+ //analyze
+ //optimize
+ //assemble
+
+ switch (output) {
+ case PREPROCESSED:
+ preprocess(inputs[i], output_path, predef);
+ break;
+ default:
+ break;
+ }
+ }
+
+ //optimize?
+ //link
+ //strip
}
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;
+}
diff --git a/libs.h b/libs.h
new file mode 100644
index 0000000..977665c
--- /dev/null
+++ b/libs.h
@@ -0,0 +1,7 @@
+#ifndef LIBS_H
+#define LIBS_H
+
+void addlibdir(char *path, int commandline);
+char *findlib(const char *lib);
+
+#endif