#====  Copyright (c) 2017-2024 Advanced Micro Devices, Inc.  All rights reserved.
#
#               Developed by: Advanced Micro Devices, Inc.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# with the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimers.
#
# Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimers in the documentation
# and/or other materials provided with the distribution.
#
# Neither the names of Advanced Micro Devices, Inc., nor the names of its
# contributors may be used to endorse or promote products derived from this
# Software without specific prior written permission.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH
# THE SOFTWARE.
#===----------------------------------------------------------------------===#

CC = gcc
CLIBFLAGS = -mrdrnd -mrdseed
CEXEFLAGS = -L. -Wall

#AOCL-SecureRNG version
#SECRNG_VERSION = ${AOCLSECRNG_SOVERSION}
SECRNG_VERSION = 5.0.0

TESTSRC = src/secrng_test.c
LIBSRC = src/secrng.c
INCLUDEPATH = include

DNAME = dbg
TESTEXE = secrng_test
TESTEXEST = secrng_testst
TESTEXEDBG = secrng_test$(DNAME)
LIBNAME = amdsecrng
LIBNAMEV = amdsecrng-$(SECRNG_VERSION)
LIBLOC = lib$(LIBNAMEV)
SYMLIBLOC = lib$(LIBNAME)
LIBEXT = .so
STATICLIBEXT = .a

#all: $(LIBLOC)$(LIBEXT) $(TESTEXEST)
all: $(TESTEXE) $(TESTEXEST)

#Flags for debug build
debug: DEBUG = -g
debug: $(TESTEXEDBG)

#Static Lib build
static: $(TESTEXEST)

#Build test application using dynamic Secure RNG library
$(TESTEXE): $(TESTSRC) $(LIBLOC)$(LIBEXT)
	@echo Build SECURE RNG Test application
	$(CC) $(CEXEFLAGS) $(LDFLAGS) -I$(INCLUDEPATH) -o $@ $(TESTSRC) -l$(LIBNAME)

$(LIBLOC)$(LIBEXT): $(LIBSRC) $(LIBLOC)$(STATICLIBEXT)
	@echo Build AMD SECURE RNG dynamic library
	$(CC) $(CLIBFLAGS) -fPIC -shared -I$(INCLUDEPATH) $(LIBSRC) -o $@
	ln -sf lib$(LIBNAMEV)$(LIBEXT) $(SYMLIBLOC)$(LIBEXT)

#Build debug test application using dynamic Secure RNG library
$(TESTEXEDBG): $(TESTSRC) $(LIBLOC)$(DNAME)$(LIBEXT)
	@echo Build debug SECURE RNG Test application
	$(CC) $(DEBUG) $(CEXEFLAGS) $(LDFLAGS) -I$(INCLUDEPATH) -o $@ $(TESTSRC) -l$(LIBNAMEV)$(DNAME)

$(LIBLOC)$(DNAME)$(LIBEXT): $(LIBSRC)
	@echo Build debug AMD SECURE RNG library
	$(CC) $(DEBUG) $(CLIBFLAGS) -fPIC -shared -I$(INCLUDEPATH) $(LIBSRC) -o $@

#Build test application using static Secure RNG library
$(TESTEXEST): $(LIBLOC)$(STATICLIBEXT)
	$(CC) -I$(INCLUDEPATH) -o $@ $(TESTSRC) $(SYMLIBLOC)$(STATICLIBEXT)

$(LIBLOC)$(STATICLIBEXT): $(LIBSRC)
	@echo Build AMD Secure RNG Static Library
	$(CC) $(CLIBFLAGS) -I$(INCLUDEPATH) $(LIBSRC) -c -o $(LIBLOC).o
	ar rcs $@ $(LIBLOC).o
	rm -rf $(LIBLOC).o
	ln -sf lib$(LIBNAMEV)$(STATICLIBEXT) $(SYMLIBLOC)$(STATICLIBEXT)

clean:
	@echo Clean the target binaries
	$(RM) $(TESTEXE) $(TESTEXEDBG) $(TESTEXEST) $(LIBLOC)$(LIBEXT) $(LIBLOC)$(DNAME)$(LIBEXT) $(SYMLIBLOC)$(LIBEXT) $(LIBLOC)$(STATICLIBEXT) $(SYMLIBLOC)$(STATICLIBEXT)
