From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by finch.gentoo.org (Postfix) with ESMTPS id 4389115808E for ; Sun, 24 Apr 2022 12:52:32 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 54755E075F; Sun, 24 Apr 2022 12:52:31 +0000 (UTC) Received: from smtp.gentoo.org (woodpecker.gentoo.org [140.211.166.183]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id 101DBE075F for ; Sun, 24 Apr 2022 12:52:31 +0000 (UTC) Received: from oystercatcher.gentoo.org (oystercatcher.gentoo.org [148.251.78.52]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id DC38E341886 for ; Sun, 24 Apr 2022 12:52:29 +0000 (UTC) Received: from localhost.localdomain (localhost [IPv6:::1]) by oystercatcher.gentoo.org (Postfix) with ESMTP id 69B1C2F9 for ; Sun, 24 Apr 2022 12:52:28 +0000 (UTC) From: "Ulrich Müller" To: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: 8bit Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "Ulrich Müller" Message-ID: <1650804713.be55691ed16c2ab4dd7c3d635fc2bd67ecd6b563.ulm@gentoo> Subject: [gentoo-commits] proj/devmanual:master commit in: eclass-writing/ X-VCS-Repository: proj/devmanual X-VCS-Files: eclass-writing/text.xml X-VCS-Directories: eclass-writing/ X-VCS-Committer: ulm X-VCS-Committer-Name: Ulrich Müller X-VCS-Revision: be55691ed16c2ab4dd7c3d635fc2bd67ecd6b563 X-VCS-Branch: master Date: Sun, 24 Apr 2022 12:52:28 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org X-Auto-Response-Suppress: DR, RN, NRN, OOF, AutoReply X-Archives-Salt: a6a77193-811f-4246-8205-c5b173dfea70 X-Archives-Hash: a4b97bcff722dad2ae900ca9174a7198 commit: be55691ed16c2ab4dd7c3d635fc2bd67ecd6b563 Author: Thomas Bracht Laumann Jespersen laumann xyz> AuthorDate: Fri Apr 15 21:42:03 2022 +0000 Commit: Ulrich Müller gentoo org> CommitDate: Sun Apr 24 12:51:53 2022 +0000 URL: https://gitweb.gentoo.org/proj/devmanual.git/commit/?id=be55691e eclass-writing: doc inherit guards, EXPORT_FUNCTIONS warning Add a section to explain what inherit guards are and their usage, and add a "rule of thumb" to put EXPORT_FUNCTIONS at the end of eclasses for max portability. This turned into a larger re-ordering of the sections so the full jmake example can showcase all the features discussed in the preceding sections, like EAPI guard, inherit guard, and EXPORT_FUNCTIONS at the very end. Signed-off-by: Thomas Bracht Laumann Jespersen laumann.xyz> Signed-off-by: Ulrich Müller gentoo.org> eclass-writing/text.xml | 156 +++++++++++++++++++++++++++++++++++++----------- 1 file changed, 122 insertions(+), 34 deletions(-) diff --git a/eclass-writing/text.xml b/eclass-writing/text.xml index f0f03c0..22b973f 100644 --- a/eclass-writing/text.xml +++ b/eclass-writing/text.xml @@ -638,11 +638,11 @@ eclass-defined defaults for example, say we had fnord.eclass:

-EXPORT_FUNCTIONS src_compile - fnord_src_compile() { do_stuff || die } + +EXPORT_FUNCTIONS src_compile

@@ -659,6 +659,115 @@ src_compile() { } +

+Eclasses may inherit other eclasses to make use of their functionality, and +historically there have been instances of eclasses calling +EXPORT_FUNCTIONS and then inheriting another eclass. As inherited +eclasses may also execute EXPORT_FUNCTIONS, it was not fully defined +which defaults should take effect. The general recommendation is now that +eclasses should not inherit other eclasses after calling +EXPORT_FUNCTIONS. +

+ + + + +
+Inherit guards + + +

+It is common practice to surround the main contents of an eclass with an +"inherit guard". Much like header guards known from C, inherit guards help +ensure that an eclass can be inherited multiple times and have its functions and +variables defined only once. An inherit guard is only needed for an eclass that +can be inherited more than once. +

+ +

+A typical inherit guard looks as follows (for a hypothetical foo.eclass): +

+ + +if [[ -z ${_FOO_ECLASS} ]]; then +_FOO_ECLASS=1 + +# Variables and functions go here + +fi + + +

+When it comes to EXPORT_FUNCTIONS and inherit guards, the call to +EXPORT_FUNCTIONS must be placed at the very end of the eclass +outside any inherit guards, like this: +

+ + +if [[ -z ${_FOO_ECLASS} ]]; then +_FOO_ECLASS=1 + +foo_src_compile() { + ... +} +fi + +EXPORT_FUNCTIONS src_compile + + +

+This helps to ensure that the last inherited eclass gets to define the default +phase functions. Consider two eclasses foo.eclass and bar.eclass +that define the same default phase function via EXPORT_FUNCTIONS. If an +ebuild inherits both as inherit foo bar, then the default phases are +defined by bar.eclass. If foo.eclass is then modified to inherit +bar as well, then the ebuild's default functions could suddenly become +those from foo if EXPORT_FUNCTIONS was placed inside the inherit +guard. +

+ + +The rule of thumb here is: put the call (if any) to EXPORT_FUNCTIONS in +the last line of an eclass, outside of any inherit guards. + + + +Old eclasses may put EXPORT_FUNCTIONS in other places, even before +inherit. They should not blindly be updated to follow the +recommended pattern here, as it could result in significant breakage. + + + +
+ +
+Handling incorrect usage of an eclass + + +

+Sometimes an eclass is used incorrectly by an ebuild and the eclass +knows it is being used incorrectly the common example is an +eclass that only works with a specific set of EAPIs, but is being +accessed (inherited) by an ebuild with a different EAPI. +In those cases, used sparingly as a last resort, it is allowed +for an eclass to invoke die from the global scope. For example: +

+ + +# Copyright 1999-2022 Gentoo Authors +# Distributed under the terms of the GNU General Public License v2 + +# @ECLASS: eapi-die.eclass +# @MAINTAINER: +# Gentoo Devmanual Project <devmanual@gentoo.org> +# @SUPPORTED_EAPIS: 7 8 +# @BLURB: Calls die when used with an invalid EAPI. + +case ${EAPI} in + 7|8) ;; + *) die "${ECLASS}: EAPI ${EAPI:-0} not supported" ;; +esac +
@@ -674,7 +783,7 @@ functions.

-# Copyright 1999-2021 Gentoo Authors +# Copyright 1999-2022 Gentoo Authors # Distributed under the terms of the GNU General Public License v2 # @ECLASS: jmake.eclass @@ -688,7 +797,13 @@ functions. # (hypothetical) jmake build system along with default src_configure and # src_compile phase functions -EXPORT_FUNCTIONS src_configure src_compile +case ${EAPI} in + 7|8) ;; + *) die "${ECLASS}: EAPI ${EAPI:-0} not supported" ;; +esac + +if [[ -z ${_JMAKE_ECLASS} ]]; then +_JMAKE_ECLASS=1 BDEPEND=">=sys-devel/jmake-2" @@ -725,40 +840,13 @@ jmake-build() { jmake_src_compile() { jmake-build || die "build failed" } +fi + +EXPORT_FUNCTIONS src_configure src_compile -
-Handling incorrect usage of an eclass - - -

-Sometimes an eclass is used incorrectly by an ebuild and the eclass -knows it is being used incorrectly the common example is an -eclass that only works with a specific set of EAPIs, but is being -accessed (inherited) by an ebuild with a different EAPI. -In those cases, used sparingly as a last resort, it is allowed -for an eclass to invoke die from the global scope. For example: -

- - -# Copyright 1999-2021 Gentoo Authors -# Distributed under the terms of the GNU General Public License v2 - -# @ECLASS: eapi-die.eclass -# @MAINTAINER: -# Gentoo Devmanual Project <devmanual@gentoo.org> -# @SUPPORTED_EAPIS: 7 8 -# @BLURB: Calls die when used with an invalid EAPI. - -case ${EAPI} in - 7|8) ;; - *) die "${ECLASS}: EAPI ${EAPI:-0} not supported" ;; -esac - - -