# 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.


# Default target for running the build test outside the Travis CI environment.
all:
	$(MAKE) install
	$(MAKE) script


## Targets for installing build dependencies of the NuttX JerryScript target.

# Install cross-compiler and tools via apt.
install-apt-get-deps:
	sudo apt-get install -q -y gcc-arm-none-eabi gperf

# Fetch and build kconfig-frontends (kconfig-conf) from nuttx/tools.
LOCAL_INSTALL:=$(CURDIR)/../local/

install-kconfig:
	git clone https://bitbucket.org/nuttx/tools.git ../tools
	mkdir -p $(LOCAL_INSTALL)
	# FIXME: 'autoreconf --force --install' is a workaround after
	#        https://bitbucket.org/nuttx/tools/commits/164450f982b404fdc2b3233db51dc3eaa1f08b7f
	cd ../tools/kconfig-frontends && autoreconf --force --install && ./configure --disable-mconf --disable-nconf --disable-gconf --disable-qconf --disable-shared --enable-static --prefix=$(LOCAL_INSTALL)
	$(MAKE) -C ../tools/kconfig-frontends
	$(MAKE) -C ../tools/kconfig-frontends install

# Fetch nuttx/{apps,nuttx} repositories.
install-clone-nuttx:
	git clone https://github.com/apache/incubator-nuttx-apps.git ../apps -b releases/9.0
	git clone https://github.com/apache/incubator-nuttx.git ../nuttx -b releases/9.0

# Perform all the necessary (JerryScript-independent) installation steps.
install-noapt: install-kconfig install-clone-nuttx
install: install-apt-get-deps install-noapt


## Targets for building NuttX with JerryScript.

# Build JerryScript.
script-build-jerryscript:
	tools/build.py --clean --toolchain cmake/toolchain_mcu_stm32f4.cmake --profile=es.next --jerry-cmdline OFF --lto OFF --jerry-math ON --amalgam ON --jerry-port-default OFF --mem-heap 70 --compile-flag='--sysroot=../nuttx'

# Link in the NuttX JerryScript target directory under the NuttX apps tree.
script-add-jerryscript-app:
	ln -s ../../jerryscript/targets/nuttx-stm32f4 ../apps/interpreters/jerryscript

# Configure USB shell.
script-configure-usbnsh:
	cd ../nuttx/tools && PATH=$(LOCAL_INSTALL)/bin:$$PATH ./configure.sh stm32f4discovery/usbnsh

# Configure and build the firmware (NuttX with JerryScript).
script: script-build-jerryscript script-add-jerryscript-app script-configure-usbnsh
	echo 'CONFIG_HOST_LINUX=y' >> ../nuttx/.config
	echo 'CONFIG_ARCH_FPU=y' >> ../nuttx/.config
	echo 'CONFIG_JERRYSCRIPT=y'>> ../nuttx/.config
	PATH=$(LOCAL_INSTALL)/bin:$$PATH $(MAKE) -C ../nuttx olddefconfig
	PATH=$(LOCAL_INSTALL)/bin:$$PATH $(MAKE) -C ../nuttx
