-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
165 lines (133 loc) · 5.08 KB
/
Makefile
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#Standard Compiling Options
ARCH ?= i686
C := $(ARCH)-elf-gcc
CXX := $(ARCH)-elf-g++
AS := $(ARCH)-elf-gcc
#Separate out source directories for better compartmentalization
SRCDIR := src
ARCHDIR := $(SRCDIR)/arch/$(ARCH)
LIBCDIR := $(SRCDIR)/libc
KERNELDIR := $(SRCDIR)/kernel
SRCDIRS := $(ARCHDIR) $(LIBCDIR) $(KERNELDIR)
BUILDDIR := build
INCDIR := include
TARGET := bin/ZoarialBareOS.iso
DEPDIR := dep
#Standard Compiling Files and Arguments
CSRCEXT := c
CINCEXT := h
CPPSRCEXT := cpp
CPPINCEXT := hpp
ASSRCEXT := S
#Locate C and C++ files
CSOURCES := $(shell find $(SRCDIR) -type f -name "*.$(CSRCEXT)")
CPPSOURCES := $(shell find $(SRCDIR) -type f -name "*.$(CPPSRCEXT)")
#Get object files from C and C++ source files
OBJECTS := $(patsubst $(SRCDIR)/%,$(BUILDDIR)/%,$(CSOURCES:.$(CSRCEXT)=.$(CSRCEXT).o)) $(patsubst $(SRCDIR)/%,$(BUILDDIR)/%,$(CPPSOURCES:.$(CPPSRCEXT)=.$(CPPSRCEXT).o))
#Get assembly source files and create objects from them
ASSOURCES := $(shell find $(SRCDIR) -name global-$(ARCH) -prune -false -o -type f -name "*.$(ASSRCEXT)" -print)
ASOBJECTS := $(patsubst $(SRCDIR)/%,$(BUILDDIR)/%,$(ASSOURCES:.$(ASSRCEXT)=.$(ASSRCEXT).o))
#Find dependencies from C and C++ source files
DEPENDENCIES := $(patsubst $(SRCDIR)/%,$(DEPDIR)/%,$(CSOURCES:.$(CSRCEXT)=.d))
DEPENDENCIES := $(DEPENDENCIES) $(patsubst $(SRCDIR)/%,$(DEPDIR)/%,$(CPPSOURCES:.$(CPPSRCEXT)=.d))
#Directories needed by gcc
LIB :=
LIBDIR :=
INC := -I include/ -I include/libc/ -I include/ACPICA
#Combined flags (Both C and C++)
FLAGS := -O3 -Werror -Wall -Wextra -Wpedantic -g -D__is_kernel -D__is_libk -ffreestanding -fstack-protector
#Specific flags
CFLAGS := $(FLAGS) -std=gnu99
CXXFLAGS := $(FLAGS) -std=c++17
ASFLAGS := -c -g #-gdwarf-5
#Make sure certain directories are made
$(shell mkdir -p $(BUILDDIR) $(DEPDIR) bin/)
#Copy the tree structure of src/ to the build/ and dep/ directories
TREE := $(shell find $(SRCDIR) -type d)
$(shell mkdir -p $(patsubst $(SRCDIR)/%, $(BUILDDIR)/%, $(TREE)))
$(shell mkdir -p $(patsubst $(SRCDIR)/%, $(DEPDIR)/%, $(TREE)))
GLOBALARCHDIR := $(BUILDDIR)/arch/global-$(ARCH)
#Define/create the global constructor objects
CRTBEGINOBJ := $(shell $(C) $(CFLAGS) -print-file-name=crtbegin.o)
CRTENDOBJ := $(shell $(C) $(CFLAGS) -print-file-name=crtend.o)
CRTBEGIN := $(GLOBALARCHDIR)/crti.S.o $(CRTBEGINOBJ)
CRTEND := $(CRTENDOBJ) $(GLOBALARCHDIR)/crtn.S.o
LINKERFILE := $(SRCDIR)/linker.lds
MAKEOBJS := $(GLOBALARCHDIR)/crti.S.o $(GLOBALARCHDIR)/crtn.S.o $(BUILDDIR)/linker.lds $(OBJECTS) $(ASOBJECTS)
#Order the objects to prevent weird gcc bugs with global constructors
MAINOBJS := $(CRTBEGIN) $(ASOBJECTS) $(OBJECTS) $(CRTEND)
#Compile Target
bin/ZoarialBareOS.iso: bin/ZoarialBareOS.bin bin/grub.cfg
grub2-file --is-x86-multiboot bin/ZoarialBareOS.bin
mkdir -p $(BUILDDIR)/isodir/boot/grub
cp bin/ZoarialBareOS.bin $(BUILDDIR)/isodir/boot/
cp $(SRCDIR)/grub.cfg $(BUILDDIR)/isodir/boot/grub/grub.cfg
grub2-mkrescue -o $@ $(BUILDDIR)/isodir
bin/ZoarialBareOS.bin: $(MAKEOBJS) Makefile
$(C) -T build/linker.lds $(MAINOBJS) -o $@ -ffreestanding -O2 -nostdlib -lgcc
bin/grub.cfg: $(SRCDIR)/grub.cfg
cp $(SRCDIR)/grub.cfg bin/grub.cfg
$(BUILDDIR)/linker.lds: $(LINKERFILE)
$(C) $(CFLAGS) $(INC) -E -P -o $@ -x c-header $^
#Include dependencies which are created
#-include $(DEPENDENCIES:)
include $(wildcard $(DEPENDENCIES))
#Create C++ object files
$(BUILDDIR)/%.$(CPPSRCEXT).o: $(SRCDIR)/%.$(CPPSRCEXT) Makefile
#Make build directory
@mkdir -p $(BUILDDIR)
@mkdir -p $(DEPDIR)
#Make Makefiles
$(CXX) $(CXXFLAGS) $(INC) -MT $@ -MM -MP $< > $(DEPDIR)/$*.Td
@cd $(DEPDIR); \
cp $*.Td $*.d; \
sed -e 's/#.*//' -e 's/^[^:]*: *//' -e 's/ *\\$$//' \
-e '/^$$/ d' -e 's/$$/ :/' < $*.Td >> $*.d; \
rm -f $*.Td; \
cd ../;
#Compile object
$(CXX) $(CXXFLAGS) $(INC) -c -o $@ $<
#Fancy deleting/copying
#Handles files that no longer exist
@cp -f $(DEPDIR)/$*.d $(DEPDIR)/$*.d.tmp
@sed -e 's/.*://' -e 's/\\$$//' < $(DEPDIR)/$*.d.tmp | fmt -1 | \
sed -e 's/^ *//' -e 's/$$/:/' >> $(DEPDIR)/$*.d
@rm -f $(DEPDIR)/$*.d.tmp
#Create C object files
$(BUILDDIR)/%.$(CSRCEXT).o: $(SRCDIR)/%.$(CSRCEXT) Makefile
#Make build directory
@mkdir -p $(BUILDDIR)
@mkdir -p $(DEPDIR)
#Make Makefiles
$(C) $(CFLAGS) $(INC) -MT $@ -MM -MP $< > $(DEPDIR)/$*.Td
@cd $(DEPDIR); \
cp $*.Td $*.d; \
sed -e 's/#.*//' -e 's/^[^:]*: *//' -e 's/ *\\$$//' \
-e '/^$$/ d' -e 's/$$/ :/' < $*.Td >> $*.d; \
rm -f $*.Td; \
cd ../;
#Compile object
$(C) $(CFLAGS) $(INC) -c -o $@ $<
#Fancy deleting/copying
#Handles files that no longer exist
@cp -f $(DEPDIR)/$*.d $(DEPDIR)/$*.d.tmp
@sed -e 's/.*://' -e 's/\\$$//' < $(DEPDIR)/$*.d.tmp | fmt -1 | \
sed -e 's/^ *//' -e 's/$$/:/' >> $(DEPDIR)/$*.d
@rm -f $(DEPDIR)/$*.d.tmp
#Create object files
$(BUILDDIR)/%.$(ASSRCEXT).o: $(SRCDIR)/%.$(ASSRCEXT) Makefile
#Make build directory
@mkdir -p $(BUILDDIR)
@mkdir -p $(DEPDIR)
#Compile object
$(AS) $(ASFLAGS) $(INC) -o $@ $<
#Clean
clean:
@echo " Cleaning...";
$(RM) -r $(BUILDDIR) $(DEPDIR) bin/
.PHONY: clean
#Basic example
Example: $(EXAMOBJS)
#Prevents failure if dependency does not exist
$(DEPDIR)/%.d: ;
.PRECIOUS: $(DEPDIR)/%.d