Distributing a buildable executable

Dave Cottlehuber dch@REDACTED
Wed Jan 8 15:31:45 CET 2020


On Tue, 7 Jan 2020, at 16:04, Р.С. Алиев wrote:
> Hello list,
> 
> What I have is:
> 1. Pure Erlang part, a simple gen_server;
> 2. Several C++ source files + cmake file, intended to be build into a 
> standalone executable.
> 
> I need to distribute both components in a single package, as my 
> gen_server does open_port({spawn, my_executable}). Moreover, it uses an 
> environmental variable to find the executable, so I need a way to set it 
> too (I use the "QT5_EPORT_DIR=~/Path/to/executable rebar3 shell --apps 
> my_app" command now, but it obviously needs to be generalized somehow).
> 
> What is the standard way to distribute such a two-component Erlang package?
> Thank you.
> 
> 
> -- 
> Regards,
> R.S. Aliev

Hey RS,

Distribute the binary in your application priv directory, & then however you
deploy your app, you can check for & locate the binary at module load time:

```erl
-module(flux_capacitor).
-on_load(init/0).
...
init() ->
    PrivDir = case code:priv_dir(?MODULE) of
        {error, _} ->
            %% ENOIDEA so make a wild guess
            EbinDir = filename:dirname(code:which(?MODULE)),
            AppPath = filename:dirname(EbinDir),
            filename:join(AppPath, "priv");
        Path ->
            Path
    end,
    do_stuff(filename:join(PrivDir, "executable")).
```

Alternatively for a gen_server you might handle this in Module:init/1.
 
see jiffy and associated rebar.config for how this all gets assembled.

https://github.com/davisp/jiffy/blob/master/src/jiffy.erl#L181-L190

There's a bunch more useful stuff in Paul's nif-examples[1] repo,
albeit targeted for NIFs obviously. If you need to compile the exe,
enc[2] is the best rebar-ish extension I've used for this, but I don't
know off-hand of any cmake-driven builds.

[1]: https://github.com/davisp/nif-examples/tree/master/apps
[2]: https://github.com/davisp/erlang-native-compiler

A+
Dave



More information about the erlang-questions mailing list