How do you setup an Erlang NIF project with rebar? - erlang

I looked through the questions here on StackOverflow and Googled around for an example of setting up a Basic NIF project in rebar for wrapping a C++ library.
I used to library project on GitHub as a guide:
https://github.com/tuncer/re2
My project is here:
https://github.com/project-z/emutton/
When I do a rebar compile && rebar eunit, I get a failure in the eunit test because it cannot find emtn.so:
$ rebar compile && rebar eunit
==> emutton (compile)
==> emutton (eunit)
undefined
*** test module not found ***
**emtn
=ERROR REPORT==== 25-Jun-2013::12:21:55 ===
The on_load function for module emtn returned {error,
{load_failed,
"Failed to load NIF library: 'dlopen(/.../source/emutton/priv/emtn.so, 2): image not found'"}}
=======================================================
Failed: 0. Skipped: 0. Passed: 0.
One or more tests were cancelled.
ERROR: One or more eunit tests failed.
ERROR: eunit failed while processing /.../source/emutton: rebar_abort
When I call rebar compile, it is only producing a single driver file, emtn_drv.so and no emtn.so:
$ tree priv
priv
└── emtn_drv.so
0 directories, 1 file
I have an echo statement in c_src/build_deps.sh that I don't see output when I call rebar clean. It seems to behave as though my pre_hook and post_hook in rebar.config are completely ignored:
{pre_hooks, [{compile, "c_src/build_deps.sh"}]}.
{post_hooks, [{clean, "c_src/build_deps.sh clean"}]}.
Examples of no output shown from rebar:
$ rebar compile
==> emutton (compile)
$ rebar clean
==> emutton (clean)
Because I have cloned tuncer's RE2 bindings project and when I do a rebar compile see output from his build_deps.sh script. The permissions on mine match those on his:
-rwxr-xr-x 1 ajl staff 891B Jun 25 12:30 c_src/build_deps.sh
Any idea what I'm missing here? I believe that rebar is configured correctly to call out to the script and do the compilation.

Your problem is that the line in your rebar.config
https://github.com/project-z/emutton/blob/master/rebar.config%20#L1
Doesn't match what you attempt to load
https://github.com/project-z/emutton/blob/master/src/emtn.erl#L25
You should either change the rebar.config to
{port_specs, [{"priv/emtn.so",["c_src/emtn_nif.c"]}]}.
and change emtn.erl to
erlang:load_nif(filename:join(PrivDir, "emtn"), 0). % ?MODULE is an atom, I believe you need a string
Or change emtn.erl to
erlang:load_nif(filename:join(PrivDir, "emtn_drv"), 0).

Related

make on ChicagoBoss failing with escript: exception error: undefined function rebar:main/1

I am trying to install ChicagoBoss based on the instructions here. Now, Make is failing with exception -
=ERROR REPORT==== 14-Dec-2022::11:15:33.605319 ===
beam/beam_load.c(148): Error loading module rebar:
please re-compile this module with an Erlang/OTP 25 compiler
escript: exception error: undefined function rebar:main/1
in function escript:run/2 (escript.erl, line 750)
in call from escript:start/1 (escript.erl, line 277)
in call from init:start_em/1
in call from init:do_boot/3
make: *** [compile] Error 127
I have further installed rebar3 using command -
homebrew install rebar3
I am running Erlang OTP/25 in mac. I haven't been able to understand if it's a problem with
rebar3
or
ChicagoBoss
The problem is with ChicagoBoss. It includes a rebar binary that was compiled with Erlang/OTP release 17, but Erlang/OTP release 25 can only use modules compiled with release 21 or later.
The rebar binary starts with the line #!/usr/bin/env escript, meaning that it looks for escript in the PATH. This picks up release 25, causing this problem. You could try installing release 24 or earlier to work around this.
You might find ASDF useful for switching between Erlang versions: https://github.com/asdf-vm/asdf-erlang

Rebar eunit runs non native (non Hipe) code

I have compiled my Erlang module using
rebar compile
using the following option in rebar.config
{erl_opts, [native, {hipe, [verbose]}, warnings_as_errors, debug_info]}.
{eunit_compile_opts, [native, {hipe, [verbose]}, warnings_as_errors, debug_info]}.
I see that the code is indeed compiled to native since I see Hipe messages during compilation and the .beam file size are also larger than non-native compilation.
However when I run
rebar eunit
which tests my module I always get false for
code:is_module_native(?MODULE)
within my module under test.
Why does rebar not run my eunit test as native code?
I have additionally added this line to the reltool.config file,
{app, hipe, [{incl_cond, include}]},
rebar 2.1.0-pre 17 20140421_192321 git 2.1.0-pre-166-ged88055
Your code is recompiled when you run "rebar eunit" using the compile options defined by
{erl_opts, [native, {hipe, [verbose]}, warnings_as_errors, debug_info]}.
and modified by the compile options defined by
{eunit_compile_opts, [???]}.
Did you check these options?

