@@ -1,50 +1,71 @@ export OUT = $(PWD)/out +# TODO: calculate these using $(MAKE_HOST) export ARCH = x86 export OS = lin export BITS = 64 export TMP = $(PWD)/tmp -export TARGET = $(ARCH).$(OS).$(BITS) +ifneq ($(BITS),) + export TARGET = $(ARCH).$(OS).$(BITS) +else + export TARGET = $(ARCH).$(OS) +endif +export gpp = gpp export lin-headers = /usr/include/asm export fbsd-headers = /usr/include/sys moddirs = $(wildcard k*) binaries = $(wildcard k*/*.exe.c) functions = $(wildcard k*/*.fn.c) -assemblies = $(wildcard k*/*.fn.${TARGET}.s) +assemblies = $(wildcard k*/*.fn.$(TARGET).s) binmods = $(sort $(dir $(binaries))) # i'm sorry -collect = $(strip $(foreach dir,$(moddirs),$(addprefix $(OUT)/$(dir).,$(notdir $(wildcard $(dir)/$1))))) +collect = $(strip $(foreach dir,$(moddirs),$(wildcard $(dir)/*.$1))) +transform = $(strip $(foreach dir,$(moddirs),$(patsubst $(dir)/%.$1,$(subst @,$(dir),$2),$(wildcard $(dir)/*.$1)))) -cfnsources = $(call collect,*.fn.c) -sfnsources = $(call collect,*.fn.${TARGET}.s) -crtsources = $(call collect,*.rt.c) -srtsources = $(call collect,*.rt.${TARGET}.s) -fnsources = $(cfnsources) $(sfnsources) -rtsources = $(crtsources) $(srtsources) -sources = $(fnsources) $(rtsources) +m-hdr-macs = $(call collect,h.m) +m-c-src-macs = $(call collect,c.m) +m-s-src-macs = $(call collect,$(TARGET).s.m) -cfnobjects = $(cfnsources:%.c=%.o) -sfnobjects = $(sfnsources:%.s=%.o) -crtobjects = $(crtsources:%.c=%.o) -srtobjects = $(srtsources:%.s=%.o) -fnobjects = $(cfnobjects) $(sfnobjects) -rtobjects = $(crtobjects) $(srtobjects) +m-c-sources = $(call transform,c.m,$(TMP)/@.%.c) +m-s-sources = $(call transform,$(TARGET).s.m,$(TMP)/@.%.$(TARGET).s) + +m-headers = $(call transform,h.m,$(OUT)/k/%.h) +m-c-objects = $(call transform,c.m,$(OUT)/@.%.o) +m-s-objects = $(call transform,$(TARGET).s.m,$(OUT)/@.%.$(TARGET).o) + +fnobjects = $(call transform,fn.c,$(OUT)/@.%.fn.o) \ + $(call transform,fn.c.m,$(OUT)/@.%.fn.o) \ + $(call transform,fn.$(TARGET).s,$(OUT)/@.%.fn.$(TARGET).o) \ + $(call transform,fn.$(TARGET).s.m,$(OUT)/@.%.fn.$(TARGET).o) + +rtobjects = $(call transform,rt.c,$(OUT)/@.%.rt.o) \ + $(call transform,rt.c.m,$(OUT)/@.%.rt.o) \ + $(call transform,rt.$(TARGET).s,$(OUT)/@.%.rt.$(TARGET).o) \ + $(call transform,rt.$(TARGET).s.m,$(OUT)/@.%.rt.$(TARGET).o) + objects = $(fnobjects) $(rtobjects) header-dir = /usr/include lib-dir = /usr/lib +unix-oses = lin fbsd dar and posix-oses = lin fbsd dar and hai mgw -ifeq ($(findstring $(OS),$(posix-oses)),$(OS)) -export POSIX = yes +ifeq ($(findstring $(OS),$(unix-oses)),$(OS)) + export UNIX = yes + export POSIX = yes else -export POSIX = no + export UNIX = no + ifeq ($(findstring $(OS),$(posix-oses)),$(OS)) + export POSIX = yes + else + export POSIX = no + endif endif # include libgcc.a in gcc builds, just in case ifeq ($(CC),gcc) @@ -53,11 +74,11 @@ all: $(OUT) defs obj tool lib.static $(OUT)/boot.o lib.shared lib.static: defs obj $(OUT)/libk.a lib.shared: defs obj $(OUT)/libk.so -obj: $(moddirs:%=%.obj) -defs: $(moddirs:%=%.def) -tool: $(OUT)/libk.a $(binmods:%=%.tool) +obj: $(moddirs:%=%.obj) +defs: $(moddirs:%=%.def) +tool: $(OUT)/boot.o $(OUT)/libk.a $(binmods:%=%.tool) clean: rm -rf $(TMP) $(OUT) install: all @@ -69,26 +90,29 @@ uninstall: $(header-dir)/k $(lib-dir)/k rm -rf $^ -lists = moddirs functions assemblies cfnobjects sfnobjects crtobjects srtobjects rtobjects binaries binmods POSIX +lists = moddirs functions assemblies fnobjects rtobjects binaries binmods POSIX dbg: @echo -e lists: $(foreach var, $(lists), "\\n - \\e[1m$(var)\\e[m = $($(var))") -%.obj: %/makefile ${TARGET}.calls $(OUT) +%.obj: %/makefile $(TMP)/precomp.g $(TARGET).calls $(OUT) cd $* && $(MAKE) obj -%.tool: %/makefile $(OUT) +%.tool: %/makefile $(TMP)/precomp.g $(OUT) cd $* && $(MAKE) tool %.dbg: %/makefile $(OUT) cd $* && $(MAKE) dbg -%.def: %/makefile $(OUT) $(OUT)/k +%.def: %/makefile $(TMP)/precomp.g $(OUT) $(OUT)/k cd $* && $(MAKE) def %.calls: arch/makefile cd arch && $(MAKE) $(TMP)/calls.$*.s + +$(TMP)/precomp.g: grammar/precomp.g.gpp $(TMP) + cd grammar && $(MAKE) $@ $(OUT)/libk.so: $(fnobjects) ld -shared $(COMPLIB) -o $@ $^ @# $(CC) -shared -fPIC -nostdlib $(COMPLIB) -o $@ $(OUT)/*.o @@ -101,6 +125,6 @@ @# `ar rcs` in case `ar` isn't the GNU version ar rc $@ $^ ranlib $@ -$(OUT) $(OUT)/k: +$(OUT) $(OUT)/k $(TMP): mkdir -p $@