summaryrefslogtreecommitdiffstats
path: root/include/custo.mk
blob: e1bea339109fb3b9569d0a030fd32d7ea3c35ac6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
define Build/Prepare
endef

define Build/Configure
endef

define Build/Compile
endef

include $(INCLUDE_DIR)/target.mk

# derive custo variables from packagename
# CONFIG_CUSTO-* / CONFIG_CUSTO_MAPPINGS-*
PKG_CUSTO_CONFIG_VAR?=$(patsubst mappings-%,CONFIG_CUSTO_MAPPINGS-%,$(patsubst conf-%,CONFIG_CUSTO-%,$(patsubst webui-%,CONFIG_CUSTO_WEBUI-%,$(patsubst cli-%,CONFIG_CUSTO_CLI-%,$(patsubst mib-%,CONFIG_CUSTO_MIB-%,$(PKG_NAME))))))
# debug
$(warning PKG_CUSTO_CONFIG_VAR = $(PKG_CUSTO_CONFIG_VAR))

ifeq ($(strip $(PKG_CUSTO_CONFIG_VAR)),$(strip $(PKG_NAME)))
  $(error PKG_CUSTO_CONFIG_VAR not explicitly set or PKG_NAME is not of the form conf-*, mappings-*, webui-*, cli-* or mib-*)
endif

ifeq ($($(PKG_CUSTO_CONFIG_VAR)),)
  #only check when actually building
  ifneq ($(strip $(BUILDNAME)),)
    $(error $(PKG_CUSTO_CONFIG_VAR) not set. Run repowrt menuconfig to correct)
  endif
endif

# if CONFIG_CUSTO-* / CONFIG_CUSTO_MAPPINGS-* is empty, replace by generic
ifneq ($($(PKG_CUSTO_CONFIG_VAR)),"")
  PKG_CUSTO_CONFIG_VALUE=$(patsubst "%",%,$($(PKG_CUSTO_CONFIG_VAR)))
else
  PKG_CUSTO_CONFIG_VALUE=generic
endif

# ensure PKG_CUSTO_FILE_PREFIXES is not empty
ifeq ($(strip $(PKG_CUSTO_FILE_PREFIXES)),)
  $(error PKG_CUSTO_FILE_PREFIXES not set)
endif

# ensure the CONFIG_CUSTO-* / CONFIG_CUSTO_MAPPINGS-* value is taken into account in the
# decision to rebuild
STAMP_PREPARED:=$(STAMP_PREPARED)_$(patsubst "%",%,$(PKG_CUSTO_CONFIG_VALUE))
# debug
$(warning STAMP_PREPARED = $(STAMP_PREPARED))

# create config section
# CUSTO- (for config)
PKG_CUSTO_VAR?=$(patsubst conf-%,CUSTO-%,$(patsubst mappings-%,CUSTO_MAPPINGS-%,$(patsubst webui-%,CUSTO_WEBUI-%,$(patsubst cli-%,CUSTO_CLI-%,$(patsubst mib-%,CUSTO_MIB-%,$(PKG_NAME))))))
#debug
$(warning PKG_CUSTO_VAR = $(PKG_CUSTO_VAR))

ifeq ($(strip $(PKG_CUSTO_VAR)),$(strip $(PKG_NAME)))
  $(error PKG_CUSTO_VAR not explicitly set or PKG_NAME is not of the form conf-*, mappings-*, webui-*, cli-* or mib-*)
endif

define create_config
  define Package/$(1)/config
    config $(2)
    depends on PACKAGE_$(1)
    string "Name of the custo to use."
    default "generic"
    $$(Package/$(1)/additionalconfig)
  endef
endef

$(eval $(call create_config,$(PKG_NAME),$(PKG_CUSTO_VAR)))

