################################################################################
#
# python3.cygclass - functions for installing Python3 modules
#
# Part of cygport - Cygwin packaging application
# Copyright (C) 2006-2020 Cygport authors
# Provided by the Cygwin project <https://cygwin.com/>
#
# cygport is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# cygport 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 cygport.  If not, see <https://www.gnu.org/licenses/>.
#
################################################################################

#****h* Cygclasses/python3.cygclass
#  SYNOPSIS
#  inherit python3
#  DESCRIPTION
#  Python is a general-purpose, interpreted programming language used in a
#  wide variety of software.  It comes with a large standard library, and is
#  easily extendible with modules written in Python and/or C/C++.  Programs
#  can also embed a Python interpreter for use with their own extensions.
#
#  This cygclass provides definitions and some install helpers which can be used
#  by any Python3-based package.  Python3 modules and programs are built in a
#  number of ways, so this cygclass does not provide any build functions.  The
#  two most common build systems for Python3 packages are Distutils and autotools.
#  NOTE
#  python3.cygclass is for the 3.x versions of Python.
#  INHERITED BY
#  python-wheel.cygclass
#  REQUIRES
#  python3
#****

# cross-compiling is not (yet?) supported
__cross_compiling_error

check_prog_req python3

#****d* python3.cygclass/PYTHON3
#  DESCRIPTION
#  Absolute path to the Python3 interpreter.
#****
PYTHON3=$(readlink -f /usr/bin/python3 | sed -e 's/\.exe$//')

#****d* python3.cygclass/PYTHON3_CONFIG
#  DESCRIPTION
#  Absolute path to the Python3 config script.
#****
PYTHON3_CONFIG=$(readlink -f /usr/bin/python3-config)

#****d* python3.cygclass/PYTHON3_VERSION
#  DESCRIPTION
#  The major.minor version of the current Python3 interpreter.
#****
PYTHON3_VERSION=$(${PYTHON3} -c 'from distutils.sysconfig import * ; print(get_python_version());')

#****d* python3.cygclass/PYTHON3_PKGVERSION
#  DESCRIPTION
#  The $major$minor version (without a dot) for use in package names.
#****
PYTHON3_PKGVERSION=${PYTHON3_VERSION/.}

#****d* python3.cygclass/PYTHON3_ABIFLAGS
#  DESCRIPTION
#  The ABI flags of the current Python3 interpreter, as used in the names of
#  the interpreter, config script and subdir, shared library, and includedir.
#****
PYTHON3_ABIFLAGS=$(${PYTHON3_CONFIG} --abiflags 2>/dev/null)

#****d* python3.cygclass/PYTHON3_INCLUDEDIR
#  DESCRIPTION
#  Path containing the Python3 C library headers.
#****
PYTHON3_INCLUDEDIR=$(${PYTHON3} -c 'from distutils.sysconfig import * ; print(get_python_inc());')

#****d* python3.cygclass/PYTHON3_LIB
#  DESCRIPTION
#  Path containing the Python3 standard library.
#  NOTE
#  Never install third-party extensions into PYTHON3_LIB; use PYTHON3_SITELIB instead.
#****
PYTHON3_LIB=$(${PYTHON3} -c 'from distutils.sysconfig import * ; print(get_python_lib(0,1));')

#****d* python3.cygclass/PYTHON3_SITELIB
#  DESCRIPTION
#  Installation path for all Python3 extension modules.
#****
PYTHON3_SITELIB=$(${PYTHON3} -c 'from distutils.sysconfig import * ; print(get_python_lib(0,0));')

#****d* python3.cygclass/LIBPYTHON3
#  DESCRIPTION
#  Link flags for the Python3 C library.  This definition must be properly quoted.
#****
LIBPYTHON3=$(${PYTHON3_CONFIG} --libs)

#****I* python3.cygclass/python3into
#  SYNOPSIS
#  python3into SUBDIRECTORY
#  DESCRIPTION
#  Subdirectory of PYTHON3_SITELIB into which dopython3 should install.  This is
#  usually unnecessary.
#****
python3into() {
	if (( $# != 1 ))
	then
	    error "python3into accepts exactly one argument";
	fi

	case ${1} in
    	/*) error "python3into argument should be only a subdirectory" ;;
	esac

	_python3into_dir=${1};
}

#****I* python3.cygclass/dopython3
#  SYNOPSIS
#  [python3into SUBDIRECTORY]
#  dopython3 MODULE1 [MODULE2] ...
#  DESCRIPTION
#  Installs the given Python3 module(s) (.py or .dll) into PYTHON3_SITELIB under $D,
#  or a subdirectory thereof if python3into was previously called.
#****
dopython3() {
	local pydir
	local i

	if defined _python3into_dir
	then
		pydir=${PYTHON3_SITELIB}/${_python3into_dir}
	else
		pydir=${PYTHON3_SITELIB}
	fi

	dodir ${pydir}

	for i
	do
		if [ -e ${i} ]
		then
			case ${i} in
				*.dll)
					__doinstall 0755 ${i} ${pydir} || error "dopython3 ${i} failed"
					;;
				*)
					__doinstall 0644 ${i} ${pydir} || error "dopython3 ${i} failed"
					;;
			esac
		else
			error "dopython3: ${i}: file not found"
		fi
	done
}

#****I* python3.cygclass/python3_optimize
#  SYNOPSIS
#  python3_optimize DIRECTORY [DIRECTORY] ...
#  DESCRIPTION
#  Bytecode-compile all Python3 modules found in the given directories under $D.
#****
python3_optimize() {
	local pyd

	for pyd
	do
		if [ ! -d ${D}${pyd} ]
		then
			error "directory ${pyd} does not exist"
		fi

		inform "Compiling ${pyd}..."
		${PYTHON3} ${PYTHON3_LIB}/compileall.py -f -q -d ${pyd} ${D}${pyd}
		${PYTHON3} -O ${PYTHON3_LIB}/compileall.py -f -q -d ${pyd} ${D}${pyd}
		${PYTHON3} -OO ${PYTHON3_LIB}/compileall.py -f -q -d ${pyd} ${D}${pyd}
	done
}

#****I* python3.cygclass/python3_fix_shebang
#  SYNOPSIS
#  python3_fix_shebang SCRIPT [SCRIPT ...]
#  DESCRIPTION
#  Fixes the designated interpreter of SCRIPT to PYTHON3.  This would be necessary
#  if the original uses an incorrect path (e.g. /usr/local/bin) or an
#  incorrectly versioned binary.  SCRIPT need not be prefixed by $D.
#****
python3_fix_shebang() {
	for f
	do
		__fix_shebang ${PYTHON3} ${D}/${f#${D}}
	done
}

readonly -f python3into dopython3 python3_optimize python3_fix_shebang
