summaryrefslogtreecommitdiff
path: root/sci-mathematics/mathematica/mathematica-15.0.1.ebuild
blob: d538467c12323aa3723179c8b57eb297b0098b8e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# Copyright 1999-2026 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

EAPI=8

CHECKREQS_DISK_BUILD=20G
inherit check-reqs desktop ffmpeg-compat unpacker xdg

DESCRIPTION="Wolfram Mathematica"

# Note: Please do not "remove old". Mathematica is licensed by version, and there
# are certainly still active installations of old versions around.
# That said, the compatibility of old versions with new Linux installs is not
# really great...

HOMEPAGE="https://www.wolfram.com/mathematica/"
SRC_URI="
	bundle?  ( Wolfram_${PV}.sh )
	!bundle? (
		Wolfram_${PV}_LIN.sh
		doc? ( WLDocs_$(ver_cut 1)_LIN.sh )
	)
"
S="${WORKDIR}"

LICENSE="all-rights-reserved"
SLOT="0"
KEYWORDS="-* ~amd64"
IUSE="bundle cuda doc ffmpeg R mqtt"

RESTRICT="strip mirror bindist fetch"

# Mathematica comes with a lot of bundled stuff. We should place here only what we
# explicitly override with LD_PRELOAD.
# RLink (libjri.so) requires dev-lang/R
# FFmpegTools (FFmpegToolsSystem-8.1.1.so) requires media-video/ffmpeg-8.1
# FFmpegTools (FFmpegToolsSystem-7.0.so) requires media-video/ffmpeg-7.0
# FFmpegTools (FFmpegToolsSystem-6.0.so) requires media-video/ffmpeg-6.0
# FFmpegTools (FFmpegToolsSystem-v5.1.2.so) requires media-video/ffmpeg-5.1.2
# Bundled mosquitto depends upon outdated shared libraries.
RDEPEND="
	dev-qt/qt5compat:6
	dev-qt/qtbase:6[eglfs,wayland]
	dev-qt/qtsvg:6
	dev-qt/qtwayland:6[compositor(+)]
	media-libs/freetype
	virtual/libcrypt
	cuda? (
		>=dev-util/nvidia-cuda-toolkit-11
		<dev-util/nvidia-cuda-toolkit-13
		)
	ffmpeg? ( || (
		media-video/ffmpeg-compat:6
		media-video/ffmpeg-compat:7
		media-video/ffmpeg-compat:8
		) )
	R? ( dev-lang/R )
	mqtt? (
		app-misc/mosquitto[bridge,srv]
		)
"

DEPEND="
	${RDEPEND}
"

BDEPEND="
	dev-util/patchelf
"

# we need this a few times
MPN="Mathematica"
MPV=$(ver_cut 1-2)
M_BINARIES="math mathematica Mathematica MathKernel mcc wolfram WolframKernel wolframscript"
M_TARGET="opt/Wolfram/${MPN}/${MPV}"

# we might as well list all files in all QA variables...
QA_PREBUILT="opt/*"

src_unpack() {
	for a in ${A}; do
		local target="unpack_app"
		case "${a}" in
			WLDocs*) target="unpack_docs" ;;
		esac
		/bin/sh "${DISTDIR}/${a}" --nox11 --keep --target "${S}/${target}" -- "-help" || die
	done
}

src_install() {
	local ARCH='-x86-64'

	pushd "${S}/unpack_app" > /dev/null || die
	# fix ACCESS DENIED issue when installer generate desktop files
	sed -e "s|xdg-desktop-icon|xdg-dummy-command|g" -i "Unix/Installer/WolframInstaller" || die
	sed -e "s|xdg-desktop-menu|xdg-dummy-command|g" -i "Unix/Installer/WolframInstaller" || die
	sed -e "s|xdg-icon-resource|xdg-dummy-command|g" -i "Unix/Installer/WolframInstaller" || die
	sed -e "s|xdg-mime|xdg-dummy-command|g" -i "Unix/Installer/WolframInstaller" || die

	# fix ACCESS DENIED issue when installing documentation
	sed -e "s|\(exec ./MathInstaller -noprompt\)|\1 -targetdir=${S}/docs_install|" \
		-i "Unix/Installer/WolframInstaller" || die

	# in the depths of the installer it tests whether it can write here
	# addpredict is by far the simplest solution
	# bug 960526, the installer may check the following two paths for write access
	addpredict /usr/share/Wolfram/thisisatest
	addpredict /usr/share/thisisatest

	/bin/sh "Unix/Installer/WolframInstaller" -auto "-targetdir=${S}/${M_TARGET}" "-execdir=${S}/opt/bin" || die

	popd > /dev/null || die

	# MathInstaller purges its target directory, so the documentation goes
	# into a scratch directory first and is merged into the application tree
	if use doc; then
		if ! use bundle; then
			/bin/sh "${S}/unpack_docs/Unix/Installer/MathInstaller" -auto \
				"-targetdir=${S}/docs_install" || die
		fi
		cp -a "${S}/docs_install/Documentation/." "${S}/${M_TARGET}/Documentation/" || die
	fi
	rm -rf "${S}/docs_install" || die

	if ! use doc; then
		einfo "Removing documentation"
		rm -r "${S}/opt/Wolfram/Mathematica/${MPV}/Documentation/English" || die
	fi

	# fix world writable file QA problem for files
	while IFS= read -r -d '' i; do
		chmod o-w "${i}" || die
	done < <(find "${S}/${M_TARGET}" -type f -print0)

	einfo 'Removing MacOS- and Windows-specific files'
	find "${S}/${M_TARGET}" -type d -\( -name Windows -o -name Windows-x86-64 \
		-o -name MacOSX -o -name MacOSX-ARM64 -o -name MacOSX-x86-64 -o -name Macintosh -\) \
		-exec rm -rv {} + || die

	if ! use cuda; then
		einfo 'Removing cuda support'
		rm "${S}/${M_TARGET}/SystemFiles/Links//WhisperLink/LibraryResources/Linux-x86-64/libwhisper_cuda.so.1.7.4" || die
		rm "${S}/${M_TARGET}/SystemFiles/Links//WhisperLink/LibraryResources/Linux-x86-64/WhisperLink_cuda.so" || die
	fi

# None of the RLink files exists in version 15.0.1, so the removal has been commented out
#
	# Linux-x86-64/AllVersions is the supported version, other versions remove
	einfo 'Removing unsupported RLink versions'
#	rm -r "${S}/${M_TARGET}/SystemFiles/Links/RLink/SystemFiles/Libraries/Linux-x86-64/3.5.0" || die
#	rm -r "${S}/${M_TARGET}/SystemFiles/Links/RLink/SystemFiles/Libraries/Linux-x86-64/3.6.0" || die

	# RLink can't use if R not used
	if ! use R; then
		einfo 'Removing RLink support'
#		rm -r "${S}/${M_TARGET}/SystemFiles/Links/RLink/SystemFiles/Libraries/Linux-x86-64/AllVersions/libjri.so" || die
	fi

	if ! use ffmpeg; then     # FFmpegTools can't use if ffmpeg not used
		einfo 'Removing FFmpegTools support'
		rm -r "${S}/${M_TARGET}/SystemFiles/Links/FFmpegTools/LibraryResources/Linux-x86-64/FFmpegToolsSystem"*.so || die
	else                      # Support ffmpeg, but remove support for versions not installed
		if has_version 'media-video/ffmpeg-compat:8'; then
			ffmpeg_compat_setup 8
		elif has_version 'media-video/ffmpeg-compat:7'; then
			ffmpeg_compat_setup 7
		elif has_version 'media-video/ffmpeg-compat:6'; then
			ffmpeg_compat_setup 6
		fi
		einfo 'Removing FFmpegTools version 5 support'
		rm -r "${S}/${M_TARGET}/SystemFiles/Links/FFmpegTools/LibraryResources/Linux-x86-64/FFmpegToolsSystem-v5.1.2.so" || die
		if ! has_version 'media-video/ffmpeg-compat:6'; then
			einfo 'Removing FFmpegTools version 6 support'
			rm -r "${S}/${M_TARGET}/SystemFiles/Links/FFmpegTools/LibraryResources/Linux-x86-64/FFmpegToolsSystem-6.0.so" || die
		fi
		if ! has_version 'media-video/ffmpeg-compat:7'; then
			einfo 'Removing FFmpegTools version 7 support'
			rm -r "${S}/${M_TARGET}/SystemFiles/Links/FFmpegTools/LibraryResources/Linux-x86-64/FFmpegToolsSystem-7.0.so" || die
		fi
		if ! has_version 'media-video/ffmpeg-compat:8'; then
			einfo 'Removing FFmpegTools version 8 support'
			rm -r "${S}/${M_TARGET}/SystemFiles/Links/FFmpegTools/LibraryResources/Linux-x86-64/FFmpegToolsSystem-8.1.1.so" || die
		fi
	fi

	# MQTT Support -- Bundled version of mosquitto requires outdated shared libraries
	if use mqtt; then
		einfo 'Enabling MQTT support'
		ln -sf "$(equery files --filter=cmd app-misc/mosquitto | \
			sed -ne /mosquitto$/p)" "${S}/${M_TARGET}/SystemFiles/Links/MQTTLink/Resources/Binaries/Linux-x86-64" || die
	else
		einfo 'Removing MQTT support'
		rm -r "${S}/${M_TARGET}/SystemFiles/Links/MQTTLink" || die
	fi

	# Remove cuda support for whisper. It requires an old version 11 of cuda.
	einfo 'Removing cuda support for Whisper'
	rm "${S}/${M_TARGET}/SystemFiles/Links//WhisperLink/LibraryResources/Linux-x86-64/libwhisper_cuda.so.1.7.4" || \
		! use cuda || die
	rm "${S}/${M_TARGET}/SystemFiles/Links//WhisperLink/LibraryResources/Linux-x86-64/WhisperLink_cuda.so" || \
		! use cuda || die

	# fix RPATH
	while IFS= read -r -d '' i; do
		# Use \x7fELF header to separate ELF executables and libraries
		# Skip .o files and static files to avoid surprises
		[[ $(od -t x1 -N 4 "${i}") == *"7f 45 4c 46"* ]] || continue
		[[ -f "${i}" && "${i: -2}" != ".o" ]] || continue
		[[ "$(file "${i}")" == *"dynamically"* ]] || continue
		einfo "Fixing RPATH of ${i}"
		patchelf --set-rpath \
'/'"${M_TARGET}"'/SystemFiles/Libraries/Linux-x86-64:'\
'/'"${M_TARGET}"'/SystemFiles/Libraries/Linux-x86-64/Qt/lib:'\
'/'"${M_TARGET}"'/SystemFiles/Java/Linux-x86-64/lib:'\
'/'"${M_TARGET}"'/SystemFiles/Java/Linux-x86-64/lib/jli:'\
'$ORIGIN' "${i}" || \
		die "patchelf failed on ${i}"
	done < <(find "${S}/${M_TARGET}" -type f -print0)

	# fix broken symbolic links
	ln -sf "/${M_TARGET}/Executables/WolframNB" "${S}/${M_TARGET}/Executables/mathematica" || die
	ln -sf "/${M_TARGET}/Executables/WolframNB" "${S}/${M_TARGET}/Executables/Mathematica" || die
	ln -sf "/${M_TARGET}/SystemFiles/Kernel/Binaries/Linux-x86-64/wolframscript" \
		"${S}/${M_TARGET}/Executables/wolframscript" || die

	# move all over
	mv "${S}"/opt "${ED}"/opt || die

	# the autogenerated symlinks point into sandbox, remove
	rm "${ED}"/opt/bin/* || die

	# install wrappers instead
	for name in ${M_BINARIES} ; do
		einfo "Generating wrapper for ${name}"
		echo '#!/bin/sh' >> "${T}/${name}" || die
		echo 'QT_QPA_PLATFORM="wayland;xcb"' >> "${T}/${name}" || die
		echo "LD_PRELOAD=/usr/$(get_libdir)/libfreetype.so.6:/usr/$(get_libdir)/libz.so.1:/usr/$(get_libdir)/libcrypt.so.1 /${M_TARGET}/Executables/${name} \"\${@}\"" \
			>> "${T}/${name}" || die
		dobin "${T}/${name}"
	done
	for name in ${M_BINARIES} ; do
		einfo "Symlinking ${name} to /opt/bin"
		dosym ../../usr/bin/${name} /opt/bin/${name}
	done

	# fix some embedded paths and install desktop files
	for filename in $(find "${ED}/${M_TARGET}/SystemFiles/Installation" -name "*wolfram[-.][wW]olfram*.desktop") ; do
		einfo "Fixing ${filename}"
		sed -e 's/^Icon=.*$/Icon=wolfram-mathematica/' -i "${filename}" || die
		sed -e "s|${S}||g" -e 's|^\t\t||g' -i "${filename}" || die
		sed -e 's|Version=2.0|Version=1.0|' -e 's|\\+|+|g' -i "${filename}" || die
		echo "Categories=Physics;Science;Engineering;2DGraphics;Graphics;" >> "${filename}" || die
		domenu "${filename}"
	done

	# install icons
	for iconsize in 16 32 64 128 256; do
		local iconfile="${ED}/${M_TARGET}/SystemFiles/FrontEnd/SystemResources/X/App-${iconsize}.png"
		if [ -e "${iconfile}" ]; then
			newicon -s "${iconsize}" "${iconfile}" wolfram-mathematica.png
		fi
	done

	# install mime types
	insinto /usr/share/mime/application
	for filename in $(find "${ED}/${M_TARGET}/SystemFiles" -name "application-*.xml"); do
		basefilename=$(basename "${filename}")
		mv "${filename}" "${T}/${basefilename#application-}" || die
		doins "${T}/${basefilename#application-}"
	done
}

pkg_nofetch() {
	einfo "Please place the Wolfram Mathematica installation file ${SRC_URI}"
	einfo "in your \$\{DISTDIR\}."
	einfo "Note that to actually run and use Mathematica you need a valid license."
	einfo "Wolfram provides time-limited evaluation licenses at ${HOMEPAGE}"
}