# create list of CUSTO sources
# $1 = PREFIX, $2 = PKG_CUSTO_CONFIG_VALUE , $3 = BOARDNAME
define find_custos
  $(1)_CUSTOS_$(3):=$(shell find -L . -regex './files/$(shell echo "$(3)" | tr '[:upper:]' '[:lower:]')/$(1)-$(2)' 2> /dev/null)
  $(1)_CUSTOS_$(3)+=$(shell find -L . -regex './files/common/$(1)-$(2)' 2> /dev/null)
  $(1)_CUSTOS_$(3)+=$(shell find -L . -regex './files/$(shell echo "$(3)" | tr '[:upper:]' '[:lower:]')/$(1)-generic' 2> /dev/null)
  $(1)_CUSTOS_$(3)+=$(shell find -L . -regex './files/common/$(1)-generic' 2> /dev/null)
endef

#select first available CUSTO source from list
define select_custo
  $(1)_CUSTO_SELECTED_$(2):=$(firstword $($(1)_CUSTOS_$(2)))
endef

# if $(1)_CUSTO_SELECTED is a symlink to .remove, clear $(1)_CUSTO_SELECTED
define remove_custo
  $(1)_CUSTO_SELECTED_$(2):=$(if $($(1)_CUSTO_SELECTED_$(2)),$(if $(findstring .remove,$(shell readlink -f $($(1)_CUSTO_SELECTED_$(2)) 2> /dev/null)),,$($(1)_CUSTO_SELECTED_$(2))))
endef

define select_global_custo
  $(1)_CUSTO_SELECTED:=$($(1)_CUSTO_SELECTED_$(2))
endef

# get current board family members, TARGET_FAMILY_MEMBERS looks like VDNT-O:PT VDNT-3:PA ..., strip the :xx to get VDNT-O VDNT-3
FAMILY_MEMBERS:=$(if $(firstword $(TARGET_FAMILY_MEMBERS)),$(foreach MEMBER,$(TARGET_FAMILY_MEMBERS),$(shell echo "$(MEMBER)" | cut -d ':' -f 1)),$(BOARDNAME))
# identify all possible "board" custo names
POSSIBLE_CUSTO_NAMES:=$(sort $(FAMILY_MEMBERS) $(BOARDNAME))
# find custo files for each member of the board family
$(foreach CUSTO_NAME,$(POSSIBLE_CUSTO_NAMES),$(foreach PREFIX,$(PKG_CUSTO_FILE_PREFIXES),$(eval $(call find_custos,$(PREFIX),$(PKG_CUSTO_CONFIG_VALUE),$(CUSTO_NAME)))))
# select correct custo from the retieved custo files for each member of the board family
$(foreach CUSTO_NAME,$(POSSIBLE_CUSTO_NAMES),$(foreach PREFIX,$(PKG_CUSTO_FILE_PREFIXES),$(eval $(call select_custo,$(PREFIX),$(CUSTO_NAME)))))
# handle .remove for the selected custo for each member of the board family
$(foreach CUSTO_NAME,$(POSSIBLE_CUSTO_NAMES),$(foreach PREFIX,$(PKG_CUSTO_FILE_PREFIXES),$(eval $(call remove_custo,$(PREFIX),$(CUSTO_NAME)))))
# select the custo applicable for the current BOARDNAME (for backwards compatibility)
$(foreach PREFIX,$(PKG_CUSTO_FILE_PREFIXES),$(eval $(call select_global_custo,$(PREFIX),$(BOARDNAME))))

# family installation
# $(1) dst root path $(2) = dst subdir
# if destination is /etc/custo/... then change to /etc/... if there is more than one family member
define FAMILY_INSTALL_DIR
$(if $(filter-out $(BOARDNAME),$(FAMILY_MEMBERS)),$(foreach MEMBER,$(FAMILY_MEMBERS),$(INSTALL_DIR) $(1)/etc/boards/$(MEMBER)/config/$(patsubst /%,%,$(patsubst /etc/custo/%,/etc/%,$(2)));),$(INSTALL_DIR) $(1)/$(patsubst /%,%,$(2));)
endef

