Repository: easybuilders/easybuild-easyblocks

Issue:
How to handle Rust/cargo packages (or how to add a cargo easyblock)
I think it would be nice to make some type of easyblock for Rust/cargo packages. They are a bit of a special animal. Typically lots and lots of dependencies in cargo making it completely infeasible to make easyconfigs for each one.

We already have some easyconfigs that use cargo, and they are downloading all their dependencies at build time.
Another neat thing is that almost all Rust apps just link down to a single binary, no dynamic linking, so they can typically live at SYSTEM level at runtime.

So, ignoring the downloading of all those cargo deps at runtime, a typical easyconfig for a Rust software today could look like `ripgrep-13.0.0.eb`
```python
easyblock = 'Binary'

name = 'ripgrep'
version = '13.0.0'

homepage = 'https://github.com/BurntSushi/ripgrep'
description = """ripgrep is a line-oriented search tool that recursively searches the current directory for a regex
pattern."""

toolchain = SYSTEM

github_account = 'BurntSushi'
source_urls = [GITHUB_SOURCE]
sources = ['%(version)s.tar.gz']
checksums = ['0fb17aaf285b3eee8ddab17b833af1e190d73de317ff9648751ab0660d763ed2']

builddependencies = [
    ('Rust', '1.65.0', '', ('GCCcore', '12.2.0')),
]

extract_sources = True

install_cmd = "cargo install --root %(installdir)s --path ."

sanity_check_paths = {
    'files': ['bin/rg'],
    'dirs': [],
}

sanity_check_commands = ["rg --help"]

moduleclass = 'tool'
```

but we can clean up some of those steps with a custom easyblock for the install command and the source extraction.
Downloading of sources would also be nice without having to rely on github for the base package. I mean, if we are download 99% of the code at build time anyway, we might as well just have it download the sources for the top level package as well. 

If we want to, we could probably cache some sources and tell cargo to pick them up from the sources dir.
https://doc.rust-lang.org/cargo/reference/registries.html
https://users.rust-lang.org/t/using-crates-in-offline-environment/72970/22


Discussion:
see also https://github.com/vscentrum/vsc-software-stack/issues/83

