#
# Top-level Look-Aside Crypto makefile, to kick-off a build for all sub-dirs
#
# @par
# This file is provided under a dual BSD/GPLv2 license.  When using or
#   redistributing this file, you may do so under either license.
# 
#   GPL LICENSE SUMMARY
# 
#   Copyright(c) 2007-2022 Intel Corporation. All rights reserved.
# 
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of version 2 of the GNU General Public License as
#   published by the Free Software Foundation.
# 
#   This program is distributed in the hope that it will be useful, but
#   WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#   General Public License for more details.
# 
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
#   The full GNU General Public License is included in this distribution
#   in the file called LICENSE.GPL.
# 
#   Contact Information:
#   Intel Corporation
# 
#   BSD LICENSE
# 
#   Copyright(c) 2007-2022 Intel Corporation. All rights reserved.
#   All rights reserved.
# 
#   Redistribution and use in source and binary forms, with or without
#   modification, are permitted provided that the following conditions
#   are met:
# 
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in
#       the documentation and/or other materials provided with the
#       distribution.
#     * Neither the name of Intel Corporation nor the names of its
#       contributors may be used to endorse or promote products derived
#       from this software without specific prior written permission.
# 
#   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
#   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
#   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
#   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
#   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
#   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
#   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
#   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
#   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
#   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# 
# 
#  version: QAT.B.3.12.0-00004
###############################################################################

# Ensure The ICP_ENV_DIR environmental var is defined.
ifndef ICP_ENV_DIR
$(error ICP_ENV_DIR is undefined. Please set the path to your environment makefile \
        "-> setenv ICP_ENV_DIR <path>")
endif

#Add your project environment Makefile
include $(ICP_ENV_DIR)/environment.mk
#Lac include paths
include $(LAC_DIR)/common.mk
include $(ICP_ENV_DIR)/$(ICP_OS)_$(ICP_OS_LEVEL).mk
##directories
ICP_BUILD_OUTPUT_DIR=build/$(ICP_OS)/$(ICP_OS_LEVEL)
#the folder where the output will be created.
ICP_FINAL_OUTPUT_DIR=$(ICP_BUILD_OUTPUT_DIR)

#<ICP_EXPORT_OSAL_SYMBOLS_FOR_INTEG_TEST>
ifeq ($(ICP_OS_LEVEL), kernel_space)
EXTRA_CFLAGS += -DICP_EXPORT_OSAL_SYMBOLS_FOR_INTEG_TEST
MAKE := MAKEFLAGS= make
endif

#</ICP_EXPORT_OSAL_SYMBOLS_FOR_INTEG_TEST>
OSAL_PATH=$(ICP_ROOT)/quickassist/utilities/osal/

ifdef ICP_DC_ONLY
EXTRA_CFLAGS += -DICP_DC_ONLY
endif

#############Define the output section###############
BASENAME=icp_qa_al

ifdef ONE_KO_RELEASE_PACKAGE
	ifeq ($(ADF_PLATFORM), ACCELDEVVF)
		EXTRA_CFLAGS += -DACCELDEVVF
		ifeq ($(ICP_OS_LEVEL), user_space)
			OUTPUT_NAME+=lib$(BASENAME)_vf
		else
			OUTPUT_NAME+=$(BASENAME)_vf
		endif
	else #($(ADF_PLATFORM), ACCELDEVVF)
		ifeq ($(ICP_OS_LEVEL), user_space)
			OUTPUT_NAME+=lib$(BASENAME)
		else
			OUTPUT_NAME+=$(BASENAME)
		endif
	endif #($(ADF_PLATFORM), ACCELDEVVF)
	MODULE_SOURCES=linux/icp_qa_module.c
else #ONE_KO_RELEASE_PACKAGE
	ifeq ($(ADF_PLATFORM), ACCELDEVVF)
		EXTRA_CFLAGS += -DACCELDEVVF
		OUTPUT_NAME?=lacvf
	else #($(ADF_PLATFORM), ACCELDEVVF)
		OUTPUT_NAME?=lac
	endif #($(ADF_PLATFORM), ACCELDEVVF)
	MODULE_SOURCES=linux/lac_module.c
