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.hs | 65 ++++++++++++++++++++++++++++-------------------------------- 1 file changed, 30 insertions(+), 35 deletions(-) mode change 100644 => 100755 Omni/Bild.hs (limited to 'Omni/Bild.hs') diff --git a/Omni/Bild.hs b/Omni/Bild.hs old mode 100644 new mode 100755 index 648bfcb..92bbb78 --- 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 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 -- cgit v1.2.3