perl-functions.eclass: should 'just work' in EAPI=6
[gentoo.git] / eclass / games.eclass
1 # Copyright 1999-2015 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3 # $Id$
4
5 # @ECLASS: games.eclass
6 # @MAINTAINER:
7 # Games team <games@gentoo.org>
8 # @BLURB: Standardizing the install of games.
9 # @DESCRIPTION:
10 # This eclass makes sure that games are consistently handled in gentoo.
11 # It installs game files by default in FHS-compatible directories
12 # like /usr/share/games and sets more restrictive permissions in order
13 # to avoid some security bugs.
14 #
15 # The installation directories as well as the user and group files are
16 # installed as can be controlled by the user. See the variables like
17 # GAMES_BINDIR, GAMES_USER etc. below. These are NOT supposed to be set
18 # by ebuilds!
19 #
20 # For a general guide on writing games ebuilds, see:
21 # https://wiki.gentoo.org/wiki/Project:Games/Ebuild_howto
22
23
24 if [[ -z ${_GAMES_ECLASS} ]]; then
25 _GAMES_ECLASS=1
26
27 inherit base multilib toolchain-funcs eutils user
28
29 case ${EAPI:-0} in
30         0|1) EXPORT_FUNCTIONS pkg_setup src_compile pkg_preinst pkg_postinst ;;
31         2|3|4|5) EXPORT_FUNCTIONS pkg_setup src_configure src_compile pkg_preinst pkg_postinst ;;
32         *) die "no support for EAPI=${EAPI} yet" ;;
33 esac
34
35 if [[ ${CATEGORY}/${PN} != "games-misc/games-envd" ]] ; then
36         # environment file
37         RDEPEND="games-misc/games-envd"
38 fi
39
40 # @ECLASS-VARIABLE: GAMES_PREFIX
41 # @DESCRIPTION:
42 # Prefix where to install games, mostly used by GAMES_BINDIR. Games data should
43 # still go into GAMES_DATADIR. May be set by the user.
44 GAMES_PREFIX=${GAMES_PREFIX:-/usr/games}
45
46 # @ECLASS-VARIABLE: GAMES_PREFIX_OPT
47 # @DESCRIPTION:
48 # Prefix where to install precompiled/blob games, usually followed by
49 # package name. May be set by the user.
50 GAMES_PREFIX_OPT=${GAMES_PREFIX_OPT:-/opt}
51
52 # @ECLASS-VARIABLE: GAMES_DATADIR
53 # @DESCRIPTION:
54 # Base directory where to install game data files, usually followed by
55 # package name. May be set by the user.
56 GAMES_DATADIR=${GAMES_DATADIR:-/usr/share/games}
57
58 # @ECLASS-VARIABLE: GAMES_DATADIR_BASE
59 # @DESCRIPTION:
60 # Similar to GAMES_DATADIR, but only used when a package auto appends 'games'
61 # to the path. May be set by the user.
62 GAMES_DATADIR_BASE=${GAMES_DATADIR_BASE:-/usr/share}
63
64 # @ECLASS-VARIABLE: GAMES_SYSCONFDIR
65 # @DESCRIPTION:
66 # Where to install global games configuration files, usually followed by
67 # package name. May be set by the user.
68 GAMES_SYSCONFDIR=${GAMES_SYSCONFDIR:-/etc/games}
69
70 # @ECLASS-VARIABLE: GAMES_STATEDIR
71 # @DESCRIPTION:
72 # Where to install/store global variable game data, usually followed by
73 # package name. May be set by the user.
74 GAMES_STATEDIR=${GAMES_STATEDIR:-/var/games}
75
76 # @ECLASS-VARIABLE: GAMES_LOGDIR
77 # @DESCRIPTION:
78 # Where to store global game log files, usually followed by
79 # package name. May be set by the user.
80 GAMES_LOGDIR=${GAMES_LOGDIR:-/var/log/games}
81
82 # @ECLASS-VARIABLE: GAMES_BINDIR
83 # @DESCRIPTION:
84 # Where to install the game binaries. May be set by the user. This is in PATH.
85 GAMES_BINDIR=${GAMES_BINDIR:-${GAMES_PREFIX}/bin}
86
87 # @ECLASS-VARIABLE: GAMES_ENVD
88 # @INTERNAL
89 # @DESCRIPTION:
90 # The games environment file name which sets games specific LDPATH and PATH.
91 GAMES_ENVD="90games"
92
93 # @ECLASS-VARIABLE: GAMES_USER
94 # @DESCRIPTION:
95 # The USER who owns all game files and usually has write permissions.
96 # May be set by the user.
97 GAMES_USER=${GAMES_USER:-root}
98
99 # @ECLASS-VARIABLE: GAMES_USER_DED
100 # @DESCRIPTION:
101 # The USER who owns all game files related to the dedicated server part
102 # of a package. May be set by the user.
103 GAMES_USER_DED=${GAMES_USER_DED:-games}
104
105 # @ECLASS-VARIABLE: GAMES_GROUP
106 # @DESCRIPTION:
107 # The GROUP that owns all game files and usually does not have
108 # write permissions. May be set by the user.
109 # If you want games world-executable, then you can at least set this variable
110 # to 'users' which is almost the same.
111 GAMES_GROUP=${GAMES_GROUP:-games}
112
113 # @FUNCTION: games_get_libdir
114 # @DESCRIPTION:
115 # Gets the directory where to install games libraries. This is in LDPATH.
116 games_get_libdir() {
117         echo ${GAMES_PREFIX}/$(get_libdir)
118 }
119
120 # @FUNCTION: egamesconf
121 # @USAGE: [<args>...]
122 # @DESCRIPTION:
123 # Games equivalent to 'econf' for autotools based build systems. It passes
124 # the necessary games specific directories automatically.
125 egamesconf() {
126         # handle verbose build log pre-EAPI5
127         local _gamesconf
128         if has "${EAPI:-0}" 0 1 2 3 4 ; then
129                 if grep -q -s disable-silent-rules "${ECONF_SOURCE:-.}"/configure ; then
130                         _gamesconf="--disable-silent-rules"
131                 fi
132         fi
133
134         # bug 493954
135         if grep -q -s datarootdir "${ECONF_SOURCE:-.}"/configure ; then
136                 _gamesconf="${_gamesconf} --datarootdir=/usr/share"
137         fi
138
139         econf \
140                 --prefix="${GAMES_PREFIX}" \
141                 --libdir="$(games_get_libdir)" \
142                 --datadir="${GAMES_DATADIR}" \
143                 --sysconfdir="${GAMES_SYSCONFDIR}" \
144                 --localstatedir="${GAMES_STATEDIR}" \
145                 ${_gamesconf} \
146                 "$@"
147 }
148
149 # @FUNCTION: gameswrapper
150 # @USAGE: <command> [<args>...]
151 # @INTERNAL
152 # @DESCRIPTION:
153 # Wraps an install command like dobin, dolib etc, so that
154 # it has GAMES_PREFIX as prefix.
155 gameswrapper() {
156         # dont want to pollute calling env
157         (
158                 into "${GAMES_PREFIX}"
159                 cmd=$1
160                 shift
161                 ${cmd} "$@"
162         )
163 }
164
165 # @FUNCTION: dogamesbin
166 # @USAGE: <path>...
167 # @DESCRIPTION:
168 # Install one or more games binaries.
169 dogamesbin() { gameswrapper ${FUNCNAME/games} "$@"; }
170
171 # @FUNCTION: dogamessbin
172 # @USAGE: <path>...
173 # @DESCRIPTION:
174 # Install one or more games system binaries.
175 dogamessbin() { gameswrapper ${FUNCNAME/games} "$@"; }
176
177 # @FUNCTION: dogameslib
178 # @USAGE: <path>...
179 # @DESCRIPTION:
180 # Install one or more games libraries.
181 dogameslib() { gameswrapper ${FUNCNAME/games} "$@"; }
182
183 # @FUNCTION: dogameslib.a
184 # @USAGE: <path>...
185 # @DESCRIPTION:
186 # Install one or more static games libraries.
187 dogameslib.a() { gameswrapper ${FUNCNAME/games} "$@"; }
188
189 # @FUNCTION: dogameslib.so
190 # @USAGE: <path>...
191 # @DESCRIPTION:
192 # Install one or more shared games libraries.
193 dogameslib.so() { gameswrapper ${FUNCNAME/games} "$@"; }
194
195 # @FUNCTION: newgamesbin
196 # @USAGE: <path> <newname>
197 # @DESCRIPTION:
198 # Install one games binary with a new name.
199 newgamesbin() { gameswrapper ${FUNCNAME/games} "$@"; }
200
201 # @FUNCTION: newgamessbin
202 # @USAGE: <path> <newname>
203 # @DESCRIPTION:
204 # Install one system games binary with a new name.
205 newgamessbin() { gameswrapper ${FUNCNAME/games} "$@"; }
206
207 # @FUNCTION: games_make_wrapper
208 # @USAGE: <wrapper> <target> [chdir] [libpaths] [installpath]
209 # @DESCRIPTION:
210 # Create a shell wrapper script named wrapper in installpath
211 # (defaults to the games bindir) to execute target (default of wrapper) by
212 # first optionally setting LD_LIBRARY_PATH to the colon-delimited
213 # libpaths followed by optionally changing directory to chdir.
214 games_make_wrapper() { gameswrapper ${FUNCNAME/games_} "$@"; }
215
216 # @FUNCTION: gamesowners
217 # @USAGE: [<args excluding owner/group>...] <path>...
218 # @DESCRIPTION:
219 # Run 'chown' with the given args on the given files. Owner and
220 # group are GAMES_USER and GAMES_GROUP and must not be passed
221 # as args.
222 gamesowners() { chown ${GAMES_USER}:${GAMES_GROUP} "$@"; }
223
224 # @FUNCTION: gamesperms
225 # @USAGE: <path>...
226 # @DESCRIPTION:
227 # Run 'chmod' with games specific permissions on the given files.
228 gamesperms() { chmod u+rw,g+r-w,o-rwx "$@"; }
229
230 # @FUNCTION: prepgamesdirs
231 # @DESCRIPTION:
232 # Fix all permissions/owners of files in games related directories,
233 # usually called at the end of src_install().
234 prepgamesdirs() {
235         local dir f mode
236         for dir in \
237                 "${GAMES_PREFIX}" "${GAMES_PREFIX_OPT}" "${GAMES_DATADIR}" \
238                 "${GAMES_SYSCONFDIR}" "${GAMES_STATEDIR}" "$(games_get_libdir)" \
239                 "${GAMES_BINDIR}" "$@"
240         do
241                 [[ ! -d ${D}/${dir} ]] && continue
242                 (
243                         gamesowners -R "${D}/${dir}"
244                         find "${D}/${dir}" -type d -print0 | xargs -0 chmod 750
245                         mode=o-rwx,g+r,g-w
246                         [[ ${dir} = ${GAMES_STATEDIR} ]] && mode=o-rwx,g+r
247                         find "${D}/${dir}" -type f -print0 | xargs -0 chmod $mode
248
249                         # common trees should not be games owned #264872 #537580
250                         fowners root:0 "${dir}"
251                         fperms 755 "${dir}"
252                         if [[ ${dir} == "${GAMES_PREFIX}" \
253                                                 || ${dir} == "${GAMES_PREFIX_OPT}" ]] ; then
254                                 for d in $(get_libdir) bin ; do
255                                         # check if dirs exist to avoid "nonfatal" option
256                                         if [[ -e ${D}/${dir}/${d} ]] ; then
257                                                 fowners root:0 "${dir}/${d}"
258                                                 fperms 755 "${dir}/${d}"
259                                         fi
260                                 done
261                         fi
262                 ) &>/dev/null
263
264                 f=$(find "${D}/${dir}" -perm +4000 -a -uid 0 2>/dev/null)
265                 if [[ -n ${f} ]] ; then
266                         eerror "A game was detected that is setuid root!"
267                         eerror "${f}"
268                         die "refusing to merge a setuid root game"
269                 fi
270         done
271         [[ -d ${D}/${GAMES_BINDIR} ]] || return 0
272         find "${D}/${GAMES_BINDIR}" -maxdepth 1 -type f -exec chmod 750 '{}' \;
273 }
274
275 # @FUNCTION: games_pkg_setup
276 # @DESCRIPTION:
277 # Export some toolchain specific variables and create games related groups
278 # and users. This function is exported as pkg_setup().
279 games_pkg_setup() {
280         tc-export CC CXX LD AR RANLIB
281
282         enewgroup "${GAMES_GROUP}" 35
283         [[ ${GAMES_USER} != "root" ]] \
284                 && enewuser "${GAMES_USER}" 35 -1 "${GAMES_PREFIX}" "${GAMES_GROUP}"
285         [[ ${GAMES_USER_DED} != "root" ]] \
286                 && enewuser "${GAMES_USER_DED}" 36 /bin/bash "${GAMES_PREFIX}" "${GAMES_GROUP}"
287
288         # Dear portage team, we are so sorry.  Lots of love, games team.
289         # See Bug #61680
290         [[ ${USERLAND} != "GNU" ]] && return 0
291         [[ $(egetshell "${GAMES_USER_DED}") == "/bin/false" ]] \
292                 && usermod -s /bin/bash "${GAMES_USER_DED}"
293 }
294
295 # @FUNCTION: games_src_configure
296 # @DESCRIPTION:
297 # Runs egamesconf if there is a configure file.
298 # This function is exported as src_configure().
299 games_src_configure() {
300         [[ -x "${ECONF_SOURCE:-.}"/configure ]] && egamesconf
301 }
302
303 # @FUNCTION: games_src_compile
304 # @DESCRIPTION:
305 # Runs base_src_make(). This function is exported as src_compile().
306 games_src_compile() {
307         case ${EAPI:-0} in
308                 0|1) games_src_configure ;;
309         esac
310         base_src_make
311 }
312
313 # @FUNCTION: games_pkg_preinst
314 # @DESCRIPTION:
315 # Synchronizes GAMES_STATEDIR of the ebuild image with the live filesystem.
316 games_pkg_preinst() {
317         local f
318
319         while read f ; do
320                 if [[ -e ${ROOT}/${GAMES_STATEDIR}/${f} ]] ; then
321                         cp -p \
322                                 "${ROOT}/${GAMES_STATEDIR}/${f}" \
323                                 "${D}/${GAMES_STATEDIR}/${f}" \
324                                 || die "cp failed"
325                         # make the date match the rest of the install
326                         touch "${D}/${GAMES_STATEDIR}/${f}"
327                 fi
328         done < <(find "${D}/${GAMES_STATEDIR}" -type f -printf '%P\n' 2>/dev/null)
329 }
330
331 # @FUNCTION: games_pkg_postinst
332 # @DESCRIPTION:
333 # Prints some warnings and infos, also related to games groups.
334 games_pkg_postinst() {
335         if [[ -z "${GAMES_SHOW_WARNING}" ]] ; then
336                 ewarn "Remember, in order to play games, you have to"
337                 ewarn "be in the '${GAMES_GROUP}' group."
338                 echo
339                 case ${CHOST} in
340                         *-darwin*) ewarn "Just run 'niutil -appendprop / /groups/games users <USER>'";;
341                         *-freebsd*|*-dragonfly*) ewarn "Just run 'pw groupmod ${GAMES_GROUP} -m <USER>'";;
342                         *) ewarn "Just run 'gpasswd -a <USER> ${GAMES_GROUP}', then have <USER> re-login.";;
343                 esac
344                 echo
345                 einfo "For more info about Gentoo gaming in general, see our website:"
346                 einfo "   https://games.gentoo.org/"
347                 echo
348         fi
349 }
350
351 # @FUNCTION: games_ut_unpack
352 # @USAGE: <directory or file to unpack>
353 # @DESCRIPTION:
354 # Unpack .uz2 files for UT2003/UT2004.
355 games_ut_unpack() {
356         local ut_unpack="$1"
357         local f=
358
359         if [[ -z ${ut_unpack} ]] ; then
360                 die "You must provide an argument to games_ut_unpack"
361         fi
362         if [[ -f ${ut_unpack} ]] ; then
363                 uz2unpack "${ut_unpack}" "${ut_unpack%.uz2}" \
364                         || die "uncompressing file ${ut_unpack}"
365         fi
366         if [[ -d ${ut_unpack} ]] ; then
367                 while read f ; do
368                         uz2unpack "${ut_unpack}/${f}" "${ut_unpack}/${f%.uz2}" \
369                                 || die "uncompressing file ${f}"
370                         rm -f "${ut_unpack}/${f}" || die "deleting compressed file ${f}"
371                 done < <(find "${ut_unpack}" -maxdepth 1 -name '*.uz2' -printf '%f\n' 2>/dev/null)
372         fi
373 }
374
375 # @FUNCTION: games_umod_unpack
376 # @USAGE: <file to unpack>
377 # @DESCRIPTION:
378 # Unpacks .umod/.ut2mod/.ut4mod files for UT/UT2003/UT2004.
379 # Don't forget to set 'dir' and 'Ddir'.
380 games_umod_unpack() {
381         local umod=$1
382         mkdir -p "${Ddir}"/System
383         cp "${dir}"/System/{ucc-bin,{Manifest,Def{ault,User}}.ini,{Engine,Core,zlib,ogg,vorbis}.so,{Engine,Core}.int} "${Ddir}"/System
384         cd "${Ddir}"/System
385         UT_DATA_PATH=${Ddir}/System ./ucc-bin umodunpack -x "${S}/${umod}" -nohomedir &> /dev/null \
386                 || die "uncompressing file ${umod}"
387         rm -f "${Ddir}"/System/{ucc-bin,{Manifest,Def{ault,User},User,UT200{3,4}}.ini,{Engine,Core,zlib,ogg,vorbis}.so,{Engine,Core}.int,ucc.log} &>/dev/null \
388                 || die "Removing temporary files"
389 }
390
391 fi