endif
MODULE_SOURCES+= linux/lac_symbols.c

LIB_STATIC=$(OUTPUT_NAME).a
LIB_SHARED=$(OUTPUT_NAME).so

# add the path and list of source libraries,
ADDITIONAL_KERNEL_LIBS= common/utils/$(ICP_BUILD_OUTPUT_DIR)/utils.a \
			common/ctrl/$(ICP_BUILD_OUTPUT_DIR)/init.a \
                        common/compression/$(ICP_BUILD_OUTPUT_DIR)/compression.a

ifeq ($(ICP_OS_LEVEL), user_space)
ifndef ICP_DC_ONLY
ADDITIONAL_KERNEL_LIBS += common/crypto/sym/$(ICP_BUILD_OUTPUT_DIR)/sym.a \
			  common/crypto/sym/qat/$(ICP_BUILD_OUTPUT_DIR)/sym_qat.a \
			  common/crypto/sym/key/$(ICP_BUILD_OUTPUT_DIR)/sym_key.a \
			  common/crypto/asym/pke_common/$(ICP_BUILD_OUTPUT_DIR)/pke_common.a \
			  common/crypto/asym/diffie_hellman/$(ICP_BUILD_OUTPUT_DIR)/diffie_hellman.a \
			  common/crypto/asym/rsa/$(ICP_BUILD_OUTPUT_DIR)/rsa.a \
			  common/crypto/asym/dsa/$(ICP_BUILD_OUTPUT_DIR)/dsa.a \
		  	  common/crypto/asym/prime/$(ICP_BUILD_OUTPUT_DIR)/prime.a \
		  	  common/crypto/asym/large_number/$(ICP_BUILD_OUTPUT_DIR)/ln.a \
			  common/crypto/asym/ecc/$(ICP_BUILD_OUTPUT_DIR)/elliptic_curve.a
endif
endif

ADDITIONAL_KERNEL_LIBS += common/qat_comms/$(ICP_BUILD_OUTPUT_DIR)/qat_comms.a
ADDITIONAL_KERNEL_LIBS += device/$(ICP_BUILD_OUTPUT_DIR)/dev_info.a
ADDITIONAL_KERNEL_LIBS += common/stubs/$(ICP_BUILD_OUTPUT_DIR)/stubs.a

ifdef ICP_NRBG_SUPPORTED
ADDITIONAL_KERNEL_LIBS += common/crypto/sym/nrbg/$(ICP_BUILD_OUTPUT_DIR)/nrbg.a
endif

ifdef ONE_KO_RELEASE_PACKAGE
EXTRA_CFLAGS += -DONE_KO_RELEASE_PACKAGE
ifeq ($(ICP_OS_LEVEL), kernel_space)
    ADDITIONAL_OBJECTS =  ../build/libs/libadf.a
    ADDITIONAL_OBJECTS += ../build/libs/libosal.a
ifneq ($(ADF_PLATFORM), ACCELDEVVF)
    ADDITIONAL_OBJECTS += ../build/libs/icp_ae_loader_kernel.a
endif
endif
endif

ifdef ICP_WITHOUT_THREAD
EXTRA_CFLAGS += -DICP_WITHOUT_THREAD
endif

ifdef ICP_NRBG_SUPPORTED
EXTRA_CFLAGS += -DICP_WITHOUT_THREAD
endif

ifeq ($(ICP_OS_LEVEL), user_space)
ADDITIONAL_KERNEL_LIBS += user/user.a
endif

###########End of Define the output section#######################
ifeq ($(ICP_OS_LEVEL), user_space)
SUBDIRS=$(filter-out common/stubs, $(patsubst %/$(ICP_BUILD_OUTPUT_DIR)/,%,$(dir $(ADDITIONAL_KERNEL_LIBS))))
else
SUBDIRS=$(patsubst %/$(ICP_BUILD_OUTPUT_DIR)/,%,$(dir $(ADDITIONAL_KERNEL_LIBS)))
endif