Resolving patch:
diff --git a/easybuild/easyblocks/generic/cargo.py b/easybuild/easyblocks/generic/cargo.py
new file mode 100644
--- /dev/null
+++ b/easybuild/easyblocks/generic/cargo.py
@@ -0,0 +1,241 @@
+##
+# Copyright 2009-2023 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 Cargo packages (Rust lang package system)
+
+@author: Mikael Oehman (Chalmers University of Technology)
+"""
+
+import os
+
+import easybuild.tools.environment as env
+from easybuild.tools.build_log import EasyBuildError
+from easybuild.framework.easyconfig import CUSTOM
+from easybuild.framework.extensioneasyblock import ExtensionEasyBlock
+from easybuild.tools.filetools import extract_file, change_dir
+from easybuild.tools.run import run_cmd
+from easybuild.tools.config import build_option
+from easybuild.tools.filetools import write_file, compute_checksum
+
+CRATESIO_SOURCE = "https://crates.io/api/v1/crates"
+
+
+class Cargo(ExtensionEasyBlock):
+    """Support for installing Cargo packages (Rust)"""
+
+    @staticmethod
+    def extra_options(extra_vars=None):
+        """Define extra easyconfig parameters specific to Cargo"""
+        extra_vars = ExtensionEasyBlock.extra_options(extra_vars)
+        extra_vars.update({
+            'enable_tests': [True, "Enable building of tests", CUSTOM],
+            'offline': [True, "Build offline", CUSTOM],
+            'lto': [None, "Override default LTO flag ('fat', 'thin', 'off')", CUSTOM],
+            'crates': [[], "List of (crate, version, [repo, rev]) tuples to use", CUSTOM],
+        })
+
+        return extra_vars
+
+    def __init__(self, *args, **kwargs):
+        """Constructor for Cargo easyblock."""
+        super(Cargo, self).__init__(*args, **kwargs)
+        self.cargo_home = os.path.join(self.builddir, '.cargo')
+        env.setvar('CARGO_HOME', self.cargo_home)
+        env.setvar('RUSTC', 'rustc')
+        env.setvar('RUSTDOC', 'rustdoc')
+        env.setvar('RUSTFMT', 'rustfmt')
+        optarch = build_option('optarch')
+        if not optarch:
+            optarch = 'native'
+        env.setvar('RUSTFLAGS', '-C target-cpu=%s' % optarch)
+        env.setvar('RUST_LOG', 'DEBUG')
+        env.setvar('RUST_BACKTRACE', '1')
+
+        # Populate sources from "crates" list of tuples
+        sources = []
+        for crate_info in self.cfg['crates']:
+            if len(crate_info) == 2:
+                crate, version = crate_info
+                sources.append({
+                    'download_filename': crate + '/' + version + '/download',
+                    'filename': crate + '-' + version + '.tar.gz',
+                    'source_urls': [CRATESIO_SOURCE],
+                    'alt_location': 'crates.io',
+                })
+            else:
+                crate, version, repo, rev = crate_info
+                url, repo_name_git = repo.rsplit('/', maxsplit=1)
+                sources.append({
+                    'git_config': {'url': url, 'repo_name': repo_name_git[:-4], 'commit': rev},
+                    'filename': crate + '-' + version + '.tar.gz',
+                    'source_urls': [CRATESIO_SOURCE],
+                })
+
+        self.cfg.update('sources', sources)
+
+    def configure_step(self):
+        pass
+
+    def extract_step(self):
+        """
+        Unpack the source files and populate them with required .cargo-checksum.json if offline
+        """
+        if self.cfg['offline']:
+            self.log.info("Setting vendored crates dir")
+            # Replace crates-io with vendored sources using build dir wide toml file in CARGO_HOME
+            # because the rust source subdirectories might differ with python packages
+            config_toml = os.path.join(self.cargo_home, 'config.toml')
+            write_file(config_toml, '[source.vendored-sources]\ndirectory = "%s"\n\n' % self.builddir, append=True)
+            write_file(config_toml, '[source.crates-io]\nreplace-with = "vendored-sources"\n\n', append=True)
+
+            # also vendor sources from other git sources (could be many crates for one git source)
+            git_sources = set()
+            for crate_info in self.cfg['crates']:
+                if len(crate_info) == 4:
+                    _, _, repo, rev = crate_info
+                    git_sources.add((repo, rev))
+            for repo, rev in git_sources:
+                write_file(config_toml, '[source."%s"]\ngit = "%s"\nrev = "%s"\n'
+                                        'replace-with = "vendored-sources"\n\n' % (repo, repo, rev), append=True)
+
+            # Use environment variable since it would also be passed along to builds triggered via python packages
+            env.setvar('CARGO_NET_OFFLINE', 'true')
+
+        # More work is needed here for git sources to work, especially those repos with multiple packages.
+        for src in self.src:
+            existing_dirs = set(os.listdir(self.builddir))
+            self.log.info("Unpacking source %s" % src['name'])
+            srcdir = extract_file(src['path'], self.builddir, cmd=src['cmd'],
+                                  extra_options=self.cfg['unpack_options'], change_into_dir=False)
+            change_dir(srcdir)
+            if srcdir:
+                self.src[self.src.index(src)]['finalpath'] = srcdir
+            else:
+                raise EasyBuildError("Unpacking source %s failed", src['name'])
+
+            # Create checksum file for all sources required by vendored crates.io sources
+            new_dirs = set(os.listdir(self.builddir)) - existing_dirs
+            if self.cfg['offline'] and len(new_dirs) == 1:
+                cratedir = new_dirs.pop()
+                self.log.info('creating .cargo-checksums.json file for : %s', cratedir)
+                chksum = compute_checksum(src['path'], checksum_type='sha256')
+                chkfile = os.path.join(self.builddir, cratedir, '.cargo-checksum.json')
+                write_file(chkfile, '{"files":{},"package":"%s"}' % chksum)
+
+    @property
+    def profile(self):
+        return 'debug' if self.toolchain.options.get('debug', None) else 'release'
+
+    def build_step(self):
+        """Build with cargo"""
+        parallel = ''
+        if self.cfg['parallel']:
+            parallel = "-j %s" % self.cfg['parallel']
+
+        tests = ''
+        if self.cfg['enable_tests']:
+            tests = "--tests"
+
+        lto = ''
+        if self.cfg['lto'] is not None:
+            lto = '--config profile.%s.lto=\\"%s\\"' % (self.profile, self.cfg['lto'])
+
+        run_cmd('rustc --print cfg', log_all=True, simple=True)  # for tracking in log file
+        cmd = ' '.join([
+            self.cfg['prebuildopts'],
+            'cargo build',
+            '--profile=' + self.profile,
+            lto,
+            tests,
+            parallel,
+            self.cfg['buildopts'],
+        ])
+        run_cmd(cmd, log_all=True, simple=True)
+
+    def test_step(self):
+        """Test with cargo"""
+        if self.cfg['enable_tests']:
+            cmd = ' '.join([
+                self.cfg['pretestopts'],
+                'cargo test',
+                '--profile=' + self.profile,
+                self.cfg['testopts'],
+            ])
+            run_cmd(cmd, log_all=True, simple=True)
+
+    def install_step(self):
+        """Install with cargo"""
+        cmd = ' '.join([
+            self.cfg['preinstallopts'],
+            'cargo install',
+            '--profile=' + self.profile,
+            '--root=' + self.installdir,
+            '--path=.',
+            self.cfg['installopts'],
+        ])
+        run_cmd(cmd, log_all=True, simple=True)
+
+
+def generate_crate_list(sourcedir):
+    """Helper for generating crate list"""
+    import toml
+
+    cargo_toml = toml.load(os.path.join(sourcedir, 'Cargo.toml'))
+    cargo_lock = toml.load(os.path.join(sourcedir, 'Cargo.lock'))
+
+    app_name = cargo_toml['package']['name']
+    deps = cargo_lock['package']
+
+    app_in_cratesio = False
+    crates = []
+    other_crates = []
+    for dep in deps:
+        name = dep['name']
+        version = dep['version']
+        if 'source' in dep:
+            if name == app_name:
+                app_in_cratesio = True  # exclude app itself, needs to be first in crates list or taken from pypi
+            else:
+                if dep['source'] == 'registry+https://github.com/rust-lang/crates.io-index':
+                    crates.append((name, version))
+                else:
+                    # Lock file has revision#revision in the url for some reason.
+                    crates.append((name, version, dep['source'].rsplit('#', maxsplit=1)[0]))
+        else:
+            other_crates.append((name, version))
+    return app_in_cratesio, crates, other_crates
+
+
+if __name__ == '__main__':
+    import sys
+    app_in_cratesio, crates, other = generate_crate_list(sys.argv[1])
+    print(other)
+    if app_in_cratesio or crates:
+        print('crates = [')
+        if app_in_cratesio:
+            print('    (name, version),')
+        for crate_info in crates:
+            print("    ('" + "', '".join(crate_info) + "'),")
+        print(']')
diff --git a/easybuild/easyblocks/generic/cargopythonpackage.py b/easybuild/easyblocks/generic/cargopythonpackage.py
new file mode 100644
--- /dev/null
+++ b/easybuild/easyblocks/generic/cargopythonpackage.py
@@ -0,0 +1,57 @@
+##
+# Copyright 2009-2023 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 Cargo packages (Rust lang package system)
+
+@author: Mikael Oehman (Chalmers University of Technology)
+"""
+
+from easybuild.easyblocks.generic.cargo import Cargo
+from easybuild.easyblocks.generic.pythonpackage import PythonPackage
+
+
+class CargoPythonPackage(PythonPackage, Cargo):  # PythonPackage must come first to take precedence
+    """Build a Python package with setup from Cargo but build/install step from PythonPackage
+
+    The cargo init step will set up the environment variables for rustc and vendor sources
+    but all the build steps are triggered via normal PythonPackage steps like normal.
+    """
+
+    @staticmethod
+    def extra_options(extra_vars=None):
+        """Define extra easyconfig parameters specific to Cargo"""
+        extra_vars = PythonPackage.extra_options(extra_vars)
+        extra_vars = Cargo.extra_options(extra_vars)  # not all extra options here will used here
+
+        return extra_vars
+
+    def __init__(self, *args, **kwargs):
+        """Constructor for CargoPythonPackage easyblock."""
+        Cargo.__init__(self, *args, **kwargs)
+        PythonPackage.__init__(self, *args, **kwargs)
+
+    def extract_step(self):
+        """Specifically use the overloaded variant from Cargo as is populates vendored sources with checksums."""
+        return Cargo.extract_step(self)


Test patch: