From 7508d51c1f47b1a717ee9c0445b8f8b90173ca67 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jens=20Henrik=20G=C3=B6bbert?= <goebbert1@jwlogin23.juwels>
Date: Thu, 17 Mar 2022 23:14:19 +0100
Subject: [PATCH] initial commit

---
 Custom_EasyBlocks/matlab.py          | 213 +++++++++++++++++++++++++++
 Golden_Repo/m/MATLAB/MATLAB-2020b.eb |  51 +++++++
 Golden_Repo/m/MATLAB/README.txt      |   9 ++
 3 files changed, 273 insertions(+)
 create mode 100644 Custom_EasyBlocks/matlab.py
 create mode 100644 Golden_Repo/m/MATLAB/MATLAB-2020b.eb
 create mode 100644 Golden_Repo/m/MATLAB/README.txt

diff --git a/Custom_EasyBlocks/matlab.py b/Custom_EasyBlocks/matlab.py
new file mode 100644
index 000000000..df6cf5b70
--- /dev/null
+++ b/Custom_EasyBlocks/matlab.py
@@ -0,0 +1,213 @@
+##
+# Copyright 2009-2021 Ghent University
+#
+# This file is part of EasyBuild,
+# originally created by the HPC team of Ghent University (http://ugent.be/hpc/en),
+# with support of Ghent University (http://ugent.be/hpc),
+# the Flemish Supercomputer Centre (VSC) (https://www.vscentrum.be),
+# Flemish Research Foundation (FWO) (http://www.fwo.be/en)
+# and the Department of Economy, Science and Innovation (EWI) (http://www.ewi-vlaanderen.be/en).
+#
+# https://github.com/easybuilders/easybuild
+#
+# EasyBuild is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation v2.
+#
+# EasyBuild is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with EasyBuild.  If not, see <http://www.gnu.org/licenses/>.
+##
+"""
+EasyBuild support for installing MATLAB, implemented as an easyblock
+
+@author: Stijn De Weirdt (Ghent University)
+@author: Dries Verdegem (Ghent University)
+@author: Kenneth Hoste (Ghent University)
+@author: Pieter De Baets (Ghent University)
+@author: Jens Timmerman (Ghent University)
+@author: Fotis Georgatos (Uni.Lu, NTUA)
+"""
+import re
+import os
+import stat
+import tempfile
+
+from distutils.version import LooseVersion
+
+from easybuild.easyblocks.generic.packedbinary import PackedBinary
+from easybuild.framework.easyconfig import CUSTOM
+from easybuild.tools.build_log import EasyBuildError
+from easybuild.tools.filetools import adjust_permissions, change_dir, copy_file, read_file, write_file
+from easybuild.tools.py2vs3 import string_type
+from easybuild.tools.run import run_cmd
+
+
+class EB_MATLAB(PackedBinary):
+    """Support for installing MATLAB."""
+
+    def __init__(self, *args, **kwargs):
+        """Add extra config options specific to MATLAB."""
+        super(EB_MATLAB, self).__init__(*args, **kwargs)
+        self.comp_fam = None
+        self.configfile = os.path.join(self.builddir, 'my_installer_input.txt')
+
+    @staticmethod
+    def extra_options():
+        extra_vars = {
+            'java_options': ['-Xmx256m', "$_JAVA_OPTIONS value set for install and in module file.", CUSTOM],
+            'key': [None, "Installation key(s), make one install for each key. Single key or a list of keys", CUSTOM],
+            'extend_libpath': [True, "Extend LD_LIBRARY_PATH", CUSTOM],
+        }
+        return PackedBinary.extra_options(extra_vars)
+
+    def configure_step(self):
+        """Configure MATLAB installation: create license file."""
+
+        licfile = self.cfg['license_file']
+        if licfile is None and os.getenv('EB_MATLAB_LICFILE') is not None:
+            licfile = os.getenv('EB_MATLAB_LICFILE', 'matlab.lic')
+        else:
+            licserv = self.cfg['license_server']
+            if licserv is None:
+                licserv = os.getenv('EB_MATLAB_LICENSE_SERVER', 'license.example.com')
+            licport = self.cfg['license_server_port']
+            if licport is None:
+                licport = os.getenv('EB_MATLAB_LICENSE_SERVER_PORT', '00000')
+            # create license file
+            lictxt = '\n'.join([
+                "SERVER %s 000000000000 %s" % (licserv, licport),
+                "USE_SERVER",
+            ])
+
+            licfile = os.path.join(self.builddir, 'matlab.lic')
+            write_file(licfile, lictxt)
+
+        try:
+            copy_file(os.path.join(self.cfg['start_dir'], 'installer_input.txt'), self.configfile)
+
+            # read file in binary mode to avoid UTF-8 encoding issues when using Python 3,
+            # due to non-UTF-8 characters...
+            config = read_file(self.configfile, mode='rb')
+
+            # use raw byte strings (must be 'br', not 'rb'),
+            # required when using Python 3 because file was read in binary mode
+            regdest = re.compile(br"^# destinationFolder=.*", re.M)
+            regagree = re.compile(br"^# agreeToLicense=.*", re.M)
+            regmode = re.compile(br"^# mode=.*", re.M)
+            reglicpath = re.compile(br"^# licensePath=.*", re.M)
+
+            # must use byte-strings here when using Python 3, see above
+            config = regdest.sub(b"destinationFolder=%s" % self.installdir.encode('utf-8'), config)
+            config = regagree.sub(b"agreeToLicense=Yes", config)
+            config = regmode.sub(b"mode=silent", config)
+            config = reglicpath.sub(b"licensePath=%s" % licfile.encode('utf-8'), config)
+
+            write_file(self.configfile, config)
+
+        except IOError as err:
+            raise EasyBuildError("Failed to create installation config file %s: %s", self.configfile, err)
+
+        self.log.debug('configuration file written to %s:\n %s', self.configfile, config)
+
+    def install_step(self):
+        """MATLAB install procedure using 'install' command."""
+
+        src = os.path.join(self.cfg['start_dir'], 'install')
+
+        # make sure install script is executable
+        adjust_permissions(src, stat.S_IXUSR)
+
+        if LooseVersion(self.version) >= LooseVersion('2016b'):
+            jdir = os.path.join(self.cfg['start_dir'], 'sys', 'java', 'jre', 'glnxa64', 'jre', 'bin')
+            for perm_dir in [os.path.join(self.cfg['start_dir'], 'bin', 'glnxa64'), jdir]:
+                adjust_permissions(perm_dir, stat.S_IXUSR)
+
+        # make sure $DISPLAY is not defined, which may lead to (hard to trace) problems
+        # this is a workaround for not being able to specify --nodisplay to the install scripts
+        if 'DISPLAY' in os.environ:
+            os.environ.pop('DISPLAY')
+
+        if '_JAVA_OPTIONS' not in self.cfg['preinstallopts']:
+            java_opts = 'export _JAVA_OPTIONS="%s" && ' % self.cfg['java_options']
+            self.cfg['preinstallopts'] = java_opts + self.cfg['preinstallopts']
+        if LooseVersion(self.version) >= LooseVersion('2016b'):
+            change_dir(self.builddir)
+
+        # Build the cmd string
+        cmdlist = [
+            self.cfg['preinstallopts'],
+            src,
+            '-inputFile',
+            self.configfile,
+        ]
+        if LooseVersion(self.version) < LooseVersion('2020a'):
+            # MATLAB installers < 2020a ignore $TMPDIR (always use /tmp) and might need a large tmpdir
+            tmpdir = tempfile.mkdtemp()
+            cmdlist.extend([
+                '-v',
+                '-tmpdir',
+                tmpdir,
+            ])
+        cmdlist.append(self.cfg['installopts'])
+        cmd = ' '.join(cmdlist)
+
+        keys = self.cfg['key']
+        if keys is None:
+            keys = os.getenv('EB_MATLAB_KEY', '00000-00000-00000-00000-00000-00000-00000-00000-00000-00000')
+        if isinstance(keys, string_type):
+            keys = keys.split(',')
+
+        # Compile the installation key regex outside of the loop
+        regkey = re.compile(br"^(# )?fileInstallationKey=.*", re.M)
+
+        # Run an install for each key
+        for i, key in enumerate(keys):
+
+            self.log.info('Installing MATLAB with key %s of %s', i + 1, len(keys))
+
+            try:
+                config = read_file(self.configfile, mode='rb')
+                config = regkey.sub(b"fileInstallationKey=%s" % key.encode('utf-8'), config)
+                write_file(self.configfile, config)
+
+            except IOError as err:
+                raise EasyBuildError("Failed to update config file %s: %s", self.configfile, err)
+
+            (out, _) = run_cmd(cmd, log_all=True, simple=False)
+
+            # check installer output for known signs of trouble
+            patterns = [
+                "Error: You have entered an invalid File Installation Key",
+            ]
+
+            for pattern in patterns:
+                regex = re.compile(pattern, re.I)
+                if regex.search(out):
+                    raise EasyBuildError("Found error pattern '%s' in output of installation command '%s': %s",
+                                         regex.pattern, cmd, out)
+
+    def sanity_check_step(self):
+        """Custom sanity check for MATLAB."""
+        custom_paths = {
+            'files': ["bin/matlab", "bin/glnxa64/MATLAB", "toolbox/local/classpath.txt"],
+            'dirs': ["java/jar"],
+        }
+        super(EB_MATLAB, self).sanity_check_step(custom_paths=custom_paths)
+
+    def make_module_extra(self):
+        """Extend PATH and set proper _JAVA_OPTIONS (e.g., -Xmx)."""
+        txt = super(EB_MATLAB, self).make_module_extra()
+
+        # make MATLAB runtime available
+        if self.cfg['extend_libpath'] and LooseVersion(self.version) >= LooseVersion('2017a'):
+            for ldlibdir in ['runtime', 'bin', os.path.join('sys', 'os')]:
+                libdir = os.path.join(ldlibdir, 'glnxa64')
+                txt += self.module_generator.prepend_paths('LD_LIBRARY_PATH', libdir)
+        if self.cfg['java_options']:
+            txt += self.module_generator.set_environment('_JAVA_OPTIONS', self.cfg['java_options'])
+        return txt
diff --git a/Golden_Repo/m/MATLAB/MATLAB-2020b.eb b/Golden_Repo/m/MATLAB/MATLAB-2020b.eb
new file mode 100644
index 000000000..e4480265c
--- /dev/null
+++ b/Golden_Repo/m/MATLAB/MATLAB-2020b.eb
@@ -0,0 +1,51 @@
+name = 'MATLAB'
+version = '2020b'
+
+homepage = 'https://www.mathworks.com/products/matlab.html'
+description = """MATLAB is a high-level language and interactive environment
+that enables you to perform computationally intensive tasks faster than with
+traditional programming languages such as C, C++, and Fortran.
+"""
+
+toolchain = SYSTEM
+
+sources = [SOURCELOWER_TAR_GZ]
+# checksums = ['']
+
+dependencies = [('Java', '15', '', SYSTEM)]
+
+java_options = '-Xmx2048m'
+extend_libpath = False
+
+# Attention: before calling 'eb':
+# export EB_MATLAB_KEY as fileInstallationKey
+# export EB_MATLAB_LICFILE as license file
+
+postinstallcmds = [
+    # create a wrapper script to ensure we do not mess up the environment
+    # because MATLAB comes with its own libstdc++ and other system libs
+    # in $EBROOTMATLAB/sys/os/glnxa64/
+    'mv %(installdir)s/bin/matlab %(installdir)s/bin/matlab.bin ',
+    (
+        '{ cat > %(installdir)s/bin/matlab; } << EOF\n'
+        '#!/bin/bash\n'
+        '\n'
+        'MYPATH=\$(readlink -f \$0)\n'
+        'export MATLAB_PATH=\$(realpath \$(dirname "\$MYPATH")/..)\n'
+        '\n'
+        'export LD_LIBRARY_PATH=\$MATLAB_PATH/runtime/glnxa64:\$LD_LIBRARY_PATH\n'
+        'export LD_LIBRARY_PATH=\$MATLAB_PATH/bin/glnxa64:\$LD_LIBRARY_PATH\n'
+        'export LD_LIBRARY_PATH=\$MATLAB_PATH/sys/os/glnxa64:\$LD_LIBRARY_PATH\n'
+        '\n'
+        '"\$MATLAB_PATH/bin/matlab.bin" "\$@"\n'
+        'EOF'
+    ),
+    'chmod +x %(installdir)s/bin/matlab',
+]
+
+sanity_check_paths = {
+    'files': ['bin/matlab', 'bin/glnxa64/MATLAB'],
+    'dirs': [],
+}
+
+moduleclass = 'math'
diff --git a/Golden_Repo/m/MATLAB/README.txt b/Golden_Repo/m/MATLAB/README.txt
new file mode 100644
index 000000000..464dd138c
--- /dev/null
+++ b/Golden_Repo/m/MATLAB/README.txt
@@ -0,0 +1,9 @@
+Before running `eb` to install MATLAB you need to
+
+a) export the file installation key as EB_MATLAB_KEY, like the following example
+export EB_MATLAB_KEY='00000-00000-00000-00000-00000-00000-00000-00000-00000-00000-00000-00000-00000-00000-00000-00000-00000-00000-00000-00000-00000-00000'
+
+b) export the absolute path to the license file as EB_MATLAB_LICFILE, like the following example
+export EB_MATLAB_LICFILE=/home/matlab/matlab.lic
+
+c) copy the packed matlab sources as 'matlab-2020b.tar.gz' to the working directory
-- 
GitLab