-export BUILD_TOPDIR=$(PWD)
-export STAGING_DIR=$(BUILD_TOPDIR)/tmp
-
-export MAKECMD=make --silent ARCH=mips CROSS_COMPILE=mips-linux-gnu-
-
-# boot delay (time to autostart boot command)
-export CONFIG_BOOTDELAY=1
-
-tplink_mr3020: export UBOOT_FILE_NAME=uboot_for_tp-link_tl-mr3020
-tplink_mr3020: export MAX_UBOOT_SIZE=64
-tplink_mr3020: export COMPRESSED_UBOOT=1
-tplink_mr3020:
- @cd $(BUILD_TOPDIR)/u-boot/ && $(MAKECMD) mr3020_config
- @cd $(BUILD_TOPDIR)/u-boot/ && $(MAKECMD) ENDIANNESS=-EB V=1 all
- @cp $(BUILD_TOPDIR)/u-boot/tuboot.bin $(BUILD_TOPDIR)/bin/temp.bin
- @make show_size
-
-tplink_wr703n: export UBOOT_FILE_NAME=uboot_for_tp-link_tl-wr703n
-tplink_wr703n: export MAX_UBOOT_SIZE=64
-tplink_wr703n: export COMPRESSED_UBOOT=1
-tplink_wr703n:
- @cd $(BUILD_TOPDIR)/u-boot/ && $(MAKECMD) wr703n_config
- @cd $(BUILD_TOPDIR)/u-boot/ && $(MAKECMD) ENDIANNESS=-EB V=1 all
- @cp $(BUILD_TOPDIR)/u-boot/tuboot.bin $(BUILD_TOPDIR)/bin/temp.bin
- @make show_size
-
-tplink_wr720n_v3_CH: export UBOOT_FILE_NAME=uboot_for_tp-link_tl-wr720n_v3_CH
-tplink_wr720n_v3_CH: export MAX_UBOOT_SIZE=64
-tplink_wr720n_v3_CH: export COMPRESSED_UBOOT=1
-tplink_wr720n_v3_CH:
- @cd $(BUILD_TOPDIR)/u-boot/ && $(MAKECMD) wr720n_v3_CH_config
- @cd $(BUILD_TOPDIR)/u-boot/ && $(MAKECMD) ENDIANNESS=-EB V=1 all
- @cp $(BUILD_TOPDIR)/u-boot/tuboot.bin $(BUILD_TOPDIR)/bin/temp.bin
- @make show_size
-
-tplink_wr710n: export UBOOT_FILE_NAME=uboot_for_tp-link_tl-wr710n
-tplink_wr710n: export MAX_UBOOT_SIZE=64
-tplink_wr710n: export COMPRESSED_UBOOT=1
-tplink_wr710n:
- @cd $(BUILD_TOPDIR)/u-boot/ && $(MAKECMD) wr710n_config
- @cd $(BUILD_TOPDIR)/u-boot/ && $(MAKECMD) ENDIANNESS=-EB V=1 all
- @cp $(BUILD_TOPDIR)/u-boot/tuboot.bin $(BUILD_TOPDIR)/bin/temp.bin
- @make show_size
-
-tplink_mr3040: export UBOOT_FILE_NAME=uboot_for_tp-link_tl-mr3040
-tplink_mr3040: export MAX_UBOOT_SIZE=64
-tplink_mr3040: export COMPRESSED_UBOOT=1
-tplink_mr3040:
- @cd $(BUILD_TOPDIR)/u-boot/ && $(MAKECMD) mr3040_config
- @cd $(BUILD_TOPDIR)/u-boot/ && $(MAKECMD) ENDIANNESS=-EB V=1 all
- @cp $(BUILD_TOPDIR)/u-boot/tuboot.bin $(BUILD_TOPDIR)/bin/temp.bin
- @make show_size
-
-tplink_mr10u: export UBOOT_FILE_NAME=uboot_for_tp-link_tl-mr10u
-tplink_mr10u: export MAX_UBOOT_SIZE=64
-tplink_mr10u: export COMPRESSED_UBOOT=1
-tplink_mr10u:
- @cd $(BUILD_TOPDIR)/u-boot/ && $(MAKECMD) mr10u_config
- @cd $(BUILD_TOPDIR)/u-boot/ && $(MAKECMD) ENDIANNESS=-EB V=1 all
- @cp $(BUILD_TOPDIR)/u-boot/tuboot.bin $(BUILD_TOPDIR)/bin/temp.bin
- @make show_size
-
-tplink_mr13u: export UBOOT_FILE_NAME=uboot_for_tp-link_tl-mr13u
-tplink_mr13u: export MAX_UBOOT_SIZE=64
-tplink_mr13u: export COMPRESSED_UBOOT=1
-tplink_mr13u:
- @cd $(BUILD_TOPDIR)/u-boot/ && $(MAKECMD) mr13u_config
- @cd $(BUILD_TOPDIR)/u-boot/ && $(MAKECMD) ENDIANNESS=-EB V=1 all
- @cp $(BUILD_TOPDIR)/u-boot/tuboot.bin $(BUILD_TOPDIR)/bin/temp.bin
- @make show_size
-
-tplink_wr740n_v4: export UBOOT_FILE_NAME=uboot_for_tp-link_tl-wr740n_v4
-tplink_wr740n_v4: export MAX_UBOOT_SIZE=64
-tplink_wr740n_v4: export COMPRESSED_UBOOT=1
-tplink_wr740n_v4:
- @cd $(BUILD_TOPDIR)/u-boot/ && $(MAKECMD) wr740n_v4_config
- @cd $(BUILD_TOPDIR)/u-boot/ && $(MAKECMD) ENDIANNESS=-EB V=1 all
- @cp $(BUILD_TOPDIR)/u-boot/tuboot.bin $(BUILD_TOPDIR)/bin/temp.bin
- @make show_size
-
-tplink_mr3220_v2: export UBOOT_FILE_NAME=uboot_for_tp-link_tl-mr3220_v2
-tplink_mr3220_v2: export MAX_UBOOT_SIZE=64
-tplink_mr3220_v2: export COMPRESSED_UBOOT=1
-tplink_mr3220_v2:
- @cd $(BUILD_TOPDIR)/u-boot/ && $(MAKECMD) mr3220_v2_config
- @cd $(BUILD_TOPDIR)/u-boot/ && $(MAKECMD) ENDIANNESS=-EB V=1 all
- @cp $(BUILD_TOPDIR)/u-boot/tuboot.bin $(BUILD_TOPDIR)/bin/temp.bin
- @make show_size
-
-tplink_wdr3600_43x0: export UBOOT_FILE_NAME=uboot_for_tp-link_tl-wdr3600-43x0
-tplink_wdr3600_43x0: export MAX_UBOOT_SIZE=64
-tplink_wdr3600_43x0: export COMPRESSED_UBOOT=1
-tplink_wdr3600_43x0: export ETH_CONFIG=_s17
-tplink_wdr3600_43x0:
- @cd $(BUILD_TOPDIR)/u-boot/ && $(MAKECMD) wdr3600_43x0_config
- @cd $(BUILD_TOPDIR)/u-boot/ && $(MAKECMD) ENDIANNESS=-EB V=1 all
- @cp $(BUILD_TOPDIR)/u-boot/tuboot.bin $(BUILD_TOPDIR)/bin/temp.bin
- @make show_size
-
-tplink_wr841n_v8: export UBOOT_FILE_NAME=uboot_for_tp-link_tl-wr841n_v8
-tplink_wr841n_v8: export MAX_UBOOT_SIZE=64
-tplink_wr841n_v8: export COMPRESSED_UBOOT=1
-tplink_wr841n_v8: export ETH_CONFIG=_s27
-tplink_wr841n_v8:
- @cd $(BUILD_TOPDIR)/u-boot/ && $(MAKECMD) wr841n_v8_config
- @cd $(BUILD_TOPDIR)/u-boot/ && $(MAKECMD) ENDIANNESS=-EB V=1 all
- @cp $(BUILD_TOPDIR)/u-boot/tuboot.bin $(BUILD_TOPDIR)/bin/temp.bin
- @make show_size
-
-dlink_dir505: export UBOOT_FILE_NAME=uboot_for_d-link_dir-505
-dlink_dir505: export MAX_UBOOT_SIZE=64
-dlink_dir505: export COMPRESSED_UBOOT=1
-dlink_dir505:
- @cd $(BUILD_TOPDIR)/u-boot/ && $(MAKECMD) dir505_config
- @cd $(BUILD_TOPDIR)/u-boot/ && $(MAKECMD) ENDIANNESS=-EB V=1 all
- @cp $(BUILD_TOPDIR)/u-boot/tuboot.bin $(BUILD_TOPDIR)/bin/temp.bin
- @make show_size
-
-8devices_carambola2: export UBOOT_FILE_NAME=uboot_for_8devices_carambola2
-8devices_carambola2: export MAX_UBOOT_SIZE=256
-8devices_carambola2:
- @cd $(BUILD_TOPDIR)/u-boot/ && $(MAKECMD) carambola2_config
- @cd $(BUILD_TOPDIR)/u-boot/ && $(MAKECMD) ENDIANNESS=-EB V=1 all
- @cp $(BUILD_TOPDIR)/u-boot/u-boot.bin $(BUILD_TOPDIR)/bin/temp.bin
- @make show_size
-
-show_size:
- @echo -e "\n======= Preparing $(MAX_UBOOT_SIZE)KB file filled with 0xFF... ======="
- @`tr "\000" "\377" < /dev/zero | dd ibs=1k count=$(MAX_UBOOT_SIZE) of=$(BUILD_TOPDIR)/bin/$(UBOOT_FILE_NAME).bin`
- @echo -e "\n======= Copying U-Boot image... ======="
- @`dd if=$(BUILD_TOPDIR)/bin/temp.bin of=$(BUILD_TOPDIR)/bin/$(UBOOT_FILE_NAME).bin conv=notrunc`
- @`rm $(BUILD_TOPDIR)/bin/temp.bin`
- @echo -e "\n======= U-Boot image ready, size:" `wc -c < $(BUILD_TOPDIR)/bin/$(UBOOT_FILE_NAME).bin`" bytes =======\n"
- @`md5sum $(BUILD_TOPDIR)/bin/$(UBOOT_FILE_NAME).bin | awk '{print $$1}' | tr -d '\n' > $(BUILD_TOPDIR)/bin/$(UBOOT_FILE_NAME).md5`
- @`echo ' *'$(UBOOT_FILE_NAME).bin >> $(BUILD_TOPDIR)/bin/$(UBOOT_FILE_NAME).md5`
- @if [ "`wc -c < $(BUILD_TOPDIR)/bin/$(UBOOT_FILE_NAME).bin`" -gt "`echo '$(MAX_UBOOT_SIZE)*1024' | bc`" ]; then \
- echo -e "\n **********************************"; \
- echo " * U-BOOT IMAGE SIZE TOO BIG! *"; \
- echo -e " **********************************\n"; \
- fi;
+#
+# Copyright (C) 2016 Piotr Dymacz <piotr@dymacz.pl>
+#
+# SPDX-License-Identifier: GPL-2.0
+#
+
+SHELL = bash
+
+HOSTARCH := $(shell uname -m | \
+ sed -e s/i.86/x86_32/ \
+ -e s/sun4u/sparc64/ \
+ -e s/arm.*/arm/ \
+ -e s/sa110/arm/ \
+ -e s/powerpc/ppc/ \
+ -e s/macppc/ppc/)
+
+HOSTOS := $(shell uname -s | tr '[:upper:]' '[:lower:]' | \
+ sed -e 's/\(cygwin\).*/cygwin/')
+
+ifneq ($(HOSTOS), darwin)
+ ifneq ($(HOSTOS), linux)
+ $(error Error! Unsupported host operating system/arch: "$(HOSTOS)-$(HOSTARCH)")
+ endif
+endif
+
+export HOSTOS
+export HOSTARCH
+export BUILD_TOPDIR = $(PWD)
+export STAGING_DIR = $(BUILD_TOPDIR)/tmp
+export SOURCE_DIR = $(BUILD_TOPDIR)/u-boot
+export BIN_DIR = $(BUILD_TOPDIR)/bin
+export SUB_MAKE_CMD = $(MAKE) --silent --no-print-directory \
+ ARCH=mips V=1 SHELL=$(SHELL)
+
+# ==========================================================================
+# You can override some default configuration options below or pass them on
+# command line, for example:
+# make ... IMG_SIZE=256 IMG_LZMA=0 CROSS_COMPILE=...
+
+# Set to 1 if you want to build RAM-loadable image, without low level
+# initialization of the hardware (PLL/clocks, DRAM):
+# IMG_RAM =
+
+# You can change limit of the image size in KB with below option (image will
+# be filled up to the selected size with 0xFF):
+# IMG_SIZE =
+
+# If you don't want LZMA compressed image, set below option to 1 (by default
+# images for all targets are LZMA compressed):
+# IMG_LZMA =
+
+# Define _absolute_ path to your toolchain directory, for example:
+# export TOOLCHAIN_DIR:=/home/user/toolchain-mips_24kc_gcc-5.4.0_musl-1.1.15
+# export PATH:=$(TOOLCHAIN_DIR)/bin:$(PATH)
+
+ifndef CROSS_COMPILE
+ CROSS_COMPILE = mips-openwrt-linux-musl-
+endif
+export CROSS_COMPILE
+
+# By default, optimization for size (-Os) is enabled, set below option
+# to n or remove it if you want only basic optimization (-O/-O1)
+# BUILD_OPTIMIZED = n
+
+ifneq ($(BUILD_OPTIMIZED), n)
+ BUILD_OPTIMIZED = y
+endif
+export BUILD_OPTIMIZED
+
+# ==========================================================================
+
+# =======================
+# CUSTOM HELPER FUNCTIONS
+# =======================
+
+define echo_green
+ echo -e "\e[92m$(1)\e[0m"
+endef
+
+define echo_red
+ echo -e "\e[91m$(1)\e[0m"
+endef
+
+define echo_blue
+ echo -e "\e[96m$(1)\e[0m"
+endef
+
+# $(1): size
+define img_size
+$(if $(IMG_SIZE),$(strip $(IMG_SIZE)),$(strip $(1)))
+endef
+
+# $(1): value
+define is_lzma
+$(if $(IMG_LZMA),\
+ $(if $(filter $(strip $(IMG_LZMA)),1),1,0),\
+ $(if $(filter $(strip $(1)),1),1,0)\
+)
+endef
+
+define git_branch
+$(shell git symbolic-ref --short -q HEAD 2>/dev/null || echo "unknown")
+endef
+
+define git_hash
+$(shell git rev-parse --short=8 -q HEAD 2>/dev/null || echo "unknown")
+endef
+
+define git_branch_hash
+git_$(call git_branch)-$(call git_hash)
+endef
+
+# $(1): file extension
+define img_name
+u-boot_mod__$@__$(shell date +"%Y%m%d")__$(call git_branch_hash)$(if \
+$(filter $(IMG_RAM),1),__RAM-LOAD-ONLY)$(if $(1),.$(1))
+endef
+
+define md5_sum
+ $(call echo_green,Calculating MD5 sum for the final image...)
+
+ md5sum $(BIN_DIR)/$(call img_name,bin) | \
+ awk '{print $$1}' | \
+ tr -d '\n' > $(BIN_DIR)/$(call img_name).md5
+
+ echo ' *'$(call img_name,bin) >> $(BIN_DIR)/$(call img_name,md5)
+endef
+
+# $(1): size
+define padded_img
+ $(call echo_green,Preparing $(1) KB image padded with 0xFF...)
+ tr "\000" "\377" < /dev/zero | dd ibs=1k count=$(1) \
+ of=$(BIN_DIR)/$(call img_name,bin) 2> /dev/null
+endef
+
+define final_img
+ $(call echo_green,Preparing final image...)
+ dd if=$(BIN_DIR)/temp.bin of=$(BIN_DIR)/$(call img_name,bin) \
+ conv=notrunc 2> /dev/null
+
+ rm -f $(BIN_DIR)/temp.bin
+endef
+
+# $(1): path to image
+# $(2): size limit in KB
+define size_chk
+ $(call echo_green,Checking size of the image...)
+
+ if [ `wc -c < $(1)` -gt $$(($(2) * 1024)) ]; then \
+ echo; \
+ $(call echo_red, ======================); \
+ $(call echo_red, IMAGE SIZE IS TOO BIG!); \
+ $(call echo_red, ======================); \
+ echo; \
+ rm -f $(1); \
+ exit 1; \
+ fi;
+endef
+
+# $(1): filename of image to copy
+# $(2): image size limit (check if set)
+define copy_img
+ echo;
+ $(call echo_green,Copying compiled image...)
+
+ cp $(SOURCE_DIR)/$(strip $(1)).bin $(BIN_DIR)/temp.bin
+ $(if $(2),$(call size_chk,$(BIN_DIR)/temp.bin,$(2)))
+endef
+
+# $(1): size limit in KB
+# $(2): if set to 1, use LZMA
+# $(3): other parameters passed to subdir make
+define build
+ args="IMG_SIZE=$$((1024*$(call img_size,$(1)))) \
+ IMG_LZMA=$(strip $(call is_lzma,$(2))) \
+ $(strip $(3))"; \
+ cd $(SOURCE_DIR) && \
+ $(SUB_MAKE_CMD) $@ $$args && \
+ $(SUB_MAKE_CMD) all $$args
+
+ $(if $(filter $(IMG_RAM),1),\
+ $(call copy_img,u-boot), \
+ $(if $(filter $(strip $(call is_lzma,$(2))),1), \
+ $(call copy_img,tuboot,$(call img_size,$(1))), \
+ $(call copy_img,u-boot,$(call img_size,$(1))) \
+ ) \
+ )
+
+ $(if $(filter $(IMG_RAM),1),,$(call padded_img,$(1)))
+ $(call final_img)
+ $(call md5_sum)
+ echo;
+ $(call echo_green,DONE!)
+ $(call echo_green,Image 'bin/$(call img_name,bin)' is ready!)
+
+ if [ "x$$IMG_RAM" = "x1" ]; then \
+ echo; \
+ $(call echo_blue, ================================); \
+ $(call echo_blue, THIS IMAGE IS ONLY FOR RAM LOAD!); \
+ $(call echo_blue, DO NOT WRITE IT INTO FLASH CHIP!); \
+ $(call echo_blue, ================================); \
+ echo; \
+ fi;
+endef
+
+# ===========================================
+# TARGETS IN ALPHABETICAL ORDER, SHARED FIRST
+# ===========================================
+
+COMMON_AR933X_TARGETS = \
+ gainstrong_oolite_v1_dev \
+ gl-inet_6416 \
+ tp-link_tl-mr10u_v1 \
+ tp-link_tl-mr13u_v1 \
+ tp-link_tl-mr3020_v1 \
+ tp-link_tl-mr3040_v1v2 \
+ tp-link_tl-mr3220_v2 \
+ tp-link_tl-wr703n_v1 \
+ tp-link_tl-wr710n_v1 \
+ tp-link_tl-wr720n_v3_CN \
+ tp-link_tl-wr740n_v4
+
+$(COMMON_AR933X_TARGETS):
+ @$(call build,123,1)
+
+COMMON_ETHS27_TARGETS = \
+ tp-link_tl-mr3420_v2 \
+ tp-link_tl-mr3420_v3 \
+ tp-link_tl-mr6400_v1v2 \
+ tp-link_tl-wa801nd_v2 \
+ tp-link_tl-wa850re_v2 \
+ tp-link_tl-wa830re_v2 \
+ tp-link_tl-wdr3500_v1 \
+ tp-link_tl-wr802n_v1 \
+ tp-link_tl-wr810n_v1 \
+ tp-link_tl-wr820n_v1_CN \
+ tp-link_tl-wr841n_v10 \
+ tp-link_tl-wr841n_v11 \
+ tp-link_tl-wr841n_v8 \
+ tp-link_tl-wr841n_v9 \
+ tp-link_tl-wr842n_v3 \
+ tp-link_tl-wr902ac_v1
+
+$(COMMON_ETHS27_TARGETS):
+ @$(call build,123,1,ETH_CONFIG=_s27)
+
+8devices_carambola2 \
+alfa-network_hornet-ub \
+creatcomm-technology_d3321 \
+gl-inet_gl-ar150 \
+gl-inet_gl-usb150:
+ @$(call build,256,1)
+
+alfa-network_ap121f:
+ @$(call build,192,1)
+
+comfast_cf-e314n \
+comfast_cf-e320n_v2 \
+comfast_cf-e520n \
+comfast_cf-e530n:
+ @$(call build,64,1,ETH_CONFIG=_s27)
+
+d-link_dir-505:
+ @$(call build,64,1)
+
+dragino_v2_ms14:
+ @$(call build,192,1,DEVICE_VENDOR=dragino)
+
+engenius_ens202ext \
+p2w_cpe505n \
+p2w_r602n \
+yuncore_ap90q \
+yuncore_cpe830 \
+zbtlink_zbt-we1526:
+ @$(call build,256,1,ETH_CONFIG=_s27)
+
+tp-link_tl-wdr3600_v1 \
+tp-link_tl-wdr43x0_v1:
+ @$(call build,123,1,ETH_CONFIG=_s17)
+
+unwireddevices_unwired-one:
+ @$(call build,128,1,DEVICE_VENDOR=SE)
+
+village-telco_mesh-potato_v2:
+ @$(call build,192,1,DEVICE_VENDOR=villagetelco)
+
+wallys_dr531:
+ @$(call build,192,1,ETH_CONFIG=_s27)
+
+yuncore_cpe870:
+ @$(call build,64,1,ETH_CONFIG=_s27)
+
+# =============
+# CLEAN TARGETS
+# =============
+
+lzma_host_clean:
+ @cd $(SOURCE_DIR) && $(SUB_MAKE_CMD) $@
clean:
- @cd $(BUILD_TOPDIR)/u-boot/ && $(MAKECMD) clean
- @rm -f $(BUILD_TOPDIR)/bin/*.bin
- @rm -f $(BUILD_TOPDIR)/bin/*.md5
-
-clean_all:
- @cd $(BUILD_TOPDIR)/u-boot/ && $(MAKECMD) distclean
- @rm -f $(BUILD_TOPDIR)/bin/*.bin
- @rm -f $(BUILD_TOPDIR)/bin/*.md5
+ @cd $(SOURCE_DIR) && $(SUB_MAKE_CMD) distclean
+ @rm -f $(SOURCE_DIR)/httpd/fsdata.c
+
+clean_all: clean
+ @$(call echo_green,Removing all binary images...)
+ @rm -f $(BIN_DIR)/*.bin
+ @rm -f $(BIN_DIR)/*.md5