LAC-kernel_module:lib_kernel
	@echo 'Creating kernel module'; \
	MAKEFLAGS= make -f BSDmakefile module

obj-m := $(OUTPUT_NAME).o
$(OUTPUT_NAME)-objs := $(ADDITIONAL_KERNEL_LIBS) $(ADDITIONAL_OBJECTS) \
		$(patsubst %.c,%.o, $(MODULE_SOURCES) $(ADDITIONAL_OBJECTS))
lib-m := $(ADDITIONAL_KERNEL_LIBS) $(ADDITIONAL_OBJECTS)

lib_kernel::$(SUBDIRS)
lib::$(SUBDIRS)
lib::$(LIB_STATIC)

$(SUBDIRS)::
	echo 'Entering Directory $@' && cd $@ && $(MAKE) lib_static
.PHONY: lib

ifeq ($(ICP_OS_LEVEL),kernel_space)
lib_static: $(LIB_STATIC)
dirs:
	@echo 'Creating output directory' ;\
	test -d $(ICP_FINAL_OUTPUT_DIR) || mkdir -p $(ICP_FINAL_OUTPUT_DIR);

$(LIB_STATIC): lib_kernel dirs
	@echo 'Creating static library ${LIB_STATIC}'; \
	$(MAKE) -C $(KERNEL_SOURCE_ROOT)/ M=$(PWD) obj-m=""; \
	echo 'Copying outputs';\
	test -f lib.a  &&  mv lib.a $(ICP_FINAL_OUTPUT_DIR)/$(LIB_STATIC);\
	$(RM) -f *.o *.mod.* .*.cmd;
else

