-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile.lib
147 lines (104 loc) · 2.86 KB
/
Makefile.lib
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
PHONY += __lib
__lib:
#
# Compiler, linker and additional programs
#
ifneq ($(shell $(CC) --version 2>&1 | head -n 1 | grep clang),)
LLVM := 1
endif
ifneq ($(LLVM),)
CC := clang
LD := ld.lld
STRIP := llvm-strip
OBJCOPY := llvm-objcopy
else
CC := i686-pc-linux-gnu-gcc
LD := i686-pc-linux-gnu-gcc
STRIP := i686-pc-linux-gnu-strip
OBJCOPY := i686-pc-linux-gnu-objcopy
endif
#
# Additional programs
#
PY := python3
RM := rm
NM := nm
CP := cp
XR := xorriso -as mkisofs
#
# Flags for compiler and linker
#
DEPFLAGS = -MMD -MP -MF [email protected]
CFLAGS := -ffreestanding -Wextra -Os
CFLAGS += -Wall -Wstrict-prototypes
CFLAGS += -fno-pic
ifneq ($(LLVM),)
CFLAGS += -target i386-pc-none-elf
else
CFLAGS += -march=i386 -Wl,--gc-sections
endif
LDFLAGS :=
ifneq ($(LLVM),)
LDFLAGS += -m elf_i386 --gc-sections --no-dynamic-linker
else
LDFLAGS += -O2 -nostdlib -lgcc
endif
MODULE_LDFLAGS := $(LDFLAGS)
NMFLAGS += -ng
INCLUDE := -Isrc/include -Isrc/arch/$(ELFBOOT_ARCH)/include
#
# Calling Makefiles
#
compile = $(MAKE) -f $(srctree)/Makefile.build obj=$(1) dir=$(2)
cleanup = $(MAKE) -f $(srctree)/Makefile.clean obj=$(1) dir=$(2)
# Building modules
builtin = $(PY) tools/builtin.py -i $(1)
modules = $(MAKE) -f $(srctree)/Makefile.modules obj=$(1) dir=$(2)
# Generating configuration and header files
genconf = $(PY) tools/genconf.py -i $(ELFBOOT).config -o $@
#
# Calling functions
#
squote := '
escsq = $(subst $(squote),'\$(squote)',$1)
cec = $(if $($(quiet)cmd_$(1)), echo ' $(call escsq,$($(quiet)cmd_$(1)))';)
cmd = @$(cec) $(cmd_$(1))
# To reduce the size of the resulting binary, we remove unused sections
# like the .eh_frame which only adds unwinding information which we are
# not using anyway.
unused_sections += .comment
unused_sections += .debug_*
unused_sections += .eh_frame
OBJCOPY_SECTION := $(foreach section,$(unused_sections),-R $(section))
#
# Utility functions and Tools
#
ELFCONF := $(srctree)/tools/elfconf
quiet_cmd_ec_elf = ELFCONF $(2).elf
cmd_ec_elf = $(ELFCONF) -f $(2).elf -s $(3) -v $(shell stat -L -c %s $(4))
quiet_cmd_st_elf = STRIP $(2).elf
cmd_st_elf = $(STRIP) -S $(2).elf
quiet_cmd_oc_bin = OBJCOPY $(2).bin
cmd_oc_bin = $(OBJCOPY) -O binary $(2).elf $(2).bin
elfconf = $(call cmd,ec_elf,$(1),$(2),$(3))
gcstrip = $(call cmd,st_elf,$(1))
objcopy = $(call cmd,oc_bin,$(1))
mod_map = $(call cmd,ko_map)
#
# Collecting object files for compilation
#
define collect_target
objtree := $$(objtree)/$(1)
srctree := $$(srctree)/$(1)
$(2)-y :=
$(2)-m :=
$(2)-d :=
include $$(srctree)/Makefile
$(2)-ccfl += $$($(2)-cflags)
$(2)-ccfl := $$(sort $$($(2)-ccfl))
$(2)-objs += $$(patsubst %,$$(objtree)/%,$$($(2)-y))
$(2)-mods += $$(patsubst %,$$(objtree)/%,$$($(2)-m))
$$(foreach subdir,$$($(2)-d),$$(eval $$(call collect_target,$$(subdir),$(2))))
srctree := $$(patsubst %/$(1),%,$$(srctree))
objtree := $$(patsubst %/$(1),%,$$(objtree))
endef