From bec43ac340deb3cd11ff4ae61c5366368f9c059f Mon Sep 17 00:00:00 2001
From: Kaspar Schleiser <kaspar@schleiser.de>
Date: Thu, 24 Mar 2016 23:32:34 +0100
Subject: [PATCH] pkg: move pkgdir to $BINDIRBASE/pkg/$BOARD/$PKG_NAME, do
 proper clean

---
 pkg/ccn-lite/Makefile.include  |  2 +-
 pkg/cmsis-dsp/Makefile.include |  2 +-
 pkg/libcoap/Makefile.include   |  2 +-
 pkg/micro-ecc/Makefile.include |  2 +-
 pkg/microcoap/Makefile.include |  2 +-
 pkg/oonf_api/Makefile.include  |  2 +-
 pkg/pkg.mk                     | 18 ++++++++++++------
 pkg/relic/Makefile.include     |  2 +-
 8 files changed, 19 insertions(+), 13 deletions(-)

diff --git a/pkg/ccn-lite/Makefile.include b/pkg/ccn-lite/Makefile.include
index fdf03313e7..391f1329eb 100644
--- a/pkg/ccn-lite/Makefile.include
+++ b/pkg/ccn-lite/Makefile.include
@@ -1,2 +1,2 @@
-INCLUDES += -I$(RIOTPKG)/ccn-lite -I$(BINDIR)/pkg/ccn-lite/src
+INCLUDES += -I$(RIOTPKG)/ccn-lite -I$(BINDIRBASE)/pkg/$(BOARD)/ccn-lite/src
 INCLUDES += -I$(RIOTBASE)/sys/posix/include
diff --git a/pkg/cmsis-dsp/Makefile.include b/pkg/cmsis-dsp/Makefile.include
index 3fdfa74e9a..168751983e 100644
--- a/pkg/cmsis-dsp/Makefile.include
+++ b/pkg/cmsis-dsp/Makefile.include
@@ -1 +1 @@
-INCLUDES += -I$(BINDIR)/pkg/cmsis-dsp/include
+INCLUDES += -I$(BINDIRBASE)/pkg/$(BOARD)/cmsis-dsp/include
diff --git a/pkg/libcoap/Makefile.include b/pkg/libcoap/Makefile.include
index 7b3d3b9756..464f0f3a09 100644
--- a/pkg/libcoap/Makefile.include
+++ b/pkg/libcoap/Makefile.include
@@ -1,3 +1,3 @@
-INCLUDES += -I$(BINDIR)/pkg/libcoap \
+INCLUDES += -I$(BINDIRBASE)/pkg/$(BOARD)/libcoap \
 			-I$(RIOTBASE)/sys/posix/include \
 			-I$(RIOTBASE)/sys/net/include
diff --git a/pkg/micro-ecc/Makefile.include b/pkg/micro-ecc/Makefile.include
index 8f4031d449..88480400b1 100644
--- a/pkg/micro-ecc/Makefile.include
+++ b/pkg/micro-ecc/Makefile.include
@@ -1 +1 @@
-INCLUDES += -I$(BINDIR)/pkg/micro-ecc
+INCLUDES += -I$(BINDIRBASE)/pkg/$(BOARD)/micro-ecc
diff --git a/pkg/microcoap/Makefile.include b/pkg/microcoap/Makefile.include
index af7e8531bf..84444392db 100644
--- a/pkg/microcoap/Makefile.include
+++ b/pkg/microcoap/Makefile.include
@@ -1 +1 @@
-INCLUDES += -I$(BINDIR)/pkg/microcoap
+INCLUDES += -I$(BINDIRBASE)/pkg/$(BOARD)/microcoap
diff --git a/pkg/oonf_api/Makefile.include b/pkg/oonf_api/Makefile.include
index 3f21746b13..03a1d2e990 100644
--- a/pkg/oonf_api/Makefile.include
+++ b/pkg/oonf_api/Makefile.include
@@ -1 +1 @@
-INCLUDES += -I$(BINDIR)/pkg/oonf_api/src-api
+INCLUDES += -I$(BINDIRBASE)/pkg/$(BOARD)/oonf_api/src-api
diff --git a/pkg/pkg.mk b/pkg/pkg.mk
index 51fa3f3710..52b4c32874 100644
--- a/pkg/pkg.mk
+++ b/pkg/pkg.mk
@@ -2,23 +2,29 @@
 # Include this file if your Package needs to be checked out by git
 #
 PKG_DIR?=$(CURDIR)
-PKG_BUILDDIR?=$(BINDIR)/pkg/$(PKG_NAME)
+PKG_BUILDDIR?=$(BINDIRBASE)/pkg/$(BOARD)/$(PKG_NAME)
 
 .PHONY: git-download
 
 git-download: $(PKG_BUILDDIR)/.git-downloaded
 
+GIT_APPLY_PATCHES:=if test -d "$(PKG_DIR)"/patches; then \
+	git -C "$(PKG_BUILDDIR)" am --ignore-whitespace "$(PKG_DIR)"/patches/*.patch; \
+	fi
+
 $(PKG_BUILDDIR)/.git-downloaded:
 	mkdir -p $(PKG_BUILDDIR)
 	$(GITCACHE) clone "$(PKG_URL)" "$(PKG_VERSION)" "$(PKG_BUILDDIR)"
-	if test -d "$(PKG_DIR)"/patches; then \
-		git -C "$(PKG_BUILDDIR)" am --ignore-whitespace "$(PKG_DIR)"/patches/*.patch; \
-	fi
+	$(GIT_APPLY_PATCHES)
 	touch $@
 
 clean::
-	@echo "Cleaning package $(PKG_NAME)..."
-	rm -rf "$(PKG_BUILDDIR)"
+	@test -d $(PKG_BUILDDIR) && { \
+		git -C $(PKG_BUILDDIR) clean -f ; \
+		git -C $(PKG_BUILDDIR) checkout "$(PKG_VERSION)"; \
+		$(GIT_APPLY_PATCHES) ; \
+		touch $(PKG_BUILDDIR)/.git-downloaded ; \
+	} > /dev/null 2>&1 || true
 
 distclean::
 	rm -rf "$(PKG_BUILDDIR)"
diff --git a/pkg/relic/Makefile.include b/pkg/relic/Makefile.include
index 9cd7350a59..b6777ad443 100644
--- a/pkg/relic/Makefile.include
+++ b/pkg/relic/Makefile.include
@@ -1 +1 @@
-INCLUDES += -I$(BINDIR)/pkg/relic/include
+INCLUDES += -I$(BINDIRBASE)/pkg/$(BOARD)/relic/include
-- 
GitLab