From 70543fc1ef9733fb754cecda96805349cb36de32 Mon Sep 17 00:00:00 2001 From: Ben Sima Date: Sat, 21 Dec 2024 15:13:05 -0400 Subject: Add shebangs and x bit to executables With run.sh, we can build and run the file in one go. This means we can also use it as an interpreter in a shebang line and properly use the Unix executable bit. This is pretty cool and gives a few advantages: running any executable file is just `exec file.hs` or even `./file.hs`, finding all executables is `fd -t x`, you don't need to specify or know an `out` name to run something, execution of a program is standardized. There is a hack to get this to work. In C and Common Lisp, `#!` is illegal syntax, so I had to use shell syntax to invoke run.sh, call it on the current file, and then exit the shell script. Meanwhile, run.sh takes the file and evals the whole thing, building and running it. As long as either `//` or `;` is a comment character in the target language, then this works. Maybe a better thing to do would be to pre-process the file and remove the `#!` before passing it to the C compiler, like [ryanmjacobs/c][1] and [tcc][2]? However this won't work in Lisp because then I can't just load the file directly into the repl, so maybe the comment hack needs to stay. [1]: https://github.com/ryanmjacobs/c/tree/master [2]: https://repo.or.cz/tinycc.git/blob/HEAD:/tccrun.c --- Omni/Bild/Builder.nix | 7 ++++++- Omni/Bild/Deps.hs | 1 + Omni/Bild/Example.c | 1 + Omni/Bild/Example.hs | 1 + Omni/Bild/Example.lisp | 1 + Omni/Bild/Example.py | 1 + Omni/Bild/Example.rs | 1 + Omni/Bild/Meta.hs | 34 +++++++++++----------------------- 8 files changed, 23 insertions(+), 24 deletions(-) mode change 100644 => 100755 Omni/Bild/Deps.hs mode change 100644 => 100755 Omni/Bild/Example.c mode change 100644 => 100755 Omni/Bild/Example.hs mode change 100644 => 100755 Omni/Bild/Example.lisp mode change 100644 => 100755 Omni/Bild/Example.py mode change 100644 => 100755 Omni/Bild/Example.rs (limited to 'Omni/Bild') diff --git a/Omni/Bild/Builder.nix b/Omni/Bild/Builder.nix index a78f311..b4e6780 100644 --- a/Omni/Bild/Builder.nix +++ b/Omni/Bild/Builder.nix @@ -124,7 +124,12 @@ let . ${commonBash} cp ${../../pyproject.toml} ./pyproject.toml check ruff format --exclude 'setup.py' --check . - check ruff check --exclude 'setup.py' --exclude '__init__.py' . + # ignore EXE here to support run.sh shebangs + check ruff check \ + --ignore EXE \ + --exclude 'setup.py' \ + --exclude '__init__.py' \ + . touch ./py.typed check python -m mypy \ --explicit-package-bases \ diff --git a/Omni/Bild/Deps.hs b/Omni/Bild/Deps.hs old mode 100644 new mode 100755 index c2fe53f..7183c26 --- a/Omni/Bild/Deps.hs +++ b/Omni/Bild/Deps.hs @@ -1,3 +1,4 @@ +#!/usr/bin/env run.sh {-# LANGUAGE DerivingStrategies #-} {-# LANGUAGE GeneralizedNewtypeDeriving #-} {-# LANGUAGE LambdaCase #-} diff --git a/Omni/Bild/Example.c b/Omni/Bild/Example.c old mode 100644 new mode 100755 index 2f4bfd4..49b93f8 --- a/Omni/Bild/Example.c +++ b/Omni/Bild/Example.c @@ -1,3 +1,4 @@ +//usr/bin/env run.sh $0; exit // : out examplesodium.exe // : dep libsodium // : arg -lsodium diff --git a/Omni/Bild/Example.hs b/Omni/Bild/Example.hs old mode 100644 new mode 100755 index 025391c..3c7c144 --- a/Omni/Bild/Example.hs +++ b/Omni/Bild/Example.hs @@ -1,3 +1,4 @@ +#!/usr/bin/env run.sh {-# LANGUAGE QuasiQuotes #-} {-# LANGUAGE NoImplicitPrelude #-} diff --git a/Omni/Bild/Example.lisp b/Omni/Bild/Example.lisp old mode 100644 new mode 100755 index cdabe7c..1063047 --- a/Omni/Bild/Example.lisp +++ b/Omni/Bild/Example.lisp @@ -1,3 +1,4 @@ +;/usr/bin/env run.sh $0; exit ;; : out helloworld.exe (require 'alexandria) (defun main () diff --git a/Omni/Bild/Example.py b/Omni/Bild/Example.py old mode 100644 new mode 100755 index e5766ba..462a497 --- a/Omni/Bild/Example.py +++ b/Omni/Bild/Example.py @@ -1,3 +1,4 @@ +#!/usr/bin/env run.sh """ Test that bild can build Python stuff. diff --git a/Omni/Bild/Example.rs b/Omni/Bild/Example.rs old mode 100644 new mode 100755 index ba98dda..d6d738b --- a/Omni/Bild/Example.rs +++ b/Omni/Bild/Example.rs @@ -1,3 +1,4 @@ +#!/usr/bin/env run.sh // : out helloworld fn main() { println!("Hello world!"); diff --git a/Omni/Bild/Meta.hs b/Omni/Bild/Meta.hs index 44bcff0..a5f722e 100644 --- a/Omni/Bild/Meta.hs +++ b/Omni/Bild/Meta.hs @@ -1,4 +1,3 @@ -{-# LANGUAGE LambdaCase #-} {-# LANGUAGE RecordWildCards #-} {-# LANGUAGE NoImplicitPrelude #-} @@ -6,7 +5,6 @@ module Omni.Bild.Meta where import Alpha -import qualified Data.Aeson as Aeson import qualified Data.Char as Char import qualified Data.Set as Set import qualified Data.Text as Text @@ -29,15 +27,7 @@ type Run = String -- command. Should be used sparingly, and not all builds will support this. type Arg = String -data Out = Lib String | Bin String | None - deriving (Show, Eq) - -instance Aeson.ToJSON Out where - toJSON = - Aeson.String <. Text.pack <. \case - Bin a -> a - Lib a -> a - None -> "" +type Out = Maybe String data Parsed = Parsed { pdep :: Set Dep, @@ -56,19 +46,24 @@ detect m cl = |> Set.fromList -- | 'Out' is always singular, so it gets a special function -detectOut :: Regex.RE Char Out -> [Text] -> Out +detectOut :: Regex.RE Char String -> [Text] -> Out detectOut m cl = cl /> Text.unpack /> Regex.match m |> catMaybes |> head - |> fromMaybe None -detectAll :: [Char] -> [Text] -> Parsed +-- | Detect all metadata +detectAll :: + -- | The comment character(s) to match on + [Char] -> + -- | Lines of text to parse + [Text] -> + Parsed detectAll m cl = Parsed {..} where - pout = detectOut (out m <|> lib m) cl + pout = detectOut (out m) cl detect_ re = detect (re m) cl pdep = detect_ dep psys = detect_ sys @@ -85,17 +80,10 @@ sys comment = Regex.string (comment ++ " : sys ") *> Regex.many (Regex.psym (not <. Char.isSpace)) -out :: [Char] -> Regex.RE Char Out +out :: [Char] -> Regex.RE Char String out comment = Regex.string (comment ++ " : out ") *> Regex.many (Regex.psym (/= ' ')) - /> Bin - -lib :: [Char] -> Regex.RE Char Out -lib comment = - Regex.string (comment ++ " : lib ") - *> Regex.many (Regex.psym (/= ' ')) - /> Lib arg :: [Char] -> Regex.RE Char Arg arg comment = -- cgit v1.2.3