Revert "portage-stable: update cmake-utils.eclass."

This reverts commit 87d9983f080ced380eb6cfc20ec6f5a229ab0d58.

I think this is causing the mariadb build failures.
Let's see if it helps the CQ pass.

BUG=None
TEST=cq passes

Change-Id: I8cc77c974ce959b9f0ac74dad42e3376635867ae
Reviewed-on: https://chromium-review.googlesource.com/351860
Commit-Ready: Mike Frysinger <vapier@chromium.org>
Tested-by: Mike Frysinger <vapier@chromium.org>
Reviewed-by: Mike Frysinger <vapier@chromium.org>
diff --git a/eclass/cmake-utils.eclass b/eclass/cmake-utils.eclass
index 02b06bb..018ce93 100644
--- a/eclass/cmake-utils.eclass
+++ b/eclass/cmake-utils.eclass
@@ -1,6 +1,6 @@
-# Copyright 1999-2016 Gentoo Foundation
+# Copyright 1999-2013 Gentoo Foundation
 # Distributed under the terms of the GNU General Public License v2
-# $Id$
+# $Header: /var/cvsroot/gentoo-x86/eclass/cmake-utils.eclass,v 1.100 2013/07/24 20:57:38 scarabeus Exp $
 
 # @ECLASS: cmake-utils.eclass
 # @MAINTAINER:
@@ -17,120 +17,65 @@
 # builds (default), in-source builds and an implementation of the well-known use_enable
 # and use_with functions for CMake.
 
-if [[ -z ${_CMAKE_UTILS_ECLASS} ]]; then
-_CMAKE_UTILS_ECLASS=1
+if [[ ${___ECLASS_ONCE_CMAKE_UTILS} != "recur -_+^+_- spank" ]] ; then
+___ECLASS_ONCE_CMAKE_UTILS="recur -_+^+_- spank"
 
-# @ECLASS-VARIABLE: BUILD_DIR
+# @ECLASS-VARIABLE: WANT_CMAKE
 # @DESCRIPTION:
-# Build directory where all cmake processed files should be generated.
-# For in-source build it's fixed to ${CMAKE_USE_DIR}.
-# For out-of-source build it can be overridden, by default it uses
-# ${WORKDIR}/${P}_build.
-#
-# This variable has been called CMAKE_BUILD_DIR formerly.
-# It is set under that name for compatibility.
-
-# @ECLASS-VARIABLE: CMAKE_BINARY
-# @DESCRIPTION:
-# Eclass can use different cmake binary than the one provided in by system.
-: ${CMAKE_BINARY:=cmake}
-
-# @ECLASS-VARIABLE: CMAKE_BUILD_TYPE
-# @DESCRIPTION:
-# Set to override default CMAKE_BUILD_TYPE. Only useful for packages
-# known to make use of "if (CMAKE_BUILD_TYPE MATCHES xxx)".
-# If about to be set - needs to be set before invoking cmake-utils_src_configure.
-# You usualy do *NOT* want nor need to set it as it pulls CMake default build-type
-# specific compiler flags overriding make.conf.
-: ${CMAKE_BUILD_TYPE:=Gentoo}
-
-# @ECLASS-VARIABLE: CMAKE_IN_SOURCE_BUILD
-# @DESCRIPTION:
-# Set to enable in-source build.
-
-# @ECLASS-VARIABLE: CMAKE_MAKEFILE_GENERATOR
-# @DESCRIPTION:
-# Specify a makefile generator to be used by cmake.
-# At this point only "emake" and "ninja" are supported.
-: ${CMAKE_MAKEFILE_GENERATOR:=emake}
+# Specify if cmake-utils eclass should depend on cmake optionaly or not.
+# This is usefull when only part of aplication is using cmake build system.
+# Valid values are: always [default], optional (where the value is the useflag
+# used for optionality)
+WANT_CMAKE="${WANT_CMAKE:-always}"
 
 # @ECLASS-VARIABLE: CMAKE_MIN_VERSION
 # @DESCRIPTION:
 # Specify the minimum required CMake version.
-: ${CMAKE_MIN_VERSION:=2.8.12}
-
-# @ECLASS-VARIABLE: CMAKE_REMOVE_MODULES
-# @DESCRIPTION:
-# Do we want to remove anything? yes or whatever else for no
-: ${CMAKE_REMOVE_MODULES:=yes}
+CMAKE_MIN_VERSION="${CMAKE_MIN_VERSION:-2.8.9}"
 
 # @ECLASS-VARIABLE: CMAKE_REMOVE_MODULES_LIST
 # @DESCRIPTION:
 # Space-separated list of CMake modules that will be removed in $S during src_prepare,
 # in order to force packages to use the system version.
-: ${CMAKE_REMOVE_MODULES_LIST:=FindBLAS FindLAPACK}
+CMAKE_REMOVE_MODULES_LIST="${CMAKE_REMOVE_MODULES_LIST:-FindBLAS FindLAPACK}"
 
-# @ECLASS-VARIABLE: CMAKE_USE_DIR
+# @ECLASS-VARIABLE: CMAKE_REMOVE_MODULES
 # @DESCRIPTION:
-# Sets the directory where we are working with cmake.
-# For example when application uses autotools and only one
-# plugin needs to be done by cmake.
-# By default it uses ${S}.
+# Do we want to remove anything? yes or whatever else for no
+CMAKE_REMOVE_MODULES="${CMAKE_REMOVE_MODULES:-yes}"
 
-# @ECLASS-VARIABLE: CMAKE_VERBOSE
+# @ECLASS-VARIABLE: CMAKE_MAKEFILE_GENERATOR
 # @DESCRIPTION:
-# Set to OFF to disable verbose messages during compilation
-: ${CMAKE_VERBOSE:=ON}
+# Specify a makefile generator to be used by cmake.
+# At this point only "emake" and "ninja" are supported.
+CMAKE_MAKEFILE_GENERATOR="${CMAKE_MAKEFILE_GENERATOR:-emake}"
 
 # @ECLASS-VARIABLE: CMAKE_WARN_UNUSED_CLI
 # @DESCRIPTION:
 # Warn about variables that are declared on the command line
 # but not used. Might give false-positives.
 # "no" to disable (default) or anything else to enable.
-
-# @ECLASS-VARIABLE: PREFIX
-# @DESCRIPTION:
-# Eclass respects PREFIX variable, though it's not recommended way to set
-# install/lib/bin prefixes.
-# Use -DCMAKE_INSTALL_PREFIX=... CMake variable instead.
-: ${PREFIX:=/usr}
-
-# @ECLASS-VARIABLE: WANT_CMAKE
-# @DESCRIPTION:
-# Specify if cmake-utils eclass should depend on cmake optionally or not.
-# This is useful when only part of application is using cmake build system.
-# Valid values are: always [default], optional (where the value is the useflag
-# used for optionality)
-#
-# This is banned in EAPI 6 and later.
-: ${WANT_CMAKE:=always}
-
-# @ECLASS-VARIABLE: CMAKE_EXTRA_CACHE_FILE
-# @DESCRIPTION:
-# Specifies an extra cache file to pass to cmake. This is the analog of EXTRA_ECONF
-# for econf and is needed to pass TRY_RUN results when cross-compiling.
-# Should be set by user in a per-package basis in /etc/portage/package.env.
-
-case ${EAPI} in
-	2|3|4|5) : ${CMAKE_WARN_UNUSED_CLI:=no} ;;
-	6) : ${CMAKE_WARN_UNUSED_CLI:=yes} ;;
-	*) die "EAPI=${EAPI:-0} is not supported" ;;
-esac
-
-inherit toolchain-funcs multilib flag-o-matic eutils versionator
-
-EXPORT_FUNCTIONS src_prepare src_configure src_compile src_test src_install
+CMAKE_WARN_UNUSED_CLI="${CMAKE_WARN_UNUSED_CLI:-no}"
 
 CMAKEDEPEND=""
 case ${WANT_CMAKE} in
 	always)
 		;;
 	*)
-		[[ ${EAPI} == [2345] ]] || die "WANT_CMAKE is banned in EAPI 6 and later"
 		IUSE+=" ${WANT_CMAKE}"
 		CMAKEDEPEND+="${WANT_CMAKE}? ( "
 		;;
 esac
+inherit toolchain-funcs multilib flag-o-matic eutils
+
+CMAKE_EXPF="src_compile src_test src_install"
+case ${EAPI:-0} in
+	2|3|4|5) CMAKE_EXPF+=" src_prepare src_configure" ;;
+	1|0) eerror "cmake-utils no longer supports EAPI 0-1." && die
+	;;
+	*) die "Unknown EAPI, bug eclass maintainers." ;;
+esac
+EXPORT_FUNCTIONS ${CMAKE_EXPF}
 
 case ${CMAKE_MAKEFILE_GENERATOR} in
 	emake)
@@ -149,20 +94,17 @@
 	CMAKEDEPEND+=" >=dev-util/cmake-${CMAKE_MIN_VERSION}"
 fi
 
+CMAKEDEPEND+=" userland_GNU? ( >=sys-apps/findutils-4.4.0 )"
+
 [[ ${WANT_CMAKE} = always ]] || CMAKEDEPEND+=" )"
 
 DEPEND="${CMAKEDEPEND}"
 unset CMAKEDEPEND
 
 # Internal functions used by cmake-utils_use_*
