##########################################################################################################################################################
#
#
#
#  Makefile for compiling the TinyXCLASS package for python
#  Copyright (C) 2012 - 2025  Thomas Moeller
#
#  I. Physikalisches Institut, University of Cologne
#
#
#
#  Versions of the program:
#
#  Who           When        What
#
#  T. Moeller    2016-04-19  Initial version
#
#
#
#
#  Makefile options:    - "all":    builds the TinyXCLASS package
#
#                                   Used files:     src/CalculateXCLASS.f90
#
#                       - "sig":    creates signature file
#
#
##########################################################################################################################################################


#=========================================================================================================================================================
#=========================================================================================================================================================
# general settings
#=========================================================================================================================================================
#=========================================================================================================================================================


# define name of the executable file
EXEC = xclasslite


# set suffixes
.SUFFIXES: $(SUFFIXES) .f90


#=========================================================================================================================================================
#=========================================================================================================================================================
# define source and objects files
#=========================================================================================================================================================
#=========================================================================================================================================================


## define names of fortran source file(s)
FSRC = src/XCLASS-lite.f90


#=========================================================================================================================================================
#=========================================================================================================================================================
# define command for F2PY compiler and corresponding flags
#=========================================================================================================================================================
#=========================================================================================================================================================


#---------------------------------------------------------------------------------------------------------------------------------------------------------
# get command for f2py compiler


# if environment variable 'XCLASS_F2PY_COMPILER' is not set, continue here
ifndef XCLASS_F2PY_COMPILER


	# Check if we're in a virtual environment and set PYTHONPATH variable
	ifneq ($(VIRTUAL_ENV),)
		# Dynamically get the Python executable path
		# PYTHON := $(VIRTUAL_ENV)/bin/python
		PYTHON := $(shell which python)


		# Ensure numpy is installed
		# $(shell $(PYTHON) -m pip install numpy 2>/dev/null)


		# Dynamically get the site-packages directory
		SITE_PACKAGES := $(shell python -c "import site; print(site.getsitepackages()[0])")


		# Set PYTHONPATH to include NumPy path
		export PYTHONPATH := $(SITE_PACKAGES):$(PYTHONPATH)


		# Remove duplicates from PYTHONPATH
		unique_pythonpath := $(shell echo $(PYTHONPATH) | tr ':' '\n' | awk '!seen[$0]++' | tr '\n' ':' | sed 's/:$$//')


		# Export the unique PYTHONPATH
		export PYTHONPATH := $(unique_pythonpath)


		# Set the FCPY command
		FCPY := $(PYTHON) -m numpy.f2py


	# if we're not in a virtual environment and if environment variable 'XCLASS_F2PY_COMPILER' is not set, use default command
	else
		FCPY = python3 -m numpy.f2py
	endif


# if environment variable 'XCLASS_F2PY_COMPILER' is set, use this
else
	FCPY = ${XCLASS_F2PY_COMPILER}
endif


#---------------------------------------------------------------------------------------------------------------------------------------------------------
# set compiler flags
FPYFLAGS = --f90flags="-O5 -ffree-form -ffree-line-length-none -fbounds-check -frange-check -Wall $(XCLASS_EXTRA_FORTRAN_FLAG)"


#=========================================================================================================================================================
#=========================================================================================================================================================
# define rules
#=========================================================================================================================================================
#=========================================================================================================================================================


#---------------------------------------------------------------------------------------------------------------------------------------------------------
# define compilation rules


## define order of commands
all: f2py clean move


#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# compile f2py


## compilation commands
f2py:
	$(FCPY) -c $(FPYFLAGS) -m $(EXEC) $(FSRC)

sig:
	$(FCPY) -h CalculateXCLASS.pyf -m $(EXEC) $(FSRC)


#---------------------------------------------------------------------------------------------------------------------------------------------------------
# clean directories and previous compilations
clean:
	rm -f $(OBJS) *.mod *.o *.pyf bin/*


#---------------------------------------------------------------------------------------------------------------------------------------------------------
# move module file to bin directory
move:
	mv *.so bin/$(EXEC).so


#---------------------------------------------------------------------------------------------------------------------------------------------------------
#---------------------------------------------------------------------------------------------------------------------------------------------------------
#---------------------------------------------------------------------------------------------------------------------------------------------------------
# done
#---------------------------------------------------------------------------------------------------------------------------------------------------------
#---------------------------------------------------------------------------------------------------------------------------------------------------------
#---------------------------------------------------------------------------------------------------------------------------------------------------------

