kv2: add new build modular system

Signed-off-by: Michael Shaw <spencershawiscool@gmail.com>
This commit is contained in:
Michael Shaw 2026-04-12 10:37:43 -04:00
commit 584a79f355
5 changed files with 127 additions and 118 deletions

View file

@ -1,30 +1,46 @@
# SPDX-License-Identifier: GPL-2.0-only
# kernel makefile
-include .config
#
# Master makefile build script for object files in the kernel
#
# Important to remember that this is run at the project-root-level,
# all of it, as it is invoked with $(MAKE) -f
include .config
export
include $(obj)Makefile
obj-y :=
subdir-y :=
# 1. Filter subdirectories (items ending in /) from files
include $(obj)/Makefile
subdir-y :=
real-objs-y :=
subdir-y := $(patsubst %/,%,$(filter %/, $(obj-y)))
real-objs-y := $(filter-out %/, $(obj-y))
real-objs-y := $(addprefix $(obj)/, $(filter-out %/, $(obj-y)))
deps := $(real-objs-y:.o=.d)
# 2. Target to build everything in this directory
__build: $(subdir-y) $(real-objs-y)
@echo " Built-in $(obj)"
-include $(deps)
# 3. Rule to descend into subdirectories
_all: $(obj)/built-in.o
$(obj)/%.o: $(obj)/%.c
$(Q)echo " CC $@"
$(Q)$(CC) $(CFLAGS) -c $< -o $@
$(obj)/%.o: $(obj)/%.S
$(Q)echo " CC $@"
$(Q)$(CC) $(CFLAGS) -c $< -o $@
$(obj)/built-in.o: $(real-objs-y) $(subdir-y)
$(Q)echo " LD $@"
$(Q)$(LD) -r -o $@ $(real-objs-y) $(foreach d,$(subdir-y),$(obj)/$(d)/built-in.o)
.PHONY: _clean
_clean: $(subdir-y)
$(Q)echo " CLEAN $(obj)"
@rm -rf $(obj)/built-in.o $(real-objs-y) $(deps)
.PHONY: $(subdir-y)
$(subdir-y):
@$(MAKE) -f scripts/Makefile.build obj=$(obj)$@/
# 4. Standard rule to compile .c files to .o
%.o: %.c
@echo " CC $(obj)$@"
@$(CC) $(CFLAGS) -c $< -o $(obj)$@
%.o: %.S
@echo " CC $(obj)$@"
@$(CC) $(CFLAGS) -c $< -o $(obj)$@
$(Q)$(MAKE) --no-print-directory -f scripts/Makefile.build $(MAKECMDGOALS) obj=$(obj)/$@
.PHONY: __build $(subdir-y)