1 # Copyright 2018-2020 Gentoo Authors
2 # Distributed under the terms of the GNU General Public License v2
4 # @ECLASS: libretro-core.eclass
8 # Cecil Curry <leycec@gmail.com>
9 # Craig Andrews <candrews@gentoo.org>
10 # @SUPPORTED_EAPIS: 6 7
11 # @BLURB: Simplify libretro core ebuilds
13 # The libretro eclass is designed to streamline the construction of
14 # ebuilds for Libretro core ebuilds.
16 # Libretro cores can be found under https://github.com/libretro/
18 # They all use the same basic make based build system, are located
19 # in the same github account, and do not release named or numbered
20 # versions (so ebuild versions for git commits are keys).
21 # This eclass covers those commonalities reducing much duplication
22 # between the ebuilds.
27 # LIBRETRO_CORE_NAME="2048"
28 # LIBRETRO_COMMIT_SHA="45655d3662e4cbcd8afb28e2ee3f5494a75888de"
29 # KEYWORDS="~amd64 ~x86"
30 # inherit libretro-core
32 # DESCRIPTION="Port of 2048 puzzle game to the libretro API"
37 if [[ -z ${_LIBRETRO_CORE_ECLASS} ]]; then
38 _LIBRETRO_CORE_ECLASS=1
42 # @ECLASS-VARIABLE: LIBRETRO_CORE_NAME
45 # Name of this Libretro core. The libretro-core_src_install() phase function
46 # will install the shared library "${S}/${LIBRETRO_CORE_NAME}_libretro.so" as a
47 # Libretro core. Defaults to the name of the current package with the
48 # "libretro-" prefix excluded and hyphens replaced with underscores
49 # (e.g. genesis_plus_gx for libretro-genesis-plus-gx)
50 if [[ -z "${LIBRETRO_CORE_NAME}" ]]; then
51 LIBRETRO_CORE_NAME=${PN#libretro-}
52 LIBRETRO_CORE_NAME=${LIBRETRO_CORE_NAME//-/_}
55 # @ECLASS-VARIABLE: LIBRETRO_COMMIT_SHA
57 # Commit SHA used for SRC_URI will die if not set in <9999 ebuilds.
58 # Needs to be set before inherit.
60 # @ECLASS-VARIABLE: LIBRETRO_REPO_NAME
63 # Contains the real repo name of the core formatted as "repouser/reponame".
64 # Needs to be set before inherit. Otherwise defaults to "libretro/${PN}"
65 : ${LIBRETRO_REPO_NAME:="libretro/libretro-${LIBRETRO_CORE_NAME}"}
67 : ${HOMEPAGE:="https://github.com/${LIBRETRO_REPO_NAME}"}
69 if [[ ${PV} == *9999 ]]; then
70 : ${EGIT_REPO_URI:="https://github.com/${LIBRETRO_REPO_NAME}.git"}
73 [[ -z "${LIBRETRO_COMMIT_SHA}" ]] && die "LIBRETRO_COMMIT_SHA must be set before inherit."
74 S="${WORKDIR}/${LIBRETRO_REPO_NAME##*/}-${LIBRETRO_COMMIT_SHA}"
75 : ${SRC_URI:="https://github.com/${LIBRETRO_REPO_NAME}/archive/${LIBRETRO_COMMIT_SHA}.tar.gz -> ${P}.tar.gz"}
77 inherit flag-o-matic toolchain-funcs
81 EXPORT_FUNCTIONS src_unpack src_prepare src_compile src_install
84 die "EAPI=${EAPI} is not supported" ;;
87 # @FUNCTION: libretro-core_src_unpack
89 # The libretro-core src_unpack function which is exported.
91 # This function retrieves the remote Libretro core info files.
92 libretro-core_src_unpack() {
93 # If this is a live ebuild, retrieve this core's remote repository.
94 if [[ ${PV} == *9999 ]]; then
96 # Add used commit SHA for version information, the above could also work.
97 LIBRETRO_COMMIT_SHA=$(git -C "${WORKDIR}/${P}" rev-parse HEAD)
98 # Else, unpack this core's local tarball.
104 # @FUNCTION: libretro-core_src_prepare
106 # The libretro-core src_prepare function which is exported.
108 # This function prepares the source by making custom modifications.
109 libretro-core_src_prepare() {
111 # Populate COMMIT for GIT_VERSION
112 local custom_libretro_commit_sha="\" ${LIBRETRO_COMMIT_SHA:0:7}\""
114 local flags_modified=0
115 local shopt_saved=$(shopt -p nullglob)
117 for makefile in "${S}"/[Mm]akefile* "${S}"/target-libretro/[Mm]akefile*; do
118 # * Convert CRLF to LF
119 # * Expand *FLAGS to prevent potential self-references
120 # * Where LDFLAGS directly define the link version
121 # script append LDFLAGS and LIBS
122 # * Where SHARED is used to provide shared linking
123 # flags ensure final link command includes LDFLAGS
125 # * Always use $(CFLAGS) when calling $(CC)
126 # * Add short-rev to Makefile
129 -e "/flags.*=/s|-O[[:digit:]]|${CFLAGS}|g" \
130 -e "/CFLAGS.*=/s|-O[[:digit:]]|${CFLAGS}|g" \
131 -e "/CXXFLAGS.*=/s|-O[[:digit:]]|${CXXFLAGS}|g" \
132 -e "/.*,--version-script=.*/s|$| ${LDFLAGS} ${LIBS}|g" \
133 -e "/\$(CC)/s|\(\$(SHARED)\)|\1 ${LDFLAGS} ${LIBS}|" \
134 -e 's|\(\$(CC)\)|\1 \$(CFLAGS)|g' \
135 -e "s/GIT_VERSION\s.=.*$/GIT_VERSION=${custom_libretro_commit_sha}/g" \
136 -i "${makefile}" || die "Failed to use custom cflags in ${makefile}"
139 export OPTFLAGS="${CFLAGS}"
142 # @VARIABLE: myemakeargs
145 # Optional emake arguments as a bash array. Should be defined before calling
149 # local myemakeargs=(
150 # $(usex neon "HAVE_NEON=1" "")
152 # libretro-core_src_compile
156 # @FUNCTION: libretro-core_src_compile
158 # The libretro-core src_compile function which is exported.
160 # This function compiles the shared library for this Libretro core.
161 libretro-core_src_compile() {
162 # most (if not all) libretro makefiles use DEBUG=1
163 # to enable additional debug features.
164 emake CC=$(tc-getCC) CXX=$(tc-getCXX) \
165 $(usex debug "DEBUG=1" "") "${myemakeargs[@]}" \
166 $([[ -f makefile.libretro ]] && echo '-f makefile.libretro') \
167 $([[ -f Makefile.libretro ]] && echo '-f Makefile.libretro')
170 # @VARIABLE: LIBRETRO_CORE_LIB_FILE
173 # Absolute path of this Libretro core's shared library.
177 # local LIBRETRO_CORE_LIB_FILE="${S}/somecore_libretro.so"
179 # libretro-core_src_install
183 # @FUNCTION: libretro-core_src_install
185 # The libretro-core src_install function which is exported.
187 # This function installs the shared library for this Libretro core.
188 libretro-core_src_install() {
189 local LIBRETRO_CORE_LIB_FILE=${LIBRETRO_CORE_LIB_FILE:-"${S}/${LIBRETRO_CORE_NAME}_libretro.so"}
191 # Absolute path of the directory containing Libretro shared libraries.
192 local libretro_lib_dir="/usr/$(get_libdir)/libretro"
193 # If this core's shared library exists, install that.
194 if [[ -f "${LIBRETRO_CORE_LIB_FILE}" ]]; then
195 exeinto "${libretro_lib_dir}"
196 doexe "${LIBRETRO_CORE_LIB_FILE}"
198 # Basename of this library.
199 local lib_basename="${LIBRETRO_CORE_LIB_FILE##*/}"
201 # Absolute path to which this library was installed.
202 local lib_file_target="${ED}${libretro_lib_dir}/${lib_basename}"
204 # If this library was *NOT* installed, fail.
205 [[ -f "${lib_file_target}" ]] ||
206 die "Libretro core shared library \"${lib_file_target}\" not installed."
210 fi # end _LIBRETRO_CORE_ECLASS guard