Command 'rebar doc' is inconsistent

First of all, 'rebar doc' works sometimes and sometimes not. It is strange.
Rebar version which I'm using is 2.5.1
My folder structure is:
Header_Directory
1.1 apps
1.1.1 sub_dir_1
1.1.2 sub_dir_2 / include
1.1.3 sub_dir_3
1.2 deps
1.3 confs
rebar.config
Modules in sub_dir_3 also use some include files from sub_dir_2/include folder.
The error I get when I use the command rebar doc is:
.sub_dir_3/src/my_log_worker.erl, in module header: at line 9: file
not found: some.hrl edoc: skipping source file
'sub_dir_3/src/my_log_worker.erl': {'EXIT',error}. edoc: error in
doclet 'edoc_doclet': {'EXIT',error}. ERROR: doc failed while
processing /home/learn/header_directory/apps/sub_dir_3: {'EXIT',error}
I do 'rebar clean' and then 'rebar compile' prior to 'rebar doc'
Also,when I do it in erl shell, I get error.
edoc:file("some_log_worker.erl", []).
edoc: error reading file 'some_log_worker.erl'.
** exception exit: {error,enoent}
in function edoc:read_source/2 (edoc.erl, line 664)
in call from edoc_extract:source/3 (edoc_extract.erl, line 52)
in call from edoc:read/2 (edoc.erl, line 537)
in call from edoc:file/2 (edoc.erl, line 116)
Is there any way by which I can include my hrl file either in rebar.config or edoc options?
I have '{edoc_opts, [{ i, "apps/sub_dir_3/include" }]}.' in rebar.config, still of no help.
It looks like you didn't follow the rebar/OTP conventions:
OTP Conventions
Rebar expects projects to follow the OTP conventions as described in
the OTP Design Principles document: Applications
An application should consists of the following set of directories:
src
ebin
priv
include
and have an application resource file: ebin/example_project.app or
src/example_project.app.src. In the later case, the
ebin/example_project.app file is generated from the
src/example_project.app.src one automatically during the compilation
phase.
Rebar & OTP convetions
I recommend you to move to that file organisation, it will be really much simpler to benefit from standard tools like rebar.
Yes, it is always in the best interest to follow the OTP principles. However, it worked for my app structure.
The only problem was because of #headerfile annotation in the erlang modules. Somehow, I don't know how to correctly use the #headerfile annotation.
Thanks for all the help. :)

Error while installing Nitrogen