# family installation
# $(1) dst root path $(2) = PREFIX $(3) = dst subdir
# if destination is /etc/custo/... then change to /etc/... if there is more than one family member
define FAMILY_INSTALL_DATA
$(if $(filter-out $(BOARDNAME),$(FAMILY_MEMBERS)),$(foreach MEMBER,$(FAMILY_MEMBERS),$(INSTALL_DATA) $($(2)_CUSTO_SELECTED_$(MEMBER)) $(1)/etc/boards/$(MEMBER)/config/$(patsubst /%,%,$(patsubst /etc/custo/%,/etc/%,$(3)));),$(INSTALL_DATA) $($(2)_CUSTO_SELECTED) $(1)/$(patsubst /%,%,$(3));)
endef

# family installation
# $(1) dst root path $(2) = PREFIX $(3) = dst subdir
# if destination is /etc/custo/... then change to /etc/... if there is more than one family member
define FAMILY_INSTALL_BIN
$(if $(filter-out $(BOARDNAME),$(FAMILY_MEMBERS)),$(foreach MEMBER,$(FAMILY_MEMBERS),$(INSTALL_BIN) $($(2)_CUSTO_SELECTED_$(MEMBER)) $(1)/etc/boards/$(MEMBER)/config/$(patsubst /%,%,$(patsubst /etc/custo/%,/etc/%,$(3)));),$(INSTALL_BIN) $($(2)_CUSTO_SELECTED) $(1)/$(patsubst /%,%,$(3));)
endef

# family installation, only proceed if source file exists
# $(1) dst root path $(2) = PREFIX $(3) = dst subdir
# if destination is /etc/custo/... then change to /etc/... if there is more than one family member
define FAMILY_INSTALL_DATA_IF_EXISTS
$(if $(filter-out $(BOARDNAME),$(FAMILY_MEMBERS)),$(foreach MEMBER,$(FAMILY_MEMBERS),$(if $($(2)_CUSTO_SELECTED_$(MEMBER)),$(INSTALL_DATA) $($(2)_CUSTO_SELECTED_$(MEMBER)) $(1)/etc/boards/$(MEMBER)/config/$(patsubst /%,%,$(patsubst /etc/custo/%,/etc/%,$(3)));)),$(INSTALL_DATA) $($(2)_CUSTO_SELECTED) $(1)/$(patsubst /%,%,$(3));)
endef

# family installation, only proceed if source file exists
# $(1) dst root path $(2) = PREFIX $(3) = dst subdir
# if destination is /etc/custo/... then change to /etc/... if there is more than one family member
define FAMILY_INSTALL_BIN_IF_EXISTS
$(if $(filter-out $(BOARDNAME),$(FAMILY_MEMBERS)),$(foreach MEMBER,$(FAMILY_MEMBERS),$(if $($(2)_CUSTO_SELECTED_$(MEMBER)),$(INSTALL_BIN) $($(2)_CUSTO_SELECTED_$(MEMBER)) $(1)/etc/boards/$(MEMBER)/config/$(patsubst /%,%,$(patsubst /etc/custo/%,/etc/%,$(3)));)),$(if $($(2)_CUSTO_SELECTED),$(INSTALL_BIN) $($(2)_CUSTO_SELECTED) $(1)/$(patsubst /%,%,$(3));))
endef

# conditional family installation, only install the dir if the file pointed to by CHECK_PREFIX contains the specified regexp 
# $(1) dst root path $(2) = CHECK_PREFIX $(3) regexp  $(4) = dst subdir
# if destination is /etc/custo/... then change to /etc/... if there is more than one family member
define FAMILY_INSTALL_DIR_CONDITIONAL
$(if $(filter-out $(BOARDNAME),$(FAMILY_MEMBERS)),$(foreach MEMBER,$(FAMILY_MEMBERS),$(if $(firstword $(shell grep "$(3)" "$($(2)_CUSTO_SELECTED_$(MEMBER))" 2> /dev/null)),$(INSTALL_DIR) $(1)/etc/boards/$(MEMBER)/config/$(patsubst /%,%,$(patsubst /etc/custo/%,/etc/%,$(4)));)),$(if $(firstword $(shell grep "$(3)" "$($(2)_CUSTO_SELECTED)" 2> /dev/null)),$(INSTALL_DIR) $(1)/$(patsubst /%,%,$(4));))
endef

