Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build castor apps as a single binary #257

Closed
tigitz opened this issue Jan 23, 2024 · 11 comments · Fixed by #260
Closed

Build castor apps as a single binary #257

tigitz opened this issue Jan 23, 2024 · 11 comments · Fixed by #260

Comments

@tigitz
Copy link
Contributor

tigitz commented Jan 23, 2024

I enjoy the simplicity of Castor for scripting and automation, yet I'd prefer to avoid the need to install PHP everywhere. I'm curious about the possibility of making my custom Castor apps more portable by turning them into a single binary file. That would be awesome.

There's been some recent advancement in PHP that supports this idea, especially seen in https://github.com/crazywhalecc/static-php-cli. This method is already effective in several projects:

Considering this, I'm thinking of developing a native Castor feature to combine Castor, PHP, and user-defined tasks into one binary.

I'm planning to tackle this project myself, but I wanted to share the concept here for any feedback or if anyone find the idea interesting enough to collaborate on this.

@lyrixx
Copy link
Member

lyrixx commented Jan 24, 2024

I already test it, but I was disappointed by the size of the binary. Can't wait what you will be able to do 🤩

@tigitz
Copy link
Contributor Author

tigitz commented Jan 24, 2024

I already test it, but I was disappointed by the size of the binary. Can't wait what you will be able to do 🤩

Interesting, that's what I was most scared of yeah. What was the final size ? Compressed ?

If you have a solution branch that could help me to kickstart working on this issue, feel free to share. Thanks in advance! 🙂

@lyrixx
Copy link
Member

lyrixx commented Jan 24, 2024

It was a POC, and I trashed everything. Sorry. I remember something about 100Mb. 😬

But I'm pretty sure we can do much better nowadays!

@tigitz
Copy link
Contributor Author

tigitz commented Jan 24, 2024

To track my progression, in an hour, I was able to produce a succesfully running single binary of castor.linux-amd64.phar of only 14M 🤩 with these instructions:

# Download SPC binary to manage binary build
curl -o spc https://dl.static-php.dev/static-php-cli/spc-bin/nightly/spc-linux-x86_64 && chmod +x ./spc

# Download castor.phar
curl -L -o castor.linux-amd64.phar https://github.com/jolicode/castor/releases/download/v0.11.1/castor.linux-amd64.phar

# Download PHP build dependencies required by Castor
./spc download --for-extensions=openssl,pcntl,mbstring,curl,phar,posix,tokenizer

# Build PHP micro version
./spc build openssl,pcntl,mbstring,curl,phar,posix,tokenizer --build-micro

# Merge built PHP and Castor into a single binary
cat buildroot/bin/micro.sfx castor.linux-amd64.phar > castor-app && chmod a+x castor-app

# Run Castor as a single binary
./castor-app

image

If you put a castor.php file next to the binary, it just works.


So, the main problem we're dealing with now is figuring out how to pack castor.phar and the user-defined castor.php, along with any dependencies it may have, into a phar of its own.

@lyrixx
Copy link
Member

lyrixx commented Jan 24, 2024

This is awesome ❤️

So, the main problem we're dealing with now is figuring out how to pack castor.phar and the user-defined castor.php, along with any dependencies it may have, into a phar of its own.

I'm not sure we want to do that. BUT if you really want, I invite you to read this doc

@tigitz
Copy link
Contributor Author

tigitz commented Jan 25, 2024

I've succeded repacking my whole Castor app into a single binary thanks to the castor repack method like suggested.

It's still around 16M and now make our scripts more portable and much easier to distribute, from local workstations to servers. I think this is a huge win over the current task runner alternatives.

In which form would you like to see the single binary feature introduced ?

  • castor.phar compiled in CI to a single binary for different archs and distributed through Github release ? with 2 basic and full PHP extensions versions (one for basic tasks, the other for big apps) ? Like downloading/installing make or task
  • Simple doc with instructions to compile castor.phar only or castor + custom castor app to a single binary ?
  • Native Castor task (similar to repack) to automate the process of building app to a single binary ?

@lyrixx
Copy link
Member

lyrixx commented Jan 29, 2024

First, thanks a lot for digging this project! And congrats for achieving it! this is really appreciated. 💛


In which form would you like to see the single binary feature introduced ?

Well, I haven't strong opinion on this. I don't really know the main usages of castor. In our company, we all have PHP installed everywhere. So this feature will not really solve any issue. BUT maybe if castor was shipped as a standalone binary, it may change our usages and maybe simplify things... I don't know wet.

castor.phar compiled in CI to a single binary for different archs and distributed through Github release ? with 2 basic and full PHP extensions versions (one for basic tasks, the other for big apps) ? Like downloading/installing make or task

I think it could be nice, but we should not replace the current versions. (size matter)

Simple doc with instructions to compile castor.phar only or castor + custom castor app to a single binary ?

May be, but It would be useless if we do the next point.

Native Castor task (similar to repack) to automate the process of building app to a single binary ?

I think it's the most important one. First it'll be needed for archiving the first point. And is will also fix the second point. We don't need instructions if something do it for us :)


So IMHO, we could go first with the last point, merge it, play a bit with it, and then adapt our plan. WDYT?

@tigitz
Copy link
Contributor Author

tigitz commented Jan 29, 2024

Sounds good 👍

I'll proceed with the native castor task then.

First, thanks a lot for digging this project! And congrats for achieving it! this is really appreciate. 💛

Thanks to you guys! You've turned a tool I envisioned for a long time into reality so I'm eager to contribute to its success. 🙂

@lyrixx
Copy link
Member

lyrixx commented Jan 29, 2024

Before you work on it, Can I suggest to add a flag --standalone ou --static to the current repack command command? Then, all the code for both mode can live in the Command, and only if it make sens, to create to service to not bloat the Command. But the current command is not so long, so I think everything can go in it

@tigitz
Copy link
Contributor Author

tigitz commented Jan 29, 2024

Before you work on it, Can I suggest to add a flag --standalone ou --static to the current repack command command? Then, all the code for both mode can live in the Command, and only if it make sens, to create to service to not bloat the Command. But the current command is not so long, so I think everything can go in it

I was thinking it's a good and pragmatic way to implement it but then I realized compiling as a binary will require a good amount of dedicated options like the target PHP version, the compilation source (php micro vs php cli) or the required PHP extensions.

I'm afraid it could bloat the repack command --help output with options that are strictly relevent to the binary compilation part.

Having a dedicated command might help avoiding that and keep a clean separation of concerns.

Anyway, I'll prefix the description of those options with "Only in --static mode, ..." in a first iteration and see how it feels DX wise.

@lyrixx
Copy link
Member

lyrixx commented Jan 29, 2024

I didn't think about all theses option! Let's go for another command then. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants