Skip to content

Commit 711c5b3

Browse files
committed
Handles multiple crates per project
1 parent 7c4d5e2 commit 711c5b3

File tree

3 files changed

+46
-21
lines changed

3 files changed

+46
-21
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# rebar3_rust
22

3-
Compile rust NIFs for erlang projects.
3+
Compile rust crates for erlang projects.
44

55
## Use
66

@@ -34,6 +34,15 @@ Add the plugin and hooks to your `rebar.config`
3434
}.
3535
```
3636

37+
Add the following to your `.gitignore`
38+
39+
```
40+
crates/*/target
41+
priv/*
42+
!priv/.keep
43+
```
44+
45+
3746
You can find an example usage [here](https://github.com/sdwolf/rustfromerl).
3847

3948
## Upgrade

src/rebar3_prv_rust_compile.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ init(State) ->
2828

2929
-spec do(rebar_state:t()) -> {ok, rebar_state:t()} | {error, string()}.
3030
do(State) ->
31-
NewState = rebar3_rust_utils:compile_nifs(State),
31+
NewState = rebar3_rust_utils:compile_crates(State),
3232

3333
{ok, NewState}.
3434

src/rebar3_rust_utils.erl

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,62 @@
11
-module(rebar3_rust_utils).
22

33
-export([
4-
compile_nifs/1
4+
compile_crates/1
55
]).
66

77
%% ===================================================================
88
%% Public API
99
%% ===================================================================
1010

11-
compile_nifs(State) ->
12-
App = rebar_state:current_app(State),
13-
AppDir = rebar_app_info:dir(App),
14-
RustNifDir = filename:join(AppDir, "rust_nif"),
11+
compile_crates(State) ->
12+
App = rebar_state:current_app(State),
13+
AppName = binary_to_list(rebar_app_info:name(App)),
14+
AppDir = rebar_app_info:dir(App),
15+
PrivDir = code:priv_dir(AppName),
1516

17+
CratesDir = filename:join(AppDir, "crates"),
18+
CratesGlob = filename:join(CratesDir, "*"),
19+
Crates = filelib:wildcard(CratesGlob),
20+
21+
lists:foreach(
22+
fun(CrateDir) -> compile_crate(CrateDir, PrivDir) end,
23+
Crates
24+
),
25+
26+
State.
27+
28+
%% ===================================================================
29+
%% Internal functions
30+
%% ===================================================================
31+
32+
compile_crate(CrateDir, PrivDir) ->
1633
Command = "cargo build --release -j$(nproc)",
17-
{ok, _} = rebar_utils:sh(Command, [{cd, rust_nif}, {use_stdout, true}]),
1834

19-
SOGlob = filename:join([RustNifDir, "target", "release", "*.so"]),
35+
{ok, _} = rebar_utils:sh(Command, [{cd, CrateDir}, {use_stdout, true}]),
36+
37+
SOGlob = filename:join([CrateDir, "target", "release", "*.so"]),
2038
SOFiles = filelib:wildcard(SOGlob),
2139

22-
AppName = binary_to_list(rebar_app_info:name(App)),
23-
PrivDir = code:priv_dir(AppName),
24-
Destination = filename:join(PrivDir, "rust_nif"),
25-
OldFilesGlob = filename:join(Destination, "*.so"),
26-
OldFiles = filelib:wildcard(OldFilesGlob),
40+
CratesPrivDir = filename:join(PrivDir, "crates"),
41+
42+
file:make_dir(CratesPrivDir),
43+
44+
CrateName = filename:basename(CrateDir),
45+
Destination = filename:join(CratesPrivDir, CrateName),
46+
OldFilesGlob = filename:join(Destination, "*.so"),
47+
OldFiles = filelib:wildcard(OldFilesGlob),
2748

2849
file:make_dir(Destination),
2950

3051
lists:foreach(
3152
fun(File) -> remove_file(File) end,
3253
OldFiles
3354
),
55+
3456
lists:foreach(
3557
fun(File) -> copy_file(File, Destination) end,
3658
SOFiles
37-
),
38-
39-
State.
40-
41-
%% ===================================================================
42-
%% Internal functions
43-
%% ===================================================================
59+
).
4460

4561
copy_file(SourceFile, DestinationDir) ->
4662
Extension = filename:extension(SourceFile),

0 commit comments

Comments
 (0)