diff --git a/.gitignore b/.gitignore
index 6a985a75df43..bfbdb307d736 100644
--- a/.gitignore
+++ b/.gitignore
@@ -33,3 +33,4 @@ wlantest/test_vectors
 wlantest/wlantest
 wlantest/wlantest_cli
 **/parallel-vm.log
+build/
diff --git a/hostapd/Makefile b/hostapd/Makefile
index a234959cd3ae..5d41b26276a3 100644
--- a/hostapd/Makefile
+++ b/hostapd/Makefile
@@ -1,6 +1,11 @@
 ALL=hostapd hostapd_cli
 VERIFY=verify_config
 
+# before common.rules for code coverage
+-include .config
+# and export for sub-makefiles
+export CONFIG_CODE_COVERAGE
+
 include ../src/common.rules
 
 ifdef LIBS
@@ -27,8 +32,6 @@ CFLAGS += -I$(abspath ../src/utils)
 
 export BINDIR ?= /usr/local/bin/
 
--include .config
-
 ifndef CONFIG_NO_GITVER
 # Add VERSION_STR postfix for builds from a git repository
 ifeq ($(wildcard ../.git),../.git)
@@ -1283,6 +1286,8 @@ install: $(addprefix $(DESTDIR)$(BINDIR)/,$(ALL))
 
 BCHECK=../src/drivers/build.hostapd
 
+$(call BUILDOBJS,OBJS)
+
 hostapd: $(BCHECK) $(OBJS)
 	$(Q)$(CC) $(LDFLAGS) -o hostapd $(OBJS) $(LIBS)
 	@$(E) "  LD " $@
@@ -1290,6 +1295,9 @@ hostapd: $(BCHECK) $(OBJS)
 ifdef CONFIG_WPA_TRACE
 OBJS_c += ../src/utils/trace.o
 endif
+
+$(call BUILDOBJS,OBJS_c)
+
 hostapd_cli: $(OBJS_c)
 	$(Q)$(CC) $(LDFLAGS) -o hostapd_cli $(OBJS_c) $(LIBS_c)
 	@$(E) "  LD " $@
@@ -1346,6 +1354,10 @@ SOBJS += ../src/crypto/sha256-kdf.o
 SOBJS += ../src/crypto/sha384-kdf.o
 SOBJS += ../src/crypto/sha512-kdf.o
 
+$(call BUILDOBJS,NOBJS)
+$(call BUILDOBJS,HOBJS)
+$(call BUILDOBJS,SOBJS)
+
 nt_password_hash: $(NOBJS)
 	$(Q)$(CC) $(LDFLAGS) -o nt_password_hash $(NOBJS) $(LIBS_n)
 	@$(E) "  LD " $@
@@ -1358,15 +1370,13 @@ sae_pk_gen: $(SOBJS)
 	$(Q)$(CC) $(LDFLAGS) -o sae_pk_gen $(SOBJS) $(LIBS_s)
 	@$(E) "  LD " $@
 
+.PHONY: lcov-html
 lcov-html:
-	lcov -c -d .. > lcov.info
+	lcov -c -d $(BUILDDIR) > lcov.info
 	genhtml lcov.info --output-directory lcov-html
 
 clean: common-clean
-	rm -f core *~ *.o nt_password_hash hlr_auc_gw
+	rm -f core *~ nt_password_hash hlr_auc_gw
 	rm -f sae_pk_gen
-	rm -f *.d *.gcno *.gcda *.gcov
 	rm -f lcov.info
 	rm -rf lcov-html
-
--include $(OBJS:%.o=%.d)
diff --git a/hs20/client/Makefile b/hs20/client/Makefile
index a73eaca1eb96..d85cd12adc6a 100644
--- a/hs20/client/Makefile
+++ b/hs20/client/Makefile
@@ -71,19 +71,10 @@ CFLAGS += -DEAP_TLS_OPENSSL
 OBJS += ../../src/crypto/tls_openssl_ocsp.o
 LIBS += -lssl -lcrypto
 
+$(call BUILDOBJS,OBJS)
 hs20-osu-client: $(OBJS)
 	$(Q)$(LDO) $(LDFLAGS) -o hs20-osu-client $(OBJS) $(LIBS)
 	@$(E) "  LD " $@
 
-clean: common-clean
-	rm -f core *~ *.o *.d
-	rm -f ../../src/utils/*.o
-	rm -f ../../src/utils/*.d
-	rm -f ../../src/common/*.o
-	rm -f ../../src/common/*.d
-	rm -f ../../src/crypto/*.o
-	rm -f ../../src/crypto/*.d
-	rm -f ../../src/wps/*.o
-	rm -f ../../src/wps/*.d
 clean: common-clean
 	rm -f core *~
diff --git a/hs20/server/Makefile b/hs20/server/Makefile
index 127c240838bf..11ec6a0869db 100644
--- a/hs20/server/Makefile
+++ b/hs20/server/Makefile
@@ -38,10 +38,4 @@ hs20_spp_server: $(OBJS)
 	$(LDO) $(LDFLAGS) -o hs20_spp_server $(OBJS) $(LIBS)
 
 clean: common-clean
-	rm -f core *~ *.o *.d
-	rm -f ../../src/utils/*.o
-	rm -f ../../src/utils/*.d
-	rm -f ../../src/crypto/*.o
-	rm -f ../../src/crypto/*.d
-
--include $(OBJS:%.o=%.d)
+	rm -f core *~
diff --git a/src/ap/Makefile b/src/ap/Makefile
index 805181d00c12..c381ab785385 100644
--- a/src/ap/Makefile
+++ b/src/ap/Makefile
@@ -65,7 +65,6 @@ LIB_OBJS= \
 	wps_hostapd.o \
 	x_snoop.o
 
+$(call BUILDOBJS,LIB_OBJS)
 libap.a: $(LIB_OBJS)
 	$(AR) crT $@ $?
-
--include $(OBJS:%.o=%.d)
diff --git a/src/common.rules b/src/common.rules
index e70a0aa2cda7..79df1b010839 100644
--- a/src/common.rules
+++ b/src/common.rules
@@ -4,7 +4,13 @@ all: $(VERIFY) $(ALL) $(EXTRA_TARGETS)
 .SUFFIXES:
 
 ROOTDIR := $(dir $(lastword $(MAKEFILE_LIST)))
-ROOTDIR := $(dir $(ROOTDIR:%/=%))
+ROOTDIR := $(dir $(ROOTDIR:%../src/=%))../
+BUILDDIR := $(ROOTDIR)build
+_PROJ := $(abspath $(PWD))
+ABSROOT := $(abspath $(ROOTDIR))
+_PROJ := $(_PROJ:$(ABSROOT)/%=%)
+COVSUFFIX = $(if $(CONFIG_CODE_COVERAGE),-cov,)
+PROJ = $(_PROJ)$(COVSUFFIX)
 
 ifndef CC
 CC=gcc
@@ -37,17 +43,29 @@ ifeq ($(Q),@)
 MAKEFLAGS += --no-print-directory
 endif
 
-ifdef CONFIG_CODE_COVERAGE
-%.o: %.c
+_DIRS =
+$(BUILDDIR):
+	mkdir -p $(BUILDDIR)
+.PHONY: _make_dirs
+_make_dirs:
+	@mkdir -p $(_DIRS)
+
+$(BUILDDIR)/$(PROJ)/src/%.o: $(ROOTDIR)src/%.c | _make_dirs
+	$(Q)$(CC) -c -o $@ $(CFLAGS) $<
 	@$(E) "  CC " $<
-	$(Q)cd $(dir $@); $(CC) -c -o $(notdir $@) $(CFLAGS) $(notdir $<)
-else
-%.o: %.c
+$(BUILDDIR)/$(PROJ)/%.o: %.c | _make_dirs
 	$(Q)$(CC) -c -o $@ $(CFLAGS) $<
 	@$(E) "  CC " $<
-endif
+
+BUILDOBJ = $(patsubst %,$(BUILDDIR)/$(PROJ)/%,$(patsubst $(ROOTDIR)%,%,$(1)))
+
+define BUILDOBJS =
+$(eval $(1) := $$(call BUILDOBJ,$($(1))))
+$(eval -include $($(1):%.o=%.d))
+$(eval _DIRS += $(dir $($(1))))
+endef
 
 .PHONY: common-clean
 common-clean:
 	$(Q)$(MAKE) -C $(ROOTDIR)/src clean
-	$(Q)rm -f $(ALL)
+	$(Q)rm -rf $(ALL) $(BUILDDIR)/$(PROJ)
diff --git a/src/common/Makefile b/src/common/Makefile
index eeb69f0d4ff3..f369f8191d99 100644
--- a/src/common/Makefile
+++ b/src/common/Makefile
@@ -19,7 +19,6 @@ LIB_OBJS= \
 	sae.o \
 	wpa_common.o
 
+$(call BUILDOBJS,LIB_OBJS)
 libcommon.a: $(LIB_OBJS)
 	$(AR) crT $@ $?
-
--include $(OBJS:%.o=%.d)
diff --git a/src/crypto/Makefile b/src/crypto/Makefile
index 94eac66fa955..cab74cd244fc 100644
--- a/src/crypto/Makefile
+++ b/src/crypto/Makefile
@@ -66,7 +66,6 @@ LIB_OBJS += random.o
 endif
 
 
+$(call BUILDOBJS,LIB_OBJS)
 libcrypto.a: $(LIB_OBJS)
 	$(AR) crT $@ $?
-
--include $(OBJS:%.o=%.d)
diff --git a/src/eap_common/Makefile b/src/eap_common/Makefile
index 7d6777babf4f..7af4da005320 100644
--- a/src/eap_common/Makefile
+++ b/src/eap_common/Makefile
@@ -23,7 +23,6 @@ LIB_OBJS= \
 	eap_wsc_common.o \
 	ikev2_common.o
 
+$(call BUILDOBJS,LIB_OBJS)
 libeap_common.a: $(LIB_OBJS)
 	$(AR) crT $@ $?
-
--include $(OBJS:%.o=%.d)
diff --git a/src/eap_peer/Makefile b/src/eap_peer/Makefile
index 282b89696e5c..80e13e5f548e 100644
--- a/src/eap_peer/Makefile
+++ b/src/eap_peer/Makefile
@@ -15,7 +15,6 @@ LIB_OBJS= \
 	eap.o \
 	eap_methods.o
 
+$(call BUILDOBJS,LIB_OBJS)
 libeap_peer.a: $(LIB_OBJS)
 	$(AR) crT $@ $?
-
--include $(OBJS:%.o=%.d)
diff --git a/src/eap_server/Makefile b/src/eap_server/Makefile
index 4ece9aa67b35..bce3d9ce8d28 100644
--- a/src/eap_server/Makefile
+++ b/src/eap_server/Makefile
@@ -13,7 +13,6 @@ LIB_OBJS= \
 	eap_server_identity.o \
 	eap_server_methods.o
 
+$(call BUILDOBJS,LIB_OBJS)
 libeap_server.a: $(LIB_OBJS)
 	$(AR) crT $@ $?
-
--include $(OBJS:%.o=%.d)
diff --git a/src/eapol_auth/Makefile b/src/eapol_auth/Makefile
index 0175a19d940e..da61c9395a97 100644
--- a/src/eapol_auth/Makefile
+++ b/src/eapol_auth/Makefile
@@ -8,7 +8,6 @@ install:
 
 LIB_OBJS = eapol_auth_sm.o eapol_auth_dump.o
 
+$(call BUILDOBJS,LIB_OBJS)
 libeapol_auth.a: $(LIB_OBJS)
 	$(AR) crT $@ $?
-
--include $(OBJS:%.o=%.d)
diff --git a/src/eapol_supp/Makefile b/src/eapol_supp/Makefile
index c98280b469b7..2f87e8f682e2 100644
--- a/src/eapol_supp/Makefile
+++ b/src/eapol_supp/Makefile
@@ -10,7 +10,6 @@ CFLAGS += -DIEEE8021X_EAPOL
 
 LIB_OBJS = eapol_supp_sm.o
 
+$(call BUILDOBJS,LIB_OBJS)
 libeapol_supp.a: $(LIB_OBJS)
 	$(AR) crT $@ $?
-
--include $(OBJS:%.o=%.d)
diff --git a/src/l2_packet/Makefile b/src/l2_packet/Makefile
index 20f229511e9c..bd8d83d63354 100644
--- a/src/l2_packet/Makefile
+++ b/src/l2_packet/Makefile
@@ -8,7 +8,6 @@ install:
 
 LIB_OBJS = l2_packet_linux.o
 
+$(call BUILDOBJS,LIB_OBJS)
 libl2_packet.a: $(LIB_OBJS)
 	$(AR) crT $@ $?
-
--include $(OBJS:%.o=%.d)
diff --git a/src/p2p/Makefile b/src/p2p/Makefile
index 174cf6f6f7f4..9a8846dc1c82 100644
--- a/src/p2p/Makefile
+++ b/src/p2p/Makefile
@@ -21,7 +21,6 @@ LIB_OBJS= \
 	p2p_sd.o \
 	p2p_utils.o
 
+$(call BUILDOBJS,LIB_OBJS)
 libp2p.a: $(LIB_OBJS)
 	$(AR) crT $@ $?
-
--include $(OBJS:%.o=%.d)
diff --git a/src/radius/Makefile b/src/radius/Makefile
index dbe9a9a9fa71..1ca6e88065c7 100644
--- a/src/radius/Makefile
+++ b/src/radius/Makefile
@@ -14,7 +14,6 @@ LIB_OBJS= \
 	radius_das.o \
 	radius_server.o
 
+$(call BUILDOBJS,LIB_OBJS)
 libradius.a: $(LIB_OBJS)
 	$(AR) crT $@ $?
-
--include $(OBJS:%.o=%.d)
diff --git a/src/rsn_supp/Makefile b/src/rsn_supp/Makefile
index 255cb73a7505..fb3bac53ba9c 100644
--- a/src/rsn_supp/Makefile
+++ b/src/rsn_supp/Makefile
@@ -19,7 +19,6 @@ LIB_OBJS= \
 	wpa.o \
 	wpa_ie.o
 
+$(call BUILDOBJS,LIB_OBJS)
 librsn_supp.a: $(LIB_OBJS)
 	$(AR) crT $@ $?
-
--include $(OBJS:%.o=%.d)
diff --git a/src/tls/Makefile b/src/tls/Makefile
index 0a36cf9e4530..db6f9a93a8d7 100644
--- a/src/tls/Makefile
+++ b/src/tls/Makefile
@@ -31,7 +31,6 @@ LIB_OBJS= \
 	x509v3.o
 
 
+$(call BUILDOBJS,LIB_OBJS)
 libtls.a: $(LIB_OBJS)
 	$(AR) crT $@ $?
-
--include $(OBJS:%.o=%.d)
diff --git a/src/utils/Makefile b/src/utils/Makefile
index 29fb1295d726..1b987e489962 100644
--- a/src/utils/Makefile
+++ b/src/utils/Makefile
@@ -34,7 +34,6 @@ LIB_OBJS += edit.o
 
 #LIB_OBJS += pcsc_funcs.o
 
+$(call BUILDOBJS,LIB_OBJS)
 libutils.a: $(LIB_OBJS)
 	$(AR) crT $@ $?
-
--include $(OBJS:%.o=%.d)
diff --git a/src/wps/Makefile b/src/wps/Makefile
index 811b62977be8..e0b664dbab34 100644
--- a/src/wps/Makefile
+++ b/src/wps/Makefile
@@ -33,7 +33,6 @@ LIB_OBJS= \
 	wps_upnp_ssdp.o \
 	wps_upnp_web.o
 
+$(call BUILDOBJS,LIB_OBJS)
 libwps.a: $(LIB_OBJS)
 	$(AR) crT $@ $?
-
--include $(OBJS:%.o=%.d)
diff --git a/wlantest/Makefile b/wlantest/Makefile
index 564732cbdb3f..c188b31d4e74 100644
--- a/wlantest/Makefile
+++ b/wlantest/Makefile
@@ -71,6 +71,9 @@ TOBJS += gcmp.o
 
 OBJS_cli = wlantest_cli.o
 
+$(call BUILDOBJS,OBJS)
+$(call BUILDOBJS,TOBJS)
+$(call BUILDOBJS,OBJS_cli)
 
 wlantest: $(OBJS) $(OWN_LIBS)
 	$(LDO) $(LDFLAGS) -o wlantest $(OBJS) $(OWN_LIBS) $(LIBS)
@@ -82,6 +85,4 @@ test_vectors: $(TOBJS) $(OWN_LIBS)
 	$(LDO) $(LDFLAGS) -o test_vectors $(TOBJS) $(OWN_LIBS) $(LIBS)
 
 clean: common-clean
-	rm -f core *~ *.o *.d
-
--include $(OBJS:%.o=%.d)
+	rm -f core *~
diff --git a/wpa_supplicant/Makefile b/wpa_supplicant/Makefile
index ab10c9cd1d4a..dbc89e577f21 100644
--- a/wpa_supplicant/Makefile
+++ b/wpa_supplicant/Makefile
@@ -17,6 +17,11 @@ endif
 VERIFY=verify_config
 EXTRA_TARGETS=dynamic_eap_methods
 
+# before common.rules for code coverage
+-include .config
+# and export for sub-makefiles
+export CONFIG_CODE_COVERAGE
+
 include ../src/common.rules
 
 ifdef LIBS
@@ -39,8 +44,6 @@ CFLAGS += $(EXTRA_CFLAGS)
 CFLAGS += -I$(abspath ../src)
 CFLAGS += -I$(abspath ../src/utils)
 
--include .config
-
 ifndef CONFIG_NO_GITVER
 # Add VERSION_STR postfix for builds from a git repository
 ifeq ($(wildcard ../.git),../.git)
@@ -1870,28 +1873,34 @@ dynamic_eap_methods: $(EAPDYN)
 
 BCHECK=../src/drivers/build.wpa_supplicant
 
+$(call BUILDOBJS,OBJS_priv)
 wpa_priv: $(BCHECK) $(OBJS_priv)
 	$(Q)$(LDO) $(LDFLAGS) -o wpa_priv $(OBJS_priv) $(LIBS)
 	@$(E) "  LD " $@
 
 $(OBJS_c) $(OBJS_t) $(OBJS_t2) $(OBJS) $(BCHECK) $(EXTRA_progs): .config
 
+$(call BUILDOBJS,OBJS)
 wpa_supplicant: $(BCHECK) $(OBJS) $(EXTRA_progs)
 	$(Q)$(LDO) $(LDFLAGS) -o wpa_supplicant $(OBJS) $(LIBS) $(EXTRALIBS)
 	@$(E) "  LD " $@
 
+$(call BUILDOBJS,OBJS_t)
 eapol_test: $(OBJS_t)
 	$(Q)$(LDO) $(LDFLAGS) -o eapol_test $(OBJS_t) $(LIBS)
 	@$(E) "  LD " $@
 
+$(call BUILDOBJS,OBJS_t2)
 preauth_test: $(OBJS_t2)
 	$(Q)$(LDO) $(LDFLAGS) -o preauth_test $(OBJS_t2) $(LIBS)
 	@$(E) "  LD " $@
 
+$(call BUILDOBJS,OBJS_p)
 wpa_passphrase: $(OBJS_p)
 	$(Q)$(LDO) $(LDFLAGS) -o wpa_passphrase $(OBJS_p) $(LIBS_p) $(LIBS)
 	@$(E) "  LD " $@
 
+$(call BUILDOBJS,OBJS_c)
 wpa_cli: $(OBJS_c)
 	$(Q)$(LDO) $(LDFLAGS) -o wpa_cli $(OBJS_c) $(LIBS_c)
 	@$(E) "  LD " $@
@@ -1905,6 +1914,7 @@ LIBCTRLSO += ../src/utils/os_$(CONFIG_OS).c
 LIBCTRLSO += ../src/utils/common.c
 LIBCTRLSO += ../src/utils/wpa_debug.c
 
+$(call BUILDOBJS,LIBCTRL)
 libwpa_client.a: $(LIBCTRL)
 	$(Q)rm -f $@
 	$(Q)$(AR) crs $@ $?
@@ -1914,14 +1924,17 @@ libwpa_client.so: $(LIBCTRLSO)
 	@$(E) "  CC  $@ ($^)"
 	$(Q)$(CC) $(LDFLAGS) -o $@ $(CFLAGS) -shared -fPIC $^
 
-libwpa_test1: libwpa_test.o libwpa_client.a
-	$(Q)$(LDO) $(LDFLAGS) -o libwpa_test1 libwpa_test.o libwpa_client.a $(LIBS_c)
+OBJS_wpatest := libwpa_test.o
+$(call BUILDOBJS,OBJS_wpatest)
+libwpa_test1: $(OBJS_wpatest) libwpa_client.a
+	$(Q)$(LDO) $(LDFLAGS) -o libwpa_test1 $(OBJS_wpatest) libwpa_client.a $(LIBS_c)
 	@$(E) "  LD " $@
 
-libwpa_test2: libwpa_test.o libwpa_client.so
-	$(Q)$(LDO) $(LDFLAGS) -o libwpa_test2 libwpa_test.o -L. -lwpa_client $(LIBS_c)
+libwpa_test2: $(OBJS_wpatest) libwpa_client.so
+	$(Q)$(LDO) $(LDFLAGS) -o libwpa_test2 $(OBJS_wpatest) -L. -lwpa_client $(LIBS_c)
 	@$(E) "  LD " $@
 
+$(call BUILDOBJS,OBJS_nfc)
 nfc_pw_token: $(OBJS_nfc)
 	$(Q)$(LDO) $(LDFLAGS) -o nfc_pw_token $(OBJS_nfc) $(LIBS)
 	@$(E) "  LD " $@
@@ -1999,8 +2012,9 @@ FIPSLD=$(FIPSDIR)/bin/fipsld
 fips:
 	$(MAKE) CC=$(FIPSLD) FIPSLD_CC="$(CC)"
 
-lcov-html: wpa_supplicant.gcda
-	lcov -c -d .. > lcov.info
+.PHONY: lcov-html
+lcov-html: $(call BUILDOBJ,wpa_supplicant.gcda)
+	lcov -c -d $(BUILDDIR) > lcov.info
 	genhtml lcov.info --output-directory lcov-html
 
 clean: common-clean
@@ -2015,5 +2029,3 @@ clean: common-clean
 	rm -f libwpa_client.a
 	rm -f libwpa_client.so
 	rm -f libwpa_test1 libwpa_test2
-
--include $(OBJS:%.o=%.d)
