igor-petruk / scriptisto

A language-agnostic "shebang interpreter" that enables you to write scripts in compiled languages.
Apache License 2.0
909 stars 25 forks source link

docs/design: document windows support or use PE file for Windows/DOS and shebang hack #36

Open matu3ba opened 1 year ago

matu3ba commented 1 year ago

For example with zig executable in PATH (but also without and given the path to the executable) I can do:

//usr/bin/env zig run --global-cache-dir "/tmp/zigruns" --enable-cache "$0" -- "$@" | tail -n +2; exit
// ^
// for no cache dir printed.
// with cache dir:
//usr/bin/env zig run --global-cache-dir "/tmp/zigruns" --enable-cache "$0" -- "$@"; exit
// without cache dir:
//usr/bin/env zig run "$0" -- "$@"; exit

// Only works with files with file ending .zig

pub fn main() void {
    @import("std").debug.print("hello there\n", .{});
}

and chmod +x ./example.zig && ./example.zig just works. Likewise one can use the cache to compile C and C++ programs and afaik newer compiled languages all come up with their own caching system.

I would assume that Rust has a similar option to let the user set the cache directory?

Note, that this shebang hack is also used by https://justine.lol/ape.html by building "naked" thompson shell script without a shebang line that works as a PE file for Windows/DOS and use exec to feed a binary to the shell via pipe. You could copy-paste that approach, if there is currently no Windows support.

igor-petruk commented 1 year ago

Hi, thank you for pointers.

I still do not fully understand what is suggested here.

In the example provided with Zig, I do not see a particular hack. I see a shebang line pointing directly at zig run because it supports this as a feature. There is no equivalent rust run or clang++ run that would take a Rust program or C++ program as text, build it, run it and cache it for the next time. Scriptisto does it in agnostic way, the compilers are used in normal mode.

As for Windows support, there is none. I have no objections to have it, but I haven't done any work on it just yet. Some work might be needed because I am not sure if all my path construction are correct, or if all dependencies work, or if I can do exec or similar under Windows. I have never even build a scriptisto binary under Windows. Windows support is complicated. Scriptisto combines an ability to edit a file that has it's natural extension like .cc in the editor that will recognize it as a C++ file to highlight it and an ability to run it. The ability to run it with, say, double-click, on Windows requires a completely different extension and this is a conflicting requirement from the Windows OS.

I've looked at https://justine.lol/ape.html and it is known that in some OS, Linux for example, you can install your own "interpreters" and "loaders" for binary files with a certain magic number via binfmt. I don't see how to apply this to scriptisto which are just normal text files. I would not be asking a user to have program.cc with a binary prefix at the beginning of the file to scare away any editor :)

One idea that comes to my mind for Windows is to have a dedicated file extension of scriptisto and register the tool to be an interpreter. Unfortunately in that case the use will have to manually set the highlighting in the editor each time, but that's a minor problem. The rest of the issues I probably not so fundamental.

I feel like I am missing some point.

matu3ba commented 1 year ago

There is no equivalent rust run or clang++ run that would take a Rust program or C++ program as text, build it, run it and cache it for the next time.

That's true. There is only cargo run. C and C++ have no convention on how to build + run code. However I dont quite understand, why

ccache clang -O2 main.c `pkg-config --libs --cflags glib-2.0 \` -o ./script && ./script

would not work. I do however agree, more effort to handle ccache additional programs, if needed.

The ability to run it with, say, double-click, on Windows requires a completely different extension and this is a conflicting requirement from the Windows OS.

Thats very much true. This is how python does it: https://lwn.net/Articles/909410/