public inbox for gentoo-dev@lists.gentoo.org
 help / color / mirror / Atom feed
* [gentoo-dev] [PATCH] libretro-core.eclass: An eclass to streamline the construction of Libretro core ebuilds
@ 2018-07-26 19:12 Craig Andrews
  2018-07-26 20:14 ` Michał Górny
  0 siblings, 1 reply; 2+ messages in thread
From: Craig Andrews @ 2018-07-26 19:12 UTC (permalink / raw
  To: gentoo-dev

I'm proposing the addition of a new eclass,  libretro-core.eclass, which 
I'll use when adding a number of libretro ebuilds.

The pull request which includes this eclass as well as a few ebuilds 
using it (with more to come) can be found at 
https://github.com/gentoo/gentoo/pull/9330

Thanks,
~Craig

---
  eclass/libretro-core.eclass | 168 ++++++++++++++++++++++++++++++++++++
  1 file changed, 168 insertions(+)
  create mode 100644 eclass/libretro-core.eclass

diff --git a/eclass/libretro-core.eclass b/eclass/libretro-core.eclass
new file mode 100644
index 000000000000..c82420ac98cc
--- /dev/null
+++ b/eclass/libretro-core.eclass
@@ -0,0 +1,168 @@
+# Copyright 1999-2018 Gentoo Foundation
+# Distributed under the terms of the GNU General Public License v2
+
+# @ECLASS: libretro-core.eclass
+# @MAINTAINER:
+# candrews@gentoo.org
+# @AUTHOR:
+# Cecil Curry <leycec@gmail.com>
+# Craig Andrews <candrews@gentoo.org>
+# @BLURB: An eclass to streamline the construction of Libretro core 
ebuilds
+# @DESCRIPTION:
+# The libretro eclass is designed to streamline the construction of
+# ebuilds for low-level Libretro core ebuilds.
+
+if [[ -z ${_LIBRETRO_CORE_ECLASS} ]]; then
+_LIBRETRO_CORE_ECLASS=1
+
+IUSE="debug"
+RDEPEND=" games-emulation/libretro-info"
+
+# @ECLASS-VARIABLE: LIBRETRO_CORE_NAME
+# @REQUIRED
+# @DESCRIPTION:
+# Name of this Libretro core. The libretro-core_src_install() phase 
function
+# will install the shared library 
"${S}/${LIBRETRO_CORE_NAME}_libretro.so" as a
+# Libretro core. Defaults to the name of the current package excluding 
the
+# "libretro-" prefix (e.g., "mgba" for the package "libretro-mgba").
+: ${LIBRETRO_CORE_NAME:=${PN#libretro-}}
+
+# @ECLASS-VARIABLE: LIBRETRO_COMMIT_SHA
+# @DESCRIPTION:
+# Commit SHA used for SRC_URI will die if not set in <9999 ebuilds.
+# Needs to be set before inherit.
+
+# @ECLASS-VARIABLE: LIBRETRO_REPO_NAME
+# @REQUIRED
+# @DESCRIPTION:
+# Contains the real repo name of the core formatted as 
"repouser/reponame".
+# Needs to be set before inherit. Otherwise defaults to 
"libretro/${PN}"
+: ${LIBRETRO_REPO_NAME:="libretro/libretro-${LIBRETRO_CORE_NAME}"}
+
+: ${HOMEPAGE:="https://github.com/${LIBRETRO_REPO_NAME}"}
+
+if [[ ${PV} == *9999 ]]; then
+	: ${EGIT_REPO_URI:="https://github.com/${LIBRETRO_REPO_NAME}.git"}
+	inherit git-r3
+else
+	[[ -z "${LIBRETRO_COMMIT_SHA}" ]] && die "LIBRETRO_COMMIT_SHA must be 
set before inherit."
+	S="${WORKDIR}/${LIBRETRO_REPO_NAME##*/}-${LIBRETRO_COMMIT_SHA}"
+	: 
${SRC_URI:="https://github.com/${LIBRETRO_REPO_NAME}/archive/${LIBRETRO_COMMIT_SHA}.tar.gz 
-> ${P}.tar.gz"}
+fi
+inherit flag-o-matic
+
+# @ECLASS-VARIABLE: LIBRETRO_CORE_LIB_FILE
+# @REQUIRED
+# @DESCRIPTION:
+# Absolute path of this Libretro core's shared library.
+: ${LIBRETRO_CORE_LIB_FILE:="${S}/${LIBRETRO_CORE_NAME}_libretro.so"}
+
+case "${EAPI:-0}" in
+	6)
+		EXPORT_FUNCTIONS src_unpack src_prepare src_compile src_install
+		;;
+	*)
+		die "EAPI=${EAPI} is not supported" ;;
+esac
+
+# @FUNCTION: libretro-core_src_unpack
+# @DESCRIPTION:
+# The libretro-core src_unpack function which is exported.
+#
+# This function retrieves the remote Libretro core info files.
+libretro-core_src_unpack() {
+	# If this is a live ebuild, retrieve this core's remote repository.
+	if [[ ${PV} == *9999 ]]; then
+		git-r3_src_unpack
+		# Add used commit SHA for version information, the above could also 
work.
+		LIBRETRO_COMMIT_SHA=$(git -C 
"${EGIT3_STORE_DIR}/${LIBRETRO_REPO_NAME//\//_}.git" rev-parse HEAD)
+	# Else, unpack this core's local tarball.
+	else
+		default_src_unpack
+	fi
+}
+
+# @FUNCTION: libretro-core_src_prepare
+# @DESCRIPTION:
+# The libretro-core src_prepare function which is exported.
+#
+# This function prepares the source by making custom modifications.
+libretro-core_src_prepare() {
+	local flags_modified=0
+	ebegin "Attempting to hack Makefiles to use custom-cflags"
+	for makefile in "${S}"/?akefile* "${S}"/target-libretro/?akefile*; do
+		# * Convert CRLF to LF
+		# * Expand *FLAGS to prevent potential self-references
+		# * Where LDFLAGS directly define the link version
+		#   script append LDFLAGS and LIBS
+		# * Where SHARED is used to provide shared linking
+		#   flags ensure final link command includes LDFLAGS
+		#   and LIBS
+		# * Always use $(CFLAGS) when calling $(CC)
+		sed \
+			-e 's/\r$//g' \
+			-e "/flags.*=/s/-O[[:digit:]]/${CFLAGS}/g" \
+			-e "/CFLAGS.*=/s/-O[[:digit:]]/${CFLAGS}/g" \
+			-e "/.*,--version-script=.*/s/$/ ${LDFLAGS} ${LIBS}/g" \
+			-e "/\$(CC)/s/\(\$(SHARED)\)/\1 ${LDFLAGS} ${LIBS}/" \
+			-e 's/\(\$(CC)\)/\1 \$(CFLAGS)/g' \
+			-i "${makefile}" \
+			&> /dev/null && flags_modified=1
+	done
+	[[ ${flags_modified} == 1 ]] && true || false
+	eend $?
+	export OPTFLAGS="${CFLAGS}"
+
+	# Populate COMMIT for GIT_VERSION
+	if [[ -z "${CUSTOM_LIBRETRO_COMMIT_SHA}" ]]; then
+		CUSTOM_LIBRETRO_COMMIT_SHA="\" ${LIBRETRO_COMMIT_SHA:0:7}\""
+	fi
+
+	for makefile in "${S}"/?akefile* "${S}"/target-libretro/?akefile*; do
+		# Add short-rev to Makefile
+		sed \
+			-e 
"s/GIT_VERSION\s.=.*$/GIT_VERSION=${CUSTOM_LIBRETRO_COMMIT_SHA}/g" \
+			-i "${makefile}" \
+			&> /dev/null
+	done
+	default_src_prepare
+}
+
+# @FUNCTION: libretro-core_src_compile
+# @DESCRIPTION:
+# The libretro-core src_compile function which is exported.
+#
+# This function compiles the shared library for this Libretro core.
+libretro-core_src_compile() {
+	emake CC=$(tc-getCC) CXX=$(tc-getCXX) \
+		$(usex debug "DEBUG=1" "") "${myemakeargs[@]}" \
+		$([[ -f makefile.libretro ]] && echo '-f makefile.libretro') \
+		$([[ -f Makefile.libretro ]] && echo '-f Makefile.libretro')
+}
+
+# @FUNCTION: libretro-core_src_install
+# @DESCRIPTION:
+# The libretro-core src_install function which is exported.
+#
+# This function installs the shared library for this Libretro core.
+libretro-core_src_install() {
+	# Absolute path of the directory containing Libretro shared libraries.
+	LIBRETRO_LIB_DIR="/usr/$(get_libdir)/libretro"
+	# If this core's shared library exists, install that.
+	if [[ -f "${LIBRETRO_CORE_LIB_FILE}" ]]; then
+		insinto "${LIBRETRO_LIB_DIR}"
+		doins "${LIBRETRO_CORE_LIB_FILE}"
+	else
+		# Basename of this library.
+		local lib_basename="${LIBRETRO_CORE_LIB_FILE##*/}"
+
+		# Absolute path to which this library was installed.
+		local lib_file_target="${ED}${LIBRETRO_LIB_DIR}/${lib_basename}"
+
+		# If this library was *NOT* installed, fail.
+		[[ -f "${lib_file_target}" ]] ||
+			die "Libretro core shared library \"${lib_file_target}\" not 
installed."
+	fi
+}
+
+fi
-- 
2.18.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [gentoo-dev] [PATCH] libretro-core.eclass: An eclass to streamline the construction of Libretro core ebuilds
  2018-07-26 19:12 [gentoo-dev] [PATCH] libretro-core.eclass: An eclass to streamline the construction of Libretro core ebuilds Craig Andrews
@ 2018-07-26 20:14 ` Michał Górny
  0 siblings, 0 replies; 2+ messages in thread