-_cmake_use_me_now() {
+_use_me_now() {
 	debug-print-function ${FUNCNAME} "$@"
 
-	local arg=$2
-	[[ ! -z $3 ]] && arg=$3
-
-	[[ ${EAPI} == [2345] ]] || die "${FUNCNAME[1]} is banned in EAPI 6 and later"
-
 	local uper capitalised x
 	[[ -z $2 ]] && die "cmake-utils_use-$1 <USE flag> [<flag name>]"
 	if [[ ! -z $3 ]]; then
@@ -177,16 +119,9 @@
 		done
 	fi
 }
-_cmake_use_me_now_inverted() {
+_use_me_now_inverted() {
 	debug-print-function ${FUNCNAME} "$@"
 
-	local arg=$2
-	[[ ! -z $3 ]] && arg=$3
-
-	if [[ ${EAPI} != [2345] && "${FUNCNAME[1]}" != cmake-utils_use_find_package ]] ; then
-		die "${FUNCNAME[1]} is banned in EAPI 6 and later"
-	fi
-
 	local uper capitalised x
 	[[ -z $2 ]] && die "cmake-utils_use-$1 <USE flag> [<flag name>]"
 	if [[ ! -z $3 ]]; then
@@ -202,8 +137,55 @@
 	fi
 }
 
+# @ECLASS-VARIABLE: BUILD_DIR
+# @DESCRIPTION:
+# Build directory where all cmake processed files should be generated.
+# For in-source build it's fixed to ${CMAKE_USE_DIR}.
+# For out-of-source build it can be overriden, by default it uses
+# ${WORKDIR}/${P}_build.
+#
+# This variable has been called CMAKE_BUILD_DIR formerly.
+# It is set under that name for compatibility.
+
+# @ECLASS-VARIABLE: CMAKE_BUILD_TYPE
+# @DESCRIPTION:
+# Set to override default CMAKE_BUILD_TYPE. Only useful for packages
+# known to make use of "if (CMAKE_BUILD_TYPE MATCHES xxx)".
+# If about to be set - needs to be set before invoking cmake-utils_src_configure.
+# You usualy do *NOT* want nor need to set it as it pulls CMake default build-type
+# specific compiler flags overriding make.conf.
+: ${CMAKE_BUILD_TYPE:=Gentoo}
+
+# @ECLASS-VARIABLE: CMAKE_IN_SOURCE_BUILD
+# @DESCRIPTION:
+# Set to enable in-source build.
+
+# @ECLASS-VARIABLE: CMAKE_USE_DIR
+# @DESCRIPTION:
+# Sets the directory where we are working with cmake.
+# For example when application uses autotools and only one
+# plugin needs to be done by cmake.
+# By default it uses ${S}.
+
+# @ECLASS-VARIABLE: CMAKE_VERBOSE
+# @DESCRIPTION:
+# Set to OFF to disable verbose messages during compilation
+: ${CMAKE_VERBOSE:=ON}
+
+# @ECLASS-VARIABLE: PREFIX
+# @DESCRIPTION:
+# Eclass respects PREFIX variable, though it's not recommended way to set
+# install/lib/bin prefixes.
+# Use -DCMAKE_INSTALL_PREFIX=... CMake variable instead.
+: ${PREFIX:=/usr}
+
+# @ECLASS-VARIABLE: CMAKE_BINARY
+# @DESCRIPTION:
+# Eclass can use different cmake binary than the one provided in by system.
+: ${CMAKE_BINARY:=cmake}
+
 # Determine using IN or OUT source build
-_cmake_check_build_dir() {
+_check_build_dir() {
 	: ${CMAKE_USE_DIR:=${S}}
 	if [[ -n ${CMAKE_IN_SOURCE_BUILD} ]]; then
 		# we build in source dir
@@ -230,21 +212,16 @@
 	# Backwards compatibility for getting the value.
 	CMAKE_BUILD_DIR=${BUILD_DIR}
 
-	mkdir -p "${BUILD_DIR}" || die
+	mkdir -p "${BUILD_DIR}"
 	echo ">>> Working in BUILD_DIR: \"$BUILD_DIR\""
 }
 
 # Determine which generator to use
-_cmake_generator_to_use() {
+_generator_to_use() {
 	local generator_name
 
 	case ${CMAKE_MAKEFILE_GENERATOR} in
 		ninja)
-			# if ninja is enabled but not installed, the build could fail
-			# this could happen if ninja is manually enabled (eg. make.conf) but not installed
-			if ! ROOT=/ has_version dev-util/ninja; then
-				die "CMAKE_MAKEFILE_GENERATOR is set to ninja, but ninja is not installed. Please install dev-util/ninja or unset CMAKE_MAKEFILE_GENERATOR."
-			fi
 			generator_name="Ninja"
 			;;
 		emake)
@@ -259,32 +236,6 @@
 	echo ${generator_name}
 }
 
-# @FUNCTION: cmake_comment_add_subdirectory
-# @USAGE: <subdirectory>
-# @DESCRIPTION:
-# Comment out an add_subdirectory call in CMakeLists.txt in the current directory
-cmake_comment_add_subdirectory() {
-	if [[ -z ${1} ]]; then
-		die "comment_add_subdirectory must be passed the directory name to comment"
-	fi
-
-	if [[ -e "CMakeLists.txt" ]]; then
-		sed -e "/add_subdirectory[[:space:]]*([[:space:]]*${1//\//\\/}[[:space:]]*)/I s/^/#DONOTCOMPILE /" \
-			-i CMakeLists.txt || die "failed to comment add_subdirectory(${1})"
-	fi
-}
-
-# @FUNCTION: comment_add_subdirectory
-# @USAGE: <subdirectory>
-# @DESCRIPTION:
-# Comment out an add_subdirectory call in CMakeLists.txt in the current directory
-# Banned in EAPI 6 and later - use cmake_comment_add_subdirectory instead.
-comment_add_subdirectory() {
-	[[ ${EAPI} == [2345] ]] || die "comment_add_subdirectory is banned in EAPI 6 and later - use cmake_comment_add_subdirectory instead"
-
-	cmake_comment_add_subdirectory "$@"
-}
-
 # @FUNCTION: cmake-utils_use_with
 # @USAGE: <USE flag> [flag name]
 # @DESCRIPTION:
@@ -292,7 +243,7 @@
 #
 # `cmake-utils_use_with foo FOO` echoes -DWITH_FOO=ON if foo is enabled
 # and -DWITH_FOO=OFF if it is disabled.
-cmake-utils_use_with() { _cmake_use_me_now WITH_ "$@" ; }
+cmake-utils_use_with() { _use_me_now WITH_ "$@" ; }
 
 # @FUNCTION: cmake-utils_use_enable
 # @USAGE: <USE flag> [flag name]
@@ -301,23 +252,17 @@
 #
 # `cmake-utils_use_enable foo FOO` echoes -DENABLE_FOO=ON if foo is enabled
 # and -DENABLE_FOO=OFF if it is disabled.
-cmake-utils_use_enable() { _cmake_use_me_now ENABLE_ "$@" ; }
+cmake-utils_use_enable() { _use_me_now ENABLE_ "$@" ; }
 
 # @FUNCTION: cmake-utils_use_find_package
-# @USAGE: <USE flag> <package name>
+# @USAGE: <USE flag> [flag name]
 # @DESCRIPTION:
 # Based on use_enable. See ebuild(5).
 #
 # `cmake-utils_use_find_package foo LibFoo` echoes -DCMAKE_DISABLE_FIND_PACKAGE_LibFoo=OFF
 # if foo is enabled and -DCMAKE_DISABLE_FIND_PACKAGE_LibFoo=ON if it is disabled.
-# This can be used to make find_package optional.
-cmake-utils_use_find_package() {
-	if [[ ${EAPI} != [2345] && "$#" != 2 ]] ; then
-		die "Usage: cmake-utils_use_find_package <USE flag> <package name>"
-	fi
-
-	_cmake_use_me_now_inverted CMAKE_DISABLE_FIND_PACKAGE_ "$@" ;
-}
+# This can be used to make find_package optional (since cmake-2.8.6).
+cmake-utils_use_find_package() { _use_me_now_inverted CMAKE_DISABLE_FIND_PACKAGE_ "$@" ; }
 
 # @FUNCTION: cmake-utils_use_disable
 # @USAGE: <USE flag> [flag name]
@@ -326,7 +271,7 @@
 #
 # `cmake-utils_use_enable foo FOO` echoes -DDISABLE_FOO=OFF if foo is enabled
 # and -DDISABLE_FOO=ON if it is disabled.
-cmake-utils_use_disable() { _cmake_use_me_now_inverted DISABLE_ "$@" ; }
+cmake-utils_use_disable() { _use_me_now_inverted DISABLE_ "$@" ; }
 
 # @FUNCTION: cmake-utils_use_no
 # @USAGE: <USE flag> [flag name]
@@ -335,7 +280,7 @@
 #
 # `cmake-utils_use_no foo FOO` echoes -DNO_FOO=OFF if foo is enabled
 # and -DNO_FOO=ON if it is disabled.
-cmake-utils_use_no() { _cmake_use_me_now_inverted NO_ "$@" ; }
+cmake-utils_use_no() { _use_me_now_inverted NO_ "$@" ; }
 
 # @FUNCTION: cmake-utils_use_want
 # @USAGE: <USE flag> [flag name]
@@ -344,7 +289,7 @@
 #
 # `cmake-utils_use_want foo FOO` echoes -DWANT_FOO=ON if foo is enabled
 # and -DWANT_FOO=OFF if it is disabled.
-cmake-utils_use_want() { _cmake_use_me_now WANT_ "$@" ; }
+cmake-utils_use_want() { _use_me_now WANT_ "$@" ; }
 
 # @FUNCTION: cmake-utils_use_build
 # @USAGE: <USE flag> [flag name]
@@ -353,7 +298,7 @@
 #
 # `cmake-utils_use_build foo FOO` echoes -DBUILD_FOO=ON if foo is enabled
 # and -DBUILD_FOO=OFF if it is disabled.
-cmake-utils_use_build() { _cmake_use_me_now BUILD_ "$@" ; }
+cmake-utils_use_build() { _use_me_now BUILD_ "$@" ; }
 
 # @FUNCTION: cmake-utils_use_has
 # @USAGE: <USE flag> [flag name]
@@ -362,7 +307,7 @@
 #
 # `cmake-utils_use_has foo FOO` echoes -DHAVE_FOO=ON if foo is enabled
 # and -DHAVE_FOO=OFF if it is disabled.
-cmake-utils_use_has() { _cmake_use_me_now HAVE_ "$@" ; }
+cmake-utils_use_has() { _use_me_now HAVE_ "$@" ; }
 
 # @FUNCTION: cmake-utils_use_use
 # @USAGE: <USE flag> [flag name]
@@ -371,7 +316,7 @@
 #
 # `cmake-utils_use_use foo FOO` echoes -DUSE_FOO=ON if foo is enabled
 # and -DUSE_FOO=OFF if it is disabled.
-cmake-utils_use_use() { _cmake_use_me_now USE_ "$@" ; }
+cmake-utils_use_use() { _use_me_now USE_ "$@" ; }
 
 # @FUNCTION: cmake-utils_use
 # @USAGE: <USE flag> [flag name]
@@ -380,20 +325,11 @@
 #
 # `cmake-utils_use foo FOO` echoes -DFOO=ON if foo is enabled
 # and -DFOO=OFF if it is disabled.
-cmake-utils_use() { _cmake_use_me_now "" "$@" ; }
-
-# @FUNCTION: cmake-utils_useno
-# @USAGE: <USE flag> [flag name]
-# @DESCRIPTION:
-# Based on use_enable. See ebuild(5).
-#
-# `cmake-utils_useno foo NOFOO` echoes -DNOFOO=OFF if foo is enabled
-# and -DNOFOO=ON if it is disabled.
-cmake-utils_useno() { _cmake_use_me_now_inverted "" "$@" ; }
+cmake-utils_use() { _use_me_now "" "$@" ; }
 
 # Internal function for modifying hardcoded definitions.
 # Removes dangerous definitions that override Gentoo settings.
-_cmake_modify-cmakelists() {
+_modify-cmakelists() {
 	debug-print-function ${FUNCNAME} "$@"
 
 	# Only edit the files once
@@ -409,7 +345,7 @@
 		|| die "${LINENO}: failed to disable hardcoded settings"
 
 	# NOTE Append some useful summary here
-	cat >> "${CMAKE_USE_DIR}"/CMakeLists.txt <<- _EOF_ || die
+	cat >> "${CMAKE_USE_DIR}"/CMakeLists.txt <<- _EOF_
 
 		MESSAGE(STATUS "<<< Gentoo configuration >>>
 		Build type      \${CMAKE_BUILD_TYPE}
@@ -424,47 +360,19 @@
 	_EOF_
 }
 
-# temporary function for moving cmake cleanups from from src_configure -> src_prepare.
-# bug #378850
-_cmake_cleanup_cmake() {
-	: ${CMAKE_USE_DIR:=${S}}
-
-	if [[ "${CMAKE_REMOVE_MODULES}" == "yes" ]] ; then
-		local name
-		for name in ${CMAKE_REMOVE_MODULES_LIST} ; do
-			find "${S}" -name ${name}.cmake -exec rm -v {} + || die
-		done
-	fi
-
-	# check if CMakeLists.txt exist and if no then die
-	if [[ ! -e ${CMAKE_USE_DIR}/CMakeLists.txt ]] ; then
-		eerror "Unable to locate CMakeLists.txt under:"
-		eerror "\"${CMAKE_USE_DIR}/CMakeLists.txt\""
-		eerror "Consider not inheriting the cmake eclass."
-		die "FATAL: Unable to find CMakeLists.txt"
-	fi
-
-	# Remove dangerous things.
-	_cmake_modify-cmakelists
-}
-
 enable_cmake-utils_src_prepare() {
 	debug-print-function ${FUNCNAME} "$@"
 
-	pushd "${S}" > /dev/null || die
+    debug-print "$FUNCNAME: PATCHES=$PATCHES"
 
-	if [[ ${EAPI} != [2345] ]]; then
-		default_src_prepare
-		_cmake_cleanup_cmake
-	else
-		debug-print "$FUNCNAME: PATCHES=$PATCHES"
-		[[ ${PATCHES[@]} ]] && epatch "${PATCHES[@]}"
+    pushd "${S}" > /dev/null
+    [[ ${PATCHES[@]} ]] && epatch "${PATCHES[@]}"
+		
+	debug-print "$FUNCNAME: applying user patches"
+    epatch_user
 
-		debug-print "$FUNCNAME: applying user patches"
-		epatch_user
-	fi
+    popd > /dev/null
 
-	popd > /dev/null || die
 }
 
 # @VARIABLE: mycmakeargs
@@ -477,94 +385,61 @@
 # 	local mycmakeargs=(
 # 		$(cmake-utils_use_with openconnect)
 # 	)
-#
 # 	cmake-utils_src_configure
 # }
-# @CODE
+
 
 enable_cmake-utils_src_configure() {
 	debug-print-function ${FUNCNAME} "$@"
 
-	[[ ${EAPI} == [2345] ]] && _cmake_cleanup_cmake
+	[[ "${CMAKE_REMOVE_MODULES}" == "yes" ]] && {
+		local name
+		for name in ${CMAKE_REMOVE_MODULES_LIST} ; do
+			find "${S}" -name ${name}.cmake -exec rm -v {} +
+		done
+	}
 
-	_cmake_check_build_dir
+	_check_build_dir
+
+	# check if CMakeLists.txt exist and if no then die
+	if [[ ! -e ${CMAKE_USE_DIR}/CMakeLists.txt ]] ; then
+		eerror "Unable to locate CMakeLists.txt under:"
+		eerror "\"${CMAKE_USE_DIR}/CMakeLists.txt\""
+		eerror "Consider not inheriting the cmake eclass."
+		die "FATAL: Unable to find CMakeLists.txt"
+	fi
+
+	# Remove dangerous things.
+	_modify-cmakelists
 
 	# Fix xdg collision with sandbox
-	local -x XDG_CONFIG_HOME="${T}"
+	export XDG_CONFIG_HOME="${T}"
 
 	# @SEE CMAKE_BUILD_TYPE
 	if [[ ${CMAKE_BUILD_TYPE} = Gentoo ]]; then
 		# Handle release builds
 		if ! has debug ${IUSE//+} || ! use debug; then
-			local CPPFLAGS=${CPPFLAGS}
 			append-cppflags -DNDEBUG
 		fi
 	fi
 
 	# Prepare Gentoo override rules (set valid compiler, append CPPFLAGS etc.)
 	local build_rules=${BUILD_DIR}/gentoo_rules.cmake
-	# Since cmake-3.4.0_rc1 "<FLAGS>" no longer contains includes and thus
-	# we need to add "<INCLUDES>"
-	local includes=
-	if [[ ${PN} == cmake ]] ; then
-		if $(version_is_at_least 3.4.0 $(get_version_component_range 1-3 ${PV})) ; then
-			includes="<INCLUDES>"
-		fi
-	elif ROOT=/ has_version \>=dev-util/cmake-3.4.0_rc1 ; then
-		includes="<INCLUDES>"
-	fi
-	cat > "${build_rules}" <<- _EOF_ || die
+	cat > "${build_rules}" <<- _EOF_
 		SET (CMAKE_AR $(type -P $(tc-getAR)) CACHE FILEPATH "Archive manager" FORCE)
-		SET (CMAKE_ASM_COMPILE_OBJECT "<CMAKE_C_COMPILER> <DEFINES> ${includes} ${CFLAGS} <FLAGS> -o <OBJECT> -c <SOURCE>" CACHE STRING "ASM compile command" FORCE)
-		SET (CMAKE_C_COMPILE_OBJECT "<CMAKE_C_COMPILER> <DEFINES> ${includes} ${CPPFLAGS} <FLAGS> -o <OBJECT> -c <SOURCE>" CACHE STRING "C compile command" FORCE)
-		SET (CMAKE_CXX_COMPILE_OBJECT "<CMAKE_CXX_COMPILER> <DEFINES> ${includes} ${CPPFLAGS} <FLAGS> -o <OBJECT> -c <SOURCE>" CACHE STRING "C++ compile command" FORCE)
-		SET (CMAKE_Fortran_COMPILE_OBJECT "<CMAKE_Fortran_COMPILER> <DEFINES> ${includes} ${FCFLAGS} <FLAGS> -o <OBJECT> -c <SOURCE>" CACHE STRING "Fortran compile command" FORCE)
+		SET (CMAKE_ASM_COMPILE_OBJECT "<CMAKE_C_COMPILER> <DEFINES> ${CFLAGS} <FLAGS> -o <OBJECT> -c <SOURCE>" CACHE STRING "ASM compile command" FORCE)
+		SET (CMAKE_C_COMPILER $(type -P $(tc-getCC)) CACHE FILEPATH "C compiler" FORCE)
+		SET (CMAKE_C_COMPILE_OBJECT "<CMAKE_C_COMPILER> <DEFINES> ${CPPFLAGS} <FLAGS> -o <OBJECT> -c <SOURCE>" CACHE STRING "C compile command" FORCE)
+		SET (CMAKE_CXX_COMPILER $(type -P $(tc-getCXX)) CACHE FILEPATH "C++ compiler" FORCE)
+		SET (CMAKE_CXX_COMPILE_OBJECT "<CMAKE_CXX_COMPILER> <DEFINES> ${CPPFLAGS} <FLAGS> -o <OBJECT> -c <SOURCE>" CACHE STRING "C++ compile command" FORCE)
 		SET (CMAKE_RANLIB $(type -P $(tc-getRANLIB)) CACHE FILEPATH "Archive index generator" FORCE)
 		SET (PKG_CONFIG_EXECUTABLE $(type -P $(tc-getPKG_CONFIG)) CACHE FILEPATH "pkg-config executable" FORCE)
 	_EOF_
 
-	local toolchain_file=${BUILD_DIR}/gentoo_toolchain.cmake
-	cat > ${toolchain_file} <<- _EOF_ || die
-		SET (CMAKE_C_COMPILER $(tc-getCC))
-		SET (CMAKE_CXX_COMPILER $(tc-getCXX))
-		SET (CMAKE_Fortran_COMPILER $(tc-getFC))
-	_EOF_
-
-	if tc-is-cross-compiler; then
-		local sysname
-		case "${KERNEL:-linux}" in
-			Cygwin) sysname="CYGWIN_NT-5.1" ;;
-			HPUX) sysname="HP-UX" ;;
-			linux) sysname="Linux" ;;
-			Winnt)
-				sysname="Windows"
-				cat >> "${toolchain_file}" <<- _EOF_ || die
-					SET (CMAKE_RC_COMPILER $(tc-getRC))
-				_EOF_
-				;;
-			*) sysname="${KERNEL}" ;;
-		esac
-
-		cat >> "${toolchain_file}" <<- _EOF_ || die
-			SET (CMAKE_SYSTEM_NAME "${sysname}")
-		_EOF_
-
-		if [ "${SYSROOT:-/}" != "/" ] ; then
-			# When cross-compiling with a sysroot (e.g. with crossdev's emerge wrappers)
-			# we need to tell cmake to use libs/headers from the sysroot but programs from / only.
-			cat >> "${toolchain_file}" <<- _EOF_ || die
-				set(CMAKE_FIND_ROOT_PATH "${SYSROOT}")
-				set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
-				set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
-				set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
-			_EOF_
-		fi
-	fi
-
-	[[ ${EAPI} == 2 ]] && ! use prefix && local EPREFIX=
+	has "${EAPI:-0}" 0 1 2 && ! use prefix && EPREFIX=
 
 	if [[ ${EPREFIX} ]]; then
-		cat >> "${build_rules}" <<- _EOF_ || die
+		cat >> "${build_rules}" <<- _EOF_
 			# in Prefix we need rpath and must ensure cmake gets our default linker path
 			# right ... except for Darwin hosts
 			IF (NOT APPLE)
@@ -574,7 +449,7 @@
 
 			ELSE ()
 
-			SET(CMAKE_PREFIX_PATH "${EPREFIX}${PREFIX}" CACHE STRING "" FORCE)
+			SET(CMAKE_PREFIX_PATH "${EPREFIX}${PREFIX}" CACHE STRING ""FORCE)
 			SET(CMAKE_SKIP_BUILD_RPATH OFF CACHE BOOL "" FORCE)
 			SET(CMAKE_SKIP_RPATH OFF CACHE BOOL "" FORCE)
 			SET(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE CACHE BOOL "")
@@ -589,7 +464,7 @@
 	# Common configure parameters (invariants)
 	local common_config=${BUILD_DIR}/gentoo_common_config.cmake
 	local libdir=$(get_libdir)
-	cat > "${common_config}" <<- _EOF_ || die
+	cat > "${common_config}" <<- _EOF_
 		SET (LIB_SUFFIX ${libdir/lib} CACHE STRING "library path suffix" FORCE)
 		SET (CMAKE_INSTALL_LIBDIR ${libdir} CACHE PATH "Output directory for libraries")
 	_EOF_
@@ -598,15 +473,7 @@
 	# Convert mycmakeargs to an array, for backwards compatibility
 	# Make the array a local variable since <=portage-2.1.6.x does not
 	# support global arrays (see bug #297255).
-	local mycmakeargstype=$(declare -p mycmakeargs 2>&-)
-	if [[ "${mycmakeargstype}" != "declare -a mycmakeargs="* ]]; then
-		if [[ -n "${mycmakeargstype}" ]] ; then
-			if [[ ${EAPI} == [2345] ]]; then
-				eqawarn "Declaring mycmakeargs as a variable is deprecated. Please use an array instead."
-			else
-				die "Declaring mycmakeargs as a variable is banned in EAPI=${EAPI}. Please use an array instead."
-			fi
-		fi
+	if [[ $(declare -p mycmakeargs 2>&-) != "declare -a mycmakeargs="* ]]; then
 		local mycmakeargs_local=(${mycmakeargs})
 	else
 		local mycmakeargs_local=("${mycmakeargs[@]}")
@@ -624,104 +491,58 @@
 	local cmakeargs=(
 		${warn_unused_cli}
 		-C "${common_config}"
-		-G "$(_cmake_generator_to_use)"
+		-G "$(_generator_to_use)"
 		-DCMAKE_INSTALL_PREFIX="${EPREFIX}${PREFIX}"
 		"${mycmakeargs_local[@]}"
 		-DCMAKE_BUILD_TYPE="${CMAKE_BUILD_TYPE}"
 		-DCMAKE_INSTALL_DO_STRIP=OFF
 		-DCMAKE_USER_MAKE_RULES_OVERRIDE="${build_rules}"
-		-DCMAKE_TOOLCHAIN_FILE="${toolchain_file}"
 		"${MYCMAKEARGS}"
 	)
 
-	if [[ -n "${CMAKE_EXTRA_CACHE_FILE}" ]] ; then
-		cmakeargs+=( -C "${CMAKE_EXTRA_CACHE_FILE}" )
-	fi
-
-	pushd "${BUILD_DIR}" > /dev/null || die
+	pushd "${BUILD_DIR}" > /dev/null
 	debug-print "${LINENO} ${ECLASS} ${FUNCNAME}: mycmakeargs is ${mycmakeargs_local[*]}"
 	echo "${CMAKE_BINARY}" "${cmakeargs[@]}" "${CMAKE_USE_DIR}"
 	"${CMAKE_BINARY}" "${cmakeargs[@]}" "${CMAKE_USE_DIR}" || die "cmake failed"
-	popd > /dev/null || die
+	popd > /dev/null
 }
 
 enable_cmake-utils_src_compile() {
 	debug-print-function ${FUNCNAME} "$@"
 
+	has src_configure ${CMAKE_EXPF} || cmake-utils_src_configure
 	cmake-utils_src_make "$@"
 }
 
