-
Notifications
You must be signed in to change notification settings - Fork 20
/
Makefile
363 lines (305 loc) · 11.5 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
KTF_ROOT := $(abspath $(CURDIR))
export KTF_ROOT
CONFIG := $(KTF_ROOT)/.config
include $(CONFIG)
THIRD_PARTY := third-party
PATCH := patch
TOOLS_DIR := tools
ifeq ($(OS),Windows_NT)
SYSTEM := WIN
else
UNAME := $(shell uname -s)
ifeq ($(UNAME),Linux)
SYSTEM := LINUX
endif
ifeq ($(UNAME),Darwin)
SYSTEM := MACOS
endif
endif
PFMLIB_ARCHIVE :=
PFMLIB_LINKER_FLAGS :=
PFMLIB_INCLUDE :=
PFMLIB_NAME := libpfm
PFMLIB_VER := 4.13.0
PFMLIB_DIR := $(KTF_ROOT)/$(THIRD_PARTY)/$(PFMLIB_NAME)
PFMLIB_TARBALL := $(PFMLIB_DIR)/$(PFMLIB_NAME)-$(PFMLIB_VER).tar.gz
ifeq ($(CONFIG_LIBPFM),y)
KTF_PFMLIB_COMPILE := 1
export KTF_PFMLIB_COMPILE
TAR_CMD_PFMLIB := tar --exclude=.git --exclude=.gitignore --strip-components=1 -xf
PFMLIB_TOOLS_DIR := $(KTF_ROOT)/$(TOOLS_DIR)/$(PFMLIB_NAME)
PFMLIB_ARCHIVE := $(PFMLIB_DIR)/$(PFMLIB_NAME).a
PFMLIB_UNTAR_FILES := $(PFMLIB_NAME)-$(PFMLIB_VER)/lib
PFMLIB_UNTAR_FILES += $(PFMLIB_NAME)-$(PFMLIB_VER)/include
PFMLIB_UNTAR_FILES += $(PFMLIB_NAME)-$(PFMLIB_VER)/rules.mk
PFMLIB_UNTAR_FILES += $(PFMLIB_NAME)-$(PFMLIB_VER)/config.mk
PFMLIB_UNTAR_FILES += $(PFMLIB_NAME)-$(PFMLIB_VER)/Makefile
PFMLIB_UNTAR_FILES += $(PFMLIB_NAME)-$(PFMLIB_VER)/README
PFMLIB_UNTAR_FILES += $(PFMLIB_NAME)-$(PFMLIB_VER)/COPYING
PFMLIB_PATCH_FILE := $(PFMLIB_TOOLS_DIR)/libpfm_diff.patch
PFMLIB_LINKER_FLAGS += -L$(PFMLIB_DIR) -lpfm
PFMLIB_INCLUDE += $(PFMLIB_DIR)/include
endif
ACPICA_DEST_DIR := $(KTF_ROOT)/drivers/acpi/acpica
ifeq ($(CONFIG_ACPICA),y)
TAR_CMD_ACPICA := tar --exclude=.git --exclude=.gitignore --strip-components=1 -C $(ACPICA_DEST_DIR) -xf
ACPICA_VER := unix-20230628
ACPICA_NAME := acpica
ACPICA_DIR := $(KTF_ROOT)/$(THIRD_PARTY)/$(ACPICA_NAME)
ACPICA_TARBALL := $(ACPICA_DIR)/$(ACPICA_NAME)-$(ACPICA_VER).tar.gz
ACPICA_PATCH := $(ACPICA_DIR)/acpica_ktf.patch
ACPICA_UNTAR_DIRS := $(ACPICA_NAME)-$(ACPICA_VER)/source/components/dispatcher
ACPICA_UNTAR_DIRS += $(ACPICA_NAME)-$(ACPICA_VER)/source/components/events
ACPICA_UNTAR_DIRS += $(ACPICA_NAME)-$(ACPICA_VER)/source/components/executer
ACPICA_UNTAR_DIRS += $(ACPICA_NAME)-$(ACPICA_VER)/source/components/hardware
ACPICA_UNTAR_DIRS += $(ACPICA_NAME)-$(ACPICA_VER)/source/components/namespace
ACPICA_UNTAR_DIRS += $(ACPICA_NAME)-$(ACPICA_VER)/source/components/parser
ACPICA_UNTAR_DIRS += $(ACPICA_NAME)-$(ACPICA_VER)/source/components/tables
ACPICA_UNTAR_DIRS += $(ACPICA_NAME)-$(ACPICA_VER)/source/components/utilities
ACPICA_UNTAR_DIRS += $(ACPICA_NAME)-$(ACPICA_VER)/source/include
ACPICA_INCLUDE := $(ACPICA_DEST_DIR)/source/include
endif
ifeq ($(CC),cc) # overwrite on default, otherwise use whatever is in the CC env variable
CC := gcc
endif
LD := ld
NM := nm
CTAGS := ctags
PYTHON := $(shell tools/ci/get-python.sh)
SHELL := bash
RM := rm
LN := ln
SYMLINK := $(LN) -s -f
HARDLINK := $(LN) -f
OBJCOPY := objcopy
STRIP := strip
ifeq ($(SYSTEM), MACOS)
STRIP_OPTS := -S
else
STRIP_OPTS := -s
endif
GRUB_DIR := grub/boot
GRUB_FILE := grub-file
GRUB_MKRESCUE := grub-mkrescue
GRUB_MODULES := multiboot2 iso9660 serial normal
ifneq ($(UNITTEST),)
GRUB_CONFIG := grub/grub-test.cfg
else
GRUB_CONFIG := grub/grub-prod.cfg
endif
XORRISO := xorriso
QEMU_BIN := qemu-system-x86_64
GDB := gdb
XZ := xz
COMMON_INCLUDES := -I$(KTF_ROOT)/include -I$(KTF_ROOT)/include/arch/x86
ifeq ($(CONFIG_LIBPFM),y)
COMMON_INCLUDES += -I$(PFMLIB_INCLUDE)
endif
ifeq ($(CONFIG_ACPICA),y)
COMMON_INCLUDES += -I$(ACPICA_INCLUDE)
endif
COMMON_FLAGS := $(COMMON_INCLUDES) -pipe -MP -MMD -m64 -D__x86_64__ -D__KTF__ -DEARLY_VIRT_MEM=$(CONFIG_EARLY_VIRT_MEM)
ifeq ($(CONFIG_DEBUG),y)
COMMON_FLAGS += -DKTF_DEBUG
endif
ifeq ($(CONFIG_LIBPFM),y)
COMMON_FLAGS += -DKTF_PMU
endif
ifeq ($(CONFIG_ACPICA),y)
COMMON_FLAGS += -DKTF_ACPICA
endif
ifneq ($(UNITTEST),)
COMMON_FLAGS += -DKTF_UNIT_TEST
endif
AFLAGS := $(COMMON_FLAGS) -DASM_FILE -D__ASSEMBLY__ -nostdlib -nostdinc
CFLAGS := $(COMMON_FLAGS) -std=gnu99 -O2 -g -Wall -Wextra -ffreestanding -nostdlib -nostdinc
CFLAGS += -mno-red-zone -mno-mmx -mno-sse -mno-sse2
CFLAGS += -fno-stack-protector -fno-exceptions -fno-builtin -fomit-frame-pointer -fcf-protection="none"
CFLAGS += -mcmodel=kernel -fno-pic -fno-asynchronous-unwind-tables -fno-unwind-tables
CFLAGS += -Wno-unused-parameter -Wno-address-of-packed-member
CFLAGS += -Werror
# Newer versions of GCC warn about memory accesses at non-zero offsets from null pointers.
# As we use this intentionally at different places in our code, these warnings result in many false positives.
CFLAGS += -Wno-array-bounds
ifneq ($(V), 1)
VERBOSE=@
endif
-include Makeconf.local
ifeq ($(CONFIG_ACPICA),y)
ACPICA_INSTALL := $(shell [ -d $(ACPICA_DEST_DIR)/source ] || \
$(TAR_CMD_ACPICA) $(ACPICA_TARBALL) $(ACPICA_UNTAR_DIRS) && \
$(PATCH) -p0 < $(ACPICA_PATCH) && \
$(HARDLINK) $(ACPICA_DEST_DIR)/acktf.h $(ACPICA_INCLUDE)/platform/acktf.h)
endif
ASM_OFFSETS_C := asm-offsets.c
ASM_OFFSETS_SH := $(KTF_ROOT)/tools/asm-offsets/asm-offsets.sh
SOURCES := $(shell find . -name \*.c -and -not -name $(ASM_OFFSETS_C))
HEADERS := $(shell find . -name \*.h)
ASM_SOURCES := $(shell find . -name \*.S)
ASM_OFFSETS := $(shell find . -name $(ASM_OFFSETS_C))
LINK_SCRIPT := $(shell find . -name \*.ld)
ASM_OFFSETS_S := $(ASM_OFFSETS:.c=.s)
ASM_OFFSETS_H := $(addprefix include/,$(ASM_OFFSETS:.c=.h))
SYMBOLS_NAME := symbols
SYMBOLS_TOOL := symbols.py
SYMBOLS_DIR := tools/symbols
PREP_LINK_SCRIPT := $(LINK_SCRIPT:%.ld=%.lds)
OBJS := $(SOURCES:%.c=%.o)
OBJS += $(ASM_SOURCES:%.S=%.o)
TARGET := kernel64.bin
TARGET_DEBUG := $(TARGET).debug
# On Linux systems, we build directly. On non-Linux, we rely on the 'docker%'
# rule below to create an Ubuntu container and perform the Linux-specific build
# steps therein.
ifeq ($(SYSTEM), LINUX)
all: $(TARGET)
else
all: docker$(TARGET)
endif
$(PREP_LINK_SCRIPT) : $(LINK_SCRIPT)
$(VERBOSE) $(CC) $(AFLAGS) -E -P -C -x c $< -o $@
$(TARGET_DEBUG): $(OBJS) $(PREP_LINK_SCRIPT)
@echo "LD " $@
$(VERBOSE) $(LD) -T $(PREP_LINK_SCRIPT) -o $@ $(OBJS) $(PFMLIB_LINKER_FLAGS)
@echo "GEN " $(SYMBOLS_NAME).S
$(VERBOSE) $(NM) -p --format=posix $@ | $(PYTHON) $(SYMBOLS_DIR)/$(SYMBOLS_TOOL)
@echo "CC " $(SYMBOLS_NAME).S
$(VERBOSE) $(CC) -c -o $(SYMBOLS_NAME).o $(AFLAGS) $(SYMBOLS_NAME).S
$(VERBOSE) rm -rf $(SYMBOLS_NAME).S
@echo "LD " $@ $(SYMBOLS_NAME).o
$(VERBOSE) $(LD) -T $(PREP_LINK_SCRIPT) -o $@ $(OBJS) $(PFMLIB_LINKER_FLAGS) $(SYMBOLS_NAME).o
$(TARGET): $(TARGET_DEBUG)
@echo "STRIP"
$(VERBOSE) $(STRIP) $(STRIP_OPTS) -o $@ $<
$(PFMLIB_ARCHIVE): $(PFMLIB_TARBALL)
@echo "UNTAR libpfm"
$(VERBOSE) cd $(PFMLIB_DIR) &&\
$(TAR_CMD_PFMLIB) $(PFMLIB_TARBALL) $(PFMLIB_UNTAR_FILES) -C ./ &&\
$(PATCH) -s -p1 < $(PFMLIB_PATCH_FILE) &&\
cd -
@echo "BUILD libpfm"
$(VERBOSE) $(MAKE) -C $(PFMLIB_DIR) lib
$(VERBOSE) cp $(PFMLIB_DIR)/lib/$(PFMLIB_NAME).a $(PFMLIB_DIR)/
$(VERBOSE) find $(PFMLIB_DIR) -name \*.c -delete
%.o: %.S $(ASM_OFFSETS_H)
@echo "AS " $@
$(VERBOSE) $(CC) -c -o $@ $(AFLAGS) $<
%.o: %.c $(PFMLIB_ARCHIVE)
@echo "CC " $@
$(VERBOSE) $(CC) -c -o $@ $(CFLAGS) $<
$(ASM_OFFSETS_S): $(ASM_OFFSETS)
$(VERBOSE) $(CC) $(CFLAGS) -S -g0 -o $@ $<
$(ASM_OFFSETS_H): $(ASM_OFFSETS_S)
@echo "GEN" $@
$(VERBOSE) $(ASM_OFFSETS_SH) $< $@
DEPFILES := $(OBJS:.o=.d)
-include $(wildcard $(DEPFILES))
clean:
@echo "CLEAN"
$(VERBOSE) find $(KTF_ROOT) -name \*.d -delete
$(VERBOSE) find $(KTF_ROOT) -name \*.o -delete
$(VERBOSE) find $(KTF_ROOT) -name \*.lds -delete
$(VERBOSE) find $(KTF_ROOT) -name \*.bin -delete
$(VERBOSE) find $(KTF_ROOT) -name \*.iso -delete
$(VERBOSE) find $(KTF_ROOT) -name \*.img -delete
$(VERBOSE) find $(KTF_ROOT) -name \*.xz -delete
$(VERBOSE) find $(KTF_ROOT) -name cscope.\* -delete
$(VERBOSE) find $(KTF_ROOT) -maxdepth 1 \( -name tags -or -name TAGS \) -delete
$(VERBOSE) find $(PFMLIB_DIR) -mindepth 1 ! -name $(PFMLIB_NAME)-$(PFMLIB_VER).tar.gz -delete
$(VERBOSE) $(RM) -f $(ASM_OFFSETS_H) $(ASM_OFFSETS_S)
$(VERBOSE) $(RM) -rf $(ACPICA_DEST_DIR)/source
$(VERBOSE) $(RM) -f $(TARGET_DEBUG) $(TARGET)
$(VERBOSE) $(RM) -rf $(GRUB_DIR)
# Check whether we can use kvm for qemu
ifeq ($(SYSTEM),LINUX)
ifneq ($(USE_KVM), false) # you can hard-disable KVM use with the USE_KVM environment variable
HAVE_KVM=$(shell test -w /dev/kvm && echo kvm)
endif # USE_KVM
endif # SYSTEM == LINUX
# Set qemu parameters
QEMU_CONFIG := $(KTF_ROOT)/.qemu_config
include $(QEMU_CONFIG)
ifeq ($(SYSTEM)$(HAVE_KVM),LINUXkvm)
QEMU_PARAMS := -cpu host -enable-kvm
else
QEMU_PARAMS := -cpu max
endif
QEMU_PARAMS += -m $(QEMU_RAM)
QEMU_PARAMS += -serial stdio
QEMU_PARAMS += -smp cpus=$(QEMU_CPUS)
QEMU_PARAMS_NOGFX := -display none -vga none -vnc none
QEMU_PARAMS_GFX := $(QEMU_PARAMS)
QEMU_PARAMS += $(QEMU_PARAMS_NOGFX)
QEMU_PARAMS_DEBUG := -s -S &
ISO_FILE := ktf.iso
ifneq ($(SYSTEM), LINUX)
$(ISO_FILE): docker${ISO_FILE}
else
$(ISO_FILE): $(TARGET)
@echo "GEN ISO" $(ISO_FILE)
$(VERBOSE) $(GRUB_FILE) --is-x86-multiboot2 $< || { echo "Multiboot not supported"; exit 1; }
$(VERBOSE) $(RM) -rf $(GRUB_DIR) && mkdir -p $(GRUB_DIR)/grub
$(VERBOSE) cp $< $(GRUB_DIR)/
$(VERBOSE) cp $(GRUB_CONFIG) $(GRUB_DIR)/grub/grub.cfg
$(VERBOSE) $(XZ) -q -f $(GRUB_DIR)/$<
$(VERBOSE) $(GRUB_MKRESCUE) --install-modules="$(GRUB_MODULES)" --fonts=no --compress=xz -o $(ISO_FILE) grub &> /dev/null
endif
.PHONY: boot
boot: $(ISO_FILE)
@echo "QEMU START"
$(VERBOSE) $(QEMU_BIN) -cdrom $(ISO_FILE) $(QEMU_PARAMS)
.PHONY: boot_gfx
boot_gfx: $(ISO_FILE)
@echo "QEMU START"
$(VERBOSE) $(QEMU_BIN) -cdrom $(ISO_FILE) $(QEMU_PARAMS_GFX)
.PHONY: boot_debug
boot_debug: $(ISO_FILE)
$(QEMU_BIN) -cdrom $(ISO_FILE) $(QEMU_PARAMS) $(QEMU_PARAMS_DEBUG)
.PHONY: gdb
gdb: boot_debug
$(GDB) $(TARGET_DEBUG) -ex 'target remote :1234' -ex 'hb _start' -ex c
killall -9 $(QEMU_BIN)
define all_sources
find $(KTF_ROOT) -name "*.[hcsS]"
endef
.PHONY: cscope
cscope:
@echo "CSCOPE"
$(VERBOSE) $(all_sources) > cscope.files
$(VERBOSE) cscope -b -q -k
tags TAGS: $(SOURCES) $(HEADERS) $(ASM_SOURCES)
@echo "TAGS ($@)"
$(VERBOSE) $(CTAGS) -o $@ $+
.PHONY: style
style:
@echo "STYLE"
$(VERBOSE) docker run --rm --workdir /src -v $(PWD):/src$(DOCKER_MOUNT_OPTS) clang-format-lint --clang-format-executable /clang-format/clang-format10 \
-r $(SOURCES) $(HEADERS) | grep -v -E '^Processing [0-9]* files:' | patch -s -p1 ||:
.PHONY: commit_style
commit_style:
@echo "COMMIT_STYLE"
$(VERBOSE) git clang-format HEAD^1 > /dev/null
DOCKERFILE := $(shell find $(KTF_ROOT) -type f -name Dockerfile)
DOCKERIMAGE := "ktf:build"
DOCKERUSERFLAGS := --user $(shell id -u):$(shell id -g) $(shell printf -- "--group-add=%q " $(shell id -G))
ifeq ($(SYSTEM), LINUX)
DOCKER_BUILD_ARGS=--network=host
endif
.PHONY: dockerimage
dockerimage:
@echo "Creating docker image"
$(VERBOSE) docker build -t $(DOCKERIMAGE) -f $(DOCKERFILE) \
$(DOCKER_BUILD_ARGS) .
.PHONY: docker%
docker%: dockerimage
@echo "running target '$(strip $(subst :,, $*))' in docker"
$(VERBOSE) docker run -t $(DOCKERUSERFLAGS) -e UNITTEST=$(UNITTEST) -e CONFIG_LIBPFM=$(CONFIG_LIBPFM) -e CONFIG_ACPICA=$(CONFIG_ACPICA) -v $(PWD):$(PWD)$(DOCKER_MOUNT_OPTS) -w $(PWD) $(DOCKERIMAGE) bash -c "make -j $(strip $(subst :,, $*))"
.PHONY: onelinescan
onelinescan:
@echo "scanning current working directory with one-line-scan"
$(VERBOSE) docker run -t -e BASE_COMMIT=origin/mainline \
-e REPORT_NEW_ONLY=true -e OVERRIDE_ANALYSIS_ERROR=true \
-e INFER_ANALYSIS_EXTRA_ARGS="--bufferoverrun" \
-e CPPCHECK_EXTRA_ARG=" --enable=style --enable=performance --enable=information --enable=portability" \
-e VERBOSE=0 -e BUILD_COMMAND="make -B all" \
-v $(PWD):$(PWD)$(DOCKER_MOUNT_OPTS) -w $(PWD) ktf-one-line-scan