From 7e736938985f72f01467d2d48c13adb74e9bda32 Mon Sep 17 00:00:00 2001
From: Angus Gratton <gus@projectgus.com>
Date: Tue, 8 Sep 2015 09:59:59 +1000
Subject: [PATCH] Rework component makefiles to have a default <component>_ROOT

This fixes a long-standing bug where build directories sometimes
expanded many levels deeper than they should have.

Component names can now no longer contains slashes (ie the component
name for 'extras/i2c' is now just 'i2c')
---
 FreeRTOS/component.mk         | 3 +--
 axtls/component.mk            | 7 +++----
 common.mk                     | 3 +++
 core/component.mk             | 2 +-
 extras/bmp180/component.mk    | 8 ++++----
 extras/i2c/component.mk       | 9 +++++----
 extras/rboot-ota/component.mk | 8 ++++----
 lwip/component.mk             | 7 +++----
 8 files changed, 24 insertions(+), 23 deletions(-)

diff --git a/FreeRTOS/component.mk b/FreeRTOS/component.mk
index 437fccb..4b63b73 100644
--- a/FreeRTOS/component.mk
+++ b/FreeRTOS/component.mk
@@ -1,8 +1,7 @@
 
-INC_DIRS += $(freertos_MAIN)/include $(freertos_MAIN)/portable/esp8266
+INC_DIRS += $(freertos_MAIN)include $(freertos_MAIN)portable/esp8266
 
 # args for passing into compile rule generation
-freertos_ROOT = $(ROOT)FreeRTOS/
 freertos_MAIN = $(freertos_ROOT)Source/
 freertos_INC_DIR = $(freertos_MAIN)include $(freertos_MAIN)portable/esp8266
 freertos_SRC_DIR = $(freertos_MAIN) $(freertos_MAIN)portable/esp8266
diff --git a/axtls/component.mk b/axtls/component.mk
index 44da5a0..73859a2 100644
--- a/axtls/component.mk
+++ b/axtls/component.mk
@@ -6,13 +6,12 @@
 
 # We supply our own hand tweaked config.h in the external 'include' dir.
 
-AXTLS_DIR = $(ROOT)axtls/axtls/
-INC_DIRS += $(ROOT)axtls/include $(AXTLS_DIR)ssl $(AXTLS_DIR)crypto
+AXTLS_DIR = $(axtls_ROOT)axtls/
+INC_DIRS += $(axtls_ROOT)include $(AXTLS_DIR)ssl $(AXTLS_DIR)crypto
 
 # args for passing into compile rule generation
-axtls_ROOT = $(ROOT)axtls
 axtls_INC_DIR =  $(AXTLS_DIR)include $(AXTLS_DIR)
-axtls_SRC_DIR = $(AXTLS_DIR)crypto $(AXTLS_DIR)ssl $(ROOT)axtls
+axtls_SRC_DIR = $(AXTLS_DIR)crypto $(AXTLS_DIR)ssl $(axtls_ROOT)
 
 #axtls_CFLAGS = $(CFLAGS) -Wno-address
 
diff --git a/common.mk b/common.mk
index 1c2424f..6772ba8 100644
--- a/common.mk
+++ b/common.mk
@@ -200,6 +200,7 @@ all: $(PROGRAM_OUT) $(FW_FILE_1) $(FW_FILE_2) $(FW_FILE)
 # Expects that the following component-specific variables are defined:
 #
 # $(1)_ROOT    = Top-level dir containing component. Can be in-tree or out-of-tree.
+#                (if this variable isn't defined, directory containing component.mk is used)
 # $(1)_SRC_DIR = List of source directories for the component. All must be under $(1)_ROOT
 # $(1)_INC_DIR = List of include directories specific for the component
 #
@@ -214,6 +215,8 @@ all: $(PROGRAM_OUT) $(FW_FILE_1) $(FW_FILE_2) $(FW_FILE)
 # Each call appends to COMPONENT_ARS which is a list of archive files for compiled components
 COMPONENT_ARS =
 define component_compile_rules
