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