From 05abefc2a27a08f381bca41524a5d299c4839ff2 Mon Sep 17 00:00:00 2001 From: John Hodge Date: Wed, 7 Dec 2011 23:48:30 +0800 Subject: [PATCH] Usermode - Working on new build/makefile system --- Usermode/Libraries/acess.ld_src/rules.mk | 20 +++ .../Libraries/ld-acess.so_src/arch/x86.asm.h | 2 +- .../ld-acess.so_src/arch/x86_64.asm.h | 2 +- Usermode/Libraries/ld-acess.so_src/rules.mk | 42 ++++++ Usermode/Libraries/libc.so_src/rules.mk | 19 +++ Usermode/Libraries/libgcc.so_src/rules.mk | 16 +++ Usermode/Libraries/rules.mk | 17 +++ Usermode/body.mk | 14 ++ Usermode/footer.mk | 7 + Usermode/header.mk | 5 + Usermode/rules.mk | 122 ++++++++++++++++++ 11 files changed, 264 insertions(+), 2 deletions(-) create mode 100644 Usermode/Libraries/acess.ld_src/rules.mk create mode 100644 Usermode/Libraries/ld-acess.so_src/rules.mk create mode 100644 Usermode/Libraries/libc.so_src/rules.mk create mode 100644 Usermode/Libraries/libgcc.so_src/rules.mk create mode 100644 Usermode/Libraries/rules.mk create mode 100644 Usermode/body.mk create mode 100644 Usermode/footer.mk create mode 100644 Usermode/header.mk create mode 100644 Usermode/rules.mk diff --git a/Usermode/Libraries/acess.ld_src/rules.mk b/Usermode/Libraries/acess.ld_src/rules.mk new file mode 100644 index 00000000..f2f3de02 --- /dev/null +++ b/Usermode/Libraries/acess.ld_src/rules.mk @@ -0,0 +1,20 @@ +include $(BASE)header.mk + +# Variables +BIN = $(OUTPUTDIR)Libs/acess.ld + +# Rules +.PHONY: all-$(DIR) + +all-$(DIR): $(BIN) +clean-$(DIR): + $(RM) $(BIN) + +$(BIN): $(DIR)/acess_$(ARCHDIR).ld.h + @mkdir -p $(dir $(BIN)) + cpp -nostdinc -U i386 -P -C $< -o $@ -D__LIBDIR=$(OUTPUTDIR)Libs + +$(DIR)/acess_$(ARCHDIR).ld.h: + $(LD) --verbose | awk '{ if( substr($$0,0,5) == "====="){ bPrint = !bPrint; } else { if(bPrint){ print $$0;} } }' | sed 's/SEARCH_DIR\(.*\)/SEARCH_DIR(__LIBDIR)/' > $@ + +include $(BASE)footer.mk diff --git a/Usermode/Libraries/ld-acess.so_src/arch/x86.asm.h b/Usermode/Libraries/ld-acess.so_src/arch/x86.asm.h index 1f5ba1fa..7780ee8c 100644 --- a/Usermode/Libraries/ld-acess.so_src/arch/x86.asm.h +++ b/Usermode/Libraries/ld-acess.so_src/arch/x86.asm.h @@ -2,7 +2,7 @@ ; AcssMicro - System Calls ; ======================== -%include "../../../Kernel/include/syscalls.inc.asm" +#include "../../../../Kernel/include/syscalls.h" [bits 32] [section .data] diff --git a/Usermode/Libraries/ld-acess.so_src/arch/x86_64.asm.h b/Usermode/Libraries/ld-acess.so_src/arch/x86_64.asm.h index 9c520980..9f7be4ea 100644 --- a/Usermode/Libraries/ld-acess.so_src/arch/x86_64.asm.h +++ b/Usermode/Libraries/ld-acess.so_src/arch/x86_64.asm.h @@ -2,7 +2,7 @@ ; AcssMicro - System Calls ; ======================== -%include "../../../Kernel/include/syscalls.inc.asm" +#include "../../../../Kernel/include/syscalls.h" [bits 64] diff --git a/Usermode/Libraries/ld-acess.so_src/rules.mk b/Usermode/Libraries/ld-acess.so_src/rules.mk new file mode 100644 index 00000000..2c864418 --- /dev/null +++ b/Usermode/Libraries/ld-acess.so_src/rules.mk @@ -0,0 +1,42 @@ +# Acess2 - ld-acess +include $(BASE)header.mk + +# Variables +SRCS := main.c lib.c loadlib.c export.c elf.c pe.c +SRCS += arch/$(ARCHDIR).$(ASSUFFIX) +BIN := $(OUTPUTDIR)Libs/ld-acess.so +XOBJ := $(call fcn_mkobj,_stublib.o) +XBIN := $(OUTPUTDIR)Libs/libld-acess.so + +CFLAGS-$(DIR) := -Wall -fno-builtin -fno-leading-underscore -fno-stack-protector -fPIC -g +CPPFLAGS-$(DIR) := $(CPPFLAGS-$(PDIR)) +LDFLAGS-$(DIR) := -g -T $(DIR)/arch/$(ARCHDIR).ld -Map $(call fcn_mkobj,map.txt) --export-dynamic + +OBJ := $(call fcn_src2obj,$(SRCS)) +ALL_OBJ := $(ALL_OBJ) $(OBJ) +OBJ-$(DIR) := $(OBJ) $(XOBJ) +BIN-$(DIR) := $(BIN) $(XBIN) + +# Rules +.PHONY: all-$(DIR) clean-$(DIR) + +all-$(DIR): $(BIN-$(DIR)) +clean-$(DIR): clean-%: + $(eval BIN=$(BIN-$*/)) + $(eval OBJ=$(OBJ-$*/)) + $(RM) $(BIN) $(OBJ) + +$(BIN): $(OBJ) + +# Stub library +$(XBIN): $(call fcn_mkobj,_stublib.c.o) + @echo [LD] -shared -o libld-acess.so + @$(LD) -shared -o $@ $< + +# Handle preprocessed files +$(DIR)/%: $(DIR)/%.h + @echo [CPP] -o $@ + @mkdir -p $(dir $@) + @$(CPP) $(CPPFLAGS-$(DIR)) -P -D__ASSEMBLER__ $< -o $@ + +include $(BASE)footer.mk diff --git a/Usermode/Libraries/libc.so_src/rules.mk b/Usermode/Libraries/libc.so_src/rules.mk new file mode 100644 index 00000000..5bc4289a --- /dev/null +++ b/Usermode/Libraries/libc.so_src/rules.mk @@ -0,0 +1,19 @@ +# libc + +include $(BASE)header.mk + +# Variables +SRCS := stub.c heap.c stdlib.c env.c fileIO.c string.c select.c +SRCS += arch/$(ARCHDIR).$(ASSUFFIX) +# signals.c +BIN := $(OUTPUTDIR)Libs/libc.so + +CFLAGS-$(DIR) := $(CFLAGS-$(PDIR)) -g +CPPFLAGS-$(DIR) := $(CPPFLAGS-$(PDIR)) +LDFLAGS-$(DIR) := $(LDFLAGS-$(PDIR)) + +include $(BASE)body.mk + +$(BIN): $(OBJ) $(OUTPUTDIR)Libs/libgcc.so + +include $(BASE)footer.mk diff --git a/Usermode/Libraries/libgcc.so_src/rules.mk b/Usermode/Libraries/libgcc.so_src/rules.mk new file mode 100644 index 00000000..ce2c6cca --- /dev/null +++ b/Usermode/Libraries/libgcc.so_src/rules.mk @@ -0,0 +1,16 @@ +# libgcc +include $(BASE)header.mk + +# Variables +SRCS := libgcc.c +BIN := $(OUTPUTDIR)Libs/libgcc.so + +CFLAGS-$(DIR) := $(CFLAGS-$(PDIR)) +CPPFLAGS-$(DIR) := $(CPPFLAGS-$(PDIR)) +LDFLAGS-$(DIR) := $(LDFLAGS-$(PDIR)) + +include $(BASE)body.mk + +$(BIN): $(OBJ) + +include $(BASE)footer.mk diff --git a/Usermode/Libraries/rules.mk b/Usermode/Libraries/rules.mk new file mode 100644 index 00000000..e7e93029 --- /dev/null +++ b/Usermode/Libraries/rules.mk @@ -0,0 +1,17 @@ +include $(BASE)header.mk + +# Rules +ASFLAGS-$(DIR) := -D ARCHDIR=$(ARCHDIR) -D __ASSEMBLER__=1 +CPPFLAGS-$(DIR) := -I$(ACESSDIR)/Usermode/include/ -DARCHDIR=$(ARCHDIR) -DARCHDIR_is_$(ARCHDIR)=1 +CFLAGS-$(DIR) := -g -Wall -fPIC -fno-stack-protector -O3 +LDFLAGS-$(DIR) := -g -nostdlib -shared -I/Acess/Libs/ld-acess.so -lld-acess -e SoMain -x -L$(OUTPUTDIR)Libs/ --no-undefined + +SUB_DIRS = $(wildcard $(DIR)*/rules.mk) + +.PHONY: all-$(DIR) clean-$(DIR) +all-$(DIR): open-$(DIR) $(addprefix all-,$(dir $(SUB_DIRS))) close-$(DIR) +clean-$(DIR): $(addprefix clean-,$(dir $(SUB_DIRS))) + +include $(SUB_DIRS) + +include $(BASE)footer.mk diff --git a/Usermode/body.mk b/Usermode/body.mk new file mode 100644 index 00000000..83a7b813 --- /dev/null +++ b/Usermode/body.mk @@ -0,0 +1,14 @@ + +OBJ := $(call fcn_src2obj,$(SRCS)) +ALL_OBJ := $(ALL_OBJ) $(OBJ) +OBJ-$(DIR) := $(OBJ) +BIN-$(DIR) := $(BIN) + +# Rules +.PHONY: all-$(DIR) clean-$(DIR) +all-$(DIR): $(BIN) +clean-$(DIR): clean-%: + $(eval BIN=$(BIN-$*/)) + $(eval OBJ=$(OBJ-$*/)) + $(RM) $(BIN) $(OBJ) + diff --git a/Usermode/footer.mk b/Usermode/footer.mk new file mode 100644 index 00000000..d07e89d1 --- /dev/null +++ b/Usermode/footer.mk @@ -0,0 +1,7 @@ + +open-$(DIR): $(SRC-$(DIR)) +close-$(DIR): $(SRC-$(DIR)) + +# Pop State +DIR := $(DIR_$(x)) +x := $(x:%.x=%) diff --git a/Usermode/header.mk b/Usermode/header.mk new file mode 100644 index 00000000..6f3c1a73 --- /dev/null +++ b/Usermode/header.mk @@ -0,0 +1,5 @@ +# Push State +x := $(x).x +DIR_$(x) := $(DIR) +DIR := $(dir $(lastword $(filter-out %/header.mk,$(MAKEFILE_LIST)))) +PDIR := $(DIR_$(x)) diff --git a/Usermode/rules.mk b/Usermode/rules.mk new file mode 100644 index 00000000..b31b60da --- /dev/null +++ b/Usermode/rules.mk @@ -0,0 +1,122 @@ +# +# Acess2 Global Makefile +# + +# Config Options +SOURCE_DIR = +OBJECT_DIR = obj-$(ARCH) +OBJECT_SUFFIX = + +# Functions used later +fcn_src2obj_int = \ + $(patsubst %,%$(OBJECT_SUFFIX).o,$(filter %.c,$1)) \ + $(patsubst %,%$(OBJECT_SUFFIX).o,$(filter %.cpp,$1)) \ + $(patsubst %,%$(OBJECT_SUFFIX).o,$(filter %.cc,$1)) \ + $(patsubst %,%$(OBJECT_SUFFIX).o,$(filter %.S,$1)) \ + $(patsubst %,%$(OBJECT_SUFFIX).o,$(filter %.asm,$1)) +fcn_mkobj = $(addprefix $(DIR)$(OBJECT_DIR)/,$(patsubst $(SOURCE_DIR)%,%,$1)) +fcn_src2obj = $(call fcn_mkobj, $(call fcn_src2obj_int,$1)) + +# Start of Voodoo code +SUB_DIRS = $(wildcard */rules.mk) + +_REL_POS := $(dir $(lastword $(MAKEFILE_LIST))) +BASE = $(abspath $(_REL_POS))/ +ifeq ($(_REL_POS),./) + # Root makefile + DEFAULT_RULES := $(dir $(SUB_DIRS)) +else + # Build part of the tree + DEFAULT_RULES := $(abspath $(pwd)/$(_REL_POS)) +endif + +include $(_REL_POS)../Makefile.cfg + +.PHONY: all clean + +all: $(BASE)obj_rules.mk $(addprefix all-,$(DEFAULT_RULES)) +clean: $(addprefix clean-,$(DEFAULT_RULES)) + +# Sub-directory rules +x = x +include $(SUB_DIRS) + + +# === Rules === +fcn_obj2src = $(subst $(OBJECT_DIR)/,$(SOURCE_DIR)/,$(patsubst %$(OBJECT_SUFFIX).o,%,$1)) + +ifeq (x,) +$(foreach file,$(filter %.cpp$(OBJECT_SUFFIX).o,$(ALL_OBJ)), $(eval $f: $(call fcn_obj2src,$f))) +$(foreach file,$(filter %.cc$(OBJECT_SUFFIX).o,$(ALL_OBJ)), $(eval $f: $(call fcn_obj2src,$f))) +$(foreach file,$(filter %.c$(OBJECT_SUFFIX).o,$(ALL_OBJ)), $(eval $f: $(call fcn_obj2src,$f))) +$(foreach file,$(filter %.S$(OBJECT_SUFFIX).o,$(ALL_OBJ)), $(eval $f: $(call fcn_obj2src,$f)))) +$(foreach file,$(filter %.asm$(OBJECT_SUFFIX).o,$(ALL_OBJ)), $(eval $f: $(call fcn_obj2src,$f))) +else +.PHONY: $(BASE)obj_rules.mk +$(BASE)obj_rules.mk: + @echo "$(foreach f,$(filter %.cpp$(OBJECT_SUFFIX).o,$(ALL_OBJ)),$f: $(call fcn_obj2src,$f)\n)" > $@ + @echo "$(foreach f,$(filter %.cc$(OBJECT_SUFFIX).o,$(ALL_OBJ)),$f: $(call fcn_obj2src,$f)\n)" >> $@ + @echo "$(foreach f,$(filter %.c$(OBJECT_SUFFIX).o,$(ALL_OBJ)),$f: $(call fcn_obj2src,$f)\n)" >> $@ + @echo "$(foreach f,$(filter %.S$(OBJECT_SUFFIX).o,$(ALL_OBJ)),$f: $(call fcn_obj2src,$f)\n)" >> $@ + @echo "$(foreach f,$(filter %.asm$(OBJECT_SUFFIX).o,$(ALL_OBJ)),$f: $(call fcn_obj2src,$f)\n)" >> $@ +include $(BASE)obj_rules.mk +endif + +# --- Object Files --- +# C++ (.cpp) +%.cpp$(OBJECT_SUFFIX).o: + $(eval _dir=$(dir $(subst $(OBJECT_DIR)/,,$*))) + $(eval <=$(call fcn_obj2src,$@)) + @echo [CXX] -o $< + @mkdir -p $(dir $@) + @$(CCPP) $(CXXFLAGS) $(CPPFLAGS) $(CXXFLAGS-$(_dir)) $(CPPFLAGS-$(_dir))-c $(_src) -o $@ +# C++ (.cc) +%.cc$(OBJECT_SUFFIX).o: + $(eval _dir=$(dir $(subst $(OBJECT_DIR)/,,$*))) + $(eval _src=$(call fcn_obj2src,$@)) + @echo [CXX] -o $< + @mkdir -p $(dir $@) + @$(CCPP) $(CXXFLAGS) $(CXXFLAGS-$(_dir)) $(CPPFLAGS-$(_dir)) -c $(_src) -o $@ +# C (.c) +%.c$(OBJECT_SUFFIX).o: + $(eval _dir=$(dir $(subst $(OBJECT_DIR)/,,$*))) + $(eval _src=$(call fcn_obj2src,$@)) + @echo [CC] -o $@ + @mkdir -p $(dir $@) + @$(CC) $(CFLAGS) $(CPPFLAGS) $(CFLAGS-$(_dir)) $(CPPFLAGS-$(_dir)) -c $(_src) -o $@ +# Assembly (.S) +%.S$(OBJECT_SUFFIX).o: + $(eval _dir=$(dir $(subst $(OBJECT_DIR)/,,$*))) + $(eval _src=$(call fcn_obj2src,$@)) + @echo [AS] -o $@ + @mkdir -p $(dir $@) + @$(AS) $(ASFLAGS) $(ASFLAGS-$(_dir)) -o $@ $(_src) + @$(AS) $(ASFLAGS) $(ASFLAGS-$(_dir)) -o $@.dep $(_src) -M +# Assembly (.asm) +%.asm$(OBJECT_SUFFIX).o: + $(eval _dir=$(dir $(subst $(OBJECT_DIR)/,,$*))) + $(eval _src=$(call fcn_obj2src,$@)) + @echo [AS] -o $@ + @mkdir -p $(dir $@) + @$(AS) $(ASFLAGS) $(ASFLAGS-$(_dir)) -o $@ $(_src) + @$(AS) $(ASFLAGS) $(ASFLAGS-$(_dir)) -o $@ $(_src) -MD $@.dep + +# --- Binaries --- +# Static Library (.a) +%.a: + $(eval _dir=$(subst $(OBJECT_DIR),,$(dir $<))) + @echo [AR] ru $@ + @$(RM) $@ + @$(AR) ru $@ $(OBJ-$(_dir)) +# Dynamic Library (.so) +%.so: + $(eval _dir=$(subst $(OBJECT_DIR),,$(dir $<))) + @echo [LD] -shared -o $@ + @$(LD) $(LDFLAGS) -shared -soname $(basename $@) -o $@ $(filter %.o,$^) $(LDFLAGS-$(_dir)) +# Executable (.bin) +%.bin: + $(eval _dir=$(subst $(OBJECT_DIR),,$(dir $<))) + @echo [LD] -o $@ + @$(ld) $(LDFLAGS) -o $@ $(OBJ-$(_dir)) $(LDFLAGS-$(_dir)) + @$(CP) $@ $(@:%.bin=%) + -- 2.20.1