+$(1)_DEFAULT_ROOT := $(dir $(lastword $(MAKEFILE_LIST)))
+$(1)_ROOT ?= $$($(1)_DEFAULT_ROOT)
 $(1)_OBJ_DIR   = $(call lc,$(BUILD_DIR)$(1)/)
 ### determine source files and object files ###
 $(1)_SRC_FILES ?= $$(foreach sdir,$$($(1)_SRC_DIR), \
diff --git a/core/component.mk b/core/component.mk
index b4eca35..d6bc1da 100644
--- a/core/component.mk
+++ b/core/component.mk
@@ -1,6 +1,6 @@
 INC_DIRS += $(core_ROOT)include
 
 # args for passing into compile rule generation
-core_ROOT = $(ROOT)core/
 core_SRC_DIR = $(core_ROOT)
+
 $(eval $(call component_compile_rules,core))
diff --git a/extras/bmp180/component.mk b/extras/bmp180/component.mk
index 5efc68a..2c6b4c8 100644
--- a/extras/bmp180/component.mk
+++ b/extras/bmp180/component.mk
@@ -1,9 +1,9 @@
 # Component makefile for extras/bmp180
 
-INC_DIRS += $(ROOT)extras
+# expected anyone using bmp driver includes it as 'bmp180/bmp180.h'
+INC_DIRS += $(bmp180_ROOT)..
 
 # args for passing into compile rule generation
-extras/bmp180_INC_DIR =  $(ROOT)extras
-extras/bmp180_SRC_DIR =  $(ROOT)extras/bmp180
+bmp180_SRC_DIR =  $(bmp180_ROOT)
 
-$(eval $(call component_compile_rules,extras/bmp180))
+$(eval $(call component_compile_rules,bmp180))
diff --git a/extras/i2c/component.mk b/extras/i2c/component.mk
index 13b0f83..0ac3165 100644
--- a/extras/i2c/component.mk
+++ b/extras/i2c/component.mk
@@ -1,9 +1,10 @@
 # Component makefile for extras/i2c
 
-INC_DIRS += $(ROOT)extras
+# expected anyone using i2c driver includes it as 'i2c/i2c.h'
+INC_DIRS += $(i2c_ROOT)..
 
 # args for passing into compile rule generation
-extras/i2c_INC_DIR =  $(ROOT)extras
-extras/i2c_SRC_DIR =  $(ROOT)extras/i2c
+i2c_INC_DIR =
+i2c_SRC_DIR = $(i2c_ROOT)
 
-$(eval $(call component_compile_rules,extras/i2c))
+$(eval $(call component_compile_rules,i2c))
diff --git a/extras/rboot-ota/component.mk b/extras/rboot-ota/component.mk
index 598e355..690b7bc 100644
--- a/extras/rboot-ota/component.mk
+++ b/extras/rboot-ota/component.mk
@@ -1,9 +1,9 @@
 # Component makefile for extras/rboot-ota
 
-INC_DIRS += $(ROOT)extras/rboot-ota
+INC_DIRS += $(rboot-ota_ROOT)
 
 # args for passing into compile rule generation
-extras/rboot-ota_INC_DIR =  $(ROOT)extras/rboot-ota
-extras/rboot-ota_SRC_DIR =  $(ROOT)extras/rboot-ota
+rboot-ota_SRC_DIR =  $(rboot-ota_ROOT)
+
+$(eval $(call component_compile_rules,rboot-ota))
 
-$(eval $(call component_compile_rules,extras/rboot-ota))
diff --git a/lwip/component.mk b/lwip/component.mk
index 4257689..e16baa0 100644
--- a/lwip/component.mk
+++ b/lwip/component.mk
@@ -1,12 +1,11 @@
 # Component makefile for LWIP
 
-LWIP_DIR = $(ROOT)lwip/lwip/src/
-INC_DIRS += $(LWIP_DIR)include $(ROOT)lwip/include $(ROOT)lwip/include $(LWIP_DIR)include/ipv4 $(LWIP_DIR)include/ipv4/lwip $(LWIP_DIR)include/lwip
+LWIP_DIR = $(lwip_ROOT)lwip/src/
+INC_DIRS += $(LWIP_DIR)include $(ROOT)lwip/include $(lwip_ROOT)include $(LWIP_DIR)include/ipv4 $(LWIP_DIR)include/ipv4/lwip $(LWIP_DIR)include/lwip
 
 # args for passing into compile rule generation
-lwip_ROOT = $(ROOT)/lwip
 lwip_INC_DIR =  # all in INC_DIRS, needed for normal operation
-lwip_SRC_DIR = $(ROOT)/lwip $(LWIP_DIR)api $(LWIP_DIR)core $(LWIP_DIR)core/ipv4 $(LWIP_DIR)netif
+lwip_SRC_DIR = $(lwip_ROOT) $(LWIP_DIR)api $(LWIP_DIR)core $(LWIP_DIR)core/ipv4 $(LWIP_DIR)netif
 
 # LWIP 1.4.1 generates a single warning so we need to disable -Werror when building it
 lwip_CFLAGS = $(CFLAGS) -Wno-address