#-------------------------------------------------------------------------------
#
# makefile
#
# Vitis/Eclipse really likes just GNU make; therefore, we wrap the
# OpenPowerLink cmake build script into a make file
#
# Vitis comes with its own cmake(1) installation:
#
#   /opt/Xilinx/Vitis/2022.2/tps/lnx64/cmake-3.21.4/bin
#
# Unfortunately, this cmake is linked against libcrypto.so.10 and
# libssl.so.10 which are not part of the stock Ubuntu installation. To
# make cmake(1) work in the Vitis IDE, create symbolic links to these
# shared librarires in
#
#   /opt/Xilinx/Vitis/2022.2/lib/lnx64.o/Ubuntu:
#     libcrypto.so.10 -> ../../../tps/lnx64/cmake-3.21.4/libs/Ubuntu/libcrypto.so.10
#     libssl.so.10 -> ../../../tps/lnx64/cmake-3.21.4/libs/Ubuntu/libssl.so.10
#
# (c)2023 AIT Austrian Institute of Technology
#-------------------------------------------------------------------------------

# set toolchain file
CMAKE_TOOLCHAIN ?= ../src/cmake/toolchain-microblaze-xilinx-freertos-elf.cmake

# configure OpenPowerLink library for MN or CN (default)
CONFIG_MN ?= OFF

# determine build type: Release or Debug
CMAKE_BUILD_TYPE ?= Release

# top-level CMakeLists.txt file
CMAKE_LIST_DIR ?= ../src/stack

ifeq ($(CONFIG_MN), "ON")
  $(info "building MN '${CMAKE_BUILD_TYPE}' variant of OpenPowerLink library in ${PWD}")
  TARGET = liboplkmn
else
  $(info "building CN '${CMAKE_BUILD_TYPE}' variant of OpenPowerLink library in ${PWD}")
  TARGET = liboplkcn
endif  

.PHONY : all build clean package

all : $(TARGET).a

# always have cmake rescan the build directory. Because of memory constraint, always build Release variant of stack
Makefile : FORCE
#	cmake --toolchain ${CMAKE_TOOLCHAIN} -DCMAKE_BUILD_TYPE=$(CMAKE_BUILD_TYPE) -DCMAKE_INSTALL_PREFIX="$(PWD)" -DCONFIG_MN=${CONFIG_MN} ${CMAKE_LIST_DIR}
	cmake --toolchain ${CMAKE_TOOLCHAIN} -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX="$(PWD)" -DCONFIG_MN=${CONFIG_MN} ${CMAKE_LIST_DIR}

FORCE : ;

# install target
$(TARGET).a : build
	make install 

# build target with sub-make
build : Makefile
	make

# remove all cmake artifacts
clean :
	rm -rf config-$(TARGET) $(TARGET).a 
	make clean

package :
	$(error "target 'package' not supported")