From: Michał Górny @ 2018-07-26 20:14 UTC (permalink / raw
  To: gentoo-dev

[-- Attachment #1: Type: text/plain, Size: 8449 bytes --]

W dniu czw, 26.07.2018 o godzinie 15∶12 -0400, użytkownik Craig Andrews
napisał:
> I'm proposing the addition of a new eclass,  libretro-core.eclass, which 
> I'll use when adding a number of libretro ebuilds.
> 
> The pull request which includes this eclass as well as a few ebuilds 
> using it (with more to come) can be found at 
> https://github.com/gentoo/gentoo/pull/9330
> 
> Thanks,
> ~Craig
> 
> ---
>   eclass/libretro-core.eclass | 168 ++++++++++++++++++++++++++++++++++++
>   1 file changed, 168 insertions(+)
>   create mode 100644 eclass/libretro-core.eclass
> 
> diff --git a/eclass/libretro-core.eclass b/eclass/libretro-core.eclass
> new file mode 100644
> index 000000000000..c82420ac98cc
> --- /dev/null
> +++ b/eclass/libretro-core.eclass
> @@ -0,0 +1,168 @@
> +# Copyright 1999-2018 Gentoo Foundation
> +# Distributed under the terms of the GNU General Public License v2
> +
> +# @ECLASS: libretro-core.eclass
> +# @MAINTAINER:
> +# candrews@gentoo.org
> +# @AUTHOR:
> +# Cecil Curry <leycec@gmail.com>
> +# Craig Andrews <candrews@gentoo.org>
> +# @BLURB: An eclass to streamline the construction of Libretro core 
> ebuilds
> +# @DESCRIPTION:
> +# The libretro eclass is designed to streamline the construction of
> +# ebuilds for low-level Libretro core ebuilds.

That's not a very helpful description.  Description should clearly let
a layman know when he should use the eclass, and when it should not be
used.  Also some generic tips, maybe an example would be helpful.

> +
> +if [[ -z ${_LIBRETRO_CORE_ECLASS} ]]; then
> +_LIBRETRO_CORE_ECLASS=1
> +
> +IUSE="debug"
> +RDEPEND=" games-emulation/libretro-info"
> +
> +# @ECLASS-VARIABLE: LIBRETRO_CORE_NAME
> +# @REQUIRED
> +# @DESCRIPTION:
> +# Name of this Libretro core. The libretro-core_src_install() phase 
> function
> +# will install the shared library 
> "${S}/${LIBRETRO_CORE_NAME}_libretro.so" as a
> +# Libretro core. Defaults to the name of the current package excluding 
> the
> +# "libretro-" prefix (e.g., "mgba" for the package "libretro-mgba").
> +: ${LIBRETRO_CORE_NAME:=${PN#libretro-}}
> +
> +# @ECLASS-VARIABLE: LIBRETRO_COMMIT_SHA
> +# @DESCRIPTION:
> +# Commit SHA used for SRC_URI will die if not set in <9999 ebuilds.
> +# Needs to be set before inherit.
> +
> +# @ECLASS-VARIABLE: LIBRETRO_REPO_NAME
> +# @REQUIRED
> +# @DESCRIPTION:
> +# Contains the real repo name of the core formatted as 
> "repouser/reponame".
> +# Needs to be set before inherit. Otherwise defaults to 
> "libretro/${PN}"
> +: ${LIBRETRO_REPO_NAME:="libretro/libretro-${LIBRETRO_CORE_NAME}"}
> +
> +: ${HOMEPAGE:="https://github.com/${LIBRETRO_REPO_NAME}"}
> +
> +if [[ ${PV} == *9999 ]]; then
> +	: ${EGIT_REPO_URI:="https://github.com/${LIBRETRO_REPO_NAME}.git"}
> +	inherit git-r3
> +else
> +	[[ -z "${LIBRETRO_COMMIT_SHA}" ]] && die "LIBRETRO_COMMIT_SHA must be 
> set before inherit."
> +	S="${WORKDIR}/${LIBRETRO_REPO_NAME##*/}-${LIBRETRO_COMMIT_SHA}"
> +	: 
> ${SRC_URI:="https://github.com/${LIBRETRO_REPO_NAME}/archive/${LIBRETRO_COMMIT_SHA}.tar.gz 
> -> ${P}.tar.gz"}
> +fi
> +inherit flag-o-matic
> +
> +# @ECLASS-VARIABLE: LIBRETRO_CORE_LIB_FILE
> +# @REQUIRED
> +# @DESCRIPTION:
> +# Absolute path of this Libretro core's shared library.
> +: ${LIBRETRO_CORE_LIB_FILE:="${S}/${LIBRETRO_CORE_NAME}_libretro.so"}
> +
> +case "${EAPI:-0}" in
> +	6)

Why no EAPI 7?

> +		EXPORT_FUNCTIONS src_unpack src_prepare src_compile src_install
> +		;;
> +	*)
> +		die "EAPI=${EAPI} is not supported" ;;
> +esac
> +
> +# @FUNCTION: libretro-core_src_unpack
> +# @DESCRIPTION:
> +# The libretro-core src_unpack function which is exported.
> +#
> +# This function retrieves the remote Libretro core info files.
> +libretro-core_src_unpack() {
> +	# If this is a live ebuild, retrieve this core's remote repository.
> +	if [[ ${PV} == *9999 ]]; then
> +		git-r3_src_unpack
> +		# Add used commit SHA for version information, the above could also 
> work.
> +		LIBRETRO_COMMIT_SHA=$(git -C 
> "${EGIT3_STORE_DIR}/${LIBRETRO_REPO_NAME//\//_}.git" rev-parse HEAD)

It's internal implementation detail.  U can't touch this.

> +	# Else, unpack this core's local tarball.
> +	else
> +		default_src_unpack
> +	fi
> +}
> +
> +# @FUNCTION: libretro-core_src_prepare
> +# @DESCRIPTION:
> +# The libretro-core src_prepare function which is exported.
> +#
> +# This function prepares the source by making custom modifications.
> +libretro-core_src_prepare() {
> +	local flags_modified=0
> +	ebegin "Attempting to hack Makefiles to use custom-cflags"
> +	for makefile in "${S}"/?akefile* "${S}"/target-libretro/?akefile*; do

Missing local for 'makefile'.

Do you expect names other than 'makefile*' and 'Makefile*'?  Because
this '?' looks like a ticking bomb.

> +		# * Convert CRLF to LF
> +		# * Expand *FLAGS to prevent potential self-references
> +		# * Where LDFLAGS directly define the link version
> +		#   script append LDFLAGS and LIBS
> +		# * Where SHARED is used to provide shared linking
> +		#   flags ensure final link command includes LDFLAGS
> +		#   and LIBS
> +		# * Always use $(CFLAGS) when calling $(CC)
> +		sed \
> +			-e 's/\r$//g' \
> +			-e "/flags.*=/s/-O[[:digit:]]/${CFLAGS}/g" \
> +			-e "/CFLAGS.*=/s/-O[[:digit:]]/${CFLAGS}/g" \
> +			-e "/.*,--version-script=.*/s/$/ ${LDFLAGS} ${LIBS}/g" \
> +			-e "/\$(CC)/s/\(\$(SHARED)\)/\1 ${LDFLAGS} ${LIBS}/" \

Don't inline ${CFLAGS} etc. in sed expressions.  This will fail hard
when they contain a slash (which is entirely possible e.g. due to -L
with a path).

> +			-e 's/\(\$(CC)\)/\1 \$(CFLAGS)/g' \
> +			-i "${makefile}" \
> +			&> /dev/null && flags_modified=1

Don't ignore the output and don't ignore the errors.  Make it || die.

> +	done
> +	[[ ${flags_modified} == 1 ]] && true || false

'&& true || false' is completely redundant here.  Not that there's any
reason to keep this thing.

> +	eend $?
> +	export OPTFLAGS="${CFLAGS}"
> +
> +	# Populate COMMIT for GIT_VERSION
> +	if [[ -z "${CUSTOM_LIBRETRO_COMMIT_SHA}" ]]; then

When can it be empty?  FWICS you're requiring it for release,
and setting it for -9999.

> +		CUSTOM_LIBRETRO_COMMIT_SHA="\" ${LIBRETRO_COMMIT_SHA:0:7}\""
> +	fi
> +
> +	for makefile in "${S}"/?akefile* "${S}"/target-libretro/?akefile*; do

Why not combine the two loops and use a single 'sed' call?

> +		# Add short-rev to Makefile
> +		sed \
> +			-e 
> "s/GIT_VERSION\s.=.*$/GIT_VERSION=${CUSTOM_LIBRETRO_COMMIT_SHA}/g" \
> +			-i "${makefile}" \
> +			&> /dev/null

Same as above sed.

> +	done
> +	default_src_prepare
> +}
> +
> +# @FUNCTION: libretro-core_src_compile
> +# @DESCRIPTION:
> +# The libretro-core src_compile function which is exported.
> +#
> +# This function compiles the shared library for this Libretro core.
> +libretro-core_src_compile() {
> +	emake CC=$(tc-getCC) CXX=$(tc-getCXX) \
> +		$(usex debug "DEBUG=1" "") "${myemakeargs[@]}" \

What does DEBUG=1 do?

myemakeargs is not documented.

> +		$([[ -f makefile.libretro ]] && echo '-f makefile.libretro') \
> +		$([[ -f Makefile.libretro ]] && echo '-f Makefile.libretro')
> +}
> +
> +# @FUNCTION: libretro-core_src_install
> +# @DESCRIPTION:
> +# The libretro-core src_install function which is exported.
> +#
> +# This function installs the shared library for this Libretro core.
> +libretro-core_src_install() {
> +	# Absolute path of the directory containing Libretro shared libraries.
> +	LIBRETRO_LIB_DIR="/usr/$(get_libdir)/libretro"

Why are you setting a global variable?

> +	# If this core's shared library exists, install that.
> +	if [[ -f "${LIBRETRO_CORE_LIB_FILE}" ]]; then
> +		insinto "${LIBRETRO_LIB_DIR}"
> +		doins "${LIBRETRO_CORE_LIB_FILE}"

Shared libraries should have +x, so doexe here.

> +	else
> +		# Basename of this library.
> +		local lib_basename="${LIBRETRO_CORE_LIB_FILE##*/}"
> +
> +		# Absolute path to which this library was installed.
> +		local lib_file_target="${ED}${LIBRETRO_LIB_DIR}/${lib_basename}"
> +
> +		# If this library was *NOT* installed, fail.
> +		[[ -f "${lib_file_target}" ]] ||
> +			die "Libretro core shared library \"${lib_file_target}\" not 
> installed."

What use case does this whole block cover?

> +	fi
> +}
> +
> +fi

-- 
Best regards,
Michał Górny

[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 963 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2018-07-26 20:14 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-07-26 19:12 [gentoo-dev] [PATCH] libretro-core.eclass: An eclass to streamline the construction of Libretro core ebuilds Craig Andrews
2018-07-26 20:14 ` Michał Górny

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox