Rework main Makefiles
[oweals/u-boot_mod.git] / Makefile
1 #
2 # Copyright (C) 2016 Piotr Dymacz <piotr@dymacz.pl>
3 #
4 # SPDX-License-Identifier: GPL-2.0
5 #
6
7 SHELL = bash
8
9 HOSTARCH := $(shell uname -m |        \
10               sed -e s/i.86/x86_32/   \
11                   -e s/sun4u/sparc64/ \
12                   -e s/arm.*/arm/     \
13                   -e s/sa110/arm/     \
14                   -e s/powerpc/ppc/   \
15                   -e s/macppc/ppc/)
16
17 HOSTOS := $(shell uname -s | tr '[:upper:]' '[:lower:]' | \
18             sed -e 's/\(cygwin\).*/cygwin/')
19
20 ifneq ($(HOSTOS), darwin)
21   ifneq ($(HOSTOS), linux)
22     $(error Error! Unsupported host operating system/arch: "$(HOSTOS)-$(HOSTARCH)")
23   endif
24 endif
25
26 export HOSTOS
27 export HOSTARCH
28 export BUILD_TOPDIR = $(PWD)
29 export STAGING_DIR  = $(BUILD_TOPDIR)/tmp
30 export SOURCE_DIR   = $(BUILD_TOPDIR)/u-boot
31 export BIN_DIR      = $(BUILD_TOPDIR)/bin
32 export SUB_MAKE_CMD = $(MAKE) --silent --no-print-directory \
33                       ARCH=mips V=1 SHELL=$(SHELL)
34
35 # ==========================================================================
36 # You can override some default configuration options below or pass them on
37 # command line, for example:
38 # make ... IMG_SIZE=256 IMG_LZMA=0 CROSS_COMPILE=...
39
40 # Set to 1 if you want to build RAM-loadable image, without low level
41 # initialization of the hardware (PLL/clocks, DRAM):
42 # IMG_RAM =
43
44 # You can change limit of the image size in KB with below option (image will
45 # be filled up to the selected size with 0xFF):
46 # IMG_SIZE =
47
48 # If you don't want LZMA compressed image, set below option to 1 (by default
49 # images for all targets are LZMA compressed):
50 # IMG_LZMA =
51
52 # Define _absolute_ path to your toolchain directory, for example:
53 # export TOOLCHAIN_DIR:=/home/user/toolchain-mips_24kc_gcc-5.4.0_musl-1.1.15
54 # export PATH:=$(TOOLCHAIN_DIR)/bin:$(PATH)
55
56 ifndef CROSS_COMPILE
57   CROSS_COMPILE = mips-openwrt-linux-musl-
58 endif
59 export CROSS_COMPILE
60
61 # ==========================================================================
62
63 # =======================
64 # CUSTOM HELPER FUNCTIONS
65 # =======================
66
67 define echo_green
68   echo -e "\e[92m$(1)\e[0m"
69 endef
70
71 define echo_red
72   echo -e "\e[91m$(1)\e[0m"
73 endef
74
75 define echo_blue
76   echo -e "\e[96m$(1)\e[0m"
77 endef
78
79 # $(1): size
80 define img_size
81 $(if $(IMG_SIZE),$(strip $(IMG_SIZE)),$(strip $(1)))
82 endef
83
84 # $(1): value
85 define is_one
86 $(if $(filter $(strip $(1)),1),1,0)
87 endef
88
89 # $(1): file extension
90 define img_name
91 u-boot_mod__$(shell date +"%Y%m%d")__$@$(if \
92 $(filter $(IMG_RAM),1),__RAM-LOAD-ONLY)$(if $(1),.$(1))
93 endef
94
95 define md5_sum
96   $(call echo_green,Calculating MD5 sum for the final image...)
97
98   md5sum $(BIN_DIR)/$(call img_name,bin) | \
99          awk '{print $$1}' | \
100          tr -d '\n' > $(BIN_DIR)/$(call img_name).md5
101
102   echo ' *'$(call img_name,bin) >> $(BIN_DIR)/$(call img_name,md5)
103 endef
104
105 # $(1): size
106 define padded_img
107   $(call echo_green,Preparing $(1) KB image padded with 0xFF...)
108   tr "\000" "\377" < /dev/zero | dd ibs=1k count=$(1) \
109      of=$(BIN_DIR)/$(call img_name,bin) 2> /dev/null
110 endef
111
112 define final_img
113   $(call echo_green,Preparing final image...)
114   dd if=$(BIN_DIR)/temp.bin of=$(BIN_DIR)/$(call img_name,bin) \
115      conv=notrunc 2> /dev/null
116
117   rm -f $(BIN_DIR)/temp.bin
118 endef
119
120 # $(1): path to image
121 # $(2): size limit in KB
122 define size_chk
123   $(call echo_green,Checking size of the image...)
124
125   if [ `wc -c < $(1)` -gt `echo $(2)*1024 | bc` ]; then \
126     echo; \
127     $(call echo_red,  ======================); \
128     $(call echo_red,  IMAGE SIZE IS TOO BIG!); \
129     $(call echo_red,  ======================); \
130     echo; \
131     rm -f $(1); \
132     exit 1; \
133   fi;
134 endef
135
136 # $(1): filename of image to copy
137 # $(2): image size limit (check if set)
138 define copy_img
139   echo;
140   $(call echo_green,Copying compiled image...)
141
142   cp $(SOURCE_DIR)/$(strip $(1)).bin $(BIN_DIR)/temp.bin
143   $(if $(2),$(call size_chk,$(BIN_DIR)/temp.bin,$(2)))
144 endef
145
146 # $(1): size limit in KB
147 # $(2): if set to 1, use LZMA
148 # $(3): other parameters passed to subdir make
149 define build
150   args="IMG_SIZE=$(call img_size,$(1)) \
151         IMG_LZMA=$(call is_one,$(2)) \
152         $(strip $(3))"; \
153   cd $(SOURCE_DIR) && \
154      $(SUB_MAKE_CMD) $@ $$args && \
155      $(SUB_MAKE_CMD) all $$args
156
157   $(if $(filter $(IMG_RAM),1),\
158     $(call copy_img,u-boot), \
159     $(if $(filter $(call is_one,$(2)),1), \
160       $(call copy_img,tuboot,$(call img_size,$(1))), \
161       $(call copy_img,u-boot,$(call img_size,$(1))) \
162     ) \
163   )
164
165   $(if $(filter $(IMG_RAM),1),,$(call padded_img,$(1)))
166   $(call final_img)
167   $(call md5_sum)
168   echo;
169   $(call echo_green,DONE!)
170   $(call echo_green,Image 'bin/$(call img_name,bin)' is ready!)
171
172   if [ "x$$IMG_RAM" = "x1" ]; then \
173     echo; \
174     $(call echo_blue,  ================================); \
175     $(call echo_blue,  THIS IMAGE IS ONLY FOR RAM LOAD!); \
176     $(call echo_blue,  DO NOT WRITE IT INTO FLASH CHIP!); \
177     $(call echo_blue,  ================================); \
178     echo; \
179   fi;
180 endef
181
182 # ===========================================
183 # TARGETS IN ALPHABETICAL ORDER, SHARED FIRST
184 # ===========================================
185
186 COMMON_AR933X_TARGETS = \
187         gainstrong_oolite_v1_dev \
188         gl-innovations_gl-inet-6416 \
189         tp-link_tl-mr10u \
190         tp-link_tl-mr13u \
191         tp-link_tl-mr3020 \
192         tp-link_tl-mr3040 \
193         tp-link_tl-mr3220_v2 \
194         tp-link_tl-wr703n \
195         tp-link_tl-wr710n \
196         tp-link_tl-wr720n_v3_CN \
197         tp-link_tl-wr740n_v4
198
199 $(COMMON_AR933X_TARGETS):
200         @$(call build,123,1)
201
202 COMMON_ETHS27_TARGETS = \
203         tp-link_tl-mr3420_v2 \
204         tp-link_tl-wa830re_v2_tl-wa801nd_v2 \
205         tp-link_tl-wdr3500 \
206         tp-link_tl-wr802n \
207         tp-link_tl-wr820n_CN \
208         tp-link_tl-wr841n_v8 \
209         tp-link_tl-wr841n_v9
210
211 $(COMMON_ETHS27_TARGETS):
212         @$(call build,123,1,ETH_CONFIG=_s27)
213
214 8devices_carambola2:
215         @$(call build,256)
216
217 d-link_dir-505:
218         @$(call build,64,1)
219
220 dragino_v2_ms14:
221         @$(call build,192,1,DEVICE_VENDOR=dragino)
222
223 tp-link_tl-wdr3600_tl-43x0:
224         @$(call build,123,1,ETH_CONFIG=_s17)
225
226 unwireddevices_unwired-one:
227         @$(call build,128,1,DEVICE_VENDOR=SE)
228
229 village-telco_mesh-potato_v2:
230         @$(call build,192,1,DEVICE_VENDOR=villagetelco)
231
232 wallys_dr531:
233         @$(call build,192,1,ETH_CONFIG=_s27)
234
235 zbtlink_zbt-we1526:
236         @$(call build,256,1,ETH_CONFIG=_s27)
237
238 # =============
239 # CLEAN TARGETS
240 # =============
241
242 clean:
243         @cd $(SOURCE_DIR) && $(SUB_MAKE_CMD) distclean
244         @rm -f $(SOURCE_DIR)/httpd/fsdata.c
245
246 clean_all: clean
247         @$(call echo_green,Removing all binary images...)
248         @rm -f $(BIN_DIR)/*.bin
249         @rm -f $(BIN_DIR)/*.md5