From 7e59ce132303837b05aaa5f173ea2050ac0203f6 Mon Sep 17 00:00:00 2001 From: Ben Sima Date: Thu, 14 Jul 2022 01:45:28 -0400 Subject: Add basic Rust support Support for packages and third-party imports will need to come later once I figure out how to lookup rust packages by their import statements. Until then, this works to compile "hello world". --- Biz/Bild.hs | 34 ++++++++++++++++++++++++++++++++++ Biz/Bild.nix | 21 +++++++++++++++++++++ Biz/Bild/Example.rs | 4 ++++ Biz/Namespace.hs | 3 ++- 4 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 Biz/Bild/Example.rs (limited to 'Biz') diff --git a/Biz/Bild.hs b/Biz/Bild.hs index d089048..cba5232 100644 --- a/Biz/Bild.hs +++ b/Biz/Bild.hs @@ -209,6 +209,7 @@ data Compiler | GhcjsLib | Guile | NixBuild + | Rustc deriving (Eq, Show, Generic, Aeson.ToJSON) data Target = Target @@ -233,6 +234,7 @@ data Target = Target isBuildableNs :: Namespace -> Bool isBuildableNs (Namespace (x : _) Namespace.Hs) | x /= "Hero" = True isBuildableNs (Namespace _ Namespace.Scm) = True +isBuildableNs (Namespace _ Namespace.Rs) = True isBuildableNs ns | ns `elem` nixTargets = True | otherwise = False @@ -370,6 +372,21 @@ analyze path = do builder = user <> "@localhost", .. } + Namespace.Rs -> do + pure + Target + { langdeps = Set.empty, + sysdeps = Set.empty, + compiler = Rustc, + out = + contentLines + /> Text.unpack + /> Regex.match (metaOut "//") + |> catMaybes + |> head, + builder = user <> "@localhost", + .. + } _ -> pure Target @@ -568,6 +585,23 @@ build andTest loud target@Target {..} = do Copy -> do Log.warn ["bild", "copy", "TODO", nschunk namespace] pure Exit.ExitSuccess + Rustc -> do + Log.info + [ "bild", + "rust", + if Text.null builder + then "local" + else builder, + nschunk namespace + ] + proc + loud + namespace + "rustc" + [ path, + "-o", + root bindir Maybe.fromJust out + ] data Proc = Proc { loud :: Bool, diff --git a/Biz/Bild.nix b/Biz/Bild.nix index b640a05..fdaba0e 100644 --- a/Biz/Bild.nix +++ b/Biz/Bild.nix @@ -153,6 +153,7 @@ in rec { ormolu python38Packages.black python38Packages.pylint + rustc shellcheck wemux ] ++ lib.optional nixpkgs.stdenv.isLinux [ @@ -168,5 +169,25 @@ in rec { pkgs = { inherit (nixpkgs) git; }; + rust = main: + let + data = analyze main; + rustc = nixpkgs.pkgs.rustc; + in stdenv.mkDerivation { + name = lib.string.concatStringsSep "::" data.namespace.path; + src = ../.; + nativeBuildInputs = [ rustc ]; + strictDeps = true; + buildPhase = '' + set -eux + mkdir -p $out/bin + : compiling with rustc + ${rustc}/bin/rustc \ + ${main} \ + -o $out/bin/${data.out} + ''; + installPhase = "exit 0"; + }; + image = nixpkgs.pkgs.dockerTools.buildImage; } diff --git a/Biz/Bild/Example.rs b/Biz/Bild/Example.rs new file mode 100644 index 0000000..ba98dda --- /dev/null +++ b/Biz/Bild/Example.rs @@ -0,0 +1,4 @@ +// : out helloworld +fn main() { + println!("Hello world!"); +} diff --git a/Biz/Namespace.hs b/Biz/Namespace.hs index 2312271..b9ef994 100644 --- a/Biz/Namespace.hs +++ b/Biz/Namespace.hs @@ -24,7 +24,7 @@ import qualified Data.List as List import qualified Data.List.Split as List import qualified Text.Regex.Applicative as Regex -data Ext = Hs | Scm | Nix | Md | Css | Py | Sh | Keys | Json | None +data Ext = Hs | Scm | Nix | Md | Css | Py | Sh | Keys | Json | None | Rs deriving (Eq, Show, Generic, Aeson.ToJSON) data Namespace = Namespace {path :: [String], ext :: Ext} @@ -87,6 +87,7 @@ reExt = <|> Sh <$ Regex.string "sh" <|> Keys <$ Regex.string "pub" <|> Json <$ Regex.string "json" + <|> Rs <$ Regex.string "rs" -- | The cab dir is for temporary files and build outputs, not for source -- inputs. -- cgit v1.2.3