ROOT_DIR := ./..
include $(ROOT_DIR)/config/config.mk
include $(ROOT_DIR)/multibuild/sdk.mk
include $(ROOT_DIR)/config/targets.mk
include $(ROOT_DIR)/config/detect_deploy_target.mk

### Disable signing ############################################################
JUST_BUILD := $(filter $(MAKECMDGOALS),build)

### Build Test libraries #######################################################
include test_library/Makefile

### Build Smoke tests ###$######################################################
include smoke/Makefile

### Build System tests #########################################################
include system/Makefile

### Build Functional tests #####################################################
include functional/authentication/Makefile
include functional/version-check/Makefile

############## HMAC SIGNER ####################################################
LOCAL_NDK_PATH := $(CONFIG_ANDROID_NDK_PATH)
LOCAL_PROJECTS := pa_hmac_signer
LOCAL_PLATFORM_VERSION := android-28
include $(BUILD_ANDROID)

.PHONY: _build_canary
_build_canary:
	@$(MAKE) -C functional canary_client

build: _build_canary

.PHONY: _build_another_tests
_build_another_tests:
	@$(MAKE) -C test_trustlet $(JUST_BUILD)
	@$(MAKE) -C functional $(JUST_BUILD)
	@$(MAKE) -C stress $(JUST_BUILD)

.PHONY: android_pa_client
android_pa_client:
	@$(MAKE) -f android_pa_client.mk
	@rm -rf android/pa_app/.externalNativeBuild

build: _build_another_tests android_pa_client

DEVICE_APP_ABI := `adb shell getprop ro.product.cpu.abi`
DEVICE_APP_ABI_32 := armeabi-v7a

.PHONY: install
install:
	@adb remount
	@$(ROOT_DIR)/tools/five_push.sh out/android/pa_hmac_signer/$(DEVICE_APP_ABI_32)/pa_hmac_signer32 --sign -d /system/bin/
	@if [ "${DEVICE_APP_ABI}" = "arm64-v8a" ]; then \
	  $(ROOT_DIR)/tools/five_push.sh out/android/pa_hmac_signer/$(DEVICE_APP_ABI)/pa_hmac_signer --sign -d /system/bin/;\
	else\
	  adb shell ln -sf /system/bin/pa_hmac_signer32 /system/bin/pa_hmac_signer;\
	fi
	@$(ROOT_DIR)/tools/five_push.sh out/android/pa_hmac_signer/$(DEVICE_APP_ABI_32)/pa_hmac_signer32 --sign -d /vendor/bin/
	@if [ "${DEVICE_APP_ABI}" = "arm64-v8a" ]; then \
	  $(ROOT_DIR)/tools/five_push.sh out/android/pa_hmac_signer/$(DEVICE_APP_ABI)/pa_hmac_signer --sign -d /vendor/bin/;\
	else\
	  adb shell ln -sf /vendor/bin/pa_hmac_signer32 /vendor/bin/pa_hmac_signer;\
	fi
	@$(MAKE) -C test_trustlet/ install
	@$(MAKE) -C functional/ install
	@$(MAKE) -C stress/ install
# Android pa client should be installed last, because in some cases it does reboot
	@$(MAKE) -f android_pa_client.mk install

### Run/execute targets ########################################################
# Attention: before running any of tests you should install it, e.g. make install
GTEST_OUT_FOLDER = $(realpath ./)
export GTEST_OUTPUT=xml:$(GTEST_OUT_FOLDER)/out/xml/

.PHONY: run_smoke_tests
run_smoke_tests:
	@if adb shell false; then\
		echo "ADB version is to old, please use 1.0.39 or higher." >&2;\
		exit 1;\
	fi
	@./out/host/smoke_tests_host/smoke_tests_host

.PHONY: run_system_tests
run_system_tests: | run_smoke_tests
	@cd system && ../out/host/system_tests_host/system_tests_host

.PHONY: run_functional_tests
run_functional_tests: | run_smoke_tests
	@adb remount && adb shell setenforce 0
	@cp ./functional/out/$(DEPLOY_TARGET)/canary/$(DEVICE_APP_ABI)/canary ./functional/out/android/canary_client/canary
	@./out/host/functional_tests_host/functional_tests_host
	@rm ./functional/out/android/canary_client/canary
	@$(MAKE) -C functional run_functional_tests

.PHONY: run_performance_tests
run_performance_tests: | run_smoke_tests
	@adb shell setenforce 0
	@adb shell ftest_pa_driver  --gtest_color=yes --gtest_filter=*Performance*

.PHONY: run_app_name_tests
run_app_name_tests:
	@./check_app_name_auth.sh

.PHONY: run_stress_tests
run_stress_tests: | run_smoke_tests
	@adb remount
	@adb shell mount -o rw,remount /firmware || echo "Skip mount /firmware"
	@$(MAKE) -C stress run_stress_tests

.PHONY: run_all_tests
run_all_tests: | run_functional_tests run_performance_tests run_stress_tests run_system_tests

.PHONY: _setup_gtest_dirs
_setup_gtest_dirs:
	@rm -f $(GTEST_OUT_FOLDER)/out/xml/*
	@adb remount && adb shell rm -f /data/local/tmp/gtest/out/xml/*
	@adb shell set | grep GTEST_OUTPUT || (adb push gtest.rc /vendor/etc/init/ \
	  && adb reboot && adb wait-for-device)
	@adb remount && adb shell mkdir -p /data/local/tmp/gtest/out/xml

.PHONY: collect_all_reports
collect_all_reports: | _setup_gtest_dirs run_all_tests
	@adb pull /data/local/tmp/gtest/out/xml/ $(GTEST_OUT_FOLDER)/out/

### Clean targets ##############################################################
.PHONY: _clean_android_pa_client
_clean_android_pa_client:
	@$(MAKE) -f android_pa_client.mk clean

.PHONY: _clean_test_trustlet
_clean_test_trustlet:
	@$(MAKE) -C test_trustlet clean

.PHONY: clean
clean: _clean_android_pa_client _clean_test_trustlet

.PHONY: force_clean
force_clean: clean
	@$(MAKE) -C functional force_clean
	@$(MAKE) -C stress force_clean
	@$(MAKE) -C test_trustlet force_clean
	@$(MAKE) -f android_pa_client.mk force_clean
	@rm -rf out/
