diff options
author | Alec Scott <hi@alecbcs.com> | 2023-11-27 13:15:16 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-11-27 20:15:16 +0000 |
commit | c482534c1d3168da8053c4c47a203da300054a71 (patch) | |
tree | 9973729cb6a5a4b15ccd1fd0ffcd98e3301231a1 /lib | |
parent | fbec91e491776717c72a61f2791473e9c19e6af6 (diff) | |
download | spack-c482534c1d3168da8053c4c47a203da300054a71.tar.gz spack-c482534c1d3168da8053c4c47a203da300054a71.tar.bz2 spack-c482534c1d3168da8053c4c47a203da300054a71.tar.xz spack-c482534c1d3168da8053c4c47a203da300054a71.zip |
CargoPackage: add new build system for Cargo packages (#41192)
Co-authored-by: Tom Scogland <scogland1@llnl.gov>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/spack/spack/build_systems/cargo.py | 89 | ||||
-rw-r--r-- | lib/spack/spack/cmd/create.py | 38 | ||||
-rw-r--r-- | lib/spack/spack/package.py | 1 |
3 files changed, 114 insertions, 14 deletions
diff --git a/lib/spack/spack/build_systems/cargo.py b/lib/spack/spack/build_systems/cargo.py new file mode 100644 index 0000000000..28da475956 --- /dev/null +++ b/lib/spack/spack/build_systems/cargo.py @@ -0,0 +1,89 @@ +# Copyright 2013-2023 Lawrence Livermore National Security, LLC and other +# Spack Project Developers. See the top-level COPYRIGHT file for details. +# +# SPDX-License-Identifier: (Apache-2.0 OR MIT) + +import inspect + +import llnl.util.filesystem as fs + +import spack.builder +import spack.package_base +from spack.directives import build_system, depends_on +from spack.multimethod import when + +from ._checks import BaseBuilder, execute_install_time_tests + + +class CargoPackage(spack.package_base.PackageBase): + """Specialized class for packages built using a Makefiles.""" + + #: This attribute is used in UI queries that need to know the build + #: system base class + build_system_class = "CargoPackage" + + build_system("cargo") + + with when("build_system=cargo"): + depends_on("rust", type="build") + + +@spack.builder.builder("cargo") +class CargoBuilder(BaseBuilder): + """The Cargo builder encodes the most common way of building software with + a rust Cargo.toml file. It has two phases that can be overridden, if need be: + + 1. :py:meth:`~.CargoBuilder.build` + 2. :py:meth:`~.CargoBuilder.install` + + For a finer tuning you may override: + + +-----------------------------------------------+----------------------+ + | **Method** | **Purpose** | + +===============================================+======================+ + | :py:meth:`~.CargoBuilder.build_args` | Specify arguments | + | | to ``cargo install`` | + +-----------------------------------------------+----------------------+ + | :py:meth:`~.CargoBuilder.check_args` | Specify arguments | + | | to ``cargo test`` | + +-----------------------------------------------+----------------------+ + """ + + phases = ("build", "install") + + #: Callback names for install-time test + install_time_test_callbacks = ["check"] + + @property + def build_directory(self): + """Return the directory containing the main Cargo.toml.""" + return self.pkg.stage.source_path + + @property + def build_args(self): + """Arguments for ``cargo build``.""" + return [] + + @property + def check_args(self): + """Argument for ``cargo test`` during check phase""" + return [] + + def build(self, pkg, spec, prefix): + """Runs ``cargo install`` in the source directory""" + with fs.working_dir(self.build_directory): + inspect.getmodule(pkg).cargo( + "install", "--root", "out", "--path", ".", *self.build_args + ) + + def install(self, pkg, spec, prefix): + """Copy build files into package prefix.""" + with fs.working_dir(self.build_directory): + fs.install_tree("out", prefix) + + spack.builder.run_after("install")(execute_install_time_tests) + + def check(self): + """Run "cargo test".""" + with fs.working_dir(self.build_directory): + inspect.getmodule(self.pkg).cargo("test", *self.check_args) diff --git a/lib/spack/spack/cmd/create.py b/lib/spack/spack/cmd/create.py index 946e9bc8b9..130242a8b1 100644 --- a/lib/spack/spack/cmd/create.py +++ b/lib/spack/spack/cmd/create.py @@ -172,6 +172,14 @@ class AutoreconfPackageTemplate(PackageTemplate): return args""" +class CargoPackageTemplate(PackageTemplate): + """Provides appropriate overrides for cargo-based packages""" + + base_class_name = "CargoPackage" + + body_def = "" + + class CMakePackageTemplate(PackageTemplate): """Provides appropriate overrides for CMake-based packages""" @@ -575,28 +583,29 @@ class SIPPackageTemplate(PackageTemplate): templates = { - "autotools": AutotoolsPackageTemplate, "autoreconf": AutoreconfPackageTemplate, - "cmake": CMakePackageTemplate, + "autotools": AutotoolsPackageTemplate, + "bazel": BazelPackageTemplate, "bundle": BundlePackageTemplate, - "qmake": QMakePackageTemplate, + "cargo": CargoPackageTemplate, + "cmake": CMakePackageTemplate, + "generic": PackageTemplate, + "intel": IntelPackageTemplate, + "lua": LuaPackageTemplate, + "makefile": MakefilePackageTemplate, "maven": MavenPackageTemplate, - "scons": SconsPackageTemplate, - "waf": WafPackageTemplate, - "bazel": BazelPackageTemplate, + "meson": MesonPackageTemplate, + "octave": OctavePackageTemplate, + "perlbuild": PerlbuildPackageTemplate, + "perlmake": PerlmakePackageTemplate, "python": PythonPackageTemplate, + "qmake": QMakePackageTemplate, "r": RPackageTemplate, "racket": RacketPackageTemplate, - "perlmake": PerlmakePackageTemplate, - "perlbuild": PerlbuildPackageTemplate, - "octave": OctavePackageTemplate, "ruby": RubyPackageTemplate, - "makefile": MakefilePackageTemplate, - "intel": IntelPackageTemplate, - "meson": MesonPackageTemplate, - "lua": LuaPackageTemplate, + "scons": SconsPackageTemplate, "sip": SIPPackageTemplate, - "generic": PackageTemplate, + "waf": WafPackageTemplate, } @@ -679,6 +688,7 @@ class BuildSystemGuesser: clues = [ (r"/CMakeLists\.txt$", "cmake"), (r"/NAMESPACE$", "r"), + (r"/Cargo\.toml$", "cargo"), (r"/configure$", "autotools"), (r"/configure\.(in|ac)$", "autoreconf"), (r"/Makefile\.am$", "autoreconf"), diff --git a/lib/spack/spack/package.py b/lib/spack/spack/package.py index f38ebec299..0fdfffc177 100644 --- a/lib/spack/spack/package.py +++ b/lib/spack/spack/package.py @@ -37,6 +37,7 @@ from spack.build_systems.cached_cmake import ( cmake_cache_path, cmake_cache_string, ) +from spack.build_systems.cargo import CargoPackage from spack.build_systems.cmake import CMakePackage, generator from spack.build_systems.cuda import CudaPackage from spack.build_systems.generic import Package |