llvm.eclass: Add initial tests
[gentoo.git] / eclass / libretro-core.eclass
1 # Copyright 2018-2020 Gentoo Authors
2 # Distributed under the terms of the GNU General Public License v2
3
4 # @ECLASS: libretro-core.eclass
5 # @MAINTAINER:
6 # candrews@gentoo.org
7 # @AUTHOR:
8 # Cecil Curry <leycec@gmail.com>
9 # Craig Andrews <candrews@gentoo.org>
10 # @SUPPORTED_EAPIS: 6 7
11 # @BLURB: Simplify libretro core ebuilds
12 # @DESCRIPTION:
13 # The libretro eclass is designed to streamline the construction of
14 # ebuilds for Libretro core ebuilds.
15 #
16 # Libretro cores can be found under https://github.com/libretro/
17 #
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.
23 # @EXAMPLE:
24 # @CODE
25 # EAPI=7
26 #
27 # LIBRETRO_CORE_NAME="2048"
28 # LIBRETRO_COMMIT_SHA="45655d3662e4cbcd8afb28e2ee3f5494a75888de"
29 # KEYWORDS="~amd64 ~x86"
30 # inherit libretro-core
31 #
32 # DESCRIPTION="Port of 2048 puzzle game to the libretro API"
33 # LICENSE="Unlicense"
34 # SLOT="0"
35 # @CODE
36
37 if [[ -z ${_LIBRETRO_CORE_ECLASS} ]]; then
38 _LIBRETRO_CORE_ECLASS=1
39
40 IUSE="debug"
41
42 # @ECLASS-VARIABLE: LIBRETRO_CORE_NAME
43 # @REQUIRED
44 # @DESCRIPTION:
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//-/_}
53 fi
54
55 # @ECLASS-VARIABLE: LIBRETRO_COMMIT_SHA
56 # @DESCRIPTION:
57 # Commit SHA used for SRC_URI will die if not set in <9999 ebuilds.
58 # Needs to be set before inherit.
59
60 # @ECLASS-VARIABLE: LIBRETRO_REPO_NAME
61 # @REQUIRED
62 # @DESCRIPTION:
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}"}
66
67 : ${HOMEPAGE:="https://github.com/${LIBRETRO_REPO_NAME}"}
68
69 if [[ ${PV} == *9999 ]]; then
70         : ${EGIT_REPO_URI:="https://github.com/${LIBRETRO_REPO_NAME}.git"}
71         inherit git-r3
72 else
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"}
76 fi
77 inherit flag-o-matic toolchain-funcs
78
79 case "${EAPI:-0}" in
80         6|7)
81                 EXPORT_FUNCTIONS src_unpack src_prepare src_compile src_install
82                 ;;
83         *)
84                 die "EAPI=${EAPI} is not supported" ;;
85 esac
86
87 # @FUNCTION: libretro-core_src_unpack
88 # @DESCRIPTION:
89 # The libretro-core src_unpack function which is exported.
90 #
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
95                 git-r3_src_unpack
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.
99         else
100                 default_src_unpack
101         fi
102 }
103
104 # @FUNCTION: libretro-core_src_prepare
105 # @DESCRIPTION:
106 # The libretro-core src_prepare function which is exported.
107 #
108 # This function prepares the source by making custom modifications.
109 libretro-core_src_prepare() {
110         default_src_prepare
111         # Populate COMMIT for GIT_VERSION
112         local custom_libretro_commit_sha="\" ${LIBRETRO_COMMIT_SHA:0:7}\""
113         local makefile
114         local flags_modified=0
115         local shopt_saved=$(shopt -p nullglob)
116         shopt -s 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
124                 #   and LIBS
125                 # * Always use $(CFLAGS) when calling $(CC)
126                 # * Add short-rev to Makefile
127                 sed \
128                         -e 's/\r$//g' \
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}"
137         done
138         ${shopt_saved}
139         export OPTFLAGS="${CFLAGS}"
140 }
141
142 # @VARIABLE: myemakeargs
143 # @DEFAULT_UNSET
144 # @DESCRIPTION:
145 # Optional emake arguments as a bash array. Should be defined before calling
146 # src_compile.
147 # @CODE
148 # src_compile() {
149 #       local myemakeargs=(
150 #               $(usex neon "HAVE_NEON=1" "")
151 #       )
152 #       libretro-core_src_compile
153 # }
154 # @CODE
155
156 # @FUNCTION: libretro-core_src_compile
157 # @DESCRIPTION:
158 # The libretro-core src_compile function which is exported.
159 #
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')
168 }
169
170 # @VARIABLE: LIBRETRO_CORE_LIB_FILE
171 # @DEFAULT_UNSET
172 # @DESCRIPTION:
173 # Absolute path of this Libretro core's shared library.
174 # src_install.
175 # @CODE
176 # src_install() {
177 #       local LIBRETRO_CORE_LIB_FILE="${S}/somecore_libretro.so"
178 #
179 #       libretro-core_src_install
180 # }
181 # @CODE
182
183 # @FUNCTION: libretro-core_src_install
184 # @DESCRIPTION:
185 # The libretro-core src_install function which is exported.
186 #
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"}
190
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}"
197         else
198                 # Basename of this library.
199                 local lib_basename="${LIBRETRO_CORE_LIB_FILE##*/}"
200
201                 # Absolute path to which this library was installed.
202                 local lib_file_target="${ED}${libretro_lib_dir}/${lib_basename}"
203
204                 # If this library was *NOT* installed, fail.
205                 [[ -f "${lib_file_target}" ]] ||
206                         die "Libretro core shared library \"${lib_file_target}\" not installed."
207         fi
208 }
209
210 fi # end _LIBRETRO_CORE_ECLASS guard