################################################################################
#
# cmake.cygclass - functions for building CMake-based packages
#
# Part of cygport - Cygwin packaging application
# Copyright (C) 2006-2015 Yaakov Selkowitz
# Provided by the Cygwin Ports project <http://sourceware.org/cygwinports/>
#
# 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 <http://www.gnu.org/licenses/>.
#
################################################################################

#****h* Cygclasses/cmake.cygclass
#  SYNOPSIS
#  inherit cmake
#  DESCRIPTION
#  CMake is a build system which can be used instead of autoconf and automake.
#  The build configuration and commands are written as CMakeLists.txt files
#  in each directory, which cmake uses to run configuration tests and create
#  Makefiles, config headers, and other files.
#
#  CMake can be used for a wide variety of software, but is most commonly
#  used in KDE4 and some Qt4 packages.
#  INHERITED BY
#  kde4.cygclass, opensync.cygclass, qt4-cmake.cygclass
#****

__cmake_system() {
	local cmsys

	# FIXME: do binutils/gcc really support all these?
	case ${CHOST} in
	*-ibm-aix*)       cmsys="AIX" ;;
	*-*-beos*)        cmsys="BeOS" ;;
	*-*-bsdi*)        cmsys="BSDOS" ;;
	*-ibm-cnk*)       cmsys="BlueGeneL" ;;
	*-cray-unicos*)   cmsys="Catamount" ;;
	*-*-cygwin*)      cmsys="CYGWIN" ;;
	*-*-darwin*)      cmsys="Darwin" ;;
	*-*-dragonfly*)   cmsys="DragonFly" ;;
	*-*-freebsd*)     cmsys="FreeBSD" ;;
	*-*-haiku*)       cmsys="Haiku" ;;
	*-*-hpux*)        cmsys="HP-UX" ;;
	*-*-irix6*)       cmsys="IRIX64" ;;
	*-*-irix*)        cmsys="IRIX" ;;
	*-*-kfreebsd*)    cmsys="kFreeBSD" ;;
	*-*-linux*)       cmsys="Linux" ;;
	*-ncr-sysv*)      cmsys="MP-RAS" ;;
	*-*-netbsd*)      cmsys="NetBSD" ;;
	*-*-openbsd*)     cmsys="OpenBSD" ;;
	*-dec-vms*)       cmsys="OpenVMS" ;;
	*-*-osf*)         cmsys="OSF1" ;;
	*-*-nto-qnx*)     cmsys="QNX" ;;
	*-*-riscos*)      cmsys="RISCos" ;;
	*-*-sco*)         cmsys="SCO_SV" ;;
	*-sni-sysv*)      cmsys="SINIX" ;;
	*-*-solaris*)     cmsys="SunOS" ;;
	*-*-ultrix*)      cmsys="ULTRIX" ;;
	*-*-sysv4.2uw*)   cmsys="UnixWare" ;;
	*-*-mingw*)       cmsys="Windows" ;;
	*-*-xenix*)       cmsys="Xenix" ;;
	# must be last to avoid false positives from *-*-KERNEL-gnu
	*-*-gnu*)         cmsys="GNU" ;;
	*)	error "Host ${CHOST} is not supported by CMake" ;;
	esac

	echo -n ${cmsys}
}

#****C* cmake.cygclass/cygcmake
#  SYNOPSIS
#  cygcmake [OPTIONS]
#  DESCRIPTION
#  Runs cmake to configure the package.  cygcmake passes cmake the flags
#  necessary to install the package into the standard prefix and according to the
#  |html <a href="http://www.pathname.com/fhs/">Filesystem Hierarchy Standard</a> and the
#  |html <a href="http://cygwin.com/setup.html#package_contents">Cygwin package guidelines</a>.
#  In addition, all arguments to cygcmake are passed to cmake, followed by
#  CYGCMAKE_ARGS, if set.
#  NOTES
#  * cygcmake should be run in or under $B, as it defines the build directory of
#    the package and creates numerous files.
#  * If the top-level CMakeLists.txt is not in $S, see CYGCMAKE_SOURCE.
#  * Packages configured with cygcmake use cygmake to compile.
#  REQUIRES
#  cmake
#****
cygcmake() {
	local buildtype cmdir crossargs exeext;

	check_prog_req cmake;

	case ${CBUILD} in
		*-cygwin*|*-mingw*|*-msys*)  exeext=".exe" ;;
	esac

#****v* cygcmake/CYGCMAKE_SOURCE
#  DESCRIPTION
#  Set this variable to the directory containing the top-level CMakeLists.txt.
#  This is only necessary when the top-level CMakeLists.txt is not in $S and
#  cygcmake is not being run in the same subdirectory of $B which under $S
#  contains the top-level CMakeLists.txt.  (IOW if the top-level CMakeLists.txt
#  is in $S/unix and cygcmake is run from $B/unix, setting CYGCMAKE_SOURCE
#  would not be necessary.)
#****
	if defined CYGCMAKE_SOURCE && [ -e ${CYGCMAKE_SOURCE}/CMakeLists.txt ]
	then
		cmdir=${CYGCMAKE_SOURCE};
	elif [ -e ${PWD/${B}/${S}}/CMakeLists.txt ]
	then
		cmdir=${PWD/${B}/${S}};
	elif [ -e ${S}/CMakeLists.txt ]
	then
		cmdir=${S};
	else
		error "cygcmake: cmake directory not found";
	fi

	if defined _CYGPORT_RESTRICT_debuginfo_
	then
		buildtype=Release
	else
		buildtype=RelWithDebInfo
	fi

	if cross_compiling
	then
		crossargs="-DCMAKE_SYSTEM_NAME=$(__cmake_system)
			-D_CMAKE_TOOLCHAIN_PREFIX=${CHOST}-
			-DCMAKE_FIND_ROOT_PATH=$(${CC} -print-sysroot)
			-DCMAKE_FIND_ROOT_PATH_MODE_INCLUDE=ONLY
			-DCMAKE_FIND_ROOT_PATH_MODE_LIBRARY=ONLY"
		case ${CHOST} in *-cygwin*|*-mingw*)
			crossargs+=" -DCMAKE_RC_COMPILER=$(which ${CHOST}-windres${exeext})"
			;;
		esac
		if check_prog ${CHOST}-pkg-config
		then
			crossargs+=" -DPKG_CONFIG_EXECUTABLE=$(which ${CHOST}-pkg-config)"
		fi
	else
		crossargs=
	fi

#****v* cygcmake/CYGCMAKE_ARGS
#  DESCRIPTION
#  Additional flags to pass to cmake during cygcmake.
#  NOTES
#  Flags in CYGCMAKE_ARGS follow, and therefore override, flags passed by
#  default and as arguments to cygcmake.
#****

	cmake -Wno-dev \
		${cmdir} \
		-DCMAKE_BINARY_DIR=. \
		-DCMAKE_BUILD_TYPE=${buildtype} \
		-DCMAKE_C_COMPILER=$(which ${CC}${exeext}) \
		-DCMAKE_C_FLAGS="${CFLAGS} ${CPPFLAGS}" \
		-DCMAKE_CXX_COMPILER=$(which ${CXX}${exeext}) \
		-DCMAKE_CXX_FLAGS="${CXXFLAGS} ${CPPFLAGS}" \
		-DCMAKE_Fortran_COMPILER=$(which ${FC}${exeext}) \
		-DCMAKE_Fortran_FLAGS="${FCFLAGS} ${CPPFLAGS}" \
		-DCMAKE_AR=$(which ${AR}${exeext}) \
		-DCMAKE_RANLIB=$(which ${RANLIB}${exeext}) \
		-DCMAKE_EXE_LINKER_FLAGS="${LDFLAGS}" \
		-DCMAKE_MODULE_LINKER_FLAGS="${LDFLAGS}" \
		-DCMAKE_SHARED_LINKER_FLAGS="${LDFLAGS}" \
		-DCMAKE_INSTALL_PREFIX=$(__host_prefix) \
		-DCMAKE_LEGACY_CYGWIN_WIN32=0 \
		${crossargs} \
		"${@}" \
		${CYGCMAKE_ARGS} \
		|| error "cmake failed"
}

#****o* cmake.cygclass/src_compile (cmake)
#  DEFINITION
src_compile() {
	cd ${B}
	cygcmake
	cygmake
}
#****

readonly -f __cmake_system cygcmake
