Skip to content

Add a cosmopolitan build #438

Closed
Closed

Description

@MulverineX

https://github.com/jart/cosmopolitan

The cosmo version of qjsc would build to .com cosmopolitan binaries

Activity

  1. changed the title [-]Cosmopolitan builds[/-] [+]Add a cosmopolitan build[/+] on Jun 24, 2024
  2. saghul commented on Jun 24, 2024

    @saghul
    Contributor

    I think it should be easy to add to the CI and to the release job. Would you be willing to send a PR?

    The cosmo version of qjsc would build to .com cosmopolitan binaries

    What do you mean by this?

  3. MulverineX commented on Jun 24, 2024

    @MulverineX
    Author

    I'm not really all that experienced with this, I just suggested this because upstream has a cosmo build.

    What do you mean by this?

    Meaning, instead of qjsc building a standalone executable from a script for a given platform, it would build for cosmo

  4. saghul commented on Jun 24, 2024

    @saghul
    Contributor

    Got it, it was on my mental list too.

    Our qjsc doesn't build binaries, it just generates C files that the user can compile however they see fit.

  5. MulverineX commented on Jun 24, 2024

    @MulverineX
    Author

    Ah okay, so a user would just take that c file and compile it with Cosmo, great!

  6. chqrlie commented on Jun 24, 2024

    @chqrlie
    Collaborator

    Our qjsc doesn't build binaries, it just generates C files that the user can compile however they see fit.

    qjsc does build binaries in the bellard/quickjs project. Removing this possibility on the targets where it is easy is a regression. Compiling the generated C file manually is not so straightforward. We should reactivate this feature. qjsc would still generate the C file with the -c command line option.

  7. saghul commented on Jun 24, 2024

    @saghul
    Contributor

    It's not portable at all (perhaps except with cosmo) since it assumes a given compiler and platform, and it calls exec and other Unix-only functions.

    The way Deno does it would be nice perhaps: take the qjs executable and add the compiled bytecode as trailing data. Then when qjs is run, it scans itself for some magic value and if found it executes that bytecode and skips the normal CLI run.

  8. chqrlie commented on Jun 24, 2024

    @chqrlie
    Collaborator

    The resulting executable is not portable across architectures or portable only to a limited set of targets (with cosmo), but on many architectures (linux, BSD, macOS) it can be run on a variety of versions of said targets, which is still useful.

  9. saghul commented on Jun 24, 2024

    @saghul
    Contributor

    I respectfully disagree :-) My proposal above would accomplish the same, I reckon, without even having to call the compiler. Deno has been using it succesfully roo, FWIW.

  10. chqrlie commented on Jun 24, 2024

    @chqrlie
    Collaborator

    One does not preclude the other. There are multiple options to create standalone executable files:

    • prepending a #! line on unix targets (this requires installing the qjs executable and make the implementation obvious)
    • appending the source code to the qjs interpreter is a possibility and could be a simple option (this does not hide the implementation)
    • compiling to bytecode and appending that to qjs is simple and does hide the implementation (albeit reverse engineering can still be performed, but not easily)
    • compiling to bytecode and embedding in a custom built executable, as is done in qjsc directly or via separate compilation creates smaller executables that may load faster. This is somewhat of a technical prowess and not so useful anymore in a time of global resource wastage and extravagant software bloat.

    Creating an executable from the qjs runtime and the bytecode file with a simple tail signature as in .zip files is indeed a good idea. In fact, it could be implemented inside qjs itself at a minimal cost.

  11. saghul commented on Jun 24, 2024

    @saghul
    Contributor

    I think 1 and 2 would be similar unless we strip the source when byte-compiling, right? Assuming we don't, they'd be equivalent, from the prespective of stack traces and such.

    I'll give that idea a try when I find some time. Still have some pending reviews 😅

  12. saghul commented on Jun 24, 2024

    @saghul
    Contributor

    An additional benefit from appending the data to the qjs binary would also be that one could realistically take some binary from another platform and make it standalone, no need to cross-compile!

  13. chqrlie commented on Jun 24, 2024

    @chqrlie
    Collaborator

    Implementing this inside qjs itself seems feasible at minimal cost and trivially simplifies access to the qjs runtime :)

    Loading the bytecode from an external source such as an executable for a different platform solves the portability issue (for a given byte code version).

  14. soulofmischief commented on Mar 11, 2025

    @soulofmischief

    I already do this now by bootstrapping with a small C file that runs an appended zipaligned script with qjs, similarly to how llamafile works. The result is a single cross-platform binary that runs a script which I can pass arguments to. Happy to share code if this would be useful to anyone.

    I would definitely prefer just running bytecode directly though, and without having to create a temporary file as I do now, since that can be insecure (though attack vectors are limited since the file is immediately read into memory and then ignored)

  15. saghul commented on Mar 11, 2025

    @saghul
    Contributor

    @soulofmischief We now support standalone binaries like so: #739

  16. sugoidogo commented on Oct 22, 2026

    @sugoidogo

    Is this planned to be implemented? I would like to be able to call qjs.com -c main.js to produce a js ape

  17. saghul commented on Oct 22, 2026

    @saghul
    Contributor

    A or would be welcome. I think it should be easier now, by using the amalgamated build as the source.

  18. bnoordhuis commented on Oct 30, 2026

    @bnoordhuis
    Contributor

    Fixed by #1839. Next release includes an APE binary.

  19. sugoidogo commented on Nov 2, 2026

    @sugoidogo

    Initial basic test passes on Bazzite Linux, Windows 10 LTSC, and macos 12.7.4, all x86_64. qjs opens repl and correctly prints os.platform for each. Mac required running with sh instead of the default zsh as noted in the cosmocc readme, but my macos machine is rather old and this may not be needed on modern macos versions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions