-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMakefile
More file actions
107 lines (85 loc) · 4.02 KB
/
Makefile
File metadata and controls
107 lines (85 loc) · 4.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#********************************************************
# SwiftOS buildchain v0.5
# Special thanks: This file has been adapted from
# Solar from osdev.org. Credit goes to this individual
# for contributing a better makefile example
#********************************************************
# Program definitions
TARGET := i686-elf-
CC := $(TARGET)gcc
ASM := nasm
LD := $(TARGET)ld
SYSROOT := sysroot
# Modules in the project (you could 'find' these, but stating
# them explicitly allows for subdirectories like 'tmp' or 'doc'
# without upsetting the build process.
MODULES := bootstrap kernel libc libk
# Global CFLAGS. Add to them if you must, but don't remove '-MMD -I includes',
# which is used for header dependency tracking.
CFLAGS_global := -MMD -Iincludes -O0
LDFLAGS := -e entry
# Global ARFLAGS.
ARFLAGS := rcs
.PHONY: clean mrproper
# Add whatever should be your default / global target.
all: build
@echo "Default target."
######$(libc_SOURCES)#############################################################
# What follows are several templates (think "functions"), which are
# later instantiated for each registered module ($(1) being the
# module name).
###################################################################
# Including a module's build.mk
define MK_template
include $(1)/build.mk
endef
# Setting a module's build rules for object files in <module>/obj.
define RULES_template
$(1)/obj/%.o: $(1)/src/%.c
@$$(CC) $$(CFLAGS) $$(CFLAGS_global) $$(CFLAGS_$(1)) -c $$< -o $$@
$(1)/obj/%_asm.o: $(1)/src/%.asm
@$$(ASM) $$(ASMFLAGS) $$(ASMFLAGS_global) $$(ASMFLAGS_$(1)) $$< -f elf $($(1)_INCLUDEASM) -o $$@
endef
# Setting a module's build rules for executable targets.
# (Depending on its sources' object files and any libraries.)
# Also adds a module's dependency files to the global list.
define PROGRAM_template
DEPENDENCIE := $(DEPENDENCIES) $(patsubst %,$(2)/obj/%.d,$(basename $($(1)_SOURCES)))
bin/$(1): $(patsubst %,$(2)/obj/%_asm.o,$(basename $($(1)_SOURCESASM))) $(patsubst %,$(2)/obj/%.o,$(basename $($(1)_SOURCES))) $(foreach library,$($(1)_LIBRARIES),lib/$(library))
@$$(CC) $$(LDFLAGS) $$(LDFLAGS_$(2)) -g -nostartfiles -nostdlib -o $$@ $$^
endef
# Setting a module's build rules for archive targets.
# (Depending on its sources' object files.)
define ARCHIVE_template
DEPENDENCIES := $(DEPENDENCIES) $(patsubst %,$(2)/obj/%.d,$(basename $($(1)_SOURCES)))
lib/$(1): $(patsubst %,$(2)/obj/%.o,$(basename $($(1)_SOURCES)))
@$$(AR) $$(ARFLAGS) $$@ $$?
endef
# Linking a module's global includes into the global include directory
# (where they will be available as <module>/filename.h).
define INCLUDE_template
ifeq ($(wildcard includes/$(1)),)
$$(shell ln -s ../$(1)/includes includes/$(1))
endif
endef
# Now, instantiating the templates for each module.
$(foreach module,$(MODULES),$(eval include $(module)/build.mk))
$(foreach module,$(MODULES),$(eval $(call RULES_template,$(module))))
$(foreach module,$(MODULES),$(eval $(foreach binary,$($(module)_PROGRAM),$(call PROGRAM_template,$(binary),$(module)))))
$(foreach module,$(MODULES),$(eval $(foreach library,$($(module)_ARCHIVE),$(call ARCHIVE_template,$(library),$(module)))))
$(foreach module,$(MODULES),$(eval $(call INCLUDE_template,$(module))))
# Include the dependency files (generated by GCC's -MMD option)
-include $(sort $(DEPENDENCIES))
swiftos.writebootloader: bin/bootstrap
@cp $^ $(SYSROOT)/boot/bootstrap.img
swiftos.copyFiles: bin/kernel
@cp $^ $(SYSROOT)/KERNEL.BIN
swiftos.mkiosfs: swiftos.writebootloader swiftos.copyFiles
@mkisofs -quiet -r -J -V 'swiftOS' -input-charset utf8 -no-emul-boot -boot-load-size 16 -boot-info-table -o swiftos.iso -b boot/bootstrap.img $(SYSROOT)
swiftos.writeStages: bin/bootstrap
@dd if=$^ of=swiftos.iso obs=2048 seek=1 count=15 conv=notrunc status=none
build: swiftos.mkiosfs swiftos.writeStages
clean:
@$(RM) $(foreach mod,$(MODULES),$(mod)/obj/*.o) bin/* lib/*
mrproper: clean
$(RM) $(foreach mod,$(MODULES),$(mod)/obj/*.d) includes/*