|
1 | 1 | -module(rebar3_rust_utils).
|
2 | 2 |
|
3 | 3 | -export([
|
4 |
| - compile_nifs/1 |
| 4 | + compile_crates/1 |
5 | 5 | ]).
|
6 | 6 |
|
7 | 7 | %% ===================================================================
|
8 | 8 | %% Public API
|
9 | 9 | %% ===================================================================
|
10 | 10 |
|
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), |
15 | 16 |
|
| 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) -> |
16 | 33 | Command = "cargo build --release -j$(nproc)",
|
17 |
| - {ok, _} = rebar_utils:sh(Command, [{cd, rust_nif}, {use_stdout, true}]), |
18 | 34 |
|
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"]), |
20 | 38 | SOFiles = filelib:wildcard(SOGlob),
|
21 | 39 |
|
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), |
27 | 48 |
|
28 | 49 | file:make_dir(Destination),
|
29 | 50 |
|
30 | 51 | lists:foreach(
|
31 | 52 | fun(File) -> remove_file(File) end,
|
32 | 53 | OldFiles
|
33 | 54 | ),
|
| 55 | + |
34 | 56 | lists:foreach(
|
35 | 57 | fun(File) -> copy_file(File, Destination) end,
|
36 | 58 | SOFiles
|
37 |
| - ), |
38 |
| - |
39 |
| - State. |
40 |
| - |
41 |
| -%% =================================================================== |
42 |
| -%% Internal functions |
43 |
| -%% =================================================================== |
| 59 | + ). |
44 | 60 |
|
45 | 61 | copy_file(SourceFile, DestinationDir) ->
|
46 | 62 | Extension = filename:extension(SourceFile),
|
|
0 commit comments