atomvm_rebar3_plugin
A rebar3 plugin for simplifying development of Erlang applications targeted for the AtomVM Erlang abstract machine.
Releases and
accompanying documentation are available on
hex.pm. The documentation for the current master branch is always available from
the atomvm_rebar3_plugin GitHub pages.
Quick Start
Create or edit the $HOME/.config/rebar3/rebar.config file to include the atomvm_rebar3_plugin rebar3 plugin:
{plugins, [
atomvm_rebar3_plugin
]}.
From a working directory, issue the following command:
shell$ rebar3 new atomvm_app myapp
===> Writing myapp/.gitignore
===> Writing myapp/LICENSE
===> Writing myapp/rebar.config
===> Writing myapp/README.md
===> Writing myapp/src/myapp.erl
===> Writing myapp/src/myapp.app.src
This task will create a simple rebar3 project with a minimal AtomVM application in the myapp directory.
Change to the myapp directory and issue the packbeam task to the rebar3 command:
shell$ cd myapp
shell$ rebar3 packbeam
===> Fetching atomvm_rebar3_plugin
===> Fetching packbeam
===> Compiling packbeam
===> Compiling atomvm_rebar3_plugin
===> Verifying dependencies...
===> Compiling myapp
===> AVM file written to .../myapp/_build/default/lib/myapp.avm
An AtomVM AVM file named myapp.avm is created in the _build/default/lib directory:
shell$ ls -l _build/default/lib/myapp.avm
-rw-rw-r-- 1 joe wheel 328 Jan 1 1970 00:01 _build/default/lib/myapp.avm
If you have it installed, you can use the packbeam tool to list the contents of this generated file:
shell$ packbeam list _build/default/lib/myapp.avm
myapp.beam * [384]
myapp/priv/application.bin [220]
See the various flash tasks described below for information about how to flash the generated AVM file to your device.
atomvm_rebar3_plugin tasks
The rebar3 plugin provides the following tasks under the atomvm namespace:
packbeam Generate AtomVM packbeam files from your rebar3 project and its dependencies.
esp32_flash Flash AtomVM packbeam files to ESP32 devices over a serial connection.
stm32_flash Flash AtomVM packbeam files to STM32 devices over a serial connection.
uf2create Generate a u2f binary from an AtomVM packbeam file.
pico_flash Flash "packed" uf2 files to RP2040 (RPi Pico) devices by copying to FATfs.
version Print the version of the atomvm_rebar3_plugin to the console.
bootstrap Compile Erlang files that rebar3 otherwise cannot compile. Typically, such files include modules from the OTP kernel or stdlib application that rebar3 uses internally for its own implementation.
dialyzer Use dialyzer for static analysis of AtomVM applications.
escriptize Generate a standalone binary for the current host, using AtomVM.
IMPORTANT! Some of the above tasks were previously located under the default rebar3 namespace; however, the commands under the default namespace have been DEPRECATED. Users will get a warning message on the console when using deprecated tasks, and any deprecated tasks may be removed in the future without warning. Be sure to migrate any scripts or code you have to use the atomvm namespace.
The tasks listed above are described in more detail below.
Configuration
The tasks supported by this plugin support the following modes of configuration, ordered from highest to lowest in terms of their precedence:
- Command-line arguments
rebar.config settings
- Environment variables
- Hard-wired defaults
Specifically, any options defined on the command line override options by the same name in rebar.config, which in turn override any corresponding environment variable settings, and so forth.
Configuration items for specific tasks are described in detail below for each of the supported tasks.
Any rebar.config settings are defined in the project's rebar.config project file, and are defined as property lists under the atomvm_rebar3_plugin key. The properties in this list are themselves property lists, using the task name as a key, with task-specific configuration for each entry.
A typical rebar.config entry for this plugin therefore takes the form:
{atomvm_rebar3_plugin, [
{packbeam, [...]},
{esp32_flash, [...]},
...
]}.
Configuration in rebar.config is optional but can be useful in some cases. For example, the flash tasks depend on the packbeam task, to ensure that the AVM file is up to date before flashing. However, if the AVM file is rebuilt, the flash task has no way to tell the packbeam task any task-specific properties it should use as part of the rebuild. If they are defined in rebar.config (or in environment variables), however, they will be used during an implicit rebuild of the AVM file.
The packbeam task
The packbeam task is used to generated an AtomVM packbeam (.avm) file.
shell$ rebar3 help atomvm packbeam
Use this plugin to create an AtomVM packbeam file from your rebar3 project.
Usage: rebar3 atomvm packbeam [-e <external>] [-f <force>] [-p <prune>]
[-s <start>] [-r <remove_lines>]
-e, --external External AVM modules
-f, --force Force rebuild
-p, --prune Prune unreferenced BEAM files
-s, --start Start module
-r, --remove_lines Remove line information from generated AVM files
(off by default)
-l, --list List the contents of AVM files after creation
E.g.,
shell$ rebar3 atomvm packbeam
===> Compiling packbeam
===> Compiling atomvm_rebar3_plugin
===> Compiling packbeam
===> Verifying dependencies...
===> Compiling myapp
===> AVM file written to .../myapp/_build/default/lib/myapp.avm
When using this task, an AVM file with the project name will be created in _build/<profile>/lib/, .e.g.,
shell$ ls -l _build/default/lib/myapp.avm
-rw-rw-r-- 1 joe wheel 8780 May 15 1895 22:03 _build/default/lib/myapp.avm
If your project has any erlang dependencies, the packbeam task will include any BEAM files or priv files from the dependent projects in the final AVM file.
If your project (or any of its dependencies) has multiple modules that export a start/0 entry-point function, you can specify which module to use as the entry-point for your application via the --start (or -s) option:
shell$ rebar3 atomvm packbeam --start my_start_module
...
Using this option will ensure that the generated AVM file with use my_start_module to start the application.
You may use the --prune option (or -p) to prune unnecessary beam files when creating AVM files. Pruning unnecessary files can make your AVM files smaller, leading to faster development cycles and more free space on flash media. Pruning is not enabled by default. Note that if you use the prune option, your project (or at least one of its dependencies) must have a start/0 entry-point. Otherwise, you should treat your project as a library, suitable for inclusion in a different AtomVM project.
By default, line number information is included in generated AVM files. Including line number information is useful for debugging and locating the source of application crashes. However, adding line number information makes AVM files larger and in some cases may have an impact on memory usage. You can remove line number information from your AVM files via the --remove_lines (or -r) flag, if, for example, you are prepared to deploy your application into production.
The packbeam task will use timestamps to determine whether a rebuild is necessary. However, timestamps may not be enough to trigger a rebuild, for example, if a dependency was added or removed. You can force a rebuild of AVM file by adding the --force flag (or -f), with no arguments. All AVM files, including AVM files for dependencies, will be rebuilt regardless of timestamps.
If you would like to view the contents of the AVM file after you have created it, use the --list (of -l) flag to display the entries of the file on the console. Entries which export a start/0 function are marked with an asterisk (*). All entries include their size in bytes, wrapped in square brackets ([]).
shell$ rebar3 atomvm packbeam -l
...
===> AVM file written to .../myapp/_build/default/lib/myapp.avm
AVM contents
============
myapp.beam * [384]
myapp/priv/application.bin [228]
The following table enumerates the properties that may be defined in your project's rebar.config
file for this task. Use packbeam as the key for any properties defined for this task.
Note that the --list flag is only operative when the AVM file has been written. Use the --force (-f) flag to force a rebuild of the AVM file, if desired.
| Key |
Type |
Description |
force |
boolean() |
Always force a rebuild of the AVM file, even if up to date |
prune |
boolean() |
Prune unnecessary BEAM files from generated AVM |
start |
atom() |
The start module |
remove_lines |
boolean() |
Remove line number information from generated AVM files. |
list |
boolean() |
List the AVM file contents when generating AVM files. |
Example:
{atomvm_rebar3_plugin, [{packbeam, [prune, {start, main}]}]}.
Any setting specified on the command line take precedence over settings in rebar.config, which in turn take precedence over environment variable settings, which in turn take precedence over the default values specified above.
The packbeam task depends on the compile task, so any changes to modules in the project will automatically get rebuilt when running the packbeam task.
External Dependencies
If you already have AVM modules are not available via rebar3, you can direct the packbeam task to these AVM files via the --external (or -e) flag, e.g.,
shell$ rebar3 atomvm packbeam -e <path-to-avm-1> -e <path-to-avm-2> ...
===> Fetching packbeam
===> Compiling packbeam
===> Compiling atomvm_rebar3_plugin
===> Verifying dependencies...
===> Compiling myapp
===> AVM file written to .../myapp/_build/default/lib/myapp.avm
Building OTP Applications
You can use the packbeam task to build AtomVM applications that implements the OTP application behavior, and the atomvm_rebar3_plugin will create an AVM file that contains boot information to start your application automatically when AtomVM starts.
For example, a module that implements the OTP application behavior might look as follows:
%% erlang
-module(myapp_app).
-export([start/2, stop/1]).
start(_Type, Args) ->
io:format("Starting myapp_app ...~n"),
myapp_sup:start(Args).
stop(_State) ->
myapp_sup:stop_children().
(assume myapp_sup is also a part of your OTP application).
And the application configuration file (e.g., myapp.app.src) should include the application module (myapp_app) under it's mod entry:
{
application, myapp, [
{description, "My AtomVM application"},
{vsn, "0.1.0"},
{registered, []},
{applications, [
kernel,
stdlib
]},
{env,[]},
{mod, {myapp_app, []}},
{modules, []},
{licenses, ["Apache-2.0"]},
{links, []}
]
}.
If you specify init as the start module, then an AVM file will be created:
shell$ rebar3 atovm packbeam -p -s init
===> Analyzing applications...
===> Compiling atomvm_rebar3_plugin
===> Compiling packbeam
...
===> Analyzing applications...
===> Compiling myapp
===> AVM file written to .../myapp/_build/default/lib/myapp.avm
This AVM file will contain the init.beam module, along with a boot script (init/priv/start.boot), which will be used by the init.beam module to start your application automatically.
For example:
shell$ packbeam list _build/default/lib/myapp.avm
init.beam * [1428]
myapp_worker.beam [596]
myapp_sup.beam [572]
myapp_app.beam [416]
myapp/priv/application.bin [288]
init/priv/start.boot [56]
myapp/priv/example.txt [24]
Running this AVM file will boot the myapp application automatically, without having to write an entrypoint module.