[erlang-questions] Making a minimal distributable program in Erlang.

Peer Stritzinger peer@REDACTED
Thu Sep 21 17:45:47 CEST 2017


> On 21.09.2017, at 13:15, Frank Muller <frank.muller.erl@REDACTED> wrote:
> 
> Stefan,
> 
> By statically linking bins, Joe is talking about the executables here (erl.exe, ...), not the beam files IMHO.

But what you can do is preload all needed beam files, which means they get baked into your erts executable.

So you can even have statically linked beam files.  Every Erlang system has some of those since quite a lot
of the system startup is done in Erlang already.   And to get to the point where the runtime can load beam files
you need some preexisting beam files.

When building this is done by this perl script and its surrounding Makefile magic:

erts/emulator/utils/make_preload 

Which generates a C file called preload.c starting like:

/*
 * DO NOT EDIT THIS FILE.  It was automatically generated
 * by the `make_preload' program on Fri Jun 23 12:55:01 2017.
 */
unsigned preloaded_size_otp_ring0 = 532;
unsigned char preloaded_otp_ring0[] = {
0x46,0x4f,0x52,0x31,0x00,0x00,0x02,0x0c, /* FOR1.... */
  0x42,0x45,0x41,0x4d,0x41,0x74,0x6f,0x6d, /* BEAMAtom */
  0x00,0x00,0x00,0x82,0x00,0x00,0x00,0x10, /* ........ */
  0x09,0x6f,0x74,0x70,0x5f,0x72,0x69,0x6e, /* .otp_rin */
  0x67,0x30,0x05,0x73,0x74,0x61,0x72,0x74, /* g0.start */
  0x04,0x62,0x6f,0x6f,0x74,0x04,0x69,0x6e, /* .boot.in */
  0x69,0x74,0x03,0x72,0x75,0x6e,0x06,0x65, /* it.run.e */
  0x72,0x6c,0x61,0x6e,0x67,0x11,0x66,0x75, /* rlang.fu */
  0x6e,0x63,0x74,0x69,0x6f,0x6e,0x5f,0x65, /* nction_e */
  0x78,0x70,0x6f,0x72,0x74,0x65,0x64,0x05, /* xported. */
  0x66,0x61,0x6c,0x73,0x65,0x04,0x74,0x72, /* false.tr */
  0x75,0x65,0x05,0x66,0x61,0x74,0x61,0x6c, /* ue.fatal */
  0x05,0x65,0x72,0x72,0x6f,0x72,0x06,0x6d, /* .error.m */
  0x6f,0x64,0x75,0x6c,0x65,0x07,0x64,0x69, /* odule.di */

And I suppose that you still can hot code load preloaded files, its just that their memory isn’t freed
since it is in the erts executable.

Best,
-- Peer 


> 
> You'll be able to reload your beam modules on the fly, obviously.
> 
> Moreover, nothing prevent a statically built executable to load a shared lib while running.
> 
> @Joe: just made a quick search and found UPX binary packer. It's cross-platform:
> https://upx.github.io/ <https://upx.github.io/>
> 
> Can you please try to shrink the minimal VM binaries on Windows and report back the new size?
> Use: upx.exe -9 ...
> 
> /Frank
> 
> 
> 
> >
> > So my first question is - how can I compile Erlang into a single file
> > executable? - I want no scripts/batch files to launch erlang no DLLs
> > and a statically linked binary?
> 
> Getting rid of scripts/batch files you can do if you generalize the erlexec binary a bit (in Docker for instance, as it was mentioned before, you perhaps want to be able to configure more things with environment variables for instance) but what would "statically linked" mean in the world of modules that load shared objects and even modules that can be upgraded? To statically link one would have to sacrifice the dynamic code reload, right?
> 
> /Stefan
> 
> >
> > /Joe
> >
> >
> >
> > On Wed, Sep 20, 2017 at 4:34 PM, Frank Muller
> > <frank.muller.erl@REDACTED <mailto:frank.muller.erl@REDACTED>> wrote:
> >> Do you think it's possible to package this minimal distribution in one
> >> excutable?
> >>
> >> Imagine we can ship a whole release in one binary (like in Go).
> >>
> >> /Frank
> >>
> >> Wed 20 sept. 2017 at 16:23, Joe Armstrong <erlang@REDACTED <mailto:erlang@REDACTED>> wrote :
> >>>
> >>> Thanks everybody for you help - I got it down to 5.5MB in 9 files
> >>> Relocated as follows. I've also packed/compressed the libraries
> >>>
> >>> Here are the files that are needed
> >>>
> >>> %% DEST/bin/erl.exe                     120320 bytes
> >>> %% DEST/bin/start.boot                    5603 bytes
> >>> %% DEST/erts-9.0/bin/beam.smp.dll      3346944 bytes
> >>> %% DEST/erts-9.0/bin/erlexec.dll        162304 bytes
> >>> %% DEST/erts-9.0/bin/inet_gethost.exec   45568 bytes
> >>> %% DEST/lib/compiler-7.1.ez             409091 bytes
> >>> %% DEST/lib/kernel-5.2.ez               384730 bytes
> >>> %% DEST/lib/sasl-3.0.4.ez               106278 bytes
> >>> %% DEST/lib/stdlib-3.4.ez               913013 bytes
> >>>
> >>> This seems to work :-)
> >>>
> >>> Cheers
> >>>
> >>> /Joe
> >>>
> >>> On Wed, Sep 20, 2017 at 2:24 PM, Dmitry Kolesnikov
> >>> <dmkolesnikov@REDACTED <mailto:dmkolesnikov@REDACTED>> wrote:
> >>>> Hello,
> >>>>
> >>>> On 20 Sep 2017, at 13.35, Stu Bailey <stu.bailey@REDACTED <mailto:stu.bailey@REDACTED>> wrote:
> >>>>
> >>>> "As an afterthought - it would be *very nice* to have a program which
> >>>> makes a minimal distributable program for Windows/Mac/Linux."
> >>>>
> >>>>
> >>>> This program is called relx.
> >>>>
> >>>> It generates a release package which are deployable to any vanilla linux
> >>>> or
> >>>> other vanilla os.
> >>>> The usage of Docker-image-with-Erlang complicates the process of
> >>>> distribution.
> >>>>
> >>>> Best Regards,
> >>>> Dmitry
> >>>>
> >>>>
> >>> _______________________________________________
> >>> erlang-questions mailing list
> >>> erlang-questions@REDACTED <mailto:erlang-questions@REDACTED>
> >>> http://erlang.org/mailman/listinfo/erlang-questions <http://erlang.org/mailman/listinfo/erlang-questions>
> > _______________________________________________
> > erlang-questions mailing list
> > erlang-questions@REDACTED <mailto:erlang-questions@REDACTED>
> > http://erlang.org/mailman/listinfo/erlang-questions <http://erlang.org/mailman/listinfo/erlang-questions>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20170921/75bca158/attachment.htm>


More information about the erlang-questions mailing list