scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | README | LICENSE

commit cd264c79137ceddf549b4f7e24bd6e8e98ea9f0d
parent e6433e30e428dbc29ea26915a3a4c8c08b715909
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Sun,  6 Oct 2019 21:42:56 +0200

[cc] Add meta characters in path definitions

These meta characters help to use paths that depend of the
current configuration of the driver. With these new definitions
SCCPREFIX or different architectures are supported.

Diffstat:
Minclude/scc/scc/Makefile | 31++++++++++---------------------
Minclude/scc/scc/syscrts.def.h | 3+--
Minclude/scc/scc/sysincludes.def.h | 8+++-----
Minclude/scc/scc/syslibs.def.h | 10+++-------
Msrc/cmd/cc/posix/Makefile | 9+++++----
Msrc/cmd/cc/posix/scc.c | 55++++++++++++++++++++++++++++++++++++++++++++++++-------
Msrc/cmd/ld/deps.mk | 1-
Msrc/cmd/ld/ld.h | 1+
Msrc/cmd/ld/main.c | 9++++++---
Msrc/cmd/ld/pass1.c | 3+--
10 files changed, 78 insertions(+), 52 deletions(-)

diff --git a/include/scc/scc/Makefile b/include/scc/scc/Makefile @@ -3,11 +3,12 @@ PROJECTDIR = ../../.. include $(PROJECTDIR)/scripts/rules.mk -SYSHDR = ldflags.h\ - syscrts.h\ - sysincludes.h\ - syslibs.h \ - cstd.h \ +SYSHDR =\ + ldflags.h\ + syscrts.h\ + sysincludes.h\ + syslibs.h\ + cstd.h\ all: $(SYSHDR) @@ -15,28 +16,16 @@ cstd.h: cstd-$(STD).h cp cstd-$(STD).h $@ ldflags.h: - rm -f $@ ; \ - trap "rm -f $$$$.h" 0 2 3; \ - cat < ldflags.def.h > $$$$.h; \ - mv $$$$.h $@ + cp ldflags.def.h $@ syscrts.h: - rm -f $@ ; \ - trap "rm -f $$$$.h" 0 2 3; \ - sed 's,PREFIX,"$(PREFIX)",g' < syscrts.def.h > $$$$.h && \ - mv $$$$.h $@ + cp syscrts.def.h $@ sysincludes.h: - rm -f $@ ; \ - trap "rm -f $$$$.h" 0 2 3; \ - sed 's,PREFIX,"$(PREFIX)",g' < sysincludes.def.h > $$$$.h && \ - mv $$$$.h $@ + cp sysincludes.def.h $@ syslibs.h: - rm -f $@ ; \ - trap "rm -f $$$$.h" 0 2 3; \ - sed 's,PREFIX,"$(PREFIX)",g' < syslibs.def.h > $$$$.h && \ - mv $$$$.h $@ + cp syslibs.def.h $@ distclean: rm -f $(SYSHDR) diff --git a/include/scc/scc/syscrts.def.h b/include/scc/scc/syscrts.def.h @@ -1,5 +1,4 @@ +/* configure below your system crts */ char *syscrts[] = { - /* configure below your system crts */ - /* PREFIX "/lib/scc/crt.o", */ NULL }; diff --git a/include/scc/scc/sysincludes.def.h b/include/scc/scc/sysincludes.def.h @@ -1,8 +1,6 @@ +/* configure below your standard sys include paths */ char *sysincludes[] = { - PREFIX "/include/scc/" , - PREFIX "/include/scc/bits/" ARCH "-" ABI "/", - /* configure below your standard sys include paths */ - PREFIX "/include/", - PREFIX "/local/include/", + "%p/include/scc/", + "%p/include/scc/bits/%a-%s", NULL }; diff --git a/include/scc/scc/syslibs.def.h b/include/scc/scc/syslibs.def.h @@ -1,9 +1,5 @@ -#define MAX_LIB_PATHS 32 - -char *syslibs[MAX_LIB_PATHS + 1] = { - PREFIX "/lib/scc/" , - /* configure below your standard sys include paths */ - PREFIX "/lib/", - PREFIX "/local/lib/", +/* configure below your standard sys include paths */ +char *syslibs[] = { + "%p/lib/scc/%a-%s", NULL }; diff --git a/src/cmd/cc/posix/Makefile b/src/cmd/cc/posix/Makefile @@ -6,10 +6,11 @@ include $(PROJECTDIR)/scripts/rules.mk # SYSLST is a list of backend-arch-abi-sys. First # element of the list becomes the default target -SYSLST = amd64-sysv-linux-elf z80-scc-none-none \ - i386-sysv-linux-elf amd64-sysv-openbsd-elf - -STDCFLAGS = +SYSLST =\ + amd64-sysv-linux-elf\ + z80-scc-none-none\ + i386-sysv-linux-elf\ + amd64-sysv-openbsd-elf\ TARGETS = $(BINDIR)/scc $(BINDIR)/scpp diff --git a/src/cmd/cc/posix/scc.c b/src/cmd/cc/posix/scc.c @@ -76,6 +76,50 @@ terminate(void) } } +static char * +path(char *s) +{ + char *arg, buff[FILENAME_MAX]; + size_t len, cnt; + + for ( ; *s && cnt < FILENAME_MAX; ++s) { + if (*s != '%') { + buff[cnt++] = *s; + continue; + } + + switch (*++s) { + case 'a': + arg = arch; + break; + case 's': + arg = sys; + break; + case 'p': + arg = prefix; + break; + case 'b': + arg = abi; + break; + default: + buff[cnt++] = *s; + continue; + } + + len = strlen(arg); + if (len + cnt >= FILENAME_MAX) + goto too_long; + memcpy(buff+cnt, arg, len); + cnt += len; + } + + if (cnt != FILENAME_MAX) + return xstrdup(buff); + +too_long: + die("scc: too long pathname"); +} + static void addarg(int tool, char *arg) { @@ -124,7 +168,7 @@ inittool(int tool) addarg(tool, "-w"); for (n = 0; sysincludes[n]; ++n) { addarg(tool, "-I"); - addarg(tool, sysincludes[n]); + addarg(tool, path(sysincludes[n])); } case CC2: fmt = (qbe(tool)) ? "%s-qbe_%s-%s" : "%s-%s-%s"; @@ -145,13 +189,10 @@ inittool(int tool) addarg(tool, t->outfile); for (n = 0; syslibs[n]; ++n) { addarg(tool, "-L"); - addarg(tool, syslibs[n]); - } - if (syscrts[0]) { - for (n = 0; syscrts[n]; ++n) - addarg(tool, syscrts[n]); - break; + addarg(tool, path(syslibs[n])); } + for (n = 0; syscrts[n]; ++n) + addarg(tool, path(syscrts[n])); break; case AS: addarg(tool, "-o"); diff --git a/src/cmd/ld/deps.mk b/src/cmd/ld/deps.mk @@ -1,5 +1,4 @@ #deps -./main.o: $(INCDIR)/scc/scc/syslibs.h ./main.o: ./ld.h ./pass1.o: $(INCDIR)/scc/scc/ar.h ./pass1.o: $(INCDIR)/scc/scc/mach.h diff --git a/src/cmd/ld/ld.h b/src/cmd/ld/ld.h @@ -25,6 +25,7 @@ extern void grow(Section *sec, int nbytes); extern void debugsec(void); /* globals */ +extern char *libpaths[]; extern char *filename, *membname; extern int sflag; extern int xflag; diff --git a/src/cmd/ld/main.c b/src/cmd/ld/main.c @@ -5,10 +5,11 @@ #include <string.h> #include <scc/mach.h> -#include <scc/syslibs.h> #include "ld.h" +#define MAX_LIB_PATHS 12 + int sflag; /* discard all the symbols */ int xflag; /* discard local symbols */ int Xflag; /* discard locals starting with 'L' */ @@ -26,6 +27,8 @@ Segment data = {.type = 'D'}; Segment bss = {.type = 'B'}; Segment debug = {.type = 'N'}; +char *libpaths[MAX_LIB_PATHS]; + char *output = "a.out", *entry = "start"; static int status; @@ -83,8 +86,8 @@ Lpath(char *path) { char **bp, **end; - end = &syslibs[MAX_LIB_PATHS]; - for (bp = syslibs; bp < end && *bp; ++bp) + end = &libpaths[MAX_LIB_PATHS]; + for (bp = libpaths; bp < end && *bp; ++bp) ; if (bp == end) { fputs("ld: too many -L options\n", stderr); diff --git a/src/cmd/ld/pass1.c b/src/cmd/ld/pass1.c @@ -214,7 +214,6 @@ openfile(char *name) char **bp; char libname[FILENAME_MAX]; static char buffer[FILENAME_MAX]; - extern char *syslibs[]; filename = name; membname = NULL; @@ -235,7 +234,7 @@ openfile(char *name) if ((fp = fopen(buffer, "rb")) != NULL) return fp; - for (bp = syslibs; *bp; ++bp) { + for (bp = libpaths; *bp; ++bp) { pathlen = strlen(*bp); if (pathlen + len > FILENAME_MAX-1) continue;