summaryrefslogtreecommitdiff
path: root/Omni
diff options
context:
space:
mode:
authorBen Sima <ben@bsima.me>2024-12-21 15:13:05 -0400
committerBen Sima <ben@bsima.me>2024-12-21 14:13:05 -0500
commit70543fc1ef9733fb754cecda96805349cb36de32 (patch)
tree5ef26f4be19dac1f5272799be0c7abece9f83e28 /Omni
parent32d31ae8d1ef5d5aeb03a7fe7e6a294e14905505 (diff)
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
Diffstat (limited to 'Omni')
-rwxr-xr-x[-rw-r--r--]Omni/Bild.hs65
-rw-r--r--Omni/Bild/Builder.nix7
-rwxr-xr-x[-rw-r--r--]Omni/Bild/Deps.hs1
-rwxr-xr-x[-rw-r--r--]Omni/Bild/Example.c1
-rwxr-xr-x[-rw-r--r--]Omni/Bild/Example.hs1
-rwxr-xr-x[-rw-r--r--]Omni/Bild/Example.lisp1
-rwxr-xr-x[-rw-r--r--]Omni/Bild/Example.py1
-rwxr-xr-x[-rw-r--r--]Omni/Bild/Example.rs1
-rw-r--r--Omni/Bild/Meta.hs34
-rwxr-xr-xOmni/Bot.scm2
-rwxr-xr-xOmni/Ide/hooks/post-applypatch2
-rwxr-xr-xOmni/Ide/hooks/post-commit2
-rwxr-xr-xOmni/Ide/hooks/post-rewrite2
-rwxr-xr-xOmni/Ide/hooks/pre-auto-gc2
-rwxr-xr-xOmni/Ide/hooks/reference-transaction2
-rwxr-xr-xOmni/Ide/ns.sh1
-rwxr-xr-xOmni/Ide/run.sh1
-rwxr-xr-x[-rw-r--r--]Omni/Lint.hs1
-rwxr-xr-x[-rw-r--r--]Omni/Llamacpp.py1
-rw-r--r--Omni/Namespace.hs9
-rwxr-xr-x[-rw-r--r--]Omni/Repl.py1
-rwxr-xr-x[-rw-r--r--]Omni/Wipbase.c1
22 files changed, 73 insertions, 66 deletions
diff --git a/Omni/Bild.hs b/Omni/Bild.hs
index 648bfcb..92bbb78 100644..100755
--- a/Omni/Bild.hs
+++ b/Omni/Bild.hs
@@ -1,3 +1,4 @@
+#!/usr/bin/env run.sh
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE LambdaCase #-}
@@ -479,15 +480,13 @@ cab =
outToPath :: Meta.Out -> FilePath
outToPath = \case
- Meta.Bin o -> cab </> "bin" </> o
- Meta.Lib o -> cab </> "lib" </> o
- Meta.None -> mempty
+ Just o -> cab </> "bin" </> o
+ Nothing -> mempty
outname :: Meta.Out -> FilePath
outname = \case
- Meta.Bin o -> o
- Meta.Lib o -> o
- Meta.None -> mempty
+ Just o -> o
+ Nothing -> mempty
bindir, intdir, nixdir, vardir :: FilePath
bindir = cab </> "bin"
@@ -500,8 +499,7 @@ createHier :: String -> IO ()
createHier root =
traverse_
(Dir.createDirectoryIfMissing True)
- [ root </> (outToPath <| Meta.Bin ""),
- root </> (outToPath <| Meta.Lib ""),
+ [ root </> (outToPath <| Just ""),
root </> intdir,
root </> nixdir,
root </> vardir
@@ -524,7 +522,7 @@ analyze hmap ns = case Map.lookup ns hmap of
Just _ -> pure hmap
where
analyzeOne :: Namespace -> IO (Maybe Target)
- analyzeOne namespace@(Namespace _ ext) = do
+ analyzeOne namespace@(Namespace parts ext) = do
let path = Namespace.toPath namespace
root <- Env.getEnv "CODEROOT"
let abspath = root </> path
@@ -537,6 +535,10 @@ analyze hmap ns = case Map.lookup ns hmap of
IO.hSetEncoding h IO.utf8_bom
>> Text.IO.hGetContents h
/> Text.lines
+ -- if the file is exe but doesn't have 'out' metadata, just use the
+ -- dot-separated namespace instead
+ isExe <- Dir.getPermissions quapath /> Dir.executable
+ let defaultOut = isExe ?: (Just <| Namespace.dotSeparated parts, Nothing)
case ext of
-- basically we don't support building these
Namespace.Css -> pure Nothing
@@ -569,7 +571,7 @@ analyze hmap ns = case Map.lookup ns hmap of
sysdeps = psys,
langdeps = pdep,
outPath = outToPath pout,
- out = pout,
+ out = pout <|> defaultOut,
packageSet = "python.packages",
mainModule = Namespace.toModule namespace,
rundeps = prun,
@@ -586,13 +588,13 @@ analyze hmap ns = case Map.lookup ns hmap of
wrapper = Nothing,
compiler = Gcc,
builder = "c",
- out = pout,
+ out = pout <|> defaultOut,
packageSet = "c.packages",
mainModule = Namespace.toModule namespace,
compilerFlags = case pout of
- Meta.Bin o ->
+ Just o ->
["-o", o, path] <> Set.toList parg |> map Text.pack
- _ -> panic "can only bild C exes, not libs",
+ Nothing -> panic "can only bild C exes, not libs",
outPath = outToPath pout,
-- implement detectCImports, then I can fill this out
srcs = Set.empty,
@@ -627,18 +629,18 @@ analyze hmap ns = case Map.lookup ns hmap of
"$CODEROOT" </> quapath
]
++ case pout of
- Meta.Bin o ->
+ Just o ->
[ "-main-is",
Namespace.toHaskellModule namespace,
"-o",
o
]
- _ -> []
+ Nothing -> []
|> map Text.pack,
sysdeps = Meta.detect (Meta.sys "--") contentLines,
outPath = outToPath pout,
rundeps = prun,
- out = pout,
+ out = pout <|> defaultOut,
..
}
|> Just
@@ -689,8 +691,8 @@ analyze hmap ns = case Map.lookup ns hmap of
str <| "import " <> root </> "Omni/Bild.nix {}"
]
|> map Text.pack,
- out = Meta.None,
- outPath = outToPath Meta.None,
+ out = Nothing,
+ outPath = outToPath Nothing,
srcs = Set.empty,
packageSet = "",
mainModule = Namespace.toModule namespace,
@@ -718,12 +720,12 @@ analyze hmap ns = case Map.lookup ns hmap of
|> map Text.pack,
builder = "base",
outPath = outToPath pout,
- out = pout,
+ out = pout <|> defaultOut,
srcs = Set.empty, -- implement detectSchemeImports
-- TODO: wrapper should just be removed, instead rely on
-- upstream nixpkgs builders to make wrappers
wrapper =
- (pout == Meta.None)
+ isNothing pout
?: ( Nothing,
[ "#!/usr/bin/env bash",
"guile -C \""
@@ -754,17 +756,17 @@ analyze hmap ns = case Map.lookup ns hmap of
mainModule = Namespace.toModule namespace,
wrapper = Nothing,
sysdeps = psys <> Set.singleton "rustc",
- out = pout,
+ out = pout <|> defaultOut,
compiler = Rustc,
compilerFlags = case pout of
- Meta.Bin o ->
+ Just o ->
map
Text.pack
[ "$CODEROOT" </> path,
"-o",
o
]
- _ -> panic "can't build rust libs",
+ Nothing -> panic "can't build rust libs",
builder = "base",
outPath = outToPath pout,
-- implement detectRustImports
@@ -916,31 +918,24 @@ build andTest loud jobs cpus analysis =
forM (Map.elems analysis) <| \target@Target {..} ->
fst </ case compiler of
CPython -> case out of
- Meta.Bin _ ->
+ Just _ ->
Log.info ["bild", "nix", "python", nschunk namespace]
>> nixBuild loud jobs cpus target
+> (\r -> (isSuccess (fst r) && andTest) ?: (test loud target, pure r))
- _ ->
+ Nothing ->
Log.info ["bild", "nix", "python", nschunk namespace, "cannot build library"]
>> pure (Exit.ExitSuccess, mempty)
Gcc ->
- Log.info ["bild", label, "gcc", nschunk namespace]
+ Log.info ["bild", "nix", "gcc", nschunk namespace]
>> nixBuild loud jobs cpus target
- where
- label = case out of
- Meta.Bin _ -> "bin"
- _ -> "lib"
Ghc -> case out of
- Meta.None -> pure (Exit.ExitSuccess, mempty)
- Meta.Bin _ -> do
+ Nothing -> pure (Exit.ExitSuccess, mempty)
+ Just _ -> do
Log.info ["bild", "nix", user <> "@" <> host, nschunk namespace]
result <- nixBuild loud jobs cpus target
if andTest && (isSuccess <| fst result)
then test loud target
else pure result
- Meta.Lib _ -> do
- Log.info ["bild", "dev", "ghc-lib", nschunk namespace]
- proc loud namespace (toNixFlag compiler) compilerFlags
Guile -> do
Log.info ["bild", "dev", "guile", nschunk namespace]
_ <- proc loud namespace (toNixFlag compiler) compilerFlags
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
index c2fe53f..7183c26 100644..100755
--- 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
index 2f4bfd4..49b93f8 100644..100755
--- 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
index 025391c..3c7c144 100644..100755
--- 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
index cdabe7c..1063047 100644..100755
--- 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
index e5766ba..462a497 100644..100755
--- 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
index ba98dda..d6d738b 100644..100755
--- 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 =
diff --git a/Omni/Bot.scm b/Omni/Bot.scm
index c526507..ff81c53 100755
--- a/Omni/Bot.scm
+++ b/Omni/Bot.scm
@@ -1,3 +1,5 @@
+#!/usr/bin/env run.sh
+!#
;; : out omnibot
;;
;; Usage with ii:
diff --git a/Omni/Ide/hooks/post-applypatch b/Omni/Ide/hooks/post-applypatch
index 5071dc5..4919e84 100755
--- a/Omni/Ide/hooks/post-applypatch
+++ b/Omni/Ide/hooks/post-applypatch
@@ -1,4 +1,4 @@
-#!/bin/sh
+#!/usr/bin/env bash
## START BRANCHLESS CONFIG
git branchless hook post-applypatch "$@"
diff --git a/Omni/Ide/hooks/post-commit b/Omni/Ide/hooks/post-commit
index cd1f195..bdccee3 100755
--- a/Omni/Ide/hooks/post-commit
+++ b/Omni/Ide/hooks/post-commit
@@ -1,4 +1,4 @@
-#!/bin/sh
+#!/usr/bin/env bash
## START BRANCHLESS CONFIG
git branchless hook post-commit "$@"
diff --git a/Omni/Ide/hooks/post-rewrite b/Omni/Ide/hooks/post-rewrite
index 8b3237a..711cc0c 100755
--- a/Omni/Ide/hooks/post-rewrite
+++ b/Omni/Ide/hooks/post-rewrite
@@ -1,4 +1,4 @@
-#!/bin/sh
+#!/usr/bin/env bash
## START BRANCHLESS CONFIG
git branchless hook post-rewrite "$@"
diff --git a/Omni/Ide/hooks/pre-auto-gc b/Omni/Ide/hooks/pre-auto-gc
index c92a844..30d4a44 100755
--- a/Omni/Ide/hooks/pre-auto-gc
+++ b/Omni/Ide/hooks/pre-auto-gc
@@ -1,4 +1,4 @@
-#!/bin/sh
+#!/usr/bin/env bash
## START BRANCHLESS CONFIG
git branchless hook pre-auto-gc "$@"
diff --git a/Omni/Ide/hooks/reference-transaction b/Omni/Ide/hooks/reference-transaction
index ea0cce6..474e52d 100755
--- a/Omni/Ide/hooks/reference-transaction
+++ b/Omni/Ide/hooks/reference-transaction
@@ -1,4 +1,4 @@
-#!/bin/sh
+#!/usr/bin/env bash
## START BRANCHLESS CONFIG
# Avoid canceling the reference transaction in the case that `branchless` fails
diff --git a/Omni/Ide/ns.sh b/Omni/Ide/ns.sh
index a56ed89..cddb35a 100755
--- a/Omni/Ide/ns.sh
+++ b/Omni/Ide/ns.sh
@@ -27,6 +27,7 @@ fzf_flags=(
--bind "alt-enter:execute(repl.sh --bash {})"
--bind "ctrl-space:execute(ship.sh {} ; read -p [fin])"
--bind "alt-t:execute(bild {} ; run.sh {} test ; read -p [fin])"
+ --bind "alt-e:execute({} ; read -p [fin])"
--bind "ctrl-/:change-preview-window(right,88|right,70%|hidden|)"
--bind "alt-0:change-preview(bat -p --color=always {})"
--bind "alt-1:change-preview(git log --color=always --date=relative --abbrev-commit --pretty=format:'%Cred%h%Creset %s / %an %Creset%C(yellow)%d%Creset%Cgreen(%cr)%Creset' -- {})"
diff --git a/Omni/Ide/run.sh b/Omni/Ide/run.sh
index 506aa92..e300fcc 100755
--- a/Omni/Ide/run.sh
+++ b/Omni/Ide/run.sh
@@ -3,4 +3,5 @@ set -eu
target=$1
shift
out=$(bild --plan "$target" | jq --raw-output ".\"${target}\".out")
+[[ -f "$out" ]] || bild "$target"
exec "${CODEROOT:?}/_/bin/$out" "$@"
diff --git a/Omni/Lint.hs b/Omni/Lint.hs
index 45d1523..0cc428a 100644..100755
--- a/Omni/Lint.hs
+++ b/Omni/Lint.hs
@@ -1,3 +1,4 @@
+#!/usr/bin/env run.sh
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
diff --git a/Omni/Llamacpp.py b/Omni/Llamacpp.py
index 66b57d8..b8975f2 100644..100755
--- a/Omni/Llamacpp.py
+++ b/Omni/Llamacpp.py
@@ -1,3 +1,4 @@
+#!/usr/bin/env run.sh
"""
Test that llama.cpp can build and exec in the omni repo.
diff --git a/Omni/Namespace.hs b/Omni/Namespace.hs
index ef8cefd..5884507 100644
--- a/Omni/Namespace.hs
+++ b/Omni/Namespace.hs
@@ -17,6 +17,7 @@ module Omni.Namespace
fromPythonModule,
isCab,
groupByExt,
+ dotSeparated,
)
where
@@ -93,13 +94,17 @@ fromHaskellContent c = case Regex.findFirstInfix haskellModule c of
</ (Regex.string "\nmodule " *> Regex.many (name <|> dot))
<*> pure Hs
+dotSeparated :: [String] -> String
+dotSeparated = joinWith "."
+
toModule :: Namespace -> String
-toModule (Namespace parts Hs) = joinWith "." parts
-toModule (Namespace parts Py) = joinWith "." parts
+toModule (Namespace parts Hs) = dotSeparated parts
+toModule (Namespace parts Py) = dotSeparated parts
toModule (Namespace parts Scm) = "(" ++ joinWith " " parts ++ ")"
toModule (Namespace parts Rs) = joinWith "::" parts
toModule (Namespace parts C) = joinWith "/" parts <> ".c"
toModule (Namespace parts Nix) = joinWith "/" parts <> ".nix"
+toModule (Namespace parts Lisp) = "(" ++ joinWith " " parts ++ ")"
toModule (Namespace {..}) = panic <| "toModule not implemented for " <> show ext
toHaskellModule :: Namespace -> String
diff --git a/Omni/Repl.py b/Omni/Repl.py
index 1cf2f65..8d191e2 100644..100755
--- a/Omni/Repl.py
+++ b/Omni/Repl.py
@@ -1,3 +1,4 @@
+#!/usr/bin/env run.sh
"""
Improve the standard Python REPL.
diff --git a/Omni/Wipbase.c b/Omni/Wipbase.c
index 31996a2..1ee1978 100644..100755
--- a/Omni/Wipbase.c
+++ b/Omni/Wipbase.c
@@ -1,3 +1,4 @@
+//usr/bin/env run.sh $0 "$@"; exit
// : out wip