From e2ce2c52d29f8e2cc4cf51272292a2b648103e21 Mon Sep 17 00:00:00 2001 From: d0p1 Date: Thu, 30 Jan 2025 11:59:17 +0100 Subject: [PATCH] build: add rules.mk (still WIP) --- Makefile | 36 ++++++++--------- bin/Makefile | 10 +---- bin/cmd/Makefile | 12 +++--- boot/Makefile | 10 +---- include/Makefile | 20 ++-------- lib/Makefile | 10 +---- lib/c/Makefile | 28 +++++++++----- lib/crypto/Makefile | 34 ++++++++++------- lib/csu/Makefile | 17 +++++---- lib/lzp/Makefile | 38 ++++++++++++------ modules/Makefile | 10 +---- modules/dummy/Makefile | 9 +++-- rules.mk | 87 ++++++++++++++++++++++++++++++++++++++++++ 13 files changed, 199 insertions(+), 122 deletions(-) create mode 100644 rules.mk diff --git a/Makefile b/Makefile index e7be824..ddbe0cc 100644 --- a/Makefile +++ b/Makefile @@ -1,30 +1,31 @@ -.EXPORT_ALL_VARIABLES: .DEFAULT_GOAL:=all -MAKEFLAGS += --no-print-directory +export MAKEFLAGS += --no-print-directory -TOPDIR := $(dir $(realpath $(lastword $(MAKEFILE_LIST)))) -SYSROOTDIR := $(TOPDIR)/sysroot -TOOLSDIR := $(TOPDIR)/tools +export LIB = -BINDIR = /bin -LIBDIR = /usr/lib -INCDIR = /usr/include -ASMDIR = /usr/asm +export TOPDIR := $(dir $(realpath $(lastword $(MAKEFILE_LIST)))) +export SYSROOTDIR := $(TOPDIR)/sysroot +export TOOLSDIR := $(TOPDIR)/tools -AS = fasm -CC ?= gcc -RM = rm -f +export BINDIR = /bin +export LIBDIR = /usr/lib +export INCDIR = /usr/include +export ASMDIR = /usr/asm -MK_BUGREPORT := \"https://git.cute.engineering/d0p1/StupidOS/issues\" -MK_COMMIT := \"$(shell git rev-parse --short HEAD)\" -MK_PACKAGE := \"StupidOS\" +export AS = fasm +export CC ?= gcc +export RM = rm -f -CFLAGS = -DMK_COMMIT="$(MK_COMMIT)" \ +export MK_BUGREPORT := \"https://git.cute.engineering/d0p1/StupidOS/issues\" +export MK_COMMIT := \"$(shell git rev-parse --short HEAD)\" +export MK_PACKAGE := \"StupidOS\" + +export CFLAGS = -DMK_COMMIT="$(MK_COMMIT)" \ -DMK_BUGREPORT="$(MK_BUGREPORT)" \ -DMK_PACKAGE="$(MK_PACKAGE)" \ -I$(TOPDIR)include -LDFLAGS = +export LDFLAGS = QEMU_COMMON = \ @@ -44,6 +45,7 @@ TARGET += stupid.iso stupid.hdd else EXEXT = .exe endif +export EXEXT .PHONY: all all: $(TARGET) diff --git a/bin/Makefile b/bin/Makefile index ff7a79f..215832b 100644 --- a/bin/Makefile +++ b/bin/Makefile @@ -1,11 +1,3 @@ -TOPGOALS = all clean install - SUBDIRS = cmd -.PHONY: $(TOPGOALS) -$(TOPGOALS): $(SUBDIRS) - -.PHONY: $(SUBDIRS) -$(SUBDIRS): - @echo "📁 bin/$@" - @DESTDIR=$(DESTDIR)/bin $(MAKE) -C $@ $(MAKECMDGOALS) +include $(TOPDIR)/rules.mk diff --git a/bin/cmd/Makefile b/bin/cmd/Makefile index 900d535..10ffce1 100644 --- a/bin/cmd/Makefile +++ b/bin/cmd/Makefile @@ -1,6 +1,6 @@ -all: - fasm cmd.asm - -clean: - -install: all +all: + fasm cmd.asm + +clean: + +install: all diff --git a/boot/Makefile b/boot/Makefile index a36caa8..00745f7 100644 --- a/boot/Makefile +++ b/boot/Makefile @@ -1,11 +1,3 @@ -TOPGOALS = all clean install - SUBDIRS = bootsect loader efi -.PHONY: $(TOPGOALS) -$(TOPGOALS): $(SUBDIRS) - -.PHONY: $(SUBDIRS) -$(SUBDIRS): - @echo "📁 boot/$@" - @DESTDIR=$(DESTDIR) $(MAKE) -C $@ $(MAKECMDGOALS) +include $(TOPDIR)/rules.mk diff --git a/include/Makefile b/include/Makefile index fa78381..d0831c7 100644 --- a/include/Makefile +++ b/include/Makefile @@ -1,18 +1,4 @@ -INCS = coff.h elf.h -SYSINCS = sys/errno.h -INCSYSDIR = $(INCDIR)/sys +SYSINCS = errno.h +INCS = coff.h elf.h $(addprefix sys/, $(SYSINCS)) -.PHONY: all -all: - -.PHONY: clean -clean: - -.PHONY: install -install: $(INCS) $(SYSINCS) - @ mkdir -p $(DESTDIR)$(INCDIR) - install $(INCS) $(DESTDIR)$(INCDIR) - @ mkdir -p $(DESTDIR)$(INCSYSDIR) - install $(SYSINCS) $(DESTDIR)$(INCSYSDIR) - -.PHONY: all clean install +include $(TOPDIR)/rules.mk diff --git a/lib/Makefile b/lib/Makefile index 1af7720..6a42b90 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -1,11 +1,3 @@ SUBDIRS = csu crypto lzp c -TOPGOALS = all clean install - -.PHONY: $(TOPGOALS) -$(TOPGOALS): $(SUBDIRS) - -.PHONY: $(SUBDIRS) -$(SUBDIRS): - @echo "📁 lib/$@" - @DESTDIR=$(DESTDIR) $(MAKE) -C $@ $(MAKECMDGOALS) +include $(TOPDIR)/rules.mk diff --git a/lib/c/Makefile b/lib/c/Makefile index c3f46b4..29ee6ae 100644 --- a/lib/c/Makefile +++ b/lib/c/Makefile @@ -1,20 +1,28 @@ -TARGET = libc.a +LIBS = libc.a OBJS = ctype.o INCS = stddef.h time.h ctype.h -all: $(TARGET) +INST = $(addprefix $(DESTDIR)$(INCDIR)/, $(INCS)) \ + $(addprefix $(DESTDIR)$(LIBDIR)/, $(LIBS)) -$(TARGET): $(OBJS) +.PHONY: all +all: $(LIBS) + +$(LIBS): $(OBJS) $(AR) rcs $@ $^ %.o: %.asm $(AS) $< $@ -clean: - $(RM) $(TARGET) $(OBJS) +$(DESTDIR)$(INCDIR)/%.h: %.h + install -D $< $@ -install: $(TARGET) - @ mkdir -p $(DESTDIR)$(LIBDIR) - install $(TARGET) $(DESTDIR)$(LIBDIR) - @ mkdir -p $(DESTDIR)$(INCDIR) - install $(INCS) $(DESTDIR)$(INCDIR) +$(DESTDIR)$(LIBDIR)/%.a: %.a + install -D $< $@ + +.PHONY: clean +clean: + $(RM) $(LIBS) $(OBJS) + +.PHONY: install +install: $(INST) diff --git a/lib/crypto/Makefile b/lib/crypto/Makefile index 3e3dfcc..19941f9 100644 --- a/lib/crypto/Makefile +++ b/lib/crypto/Makefile @@ -1,4 +1,4 @@ -TARGET = libcrypto.a +LIBS = libcrypto.a OBJS = sha2/sha256.o sha2/sha512.o \ chacha/chacha.o \ hchacha/hchacha.o @@ -12,27 +12,33 @@ ASMS = sha2/sha2.inc \ chacha/chacha.inc \ hchacha/hchacha.inc -INCCRYPOTDIR = $(INCDIR)/crypto -ASMCRYPTODIR = $(ASMDIR)/crypto +INST = $(addprefix $(DESTDIR)$(INCDIR)/, $(INCS)) \ + $(addprefix $(DESTDIR)$(ASMDIR)/, $(ASMS)) \ + $(addprefix $(DESTDIR)$(LIBDIR)/, $(LIBS)) -all: $(TARGET) +.PHONY: all +all: $(LIBS) -$(TARGET): $(OBJS) +$(LIBS): $(OBJS) $(AR) rcs $@ $^ %.o: %.asm $(AS) $< $@ +$(DESTDIR)$(INCDIR)/%.h: %.h + install -D $< $@ + +$(DESTDIR)$(ASMDIR)/%.inc: %.inc + install -D $< $@ + +$(DESTDIR)$(LIBDIR)/%.a: %.a + install -D $< $@ + +.PHONY: clean clean: - $(RM) $(TARGET) $(OBJS) + $(RM) $(LIBS) $(OBJS) -install: $(TARGET) - @ mkdir -p $(DESTDIR)$(LIBDIR) - install $(TARGET) $(DESTDIR)$(LIBDIR) - @ mkdir -p $(DESTDIR)$(INCCRYPOTDIR) - install $(INCS) $(DESTDIR)$(INCCRYPOTDIR) - install crypto.h $(DESTDIR)$(INCDIR) - @ mkdir -p $(DESTDIR)$(ASMCRYPTODIR) - install $(ASMS) $(DESTDIR)$(ASMCRYPTODIR) +.PHONY: install +install: $(INST) diff --git a/lib/csu/Makefile b/lib/csu/Makefile index 0084d5d..91684fc 100644 --- a/lib/csu/Makefile +++ b/lib/csu/Makefile @@ -1,13 +1,16 @@ -TARGET = crt0.o +LIBS = crt0.o -all: $(TARGET) +INST = $(addprefix $(DESTDIR)$(LIBDIR)/, $(LIBS)) + +all: $(LIBS) %.o: %.asm $(AS) $< $@ -clean: - $(RM) $(TARGET) +$(DESTDIR)$(LIBDIR)/%.o: %.o + install -D $< $@ -install: $(TARGET) - @ mkdir -p $(DESTDIR)$(LIBDIR) - install $(TARGET) $(DESTDIR)$(LIBDIR) +clean: + $(RM) $(LIBS) + +install: $(INST) diff --git a/lib/lzp/Makefile b/lib/lzp/Makefile index d72ebe5..da9aa17 100644 --- a/lib/lzp/Makefile +++ b/lib/lzp/Makefile @@ -1,21 +1,35 @@ -TARGET = liblzp.a +LIBS = liblzp.a OBJS = lzp.o -all: $(TARGET) +INCS = lzp.h +ASMINCS = lzp.inc -$(TARGET): $(OBJS) +INST = $(addprefix $(DESTDIR)$(INCDIR)/, $(INCS)) \ + $(addprefix $(DESTDIR)$(ASMDIR)/, $(ASMINCS)) \ + $(addprefix $(DESTDIR)$(LIBDIR)/, $(LIBS)) + +.PHONY: all +all: $(LIBS) + +$(LIBS): $(OBJS) $(AR) rcs $@ $^ %.o: %.asm $(AS) $< $@ -clean: - $(RM) $(TARGET) $(OBJS) +$(DESTDIR)$(INCDIR)/%.h: %.h + install -D $< $@ + +$(DESTDIR)$(ASMDIR)/%.inc: %.inc + install -D $< $@ + +$(DESTDIR)$(LIBDIR)/%.a: %.a + install -D $< $@ + +.PHONY: clean +clean: + $(RM) $(LIBS) $(OBJS) + +.PHONY: install +install: $(INST) -install: $(TARGET) - @ mkdir -p $(DESTDIR)$(LIBDIR) - install $(TARGET) $(DESTDIR)$(LIBDIR) - @ mkdir -p $(DESTDIR)$(INCDIR) - install lzp.h $(DESTDIR)$(INCDIR) - @ mkdir -p $(DESTDIR)$(ASMDIR) - install lzp.inc $(DESTDIR)$(ASMDIR) diff --git a/modules/Makefile b/modules/Makefile index cecb3ee..2f95850 100644 --- a/modules/Makefile +++ b/modules/Makefile @@ -1,11 +1,3 @@ -TOPGOALS = all clean install - SUBDIRS = dummy -.PHONY: $(TOPGOALS) -$(TOPGOALS): $(SUBDIRS) - -.PHONY: $(SUBDIRS) -$(SUBDIRS): - @echo "📁 modules/$@" - @DESTDIR=$(DESTDIR) $(MAKE) -C $@ $(MAKECMDGOALS) +include $(TOPDIR)/rules.mk diff --git a/modules/dummy/Makefile b/modules/dummy/Makefile index 2b603c2..928105c 100644 --- a/modules/dummy/Makefile +++ b/modules/dummy/Makefile @@ -1,5 +1,7 @@ MODULE = dummy.mod +INST = $(addprefix $(DESTDIR)/, $(MODULE)) + all: clean: @@ -7,7 +9,8 @@ clean: $(MODULE): $(MODULE:.mod=.asm) fasm $^ $@ +$(DESTDIR)/%.mod: %.mod + install -D $< $@ + .PHONY: install -install: $(MODULE) - @ mkdir -p $(DESTDIR) - install $< $(DESTDIR) +install: $(INST) diff --git a/rules.mk b/rules.mk new file mode 100644 index 0000000..b89f772 --- /dev/null +++ b/rules.mk @@ -0,0 +1,87 @@ +TARGETS = all install clean +.PHONY: $(TARGETS) +$(TARGETS): ; + +_INST = $(addprefix $(DESTDIR)$(INCDIR)/, $(INCS)) \ + $(addprefix $(DESTDIR)$(ASMDIR)/, $(ASMINCS)) \ + $(addprefix $(DESTDIR)$(LIBDIR)/, $(LIB)) + +# ----------------- Subdirs ----------------------- + +ifdef SUBDIRS + +_SUBDIRS:=$(filter-out .WAIT,$(SUBDIRS)) +_CURDIR=$(subst $(TOPDIR),,$(CURDIR)) +ifneq ($(_CURDIR),) +_CURDIR:=$(_CURDIR)/ +endif + +.PHONY: $(_SUBDIRS) +$(_SUBDIRS): + @echo "📁 $(_CURDIR)$@" + @DESTDIR=$(DESTDIR) $(MAKE) -C $@ $(MAKECMDGOALS) + +$(TARGETS): $(SUBDIRS) + +endif + +# --------------- Build rules ------------------ + +%.o: %.asm + $(AS) $< $@ + +# --------------- Lib rules -------------------- + +ifneq ($(LIB),) + +all: $(LIB) + +_cleanlib: + $(RM) $(LIB) + +clean: _cleanlib + +endif + +# --------------- Progs rules ------------------- + +ifneq ($(PROG),) + +all: $(PROG) + + +endif + +# --------------- Install rules ----------------- + +ifneq ($(_INST),) + +install: $(_INST) + +endif + +# headers +$(DESTDIR)$(INCDIR)/%.h: %.h + install -D $< $@ + +$(DESTDIR)$(ASMDIR)/%.inc: %.inc + install -D $< $@ + +# libs +$(DESTDIR)$(LIBDIR)/%.a: %.a + install -D $< $@ + +$(DESTDIR)$(LIBDIR)/%.o: %.o + install -D $< $@ + +# bins +$(DESTDIR)$(BINDIR)/%: % + install -D $< $@ + +# kernel & mods +$(DESTDIR)/%.sys: %.sys + install -D $< $@ + +$(DESTDIR)/%.mod: %.mod + install -D $< $@ +