# Copyright JS Foundation and other contributors, http://js.foundation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# USAGE:
# specify the board using the command line:
# make BOARD=[mbed board name]

BOARD=$(subst [mbed] ,,$(shell mbed target | sed -e '1,2d')) 

DEBUG?=0
MBED_VERBOSE?=0

MBED_CLI_FLAGS=-j0 --source ../../../

EXTRA_SRC=

IOTJS_JS_MODULES=	--external-modules=./src/modules/mbedos5		\
					--cmake-param=-DENABLE_MODULE_FIXUP=ON			\
					--cmake-param=-DENABLE_MODULE_NIC=ON			\
					--cmake-param=-DENABLE_MODULE_DGRAM=ON			\
					--cmake-param=-DENABLE_MODULE_HTTPS=ON			\
					--cmake-param=-DENABLE_MODULE_TLS=ON			\
					--cmake-param=-DENABLE_MODULE_UART=OFF			\
					--cmake-param=-DENABLE_MODULE_GPIO=OFF			\
					--cmake-param=-DENABLE_MODULE_I2C=OFF			\
					--cmake-param=-DENABLE_MODULE_SPI=OFF			\
					--cmake-param=-DENABLE_MODULE_VIDEO=ON			\
					--cmake-param=-DENABLE_MODULE_JPEG=ON			\
					--cmake-param=-DENABLE_MODULE_DISPLAY=ON		\
					--cmake-param=-DENABLE_MODULE_ALIGNED_BUFFER=ON	\
					--cmake-param=-DENABLE_MODULE_GRAPHICS=ON		\
					--no-snapshot

ifneq ($(EXTRA_SRC),)
EXTRA_SRC_MOD=--source $(subst :, --source ,$(EXTRA_SRC))
MBED_CLI_FLAGS += $(EXTRA_SRC_MOD)
endif

EXTERN_BUILD_DIR=

ifneq ($(EXTERN_BUILD_DIR),)
MBED_CLI_FLAGS += --build $(EXTERN_BUILD_DIR)
endif

ifeq ($(DEBUG), 1)
MBED_CLI_FLAGS += --profile ./profiles/debug.json
else
MBED_CLI_FLAGS += --profile ./profiles/release.json
endif

MBED_CLI_FLAGS += --app-config ./mbed_app.json

ifeq ($(MBED_VERBOSE), 1)
MBED_CLI_FLAGS += -v
else ifeq ($(MBED_VERBOSE), 2)
MBED_CLI_FLAGS += -vv
endif

MBED_CLI_FLAGS += -D JERRY_SYSTEM_ALLOCATOR -D JERRY_CPOINTER_32_BIT
MBED_CLI_FLAGS += -t GCC_ARM
ifeq ($(OS),Windows_NT)
MBED_CLI_FLAGS += -f
endif

.PHONY: all js2c getlibs rebuild library
all: ../../../src/iotjs_js.c .mbed ../../../iotjs_def.h ../../../.mbedignore
	mbed target $(BOARD)
	mbed compile $(MBED_CLI_FLAGS)

library: .mbed ../../../.mbedignore
	# delete encoded js code if it exists
	-rm -f ../../../src/iotjs_js.c
	-rm -f ../../../src/iotjs_js.h
	-rm -f ../../../src/iotjs_module_inl.h
	-rm -f ../../../src/iotjs_string_ext.inl.h
	-rm -f ../../../iotjs_def.h
	-rm -f ../../../.mbedignore
	mbed target $(BOARD)
	mbed compile $(MBED_CLI_FLAGS) --library

clean:
	-rm -rf ./BUILD/$(BOARD)
	###../../../tools/build.py --clean
	-rm -f ../../../src/iotjs_js.c
	-rm -f ../../../src/iotjs_js.h
	-rm -f ../../../src/iotjs_module_inl.h
	-rm -f ../../../src/iotjs_string_ext.inl.h
	-rm -f ../../../iotjs_def.h
	-rm -f ../../../.mbedignore

js2c:
	###python ../../../tools/js2c.py --buildtype=release --modules "$(IOTJS_JS_MODULES)"
	../../../tools/build.py $(IOTJS_JS_MODULES)

../../../src/iotjs_js.c: js2c

getlibs: .mbed

.mbed:
	echo 'ROOT=.' > .mbed
	mbed config root .
	mbed toolchain GCC_ARM
	mbed target $(BOARD)
	mbed deploy

../../../iotjs_def.h:
ifeq ($(OS),Windows_NT)
	copy template.iotjs_def.h.txt ..\..\..\iotjs_def.h
else
	cp ./template.iotjs_def.h.txt ../../../iotjs_def.h
endif

../../../.mbedignore:
ifeq ($(OS),Windows_NT)
	copy template.mbedignore.txt ..\..\..\.mbedignore
else
	cp ./template.mbedignore.txt ../../../.mbedignore
endif
