Improve cake allocator's memory utilisation (#43)
[lunaix-os.git] / lunaix-os / kernel.mk
index c8447033ed6ac35a4db9a2ddd8d02184895dec69..eb5276d779f6ee2b6bd6b0d95666ed480c8c2b72 100644 (file)
@@ -1,48 +1,71 @@
-include os.mkinc
 include toolchain.mkinc
+include lunabuild.mkinc
 
-define ksrc_dirs
-       kernel
-       hal
-       libs
-       arch/$(ARCH)
-endef
-
-define kinc_dirs
-       includes
-       includes/usr
-       arch/$(ARCH)/includes
-endef
-
+include $(lbuild_mkinc)
 
 kbin_dir := $(BUILD_DIR)
 kbin := $(BUILD_NAME)
 
-ksrc_files := $(foreach f, $(ksrc_dirs), $(shell find $(f) -name "*.[cS]"))
-ksrc_objs := $(addsuffix .o,$(ksrc_files))
+ksrc_objs := $(addsuffix .o,$(_LBUILD_SRCS))
+ksrc_deps := $(addsuffix .d,$(_LBUILD_SRCS))
+khdr_opts := $(addprefix -include ,$(_LBUILD_HDRS))
+kinc_opts := $(addprefix -I,$(_LBUILD_INCS))
+config_h += -include $(lbuild_config_h)
 
-kinc_opts := $(addprefix -I,$(kinc_dirs))
+tmp_kbin  := $(BUILD_DIR)/tmpk.bin
+ksymtable := lunaix_ksyms.o
+klinking  := link/lunaix.ld
 
+CFLAGS += $(khdr_opts) $(kinc_opts) $(config_h) -MMD -MP
 
-CFLAGS += -include flags.h
+-include $(ksrc_deps)
 
-%.S.o: %.S
+all_linkable = $(filter-out $(klinking),$(1))
+
+%.S.o: %.S $(khdr_files) kernel.mk
        $(call status_,AS,$<)
-       @$(CC) $(kinc_opts) -c $< -o $@
+       @$(CC) $(CFLAGS) -c $< -o $@
 
-%.c.o: %.c
+%.c.o: %.c kernel.mk
        $(call status_,CC,$<)
-       @$(CC) $(CFLAGS) $(kinc_opts) -c $< -o $@
+       @$(CC) $(CFLAGS) -c $< -o $@
+
 
-$(kbin): $(ksrc_objs) $(kbin_dir)
+$(klinking): link/lunaix.ldx
+       $(call status_,PP,$<)
+       @$(CC) $(CFLAGS) -x c -E -P $< -o $@
+
+
+$(tmp_kbin): $(klinking) $(ksrc_objs)
        $(call status_,LD,$@)
-       @$(CC) -T link/linker.ld -o $(kbin) $(ksrc_objs) $(LDFLAGS)
 
-$(kbin_dir)/modksyms: $(kbin)
-       $(call status_,GEN,$@)
-       @$(PY) scripts/syms_export.py --bits=32 --order=little -o "$@"  "$<" 
+       @$(CC) -T $(klinking) $(config_h) $(LDFLAGS) -o $@ \
+                       $(call all_linkable,$^)
+
+
+$(ksymtable): $(tmp_kbin)
+       $(call status_,KSYM,$@)
+       @ARCH=$(ARCH) scripts/gen_ksymtable.sh DdRrTtAGg $< > lunaix_ksymtable.S
+
+       @$(CC) $(CFLAGS) -c lunaix_ksymtable.S -o $@
+
+
+.PHONY: __do_relink
+__do_relink: $(klinking) $(ksrc_objs) $(ksymtable)
+       $(call status_,LD,$(kbin))
+
+       @$(CC) -T $(klinking) $(config_h) $(LDFLAGS) -o $(kbin) \
+                       $(call all_linkable,$^)
+       
+       @rm $(tmp_kbin)
+
+
+.PHONY: all
+all: __do_relink
 
-all: $(kbin) $(kbin_dir)/modksyms
 
 clean:
-       @rm -f $(ksrc_objs)
\ No newline at end of file
+       @rm -f $(ksrc_objs)
+       @rm -f $(ksrc_deps)
+       @rm -f $(klinking)
+       @rm -f lunaix_ksymtable.S $(ksymtable)
\ No newline at end of file