-_ninjaopts_from_makeopts() {
-	if [[ ${NINJAOPTS+set} == set ]]; then
-		return 0
-	fi
-	local ninjaopts=()
-	set -- ${MAKEOPTS}
-	while (( $# )); do
-		case $1 in
-			-j|-l)
-				if [[ $# -eq 1 || $2 == -* ]]; then
-					if [[ $1 == -j ]]; then
-						# absurdly high job limit
-						ninjaopts+=( $1 9999 )
-					else # -l
-						# remove load limit (like make does for -l)
-						ninjaopts+=( $1 0 )
-					fi
-					shift 1
-				else
-					ninjaopts+=( $1 $2 )
-					shift 2
-				fi
-				;;
-			-j*|-l*)
-				ninjaopts+=( $1 )
-				shift 1
-				;;
-			-k)
-				# -k 0 = any number of tasks can fail
-				ninjaopts+=( $1 0 )
-				shift 1
-				;;
-			*) shift ;;
-		esac
-	done
-	export NINJAOPTS="${ninjaopts[*]}"
-}
-
-# @FUNCTION: _cmake_ninja_src_make
+# @FUNCTION: ninja_src_make
 # @INTERNAL
 # @DESCRIPTION:
 # Build the package using ninja generator
-_cmake_ninja_src_make() {
+ninja_src_make() {
 	debug-print-function ${FUNCNAME} "$@"
 
-	[[ -e build.ninja ]] || die "build.ninja not found. Error during configure stage."
+		[[ -e build.ninja ]] || die "Makefile not found. Error during configure stage."
 
-	_ninjaopts_from_makeopts
-
-	if [[ "${CMAKE_VERBOSE}" != "OFF" ]]; then
-		set -- ninja ${NINJAOPTS} -v "$@"
+		if [[ "${CMAKE_VERBOSE}" != "OFF" ]]; then
+		# TODO: get load average from portage (-l option)
+		ninja ${MAKEOPTS} -v "$@" || die
 	else
-		set -- ninja ${NINJAOPTS} "$@"
+		ninja "$@" || die
 	fi
-
-	echo "$@"
-	"$@" || die
 }
 
-# @FUNCTION: _cmake_emake_src_make
+# @FUNCTION: emake_src_make
 # @INTERNAL
 # @DESCRIPTION:
 # Build the package using make generator
-_cmake_emake_src_make() {
+emake_src_make() {
 	debug-print-function ${FUNCNAME} "$@"
 
-	[[ -e Makefile ]] || die "Makefile not found. Error during configure stage."
+		[[ -e Makefile ]] || die "Makefile not found. Error during configure stage."
 
-	if [[ "${CMAKE_VERBOSE}" != "OFF" ]]; then
+		if [[ "${CMAKE_VERBOSE}" != "OFF" ]]; then
 		emake VERBOSE=1 "$@" || die
-	else
+		else
 		emake "$@" || die
 	fi
 
@@ -734,26 +555,62 @@
 cmake-utils_src_make() {
 	debug-print-function ${FUNCNAME} "$@"
 
-	_cmake_check_build_dir
-	pushd "${BUILD_DIR}" > /dev/null || die
+	_check_build_dir
+	pushd "${BUILD_DIR}" > /dev/null
 
-	_cmake_${CMAKE_MAKEFILE_GENERATOR}_src_make "$@"
+	${CMAKE_MAKEFILE_GENERATOR}_src_make $@
 
-	popd > /dev/null || die
+	popd > /dev/null
+}
+
+enable_cmake-utils_src_install() {
+	debug-print-function ${FUNCNAME} "$@"
+
+	_check_build_dir
+	pushd "${BUILD_DIR}" > /dev/null
+	DESTDIR="${D}" ${CMAKE_MAKEFILE_GENERATOR} install "$@" || die "died running ${CMAKE_MAKEFILE_GENERATOR} install"
+	popd > /dev/null
+
+	pushd "${S}" > /dev/null
+    #Install docs, copied from base_src_install_docs
+	local x
+
+    if [[ "$(declare -p DOCS 2>/dev/null 2>&1)" == "declare -a"* ]]; then
+        for x in "${DOCS[@]}"; do
+            debug-print "$FUNCNAME: docs: creating document from ${x}"
+            dodoc "${x}" || die "dodoc failed"
+        done
+    fi
+    if [[ "$(declare -p HTML_DOCS 2>/dev/null 2>&1)" == "declare -a"* ]]; then
+        for x in "${HTML_DOCS[@]}"; do
+            debug-print "$FUNCNAME: docs: creating html document from ${x}"
+            dohtml -r "${x}" || die "dohtml failed"
+        done
+    fi
+
+	# Backward compatibility, for non-array variables
+	if [[ -n "${DOCS}" ]] && [[ "$(declare -p DOCS 2>/dev/null 2>&1)" != "declare -a"* ]]; then
+		dodoc ${DOCS} || die "dodoc failed"
+	fi
+	if [[ -n "${HTML_DOCS}" ]] && [[ "$(declare -p HTML_DOCS 2>/dev/null 2>&1)" != "declare -a"* ]]; then
+		dohtml -r ${HTML_DOCS} || die "dohtml failed"
+	fi
+
+	popd > /dev/null
 }
 
 enable_cmake-utils_src_test() {
 	debug-print-function ${FUNCNAME} "$@"
 
-	_cmake_check_build_dir
-	pushd "${BUILD_DIR}" > /dev/null || die
+	_check_build_dir
+	pushd "${BUILD_DIR}" > /dev/null
 	[[ -e CTestTestfile.cmake ]] || { echo "No tests found. Skipping."; return 0 ; }
 
 	[[ -n ${TEST_VERBOSE} ]] && myctestargs+=( --extra-verbose --output-on-failure )
 
 	if ctest "${myctestargs[@]}" "$@" ; then
 		einfo "Tests succeeded."
-		popd > /dev/null || die
+		popd > /dev/null
 		return 0
 	else
 		if [[ -n "${CMAKE_YES_I_WANT_TO_SEE_THE_TEST_LOG}" ]] ; then
@@ -768,29 +625,16 @@
 		fi
 
 		# die might not die due to nonfatal
-		popd > /dev/null || die
+		popd > /dev/null
 		return 1
 	fi
 }
 
-enable_cmake-utils_src_install() {
-	debug-print-function ${FUNCNAME} "$@"
-
-	_cmake_check_build_dir
-	pushd "${BUILD_DIR}" > /dev/null || die
-	DESTDIR="${D}" ${CMAKE_MAKEFILE_GENERATOR} install "$@" || die "died running ${CMAKE_MAKEFILE_GENERATOR} install"
-	popd > /dev/null || die
-
-	pushd "${S}" > /dev/null || die
-	einstalldocs
-	popd > /dev/null || die
-}
-
 # @FUNCTION: cmake-utils_src_prepare
 # @DESCRIPTION:
 # Apply ebuild and user patches.
 cmake-utils_src_prepare() {
-	_cmake_execute_optionally "src_prepare" "$@"
+	_execute_optionaly "src_prepare" "$@"
 }
 
 # @FUNCTION: cmake-utils_src_configure
@@ -798,33 +642,34 @@
 # General function for configuring with cmake. Default behaviour is to start an
 # out-of-source build.
 cmake-utils_src_configure() {
-	_cmake_execute_optionally "src_configure" "$@"
+	_execute_optionaly "src_configure" "$@"
 }
 
 # @FUNCTION: cmake-utils_src_compile
 # @DESCRIPTION:
-# General function for compiling with cmake.
+# General function for compiling with cmake. Default behaviour is to check for
+# EAPI and respectively to configure as well or just compile.
 # Automatically detects the build type. All arguments are passed to emake.
 cmake-utils_src_compile() {
-	_cmake_execute_optionally "src_compile" "$@"
-}
-
-# @FUNCTION: cmake-utils_src_test
-# @DESCRIPTION:
-# Function for testing the package. Automatically detects the build type.
-cmake-utils_src_test() {
-	_cmake_execute_optionally "src_test" "$@"
+	_execute_optionaly "src_compile" "$@"
 }
 
 # @FUNCTION: cmake-utils_src_install
 # @DESCRIPTION:
 # Function for installing the package. Automatically detects the build type.
 cmake-utils_src_install() {
-	_cmake_execute_optionally "src_install" "$@"
+	_execute_optionaly "src_install" "$@"
+}
+
+# @FUNCTION: cmake-utils_src_test
+# @DESCRIPTION:
+# Function for testing the package. Automatically detects the build type.
+cmake-utils_src_test() {
+	_execute_optionaly "src_test" "$@"
 }
 
 # Optionally executes phases based on WANT_CMAKE variable/USE flag.
-_cmake_execute_optionally() {
+_execute_optionaly() {
 	local phase="$1" ; shift
 	if [[ ${WANT_CMAKE} = always ]]; then
 		enable_cmake-utils_${phase} "$@"