dev-python/josepy: 1.1.0 cleanup
[gentoo.git] / eclass / texlive-module.eclass
1 # Copyright 1999-2019 Gentoo Authors
2 # Distributed under the terms of the GNU General Public License v2
3
4 # @ECLASS: texlive-module.eclass
5 # @MAINTAINER:
6 # tex@gentoo.org
7 # @AUTHOR:
8 # Original Author: Alexis Ballier <aballier@gentoo.org>
9 # @SUPPORTED_EAPIS: 7
10 # @BLURB: Provide generic install functions so that modular texlive's texmf ebuild will only have to inherit this eclass
11 # @DESCRIPTION:
12 # Purpose: Provide generic install functions so that modular texlive's texmf ebuilds will
13 # only have to inherit this eclass.
14 # Ebuilds have to provide TEXLIVE_MODULE_CONTENTS variable that contains the list
15 # of packages that it will install. (See below)
16 #
17 # For TeX Live versions prior to 2009, the ebuild was supposed to unpack the
18 # texmf and texmf-dist directories to ${WORKDIR} (which is what the default
19 # src_unpack does).
20 # Starting from TeX Live 2009, the eclass provides a src_unpack function taking
21 # care of unpacking and relocating the files that need it.
22 #
23 # It inherits texlive-common.  Patching is supported via the PATCHES
24 # bash array.
25
26 # @ECLASS-VARIABLE: TEXLIVE_MODULE_CONTENTS
27 # @REQUIRED
28 # @DESCRIPTION:
29 # The list of packages that will be installed. This variable will be expanded to
30 # SRC_URI:
31 # foo -> texlive-module-foo-${PV}.tar.xz
32
33 # @ECLASS-VARIABLE: TEXLIVE_MODULE_DOC_CONTENTS
34 # @REQUIRED
35 # @DESCRIPTION:
36 # The list of packages that will be installed if the doc useflag is enabled.
37 # Expansion to SRC_URI is the same as for TEXLIVE_MODULE_CONTENTS.
38
39 # @ECLASS-VARIABLE: TEXLIVE_MODULE_SRC_CONTENTS
40 # @REQUIRED
41 # @DESCRIPTION:
42 # The list of packages that will be installed if the source useflag is enabled.
43 # Expansion to SRC_URI is the same as for TEXLIVE_MODULE_CONTENTS.
44
45 # @ECLASS-VARIABLE: TEXLIVE_MODULE_BINSCRIPTS
46 # @DEFAULT_UNSET
47 # @DESCRIPTION:
48 # A space separated list of files that are in fact scripts installed in the
49 # texmf tree and that we want to be available directly. They will be installed in
50 # /usr/bin.
51
52 # @ECLASS-VARIABLE: TEXLIVE_MODULE_BINLINKS
53 # @DEFAULT_UNSET
54 # @DESCRIPTION:
55 # A space separated list of links to add for BINSCRIPTS.
56 # The systax is: foo:bar to create a symlink bar -> foo.
57
58 # @ECLASS-VARIABLE: TL_PV
59 # @INTERNAL
60 # @DESCRIPTION:
61 # Normally the module's PV reflects the TeXLive release it belongs to.
62 # If this is not the case, TL_PV takes the version number for the
63 # needed app-text/texlive-core.
64
65 # @ECLASS-VARIABLE: TL_MODULE_INFORMATION
66 # @DEFAULT_UNSET
67 # @DESCRIPTION:
68 # Information to display about the package.
69 # e.g. for enabling/disabling a feature
70
71 case "${EAPI:-0}" in
72         0|1|2|3|4|5|6)
73                 die "EAPI='${EAPI}' is not supported anymore"
74                 ;;
75         *)
76                 inherit texlive-common
77                 ;;
78 esac
79
80 HOMEPAGE="http://www.tug.org/texlive/"
81
82 COMMON_DEPEND=">=app-text/texlive-core-${TL_PV:-${PV}}"
83
84 IUSE="source"
85
86 # Starting from TeX Live 2009, upstream provides .tar.xz modules.
87 PKGEXT=tar.xz
88
89 # We do not need anything from SYSROOT:
90 #   Everything is built from the texlive install in /
91 #   Generated files are noarch
92 BDEPEND="${COMMON_DEPEND}
93         app-arch/xz-utils"
94
95 for i in ${TEXLIVE_MODULE_CONTENTS}; do
96         SRC_URI="${SRC_URI} mirror://gentoo/texlive-module-${i}-${PV}.${PKGEXT}"
97 done
98
99 # Forge doc SRC_URI
100 [ -n "${TEXLIVE_MODULE_DOC_CONTENTS}" ] && SRC_URI="${SRC_URI} doc? ("
101 for i in ${TEXLIVE_MODULE_DOC_CONTENTS}; do
102         SRC_URI="${SRC_URI} mirror://gentoo/texlive-module-${i}-${PV}.${PKGEXT}"
103 done
104 [ -n "${TEXLIVE_MODULE_DOC_CONTENTS}" ] && SRC_URI="${SRC_URI} )"
105
106 # Forge source SRC_URI
107 if [ -n "${TEXLIVE_MODULE_SRC_CONTENTS}" ] ; then
108         SRC_URI="${SRC_URI} source? ("
109         for i in ${TEXLIVE_MODULE_SRC_CONTENTS}; do
110                 SRC_URI="${SRC_URI} mirror://gentoo/texlive-module-${i}-${PV}.${PKGEXT}"
111         done
112         SRC_URI="${SRC_URI} )"
113 fi
114
115 RDEPEND="${COMMON_DEPEND}"
116
117 IUSE="${IUSE} doc"
118
119 # @ECLASS-VARIABLE: TEXLIVE_MODULE_OPTIONAL_ENGINE
120 # @DEFAULT_UNSET
121 # @DESCRIPTION:
122 # A space separated list of Tex engines that can be made optional.
123 # e.g. "luatex luajittex"
124
125 if [ -n "${TEXLIVE_MODULE_OPTIONAL_ENGINE}" ] ; then
126         for engine in ${TEXLIVE_MODULE_OPTIONAL_ENGINE} ; do
127                 IUSE="${IUSE} +${engine}"
128         done
129 fi
130
131 S="${WORKDIR}"
132
133 # @FUNCTION: texlive-module_src_unpack
134 # @DESCRIPTION:
135 # Only for TeX Live 2009 and later.
136 # After unpacking, the files that need to be relocated are moved accordingly.
137
138 RELOC_TARGET=texmf-dist
139
140 texlive-module_src_unpack() {
141         unpack ${A}
142
143         grep RELOC tlpkg/tlpobj/* | awk '{print $2}' | sed 's#^RELOC/##' > "${T}/reloclist"
144         { for i in $(<"${T}/reloclist"); do  dirname $i; done; } | uniq > "${T}/dirlist"
145         for i in $(<"${T}/dirlist"); do
146                 [ -d "${RELOC_TARGET}/${i}" ] || mkdir -p "${RELOC_TARGET}/${i}"
147         done
148         for i in $(<"${T}/reloclist"); do
149                 mv "${i}" "${RELOC_TARGET}"/$(dirname "${i}") || die "failed to relocate ${i} to ${RELOC_TARGET}/$(dirname ${i})"
150         done
151 }
152
153 # @FUNCTION: texlive-module_add_format
154 # @DESCRIPTION:
155 # Creates/appends to a format.${PN}.cnf file for fmtutil.
156 # It parses the AddFormat directive of tlpobj files to create it.
157 # This will make fmtutil generate the formats when asked and allow the remaining
158 # src_compile phase to build the formats.
159
160 texlive-module_add_format() {
161         local name engine mode patterns options
162         eval $@
163         einfo "Appending to format.${PN}.cnf for $@"
164         [ -d texmf-dist/fmtutil ] || mkdir -p texmf-dist/fmtutil
165         [ -f texmf-dist/fmtutil/format.${PN}.cnf ] || { echo "# Generated for ${PN}     by texlive-module.eclass" > texmf-dist/fmtutil/format.${PN}.cnf; }
166         [ -n "${TEXLIVE_MODULE_OPTIONAL_ENGINE}" ] && has ${engine} ${TEXLIVE_MODULE_OPTIONAL_ENGINE} && use !${engine} && mode="disabled"
167         if [ "${mode}" = "disabled" ]; then
168                 printf "#! " >> texmf-dist/fmtutil/format.${PN}.cnf
169         fi
170         [ -z "${patterns}" ] && patterns="-"
171         printf "${name}\t${engine}\t${patterns}\t${options}\n" >> texmf-dist/fmtutil/format.${PN}.cnf
172 }
173
174 # @FUNCTION: texlive-module_make_language_def_lines
175 # @DESCRIPTION:
176 # Creates a language.${PN}.def entry to put in /etc/texmf/language.def.d.
177 # It parses the AddHyphen directive of tlpobj files to create it.
178
179 texlive-module_make_language_def_lines() {
180         local lefthyphenmin righthyphenmin synonyms name file file_patterns file_exceptions luaspecial
181         eval $@
182         einfo "Generating language.def entry for $@"
183         [ -z "$lefthyphenmin" ] && lefthyphenmin="2"
184         [ -z "$righthyphenmin" ] && righthyphenmin="3"
185         echo "\\addlanguage{$name}{$file}{}{$lefthyphenmin}{$righthyphenmin}" >> "${S}/language.${PN}.def"
186         if [ -n "$synonyms" ] ; then
187                 for i in $(echo $synonyms | tr ',' ' ') ; do
188                         einfo "Generating language.def synonym $i for $@"
189                         echo "\\addlanguage{$i}{$file}{}{$lefthyphenmin}{$righthyphenmin}" >> "${S}/language.${PN}.def"
190                 done
191         fi
192 }
193
194 # @FUNCTION: texlive-module_make_language_dat_lines
195 # @DESCRIPTION:
196 # Creates a language.${PN}.dat entry to put in /etc/texmf/language.dat.d.
197 # It parses the AddHyphen directive of tlpobj files to create it.
198
199 texlive-module_make_language_dat_lines() {
200         local lefthyphenmin righthyphenmin synonyms name file file_patterns file_exceptions luaspecial
201         eval $@
202         einfo "Generating language.dat entry for $@"
203         echo "$name $file" >> "${S}/language.${PN}.dat"
204         if [ -n "$synonyms" ] ; then
205                 for i in $(echo $synonyms | tr ',' ' ') ; do
206                         einfo "Generating language.dat synonym $i for $@"
207                         echo "=$i" >> "${S}/language.${PN}.dat"
208                 done
209         fi
210 }
211
212 # @FUNCTION: texlive-module_synonyms_to_language_lua_line
213 # @DESCRIPTION:
214 # Helper function for texlive-module_make_language_lua_lines to generate a
215 # correctly formatted synonyms entry for language.dat.lua.
216
217 texlive-module_synonyms_to_language_lua_line() {
218         local prev=""
219         for i in $(echo $@ | tr ',' ' ') ; do
220                 printf "${prev} '%s'" $i
221                 prev=","
222         done
223 }
224
225 # @FUNCTION: texlive-module_make_language_lua_lines
226 # @DESCRIPTION:
227 # Only valid for TeXLive 2010 and later.
228 # Creates a language.${PN}.dat.lua entry to put in
229 # /etc/texmf/language.dat.lua.d.
230 # It parses the AddHyphen directive of tlpobj files to create it.
231
232 texlive-module_make_language_lua_lines() {
233         local lefthyphenmin righthyphenmin synonyms name file file_patterns file_exceptions luaspecial
234         local dest="${S}/language.${PN}.dat.lua"
235         eval $@
236         [ -z "$lefthyphenmin"  ] && lefthyphenmin="2"
237         [ -z "$righthyphenmin" ] && righthyphenmin="3"
238         einfo "Generating language.dat.lua entry for $@"
239         printf "\t['%s'] = {\n" "$name"                                                                 >> "$dest"
240         printf "\t\tloader = '%s',\n" "$file"                                                           >> "$dest"
241         printf "\t\tlefthyphenmin = %s,\n\t\trighthyphenmin = %s,\n" "$lefthyphenmin" "$righthyphenmin" >> "$dest"
242         printf "\t\tsynonyms = {%s },\n" "$(texlive-module_synonyms_to_language_lua_line "$synonyms")"  >> "$dest"
243         [ -n "$file_patterns"   ] && printf "\t\tpatterns = '%s',\n" "$file_patterns"                   >> "$dest"
244         [ -n "$file_exceptions" ] && printf "\t\thyphenation = '%s',\n" "$file_exceptions"              >> "$dest"
245         [ -n "$luaspecial"      ] && printf "\t\tspecial = '%s',\n" "$luaspecial"                       >> "$dest"
246         printf "\t},\n"                                                                                 >> "$dest"
247 }
248
249 # @FUNCTION: texlive-module_src_compile
250 # @DESCRIPTION:
251 # exported function:
252 # Generates the config files that are to be installed in /etc/texmf;
253 # texmf-update script will take care of merging the different config files for
254 # different packages in a single one used by the whole tex installation.
255 #
256 # Once the config files are generated, we build the format files using fmtutil
257 # (provided by texlive-core). The compiled format files will be sent to
258 # texmf-var/web2c, like fmtutil defaults to but with some trick to stay in the
259 # sandbox.
260
261 texlive-module_src_compile() {
262         # Generate config files from the tlpobj files provided by TeX Live 2008 and
263         # later
264         for i in "${S}"/tlpkg/tlpobj/*;
265         do
266                 grep '^execute ' "${i}" | sed -e 's/^execute //' | tr ' \t' '##' >> "${T}/jobs"
267         done
268
269         for i in $(<"${T}/jobs");
270         do
271                 j="$(echo $i | tr '#' ' ')"
272                 command=${j%% *}
273                 parameter=${j#* }
274                 case "${command}" in
275                         addMap)
276                                 echo "Map ${parameter}" >> "${S}/${PN}.cfg";;
277                         addMixedMap)
278                                 echo "MixedMap ${parameter}" >> "${S}/${PN}.cfg";;
279                         addKanjiMap)
280                                 echo "KanjiMap ${parameter}" >> "${S}/${PN}.cfg";;
281                         addDvipsMap)
282                                 echo "p +${parameter}" >> "${S}/${PN}-config.ps";;
283                         addDvipdfmMap)
284                                 echo "f ${parameter}" >> "${S}/${PN}-config";;
285                         AddHyphen)
286                                 texlive-module_make_language_def_lines "$parameter"
287                                 texlive-module_make_language_dat_lines "$parameter"
288                                 texlive-module_make_language_lua_lines "$parameter"
289                                 ;;
290                         AddFormat)
291                                 texlive-module_add_format "$parameter";;
292                         BuildFormat)
293                                 einfo "Format $parameter already built.";;
294                         BuildLanguageDat)
295                                 einfo "Language file $parameter already generated.";;
296                         *)
297                                 die "No rule to proccess ${command}. Please file a bug."
298                 esac
299         done
300
301         # Determine texlive-core version for fmtutil call
302         fmt_call="$(has_version '>=app-text/texlive-core-2019' \
303          && echo "fmtutil-user" || echo "fmtutil")"
304
305         # Build format files
306         for i in texmf-dist/fmtutil/format*.cnf; do
307                 if [ -f "${i}" ]; then
308                         einfo "Building format ${i}"
309                         [ -d texmf-var ] || mkdir texmf-var
310                         [ -d texmf-var/web2c ] || mkdir texmf-var/web2c
311                         VARTEXFONTS="${T}/fonts" TEXMFHOME="${S}/texmf:${S}/texmf-dist:${S}/texmf-var"\
312                                 env -u TEXINPUTS $fmt_call --cnffile "${i}" --fmtdir "${S}/texmf-var/web2c" --all\
313                                 || die "failed to build format ${i}"
314                 fi
315         done
316
317         # Delete ls-R files, these should not be created but better be certain they
318         # do not end up being installed.
319         find . -name 'ls-R' -delete || die
320 }
321
322 # @FUNCTION: texlive-module_src_install
323 # @DESCRIPTION:
324 # exported function:
325 # Installs texmf and config files to the system.
326
327 texlive-module_src_install() {
328         for i in texmf-dist/fmtutil/format*.cnf; do
329                 [ -f "${i}" ] && etexlinks "${i}"
330         done
331
332         dodir /usr/share
333         if use doc; then
334                 [ -d texmf-doc ] && cp -pR texmf-doc "${ED}/usr/share/"
335         else
336                 [ -d texmf/doc ] && rm -rf texmf/doc
337                 [ -d texmf-dist/doc ] && rm -rf texmf-dist/doc
338         fi
339
340         [ -d texmf ] && cp -pR texmf "${ED}/usr/share/"
341         [ -d texmf-dist ] && cp -pR texmf-dist "${ED}/usr/share/"
342         [ -d tlpkg ] && use source && cp -pR tlpkg "${ED}/usr/share/"
343
344         insinto /var/lib/texmf
345         [ -d texmf-var ] && doins -r texmf-var/*
346
347         insinto /etc/texmf/updmap.d
348         [ -f "${S}/${PN}.cfg" ] && doins "${S}/${PN}.cfg"
349         insinto /etc/texmf/dvips.d
350         [ -f "${S}/${PN}-config.ps" ] && doins "${S}/${PN}-config.ps"
351         insinto /etc/texmf/dvipdfm/config
352         [ -f "${S}/${PN}-config" ] && doins "${S}/${PN}-config"
353
354         if [ -f "${S}/language.${PN}.def" ] ; then
355                 insinto /etc/texmf/language.def.d
356                 doins "${S}/language.${PN}.def"
357         fi
358
359         if [ -f "${S}/language.${PN}.dat" ] ; then
360                 insinto /etc/texmf/language.dat.d
361                 doins "${S}/language.${PN}.dat"
362         fi
363
364         if [ -f "${S}/language.${PN}.dat.lua" ] ; then
365                 insinto /etc/texmf/language.dat.lua.d
366                 doins "${S}/language.${PN}.dat.lua"
367         fi
368
369         [ -n "${TEXLIVE_MODULE_BINSCRIPTS}" ] && dobin_texmf_scripts ${TEXLIVE_MODULE_BINSCRIPTS}
370         if [ -n "${TEXLIVE_MODULE_BINLINKS}" ] ; then
371                 for i in ${TEXLIVE_MODULE_BINLINKS} ; do
372                         [ -f "${ED}/usr/bin/${i%:*}" ] || die "Trying to install an invalid     BINLINK. This should not happen. Please file a bug."
373                         dosym ${i%:*} /usr/bin/${i#*:}
374                 done
375         fi
376
377         texlive-common_handle_config_files
378         TEXMF_PATH=${TEXMF_DIST_PATH} texlive-common_handle_config_files
379 }
380
381 # @FUNCTION: texlive-module_pkg_postinst
382 # @DESCRIPTION:
383 # exported function:
384 # Run texmf-update to ensure the tex installation is consistent with the
385 # installed texmf trees.
386
387 texlive-module_pkg_postinst() {
388         etexmf-update
389         [ -n "${TL_MODULE_INFORMATION}" ] && elog "${TL_MODULE_INFORMATION}"
390 }
391
392 # @FUNCTION: texlive-module_pkg_postrm
393 # @DESCRIPTION:
394 # exported function:
395 # Run texmf-update to ensure the tex installation is consistent with the
396 # installed texmf trees.
397
398 texlive-module_pkg_postrm() {
399         etexmf-update
400 }
401
402 EXPORT_FUNCTIONS src_unpack src_compile src_install pkg_postinst pkg_postrm