# Wildcard does not work here because the files are not generated yet
ASN1_OUT := asn1/configs/out_protocol

### Build ASN1 library ########################################################
LOCAL_NAME := asn1proto

LOCAL_INCLUDE_DIRS := asn1/include asn1/configs/out_protocol
LOCAL_INCLUDE_DIRS-tbase := multibuild/include/gp-api asn1/arch/tbase/include
LOCAL_INCLUDE_DIRS-blowfish := asn1/arch/blowfish/include
LOCAL_INCLUDE_DIRS-teegris := asn1/arch/blowfish/include
LOCAL_INCLUDE_DIRS-qsee := multibuild/include/gp-api asn1/arch/qsee/include

LOCAL_SRC_C := $(wildcard asn1/source/*.c) $(wildcard $(ASN1_OUT)/*.c)
LOCAL_SRC_C-blowfish := $(wildcard asn1/arch/blowfish/*.c)
LOCAL_SRC_C-teegris := $(wildcard asn1/arch/blowfish/*.c)
LOCAL_SRC_C-tbase := $(wildcard asn1/arch/tbase/*.c)

LOCAL_CFLAGS := -DHAVE_CONFIG_H $(PA_GLOBAL_CFLAGS) -DNDEBUG
LOCAL_CFLAGS-tbase += -fsanitize=undefined-trap -fsanitize-undefined-trap-on-error
LOCAL_CFLAGS-teegris := -Werror

LOCAL_EXPORT_INCLUDE_DIRS := asn1/include asn1/configs/out_protocol
LOCAL_EXPORT_INCLUDE_DIRS-tbase := asn1/arch/tbase/include
LOCAL_EXPORT_INCLUDE_DIRS-blowfish := asn1/arch/blowfish/include
LOCAL_EXPORT_INCLUDE_DIRS-teegris := asn1/arch/blowfish/include
LOCAL_EXPORT_INCLUDE_DIRS-qsee := asn1/arch/qsee/include
LOCAL_EXPORT_CFLAGS := -DHAVE_CONFIG_H
include $(BUILD_TARGET_STATIC_LIBRARY)

### Build host ASN1 library for Unit Tests #####################################
LOCAL_NAME := asn1proto_unittests
LOCAL_INCLUDE_DIRS := asn1/include asn1/arch/nwd/include asn1/configs/out_protocol
LOCAL_SRC_C := $(wildcard asn1/source/*.c) $(wildcard $(ASN1_OUT)/*.c)
LOCAL_CFLAGS := -Werror --coverage $(PA_GLOBAL_CFLAGS) -DHAVE_CONFIG_H

LOCAL_EXPORT_INCLUDE_DIRS := asn1/include asn1/configs/out_protocol
include $(BUILD_HOST_LIBRARY)

### Build host ASN1 library ####################################################
LOCAL_NAME := asn1proto
LOCAL_INCLUDE_DIRS := asn1/include asn1/arch/nwd/include asn1/configs/out_protocol
LOCAL_SRC_C := $(wildcard asn1/source/*.c) $(wildcard $(ASN1_OUT)/*.c)
LOCAL_CFLAGS := $(PA_GLOBAL_CFLAGS) -Werror -DHAVE_CONFIG_H

LOCAL_EXPORT_INCLUDE_DIRS := asn1/include asn1/configs/out_protocol
include $(BUILD_HOST_LIBRARY)

### Build unit test for ASN1 ###################################################
LOCAL_NAME := unit_test_asn1
LOCAL_SRC_CPP := asn1/tests/PaTzSerializeUnitTests.cc \
		 asn1/tests/SerializeUnitTests.cc
LOCAL_CFLAGS := --coverage $(PA_GLOBAL_CFLAGS) -Werror 

LOCAL_STATIC_LIBRARIES := asn1proto_unittests gtest_main
LOCAL_LDFLAGS += -lstdc++ -lpthread --coverage
include $(BUILD_HOST)

.PHONY: _run_unit_test_asn1
_run_unit_test_asn1: unit_test_asn1
	@GTEST_OUTPUT="xml:./out/host/unit_test_asn1/unit_test_asn1.xml" ./out/host/unit_test_asn1/unit_test_asn1

run_utests: _run_unit_test_asn1

.PHONY: generate_asn1_files
generate_asn1_files: thirdparty/asn1c/asn1c/asn1c
	@echo "=====ASN1 is generating files..======"
	@mkdir -p $(ASN1_OUT)
	@cd $(ASN1_OUT) && ../../../thirdparty/asn1c/asn1c/asn1c -fnative-types -R ../client_protocol.asn1
	@cd $(ASN1_OUT) && ../../../thirdparty/asn1c/asn1c/asn1c -fnative-types -R ../tz_drv_lib_protocol.asn1
	@cd $(ASN1_OUT) && ../../../thirdparty/asn1c/asn1c/asn1c -fnative-types -R ../pa_signature.asn1
