xref: /kvm-unit-tests/Makefile (revision abdc5d02a7796a55802509ac9bb704c721f2a5f6)
1SHELL := /usr/bin/env bash
2
3ifeq ($(wildcard config.mak),)
4$(error run ./configure first. See ./configure -h)
5endif
6
7include config.mak
8
9# Set search path for %.c %.s and %.S files
10vpath %.c $(SRCDIR)
11vpath %.s $(SRCDIR)
12vpath %.S $(SRCDIR)
13
14libdirs-get = $(shell [ -d "lib/$(1)" ] && echo "lib/$(1) lib/$(1)/asm")
15ARCH_LIBDIRS := $(call libdirs-get,$(ARCH_LIBDIR)) $(call libdirs-get,$(TEST_DIR))
16OBJDIRS := $(ARCH_LIBDIRS)
17
18DESTDIR := $(PREFIX)/share/kvm-unit-tests/
19
20.PHONY: arch_clean clean distclean cscope
21
22# cc-option
23# Usage: OP_CFLAGS+=$(call cc-option, -falign-functions=0, -malign-functions=0)
24cc-option = $(shell if $(CC) $(CFLAGS) -Werror $(1) -S -o /dev/null -xc /dev/null \
25              > /dev/null 2>&1; then echo "$(1)"; else echo "$(2)"; fi ;)
26
27libcflat := lib/libcflat.a
28cflatobjs := \
29	lib/argv.o \
30	lib/printf.o \
31	lib/string.o \
32	lib/abort.o \
33	lib/rand.o \
34	lib/report.o \
35	lib/stack.o
36
37# libfdt paths
38LIBFDT_objdir = lib/libfdt
39LIBFDT_srcdir = $(SRCDIR)/lib/libfdt
40LIBFDT_archive = $(LIBFDT_objdir)/libfdt.a
41
42OBJDIRS += $(LIBFDT_objdir)
43
44# EFI App
45ifeq ($(CONFIG_EFI),y)
46EFI_CFLAGS := -DCONFIG_EFI -DCONFIG_RELOC
47# The following CFLAGS and LDFLAGS come from:
48#   - GNU-EFI/Makefile.defaults
49#   - GNU-EFI/apps/Makefile
50# GCC defines wchar to be 32 bits, but EFI expects 16 bits
51EFI_CFLAGS += -fshort-wchar
52# EFI applications use PIC as they are loaded to dynamic addresses, not a fixed
53# starting address
54EFI_CFLAGS += -fPIC
55# Avoid error with the initrd_dev_path struct
56EFI_CFLAGS += -Wno-gnu-variable-sized-type-not-at-end
57# Create shared library
58EFI_LDFLAGS := -Bsymbolic -shared -nostdlib
59endif
60
61#include architecture specific make rules
62include $(SRCDIR)/$(TEST_DIR)/Makefile
63
64COMMON_CFLAGS += -g $(autodepend-flags) -fno-strict-aliasing -fno-common
65COMMON_CFLAGS += -Wall -Wwrite-strings -Wempty-body -Wuninitialized
66COMMON_CFLAGS += -Wignored-qualifiers -Wno-missing-braces $(CONFIG_WERROR)
67
68frame-pointer-flag=-f$(if $(KEEP_FRAME_POINTER),no-,)omit-frame-pointer
69fomit_frame_pointer := $(call cc-option, $(frame-pointer-flag), "")
70fno_stack_protector := $(call cc-option, -fno-stack-protector, "")
71fno_stack_protector_all := $(call cc-option, -fno-stack-protector-all, "")
72wno_frame_address := $(call cc-option, -Wno-frame-address, "")
73fno_pic := $(call cc-option, -fno-pic, "")
74no_pie := $(call cc-option, -no-pie, "")
75wclobbered := $(call cc-option, -Wclobbered, "")
76wunused_but_set_parameter := $(call cc-option, -Wunused-but-set-parameter, "")
77wmissing_parameter_type := $(call cc-option, -Wmissing-parameter-type, "")
78wold_style_declaration := $(call cc-option, -Wold-style-declaration, "")
79
80COMMON_CFLAGS += $(fomit_frame_pointer)
81COMMON_CFLAGS += $(fno_stack_protector)
82COMMON_CFLAGS += $(fno_stack_protector_all)
83COMMON_CFLAGS += $(wno_frame_address)
84COMMON_CFLAGS += $(if $(U32_LONG_FMT),-D__U32_LONG_FMT__,)
85ifeq ($(CONFIG_EFI),y)
86COMMON_CFLAGS += $(EFI_CFLAGS)
87else
88COMMON_CFLAGS += $(fno_pic)
89endif
90COMMON_CFLAGS += $(wclobbered)
91COMMON_CFLAGS += $(wunused_but_set_parameter)
92
93CFLAGS += $(COMMON_CFLAGS)
94CFLAGS += $(wmissing_parameter_type)
95CFLAGS += $(wold_style_declaration)
96CFLAGS += -Woverride-init -Wmissing-prototypes -Wstrict-prototypes
97
98autodepend-flags = -MMD -MP -MF $(dir $*).$(notdir $*).d
99
100LDFLAGS += -nostdlib $(no_pie) -z noexecstack
101
102$(libcflat): $(cflatobjs)
103	$(AR) rcs $@ $^
104
105include $(LIBFDT_srcdir)/Makefile.libfdt
106$(LIBFDT_archive): CFLAGS += -ffreestanding -I $(SRCDIR)/lib -I $(SRCDIR)/lib/libfdt -Wno-sign-compare
107$(LIBFDT_archive): $(addprefix $(LIBFDT_objdir)/,$(LIBFDT_OBJS))
108	$(AR) rcs $@ $^
109
110libfdt_clean: VECHO = echo " "
111libfdt_clean: STD_CLEANFILES = *.o .*.d
112libfdt_clean: LIBFDT_dir = $(LIBFDT_objdir)
113libfdt_clean: SHAREDLIB_EXT = so
114
115# Build directory target
116.PHONY: directories
117directories:
118	@mkdir -p $(OBJDIRS)
119
120%.o: %.S
121	$(CC) $(CFLAGS) -c -nostdlib -o $@ $<
122
123-include */.*.d */*/.*.d
124
125all: directories $(shell (cd $(SRCDIR) && git rev-parse --verify --short=8 HEAD) >build-head 2>/dev/null)
126
127standalone: all
128	@scripts/mkstandalone.sh
129
130install: standalone
131	mkdir -p $(DESTDIR)
132	install tests/* $(DESTDIR)
133
134clean: arch_clean libfdt_clean
135	$(RM) $(LIBFDT_archive)
136	$(RM) lib/.*.d $(libcflat) $(cflatobjs)
137
138distclean: clean
139	$(RM) lib/asm lib/config.h config.mak $(TEST_DIR)-run msr.out cscope.* build-head
140	$(RM) -r tests logs logs.old efi-tests
141
142cscope: cscope_dirs = lib lib/libfdt lib/linux $(TEST_DIR) $(ARCH_LIBDIRS) lib/asm-generic
143cscope:
144	$(RM) ./cscope.*
145	find -L $(cscope_dirs) -maxdepth 1 \
146		-name '*.[chsS]' -exec realpath --relative-base=$(CURDIR) {} \; | sort -u > ./cscope.files
147	cscope -bk
148
149.PHONY: shellcheck
150shellcheck:
151	shellcheck -P $(SRCDIR) -a $(SRCDIR)/run_tests.sh $(SRCDIR)/*/run $(SRCDIR)/*/efi/run $(SRCDIR)/scripts/mkstandalone.sh
152
153.PHONY: tags
154tags:
155	ctags -R
156
157check-kerneldoc:
158	find $(SRCDIR) -name '*.[ch]' -exec scripts/kernel-doc -none {} +
159