# conditional family installation, only install the file referenced by INSTALL_PREFIX if the file pointed to by CHECK_PREFIX contains the specified regexp and file pointed to by INSTALL_PREFIX exists
# $(1) dst root path $(2) = CHECK_PREFIX $(3) regexp  $(4) INSTALL_PREFIX $(5) = dst subdir
# if destination is /etc/custo/... then change to /etc/... if there is more than one family member
define FAMILY_INSTALL_DATA_CONDITIONAL
$(if $(filter-out $(BOARDNAME),$(FAMILY_MEMBERS)),$(foreach MEMBER,$(FAMILY_MEMBERS),$(if $(firstword $(shell grep "$(3)" "$($(2)_CUSTO_SELECTED_$(MEMBER))" 2> /dev/null)),$(if $($(4)_CUSTO_SELECTED_$(MEMBER)),$(INSTALL_DATA) $($(4)_CUSTO_SELECTED_$(MEMBER)) $(1)/etc/boards/$(MEMBER)/config/$(patsubst /%,%,$(patsubst /etc/custo/%,/etc/%,$(5)));))),$(if $(firstword $(shell grep "$(3)" "$($(2)_CUSTO_SELECTED)" 2> /dev/null)),$(if $($(4)_CUSTO_SELECTED),$(INSTALL_DATA) $($(4)_CUSTO_SELECTED) $(1)/$(patsubst /%,%,$(5));)))
endef

# conditional family installation, only install the file referenced by INSTALL_PREFIX if the file pointed to by CHECK_PREFIX contains the specified regexp and file pointed to by INSTALL_PREFIX exists
# $(1) dst root path $(2) = CHECK_PREFIX $(3) regexp  $(4) INSTALL_PREFIX $(5) = dst subdir
# if destination is /etc/custo/... then change to /etc/... if there is more than one family member
define FAMILY_INSTALL_BIN_CONDITIONAL
$(if $(filter-out $(BOARDNAME),$(FAMILY_MEMBERS)),$(foreach MEMBER,$(FAMILY_MEMBERS),$(if $(firstword $(shell grep "$(3)" "$($(2)_CUSTO_SELECTED_$(MEMBER))" 2> /dev/null)),$(if $($(4)_CUSTO_SELECTED_$(MEMBER)),$(INSTALL_BIN) $($(4)_CUSTO_SELECTED_$(MEMBER)) $(1)/etc/boards/$(MEMBER)/config/$(patsubst /%,%,$(patsubst /etc/custo/%,/etc/%,$(5)));))),$(if $(firstword $(shell grep "$(3)" "$($(2)_CUSTO_SELECTED)" 2> /dev/null)),$(if $($(4)_CUSTO_SELECTED),$(INSTALL_BIN) $($(4)_CUSTO_SELECTED) $(1)/$(patsubst /%,%,$(5));)))
endef

# reporting
define report_custo_per_board
$(shell echo "$(PKG_NAME): $(1)_CUSTOS_$(2) = $($(1)_CUSTOS_$(2)) $(1)_CUSTO_SELECTED_$(2) = $($(1)_CUSTO_SELECTED_$(2))" >> $(TMP_DIR)/.custo_$(PKG_NAME))
endef

define report_custo_global
$(shell echo "$(PKG_NAME): BOARDNAME = $(BOARDNAME) $(1)_CUSTO_SELECTED = $($(1)_CUSTO_SELECTED)" >> $(TMP_DIR)/.custo_$(PKG_NAME))
endef

# remove existing report
$(shell rm -f $(TMP_DIR)/.custo_$(PKG_NAME))

# generate new report
$(foreach MEMBER,$(FAMILY_MEMBERS),$(foreach PREFIX,$(PKG_CUSTO_FILE_PREFIXES),$(eval $(call report_custo_per_board,$(PREFIX),$(MEMBER)))))
$(foreach PREFIX,$(PKG_CUSTO_FILE_PREFIXES),$(eval $(call report_custo_global,$(PREFIX))))