Thought I'd explore Nitrogen. Following install instructions outlined in http://nitrogenproject.com/doc/tutorial.html, I pulled source from GitHub, executed second step, make rel_inets, and hit a wall. Here is what the system told me:
nitrogen$ make rel_inets
...
make[2]: Leaving directory `/home/lloyd/Erl/nitrogen'
Running Erlang
Generating "reltool.config" with merged overlays
...Loading base file: "reltool_base.config"
...Merging Overlay from "reltool_inets.config"
...Writing "reltool.config"
...SUCCESS
make[2]: Entering directory `/home/lloyd/Erl/nitrogen'
Running Erlang
==> rel (generate)
ERROR: generate failed while processing /home/lloyd/Erl/nitrogen/rel: {'EXIT', {{badmatch,{error,"Illegal library \"/home/lloyd/Programming/Erlang/zippity/apps\": no such file or directory"}},
Turns out that /home/lloyd/Programming/Erlang/zippity/apps was deleted long ago; e.g. it's not in my directory tree.
Can any kind wizards tell me why in the world would make dredge up this ancient history? And... what can I do about it?
Many thanks,
LRP
As requested, I've edited line 231 of Makefile. Here's output of make rel_inets:
lloyd#Reliance:~/Erl/nitrogen$ make rel_inets
make[1]: Entering directory `/home/lloyd/Erl/nitrogen'
./rebar get-deps
Running Erlang
==> rel (get-deps)
==> nitrogen (get-deps)
./rebar compile
Running Erlang
==> rel (compile)
==> nitrogen (compile)
make[2]: Entering directory `/home/lloyd/Erl/nitrogen'
make[2]: Leaving directory `/home/lloyd/Erl/nitrogen'
Running Erlang
Generating "reltool.config" with merged overlays
...Loading base file: "reltool_base.config"
...Merging Overlay from "reltool_inets.config"
...Writing "reltool.config"
...SUCCESS
make[2]: Entering directory `/home/lloyd/Erl/nitrogen'
Running Erlang
DEBUG: Consult config file "/home/lloyd/Erl/nitrogen/rel/rebar.config"
DEBUG: Rebar location: "/home/lloyd/Erl/nitrogen/rel/rebar"
DEBUG: is_rel_dir(/home/lloyd/Erl/nitrogen/rel) -> {true,
"/home/lloyd/Erl/nitrogen/rel/reltool.config"}
DEBUG: Available deps: []
DEBUG: Missing deps : []
DEBUG: Predirs: []
==> rel (generate)
DEBUG: Consult config file "/home/lloyd/Erl/nitrogen/rel/reltool.config"
DEBUG: vcs_vsn: Unknown VCS atom in vsn field: "2.2.2"
DEBUG: vcs_vsn: Unknown VCS atom in vsn field: []
ERROR: generate failed while processing /home/lloyd/Erl/nitrogen/rel: {'EXIT',{{badmatch,{error,"Illegal library \"/home/lloyd/Programming/Erlang/zippity/apps\": no such file or directory"}},
[{rebar_reltool,generate,2,[]},
{rebar_core,run_modules,4,[]},
{rebar_core,execute,5,[]},
{rebar_core,process_dir1,6,[]},
{rebar_core,process_commands,2,[]},
{rebar,main,1,[]},
{escript,run,2,[{file,"escript.erl"},{line,747}]},
{escript,start,1,[{file,"escript.erl"},{line,277}]}]}}
make[2]: *** [generate] Error 1
make[2]: Leaving directory `/home/lloyd/Erl/nitrogen'
make[1]: *** [rel] Error 2
make[1]: Leaving directory `/home/lloyd/Erl/nitrogen'
make: *** [rel_inets] Error 2
Big thanks guys. Evidently I had set ERL_LIBS sometime back not fully understanding the implications. The ERL_LIBS value was orphaned when I deleted the library directory. So, unset ERL_LIBS did the trick. Nitrogen is now installed.
Re hosed Erlang installation: thanks to tip from Bob Ippolito, I purged Erlang from my system, installed the Erlang dependencies, then reinstalled Erlang via kerl. Rebar now compiles. n2o is still not cooperating--- stalls during compilation after many warnings. But I'll work on that today.
Thanks again,
LRP

Why does rebar use my erl_opts to compile my deps?

I have a structure similar to the following:
rebar.config
deps/
apps/A/rebar.config
My top-level rebar.config file looks like the following:
{sub_dirs, ["apps/A"]}.
{lib_dirs, ["deps"]}.
A/rebar.config looks like the following:
{deps, [
%% deps
]}.
{erl_opts, [debug_info, warn_missing_spec, warnings_as_errors]}.
Now, one of my dependencies doesn't provide -spec for all of its functions, so the warnings_as_errors causes the compile to fail.
I was expecting the erl_opts to only affect my code, and for the dependencies to have their own options. Why does rebar do this?
It turns out that one of my dependencies doesn't have a rebar.config, so rebar is compiling it using my options.
What's confusing is that erl_opts is not set in the top-level rebar.config, but in a local rebar.config file. This is the file that has the deps clause.
It seems that the options are inherited "sideways" in this case.
I cannot reproduce this using the following version of rebar:
rebar 2.1.0-pre R15B03 20131210_073701 git 2.1.0-pre
On my local setup, using your same config (goldrush is my dependency):
...
==> goldrush (compile)
DEBUG: Matched required ERTS version: 5.9.3.1 -> .*
DEBUG: Matched required OTP release: R15B03 -> .*
DEBUG: Min OTP version unconfigured
DEBUG: erl_opts [debug_info,warn_export_all]
...
==> myapp (compile)
DEBUG: Matched required ERTS version: 5.9.3.1 -> .*
DEBUG: Matched required OTP release: R15B03 -> .*
DEBUG: Min OTP version unconfigured
DEBUG: erl_opts [debug_info,debug_info,warn_missing_spec,warnings_as_errors]
...
As you can see, the two applications receive two different sets of options.
Could you enable debugging and post the relevant output?
./rebar -vvv clean compile
What version of rebar/Erlang are you running?

Resources