diff options
author | Jakob Kaivo <jkk@ung.org> | 2022-09-08 13:39:47 -0400 |
---|---|---|
committer | Jakob Kaivo <jkk@ung.org> | 2022-09-08 13:39:47 -0400 |
commit | db5e9fa3254dada609947ed054b52a77ebc42051 (patch) | |
tree | a320c2b63b1dbc3faf75b39b90ff2b209e0d2fdb | |
parent | fd528d27b11f1d85dbf6afcd65b6bb4140e56070 (diff) |
handle -L and -l somewhat intelligently
-rw-r--r-- | Makefile | 6 | ||||
-rw-r--r-- | cc.c | 52 | ||||
-rw-r--r-- | libs.c | 39 | ||||
-rw-r--r-- | libs.h | 7 |
4 files changed, 92 insertions, 12 deletions
@@ -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) @@ -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 } @@ -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; +} @@ -0,0 +1,7 @@ +#ifndef LIBS_H +#define LIBS_H + +void addlibdir(char *path, int commandline); +char *findlib(const char *lib); + +#endif |