ref: 0a46f3b0d1e4fbeb49101ba0ce3101981c5c6513
parent: 5ff88bbbdc2dfa981b970e96fed3244dcfd87310
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Wed Jan 16 14:05:12 EST 2019
Separate target and toolchain configuration
--- a/Makefile
+++ b/Makefile
@@ -3,8 +3,10 @@
PROJECTDIR = .
include $(PROJECTDIR)/scripts/rules.mk
+STD = c99
+PREFIX = $(PWD)/$(PROJECTDIR)/root
+MANPREFIX = $(PREFIX)/share/man
DIRS = src include/scc/scc tests
-ENV = $(SCRIPTDIR)/env.sh
all: src
@@ -14,10 +16,10 @@
xargs mkdir -p < $(SCRIPTDIR)/libc-proto
touch dirs
-$(DIRS): $(ENV) FORCE
- +@. $(ENV) && cd $@ && $(MAKE)
+$(DIRS): $(ENVIRON) FORCE
+ +@. $(ENVIRON) && cd $@ && $(MAKE)
-$(ENV):
+$(ENVIRON):
@rm -f $@; \
trap 'r=$?;rm -f $$$$.tmp;exit $r' EXIT HUP INT QUIT TERM; \
echo PATH=$$PATH:$$PWD/$(SCRIPTDIR):. > $$$$.tmp && \
@@ -24,18 +26,17 @@
echo NM=\"$(NM)\" >> $$$$.tmp && \
echo AR=\"$(AR)\" >> $$$$.tmp && \
echo RL=\"$(RL)\" >> $$$$.tmp && \
+ echo STD=\"$(STD)\" >> $$$$.tmp && \
+ echo ARFLAGS=\"$(ARFLAGS)\" >> $$$$.tmp && \
echo RLFLAGS=\"$(RLFLAGS)\" >> $$$$.tmp && \
- echo export PATH RLFLAGS NM AR RL >> $$$$.tmp && \
+ echo export PATH STD ARFLAGS RLFLAGS NM AR RL >> $$$$.tmp && \
mv $$$$.tmp $@
dep:
$(FORALL)
-clean:
+clean: $(ENVIRON)
$(FORALL)
- rm -rf lib bin libexec dirs $(ENV)
-
-distclean: clean
- +@cd include/scc/scc && $(MAKE) distclean
+ rm -rf lib bin libexec dirs $(ENVIRON)
tests: all
--- /dev/null
+++ b/config/amd64-darwin.mk
@@ -1,0 +1,4 @@
+ARCH = amd64
+SYS = darwin
+ABI = amd64-darwin
+DRIVER = posix
--- /dev/null
+++ b/config/amd64-dragonfly.mk
@@ -1,0 +1,4 @@
+ARCH = amd64
+SYS = dragonfly
+ABI = amd64-posix
+DRIVER = posix
--- a/config/amd64-linux.mk
+++ b/config/amd64-linux.mk
@@ -2,9 +2,3 @@
SYS = linux
ABI = amd64-posix
DRIVER = posix
-STD = c99
-PREFIX = $(PWD)/$(PROJECTDIR)/root
-MANPREFIX = $(PREFIX)/share/man
-AS = as
-RANLIB = ranlib
-NM = nm
--- /dev/null
+++ b/config/amd64-netbsd.mk
@@ -1,0 +1,4 @@
+ARCH = amd64
+SYS = netbsd
+ABI = amd64-posix
+DRIVER = posix
--- /dev/null
+++ b/config/amd64-openbsd.mk
@@ -1,0 +1,4 @@
+ARCH = amd64
+SYS = openbsd
+ABI = amd64-posix
+DRIVER = posix
--- /dev/null
+++ b/config/arm32-linux.mk
@@ -1,0 +1,4 @@
+ARCH = arm32
+SYS = linux
+ABI = arm32-posix
+DRIVER = posix
--- /dev/null
+++ b/config/arm64-linux.mk
@@ -1,0 +1,4 @@
+ARCH = arm64
+SYS = linux
+ABI = arm64-posix
+DRIVER = posix
--- a/config/i386-linux.mk
+++ b/config/i386-linux.mk
@@ -1,7 +1,4 @@
+ARCH = i386
+SYS = linux
+ABI = amd64-posix
DRIVER = posix
-STD = c99
-PREFIX = $(PWD)/$(PROJECTDIR)/root
-MANPREFIX = $(PREFIX)/share/man
-AS = as
-RANLIB = ranlib
-NM = nm
--- /dev/null
+++ b/config/toolchain/clang.mk
@@ -1,0 +1,7 @@
+include $(PROJECTDIR)/config/toolchain/gnu.mk
+
+COMP = clang
+ASM = clang
+LINKER = ld.lld
+OBJCOPY = llvm-objcopy
+OBJDUMP = llvm-objdump
--- /dev/null
+++ b/config/toolchain/gnu-darwin.mk
@@ -1,0 +1,3 @@
+include $(PROJECTDIR)/config/toolchain/gnu.mk
+
+RANLIBFLAGS = -c
--- /dev/null
+++ b/config/toolchain/gnu.mk
@@ -1,0 +1,11 @@
+TOOLCFLAGS = -std=c99
+
+COMP = gcc
+ASM = as
+LINKER = ld
+RANLIB = ranlib
+ARCHIVE = ar
+
+ARFLAGS = -U
+NOPIE_CFLAGS = -nopie
+NOPIE_LDFLAGS = -nopie
--- /dev/null
+++ b/config/toolchain/pcc.mk
@@ -1,0 +1,3 @@
+include $(PROJECTDIR)/config/toolchain/gnu.mk
+
+COMP = pcc
--- /dev/null
+++ b/config/toolchain/unix.mk
@@ -1,0 +1,5 @@
+COMP = c99
+ASM = as
+LINKER = ld
+RANLIB = ranlib
+ARCHIVE = ar
--- a/scripts/rules.mk
+++ b/scripts/rules.mk
@@ -1,5 +1,7 @@
CONF=amd64-linux
+TOOL=unix
include $(PROJECTDIR)/config/$(CONF).mk
+include $(PROJECTDIR)/config/toolchain/$(TOOL).mk
LIBDIR = $(PROJECTDIR)/lib/scc
SCRIPTDIR = $(PROJECTDIR)/scripts
@@ -9,12 +11,18 @@
LIBEXEC = $(PROJECTDIR)/libexec/scc
CRTDIR = $(PROJECTDIR)/lib/scc
LIBCDIR = $(CRTDIR)/$(ARCH)-$(SYS)/
+ENVIRON = $(SCRIPTDIR)/env.sh
INCLUDE = -I$(INCDIR)/scc \
-RL = $(RANLIB)
+CC = $(CROSS_COMPILE)$(COMP)
+AS = $(CROSS_COMPILE)$(ASM)
+LD = $(CROSS_COMPILE)$(LINKER)
+RL = $(CROSS_COMPILE)$(RANLIB)
+AR = $(CROSS_COMPILE)$(ARCHIVE)
SCC_CFLAGS = $(MORECFLAGS) \
+ $(TOOLCFLAGS) \
$(SYSCFLAGS) \
$(INCLUDE) \
-g \
@@ -21,14 +29,21 @@
$(CFLAGS)
SCC_LDFLAGS = $(MORELFLAGS) \
+ $(TOOLLDFLAGS) \
$(SYSLDFLAGS) \
-L$(LIBDIR) \
-g \
$(LDFLAGS)
+SCC_ASFLAGS = $(MOREASFLAGS) \
+ $(TOOLASFLAGS) \
+ $(SYSASFLAGS) \
+ $(ASFLAGS)
+
# helper macro to run over all the directories
FORALL = +@set -e ;\
pwd=$$PWD; \
+ . $(ENVIRON); \
for i in $(DIRS); \
do \
cd $$i; \
@@ -40,7 +55,7 @@
$(CC) $(SCC_LDFLAGS) -o $@ $< $(LIBS)
.s.o:
- $(AS) $< -o $@
+ $(AS) $(SCC_ASFLAGS) $< -o $@
.c.o:
$(CC) $(SCC_CFLAGS) -o $@ -c $<
--- a/src/libc/Makefile.libc
+++ b/src/libc/Makefile.libc
@@ -4,7 +4,7 @@
include rules.mk
include objlst.mk
-TARGET = $(LIBDIR)/libc.a
+TARGET = $(LIBCDIR)/libc.a
all: $(TARGET)
--- a/src/libc/rules.mk
+++ b/src/libc/rules.mk
@@ -1,5 +1,4 @@
INCLUDE = -I$(INCDIR) -I$(INCDIR)/bits/$(SYS) -I$(INCDIR)/bits/$(ARCH)
-MORECFLAGS = -w
SYSERRNO = $(INCDIR)/bits/$(SYS)/sys/errno.h