1*4882a593Smuzhiyun# This mimics the top-level Makefile. We do it explicitly here so that this 2*4882a593Smuzhiyun# Makefile can operate with or without the kbuild infrastructure. 3*4882a593Smuzhiyunifneq ($(LLVM),) 4*4882a593SmuzhiyunCC := clang 5*4882a593Smuzhiyunelse 6*4882a593SmuzhiyunCC := $(CROSS_COMPILE)gcc 7*4882a593Smuzhiyunendif 8*4882a593Smuzhiyun 9*4882a593Smuzhiyunifeq (0,$(MAKELEVEL)) 10*4882a593Smuzhiyun ifeq ($(OUTPUT),) 11*4882a593Smuzhiyun OUTPUT := $(shell pwd) 12*4882a593Smuzhiyun DEFAULT_INSTALL_HDR_PATH := 1 13*4882a593Smuzhiyun endif 14*4882a593Smuzhiyunendif 15*4882a593Smuzhiyunselfdir = $(realpath $(dir $(filter %/lib.mk,$(MAKEFILE_LIST)))) 16*4882a593Smuzhiyun 17*4882a593Smuzhiyun# The following are built by lib.mk common compile rules. 18*4882a593Smuzhiyun# TEST_CUSTOM_PROGS should be used by tests that require 19*4882a593Smuzhiyun# custom build rule and prevent common build rule use. 20*4882a593Smuzhiyun# TEST_PROGS are for test shell scripts. 21*4882a593Smuzhiyun# TEST_CUSTOM_PROGS and TEST_PROGS will be run by common run_tests 22*4882a593Smuzhiyun# and install targets. Common clean doesn't touch them. 23*4882a593SmuzhiyunTEST_GEN_PROGS := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_PROGS)) 24*4882a593SmuzhiyunTEST_GEN_PROGS_EXTENDED := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_PROGS_EXTENDED)) 25*4882a593SmuzhiyunTEST_GEN_FILES := $(patsubst %,$(OUTPUT)/%,$(TEST_GEN_FILES)) 26*4882a593Smuzhiyun 27*4882a593Smuzhiyunifdef KSFT_KHDR_INSTALL 28*4882a593Smuzhiyuntop_srcdir ?= ../../../.. 29*4882a593Smuzhiyuninclude $(top_srcdir)/scripts/subarch.include 30*4882a593SmuzhiyunARCH ?= $(SUBARCH) 31*4882a593Smuzhiyun 32*4882a593Smuzhiyun# set default goal to all, so make without a target runs all, even when 33*4882a593Smuzhiyun# all isn't the first target in the file. 34*4882a593Smuzhiyun.DEFAULT_GOAL := all 35*4882a593Smuzhiyun 36*4882a593Smuzhiyun# Invoke headers install with --no-builtin-rules to avoid circular 37*4882a593Smuzhiyun# dependency in "make kselftest" case. In this case, second level 38*4882a593Smuzhiyun# make inherits builtin-rules which will use the rule generate 39*4882a593Smuzhiyun# Makefile.o and runs into 40*4882a593Smuzhiyun# "Circular Makefile.o <- prepare dependency dropped." 41*4882a593Smuzhiyun# and headers_install fails and test compile fails. 42*4882a593Smuzhiyun# O= KBUILD_OUTPUT cases don't run into this error, since main Makefile 43*4882a593Smuzhiyun# invokes them as sub-makes and --no-builtin-rules is not necessary, 44*4882a593Smuzhiyun# but doesn't cause any failures. Keep it simple and use the same 45*4882a593Smuzhiyun# flags in both cases. 46*4882a593Smuzhiyun# Note that the support to install headers from lib.mk is necessary 47*4882a593Smuzhiyun# when test Makefile is run directly with "make -C". 48*4882a593Smuzhiyun# When local build is done, headers are installed in the default 49*4882a593Smuzhiyun# INSTALL_HDR_PATH usr/include. 50*4882a593Smuzhiyun.PHONY: khdr 51*4882a593Smuzhiyun.NOTPARALLEL: 52*4882a593Smuzhiyunkhdr: 53*4882a593Smuzhiyunifndef KSFT_KHDR_INSTALL_DONE 54*4882a593Smuzhiyunifeq (1,$(DEFAULT_INSTALL_HDR_PATH)) 55*4882a593Smuzhiyun $(MAKE) --no-builtin-rules ARCH=$(ARCH) -C $(top_srcdir) headers_install 56*4882a593Smuzhiyunelse 57*4882a593Smuzhiyun $(MAKE) --no-builtin-rules INSTALL_HDR_PATH=$$OUTPUT/usr \ 58*4882a593Smuzhiyun ARCH=$(ARCH) -C $(top_srcdir) headers_install 59*4882a593Smuzhiyunendif 60*4882a593Smuzhiyunendif 61*4882a593Smuzhiyun 62*4882a593Smuzhiyunall: khdr $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) 63*4882a593Smuzhiyunelse 64*4882a593Smuzhiyunall: $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) 65*4882a593Smuzhiyunendif 66*4882a593Smuzhiyun 67*4882a593Smuzhiyundefine RUN_TESTS 68*4882a593Smuzhiyun BASE_DIR="$(selfdir)"; \ 69*4882a593Smuzhiyun . $(selfdir)/kselftest/runner.sh; \ 70*4882a593Smuzhiyun if [ "X$(summary)" != "X" ]; then \ 71*4882a593Smuzhiyun per_test_logging=1; \ 72*4882a593Smuzhiyun fi; \ 73*4882a593Smuzhiyun run_many $(1) 74*4882a593Smuzhiyunendef 75*4882a593Smuzhiyun 76*4882a593Smuzhiyunrun_tests: all 77*4882a593Smuzhiyunifdef building_out_of_srctree 78*4882a593Smuzhiyun @if [ "X$(TEST_PROGS)$(TEST_PROGS_EXTENDED)$(TEST_FILES)" != "X" ]; then \ 79*4882a593Smuzhiyun rsync -aq $(TEST_PROGS) $(TEST_PROGS_EXTENDED) $(TEST_FILES) $(OUTPUT); \ 80*4882a593Smuzhiyun fi 81*4882a593Smuzhiyun @if [ "X$(TEST_PROGS)" != "X" ]; then \ 82*4882a593Smuzhiyun $(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) \ 83*4882a593Smuzhiyun $(addprefix $(OUTPUT)/,$(TEST_PROGS))) ; \ 84*4882a593Smuzhiyun else \ 85*4882a593Smuzhiyun $(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS)); \ 86*4882a593Smuzhiyun fi 87*4882a593Smuzhiyunelse 88*4882a593Smuzhiyun @$(call RUN_TESTS, $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS)) 89*4882a593Smuzhiyunendif 90*4882a593Smuzhiyun 91*4882a593Smuzhiyundefine INSTALL_SINGLE_RULE 92*4882a593Smuzhiyun $(if $(INSTALL_LIST),@mkdir -p $(INSTALL_PATH)) 93*4882a593Smuzhiyun $(if $(INSTALL_LIST),rsync -a $(INSTALL_LIST) $(INSTALL_PATH)/) 94*4882a593Smuzhiyunendef 95*4882a593Smuzhiyun 96*4882a593Smuzhiyundefine INSTALL_RULE 97*4882a593Smuzhiyun $(eval INSTALL_LIST = $(TEST_PROGS)) $(INSTALL_SINGLE_RULE) 98*4882a593Smuzhiyun $(eval INSTALL_LIST = $(TEST_PROGS_EXTENDED)) $(INSTALL_SINGLE_RULE) 99*4882a593Smuzhiyun $(eval INSTALL_LIST = $(TEST_FILES)) $(INSTALL_SINGLE_RULE) 100*4882a593Smuzhiyun $(eval INSTALL_LIST = $(TEST_GEN_PROGS)) $(INSTALL_SINGLE_RULE) 101*4882a593Smuzhiyun $(eval INSTALL_LIST = $(TEST_CUSTOM_PROGS)) $(INSTALL_SINGLE_RULE) 102*4882a593Smuzhiyun $(eval INSTALL_LIST = $(TEST_GEN_PROGS_EXTENDED)) $(INSTALL_SINGLE_RULE) 103*4882a593Smuzhiyun $(eval INSTALL_LIST = $(TEST_GEN_FILES)) $(INSTALL_SINGLE_RULE) 104*4882a593Smuzhiyunendef 105*4882a593Smuzhiyun 106*4882a593Smuzhiyuninstall: all 107*4882a593Smuzhiyunifdef INSTALL_PATH 108*4882a593Smuzhiyun $(INSTALL_RULE) 109*4882a593Smuzhiyunelse 110*4882a593Smuzhiyun $(error Error: set INSTALL_PATH to use install) 111*4882a593Smuzhiyunendif 112*4882a593Smuzhiyun 113*4882a593Smuzhiyunemit_tests: 114*4882a593Smuzhiyun for TEST in $(TEST_GEN_PROGS) $(TEST_CUSTOM_PROGS) $(TEST_PROGS); do \ 115*4882a593Smuzhiyun BASENAME_TEST=`basename $$TEST`; \ 116*4882a593Smuzhiyun echo "$(COLLECTION):$$BASENAME_TEST"; \ 117*4882a593Smuzhiyun done 118*4882a593Smuzhiyun 119*4882a593Smuzhiyun# define if isn't already. It is undefined in make O= case. 120*4882a593Smuzhiyunifeq ($(RM),) 121*4882a593SmuzhiyunRM := rm -f 122*4882a593Smuzhiyunendif 123*4882a593Smuzhiyun 124*4882a593Smuzhiyundefine CLEAN 125*4882a593Smuzhiyun $(RM) -r $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED) $(TEST_GEN_FILES) $(EXTRA_CLEAN) 126*4882a593Smuzhiyunendef 127*4882a593Smuzhiyun 128*4882a593Smuzhiyunclean: 129*4882a593Smuzhiyun $(CLEAN) 130*4882a593Smuzhiyun 131*4882a593Smuzhiyun# When make O= with kselftest target from main level 132*4882a593Smuzhiyun# the following aren't defined. 133*4882a593Smuzhiyun# 134*4882a593Smuzhiyunifdef building_out_of_srctree 135*4882a593SmuzhiyunLINK.c = $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH) 136*4882a593SmuzhiyunCOMPILE.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c 137*4882a593SmuzhiyunLINK.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH) 138*4882a593Smuzhiyunendif 139*4882a593Smuzhiyun 140*4882a593Smuzhiyun# Selftest makefiles can override those targets by setting 141*4882a593Smuzhiyun# OVERRIDE_TARGETS = 1. 142*4882a593Smuzhiyunifeq ($(OVERRIDE_TARGETS),) 143*4882a593SmuzhiyunLOCAL_HDRS := $(selfdir)/kselftest_harness.h $(selfdir)/kselftest.h 144*4882a593Smuzhiyun$(OUTPUT)/%:%.c $(LOCAL_HDRS) 145*4882a593Smuzhiyun $(LINK.c) $(filter-out $(LOCAL_HDRS),$^) $(LDLIBS) -o $@ 146*4882a593Smuzhiyun 147*4882a593Smuzhiyun$(OUTPUT)/%.o:%.S 148*4882a593Smuzhiyun $(COMPILE.S) $^ -o $@ 149*4882a593Smuzhiyun 150*4882a593Smuzhiyun$(OUTPUT)/%:%.S 151*4882a593Smuzhiyun $(LINK.S) $^ $(LDLIBS) -o $@ 152*4882a593Smuzhiyunendif 153*4882a593Smuzhiyun 154*4882a593Smuzhiyun.PHONY: run_tests all clean install emit_tests 155