ifdef ICP_USE_ADFCTL_API
lib_static: lib
	@for  dir in $(SUBDIRS); do \
	(cp $$dir/build/$(ICP_OS)/$(ICP_OS_LEVEL)/*.o $(ICP_BUILD_OUTPUT_DIR) ; ) \
	done ;\
	cp $(ADF_CTL_PATH)/src/build/$(ICP_OS)/user_space/*.o $(ICP_BUILD_OUTPUT_DIR); \
	cp $(CMN_MEM_PATH)/$(ICP_OS_LEVEL)/*.o $(ICP_BUILD_OUTPUT_DIR);\
	cp $(ADF_DIR)/src/build/$(ICP_OS)/$(ICP_OS_LEVEL)/*.o $(ICP_BUILD_OUTPUT_DIR);\
	cp $(OSAL_DIR)/src/$(ICP_OS)/$(ICP_OS_LEVEL)/build/$(ICP_OS)/$(ICP_OS_LEVEL)/*.o $(ICP_BUILD_OUTPUT_DIR);\
	cd $(ICP_FINAL_OUTPUT_DIR);\
	echo $(ARCHIVER) $(LIB_STATIC_FLAGS) rcD $(LIB_STATIC) $(ADDITIONAL_OBJECTS) *.o;\
	$(ARCHIVER) $(LIB_STATIC_FLAGS) rcD $(LIB_STATIC) $(ADDITIONAL_OBJECTS) *.o;
endif

ifndef ICP_USE_ADFCTL_API
lib_static: lib
	@for  dir in $(SUBDIRS); do \
	(cp $$dir/build/$(ICP_OS)/$(ICP_OS_LEVEL)/*.o $(ICP_BUILD_OUTPUT_DIR) ; ) \
	done ;\
	cp $(CMN_MEM_PATH)/$(ICP_OS_LEVEL)/*.o $(ICP_BUILD_OUTPUT_DIR);\
	cp $(ADF_DIR)/src/build/$(ICP_OS)/$(ICP_OS_LEVEL)/*.o $(ICP_BUILD_OUTPUT_DIR);\
	cp $(OSAL_DIR)/src/$(ICP_OS)/$(ICP_OS_LEVEL)/build/$(ICP_OS)/$(ICP_OS_LEVEL)/*.o $(ICP_BUILD_OUTPUT_DIR);\
	cd $(ICP_FINAL_OUTPUT_DIR);\
	echo $(ARCHIVER) $(LIB_STATIC_FLAGS) rcD $(LIB_STATIC) $(ADDITIONAL_OBJECTS) *.o;\
	$(ARCHIVER) $(LIB_STATIC_FLAGS) rcD $(LIB_STATIC) $(ADDITIONAL_OBJECTS) *.o;
endif

ifeq ($(ICP_WITHOUT_THREAD),1)
lib_shared: lib
	@for  dir in $(SUBDIRS); do \
	(cp $$dir/build/$(ICP_OS)/$(ICP_OS_LEVEL)/*.o $(ICP_BUILD_OUTPUT_DIR) ; ) \
	done ;\
	cd $(ICP_FINAL_OUTPUT_DIR);\
	echo $(LINKER) $(LIB_SHARED_FLAGS) -o $(LIB_SHARED) -lc $(OBJECTS) $(ADDITIONAL_OBJECTS) *.o -lcrypto -lz; \
	cmd="$(LINKER) $(LIB_SHARED_FLAGS) -o $(LIB_SHARED) -lc $(ADDITIONAL_OBJECTS) *.o -lcrypto -lz \
		-Bstatic -L$(ADF_DIR)/src/build/$(ICP_OS)/$(ICP_OS_LEVEL) -ladf -L$(OSAL_DIR)/src/build/$(ICP_OS)/$(ICP_OS_LEVEL)/ -losal -Bdynamic"; \
	echo "$$cmd"; \
	$$cmd
else
lib_shared: lib
	@for  dir in $(SUBDIRS); do \
	(cp $$dir/build/$(ICP_OS)/$(ICP_OS_LEVEL)/*.o $(ICP_BUILD_OUTPUT_DIR) ; ) \
	done ;\
	cd $(ICP_FINAL_OUTPUT_DIR);\
	echo $(LINKER) $(LIB_SHARED_FLAGS) -o $(LIB_SHARED) -lc $(OBJECTS) $(ADDITIONAL_OBJECTS) *.o -lpthread -lcrypto -lz; \
	cmd="$(LINKER) $(LIB_SHARED_FLAGS) -o $(LIB_SHARED) -lc $(ADDITIONAL_OBJECTS) *.o -lpthread -lcrypto -lz \
		-Bstatic -L$(ADF_DIR)/src/build/$(ICP_OS)/$(ICP_OS_LEVEL) -ladf -L$(OSAL_DIR)/src/build/$(ICP_OS)/$(ICP_OS_LEVEL)/ -losal -Bdynamic"; \
	echo "$$cmd"; \
	$$cmd

endif
endif

ifneq ($(ICP_OS_LEVEL),kernel_space)
include $(ICP_BUILDSYSTEM_PATH)/build_files/rules.mk
endif

include $(ICP_BUILDSYSTEM_PATH)/build_files/Core/$(ICP_CORE).mk
include $(ICP_BUILDSYSTEM_PATH)/build_files/OS/$(ICP_OS).mk

.DEFAULT: all

all:
	$(LOOP)

ifeq ($(ICP_OS_LEVEL), kernel_space)
lac_clean:
	@echo 'Removing derived kernel objects...'; \
	$(RM) -rf *.o *.a *.mod.* *.ko .*.cmd; \
	$(RM) -rf .tmp_versions; \
	$(RM) -rf $(ICP_BUILD_OUTPUT_DIR); \
	$(MAKE) cleandepend clean;
	@for dir in  $(SUBDIRS); do \
		(echo ; echo $$dir :; cd $$dir; \
		($(MAKE) cleandepend clean) || return 1) \
	done
else
lac_clean:
	@echo 'Removing derived objects...'; \
	$(RM) -rf *.o *.a *.mod.* *.ko .*.cmd; \
	$(RM) -rf .tmp_versions; \
	$(RM) -rf $(ICP_BUILD_OUTPUT_DIR); \
	$(MAKE) clean;
	@for dir in  $(SUBDIRS); do \
		(echo ; echo $$dir :; cd $$dir; \
		($(MAKE) clean) || return 1) \
	done
endif
