├── .github └── workflows │ └── ci.yml ├── .gitignore ├── .travis.yml ├── LICENSE-APACHE ├── LICENSE-MIT ├── README.md ├── ct ├── rebar3_cargo_SUITE.erl └── rebar3_cargo_SUITE_data │ ├── .gitignore │ ├── fails_compile │ ├── Cargo.toml │ ├── rebar.config │ ├── rust_src │ │ └── uhoh │ │ │ ├── Cargo.toml │ │ │ └── src │ │ │ └── main.rs │ └── src │ │ ├── fails_compile.app.src │ │ └── fails_compile.erl │ ├── fails_test │ ├── Cargo.toml │ ├── rebar.config │ ├── rust_src │ │ └── uhoh2 │ │ │ ├── Cargo.toml │ │ │ └── src │ │ │ └── main.rs │ └── src │ │ ├── fails_test.app.src │ │ └── fails_test.erl │ ├── release_debug │ ├── Cargo.toml │ ├── rebar.config │ ├── rust_src │ │ └── build_type │ │ │ ├── Cargo.toml │ │ │ └── src │ │ │ └── main.rs │ └── src │ │ ├── release_debug.app.src │ │ └── release_debug.erl │ ├── test_nif_app │ ├── .cargo │ │ └── config.toml │ ├── Cargo.toml │ ├── rebar.config │ ├── rust_src │ │ └── nifsys │ │ │ ├── Cargo.lock │ │ │ ├── Cargo.toml │ │ │ └── src │ │ │ └── lib.rs │ └── src │ │ ├── cargo.hrl │ │ ├── nifsys.erl │ │ ├── test_nif_app.app.src │ │ └── test_nif_app.erl │ └── test_port_app │ ├── Cargo.toml │ ├── rebar.config │ ├── rust_src │ └── erl_comm │ │ ├── Cargo.lock │ │ ├── Cargo.toml │ │ └── src │ │ └── main.rs │ └── src │ ├── cargo.hrl │ ├── complex1.erl │ ├── test_port_app.app.src │ └── test_port_app.erl ├── rebar.config ├── rebar.lock ├── rebar3 └── src ├── internal.hrl ├── rebar3_cargo.app.src ├── rebar3_cargo.erl ├── rebar3_cargo_clean_prv.erl ├── rebar3_cargo_compile.erl ├── rebar3_cargo_compile_deps_prv.erl ├── rebar3_cargo_compile_prv.erl ├── rebar3_cargo_header.erl ├── rebar3_cargo_opts.erl ├── rebar3_cargo_test_prv.erl └── rebar3_cargo_util.erl /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: 4 | push: 5 | pull_request: 6 | branches: [ master ] 7 | 8 | 9 | jobs: 10 | 11 | build: 12 | 13 | runs-on: ubuntu-latest 14 | 15 | container: 16 | image: erlang:22.0.7 17 | 18 | steps: 19 | - uses: actions/checkout@v2 20 | - uses: actions-rs/toolchain@v1 21 | with: 22 | toolchain: stable 23 | - name: Compile 24 | run: rebar3 compile 25 | - name: Run tests 26 | run: rebar3 do eunit, ct 27 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .rebar3 2 | _* 3 | .eunit 4 | *.o 5 | *.so 6 | *.beam 7 | *.plt 8 | *.swp 9 | *.swo 10 | .erlang.cookie 11 | ebin 12 | log 13 | erl_crash.dump 14 | .rebar 15 | logs 16 | _build 17 | .idea 18 | rebar3.crashdump 19 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: erlang 2 | otp_release: 3 | - 19.2 4 | 5 | sudo: required 6 | 7 | script: rebar3 ct 8 | 9 | before_install: 10 | - curl https://static.rust-lang.org/rustup.sh | sh 11 | - curl https://s3.amazonaws.com/rebar3/rebar3 >rebar3 12 | - chmod +x rebar3 13 | - mkdir -p ~/bin 14 | - mv rebar3 ~/bin 15 | 16 | before_script: 17 | - kerl list installations 18 | - kerl list releases 19 | -------------------------------------------------------------------------------- /LICENSE-APACHE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /LICENSE-MIT: -------------------------------------------------------------------------------- 1 | Copyright (c) 2014 The Rust Project Developers 2 | Copyright (c) 2019-2020 Benedikt Reinartz 3 | 4 | Permission is hereby granted, free of charge, to any 5 | person obtaining a copy of this software and associated 6 | documentation files (the "Software"), to deal in the 7 | Software without restriction, including without 8 | limitation the rights to use, copy, modify, merge, 9 | publish, distribute, sublicense, and/or sell copies of 10 | the Software, and to permit persons to whom the Software 11 | is furnished to do so, subject to the following 12 | conditions: 13 | 14 | The above copyright notice and this permission notice 15 | shall be included in all copies or substantial portions 16 | of the Software. 17 | 18 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF 19 | ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 20 | TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 21 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT 22 | SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 23 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 24 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR 25 | IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 | DEALINGS IN THE SOFTWARE. 27 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # `rebar3_cargo` 2 | ![CI](https://github.com/rusterlium/rebar3_cargo/workflows/CI/badge.svg) 3 | 4 | This plugin for [`rebar3`](https://www.rebar3.org/) enables the automatic 5 | building of Rust crates in an Erlang application. The plugin will build all 6 | crates in the `crates` directory and copy all binary outputs to 7 | `priv/crates//`. See the test application in this repository 8 | for an example of a port program and NIF module implemented in Rust. 9 | 10 | ## Todo list 11 | 12 | As of this writing `rebar3_cargo` will build crates on 13 | linux and passes tests, however it is still under construction. 14 | 15 | Todo: 16 | - allow cargo/rust compile flags 17 | - `--target` flag handling 18 | - Appveyor CI 19 | - maybe external crate support using cargo clone 20 | 21 | 22 | # Using the plugin 23 | Use the plugin by adding the following to `rebar.config`: 24 | 25 | ``` erlang 26 | {plugins, [rebar3_cargo]}. 27 | 28 | {provider_hooks, [ 29 | {pre, [ 30 | {compile, {cargo, build}} 31 | ]}, 32 | {post, [ 33 | {clean, {cargo, clean}}, 34 | {eunit, {cargo, test}} 35 | ]} 36 | ]}. 37 | ``` 38 | 39 | This will automatically download and use `rebar3_cargo`. Crates will be 40 | compiled whenever the containing app is compiled. The cargo `--release` switch 41 | will be used when the `prod` profile is active. For example: 42 | 43 | ``` sh 44 | rebar3 as prod compile 45 | ``` 46 | 47 | # Application structure with Rust crates 48 | To add crates to an Erlang application, place them in a `crates/` folder, and 49 | reference them in a Cargo Workspace. All crates found within will be built and 50 | resulting artifacts will be placed in the `priv/crates/` folder. 51 | 52 | # Acknowledgment 53 | This plugin is based on earlier work by 54 | [goertzenator](https://github.com/goertzenator): 55 | [rebar3_rust](https://github.com/goertzenator/rebar3_rust) 56 | -------------------------------------------------------------------------------- /ct/rebar3_cargo_SUITE.erl: -------------------------------------------------------------------------------- 1 | -module(rebar3_cargo_SUITE). 2 | 3 | -compile([export_all, nowarn_export_all]). 4 | 5 | -include_lib("common_test/include/ct.hrl"). 6 | 7 | all() -> 8 | [ 9 | test_test_nif_app, 10 | test_test_port_app, 11 | test_fails_compile, 12 | test_fails_test, 13 | test_release_debug 14 | ]. 15 | 16 | test_apps() -> 17 | [ 18 | "test_nif_app", 19 | "test_port_app", 20 | "fails_compile", 21 | "fails_test", 22 | "release_debug" 23 | ]. 24 | 25 | init_per_suite(Config) -> 26 | #{priv_dir := PrivDir, data_dir := DataDir} = maps:from_list(Config), 27 | 28 | %% Get plugin source directory 29 | SrcDir = filename:join( 30 | lists:takewhile( 31 | fun 32 | ("_build") -> false; 33 | (_) -> true 34 | end, 35 | filename:split(DataDir) 36 | ) 37 | ), 38 | 39 | %% For all test apps... 40 | lists:foreach( 41 | fun(App) -> 42 | %% copy from data_dir into priv_dir 43 | ok = ec_file:copy( 44 | filename:join(DataDir, App), 45 | filename:join(PrivDir, App), 46 | [recursive] 47 | ), 48 | 49 | RcCheckout = filename:join([PrivDir, App, "_checkouts", "rebar3_cargo"]), 50 | ok = filelib:ensure_dir(filename:join(RcCheckout, "dummy")), 51 | 52 | lists:foreach( 53 | fun(Entry) -> 54 | ct:pal("Copying from ~s to ~s", [filename:join(SrcDir, Entry), RcCheckout]), 55 | ok = ec_file:copy( 56 | filename:join(SrcDir, Entry), 57 | filename:join(RcCheckout, Entry), 58 | [recursive] 59 | ) 60 | end, 61 | ["rebar.config", "rebar.lock", "src"] 62 | ), 63 | 64 | ok = ec_file:copy( 65 | filename:join(SrcDir, "rebar3"), 66 | filename:join([PrivDir, App, "rebar3"]) 67 | ) 68 | end, 69 | test_apps() 70 | ), 71 | 72 | Config. 73 | 74 | end_per_suite(Config) -> 75 | Config. 76 | 77 | %% The main test application. Tests Rust port and nif. 78 | test_test_nif_app(Config) -> 79 | #{priv_dir := PrivDir} = maps:from_list(Config), 80 | AppDir = filename:join(PrivDir, "test_nif_app"), 81 | 82 | %% check for Rust build artifact 83 | {ok, _} = rebar_utils:sh("escript rebar3 as prod compile", [{cd, AppDir}, {use_stdout, true}]), 84 | {ok, _} = rebar_utils:sh("escript rebar3 as prod clean", [{cd, AppDir}, {use_stdout, true}]), 85 | {ok, _} = rebar_utils:sh("escript rebar3 eunit", [{cd, AppDir}, {use_stdout, true}]), 86 | {ok, _} = rebar_utils:sh("escript rebar3 clean", [{cd, AppDir}, {use_stdout, true}]), 87 | 88 | ok. 89 | 90 | test_test_port_app(Config) -> 91 | #{priv_dir := PrivDir} = maps:from_list(Config), 92 | AppDir = filename:join(PrivDir, "test_port_app"), 93 | 94 | ErlCommName = filename:join( 95 | [ 96 | AppDir, 97 | "priv", 98 | "crates", 99 | "erl_comm", 100 | "erl_comm" ++ 101 | case os:type() of 102 | {win32, _} -> ".exe"; 103 | _ -> "" 104 | end 105 | ] 106 | ), 107 | 108 | %% check for Rust build artifact 109 | false = filelib:is_file(ErlCommName), 110 | {ok, _} = rebar_utils:sh("escript rebar3 as prod compile", [{cd, AppDir}, {use_stdout, true}]), 111 | true = filelib:is_file(ErlCommName), 112 | {ok, _} = rebar_utils:sh("escript rebar3 as prod clean", [{cd, AppDir}, {use_stdout, true}]), 113 | false = filelib:is_file(ErlCommName), 114 | {ok, _} = rebar_utils:sh("escript rebar3 eunit", [{cd, AppDir}, {use_stdout, true}]), 115 | {ok, _} = rebar_utils:sh("escript rebar3 clean", [{cd, AppDir}, {use_stdout, true}]), 116 | false = filelib:is_file(ErlCommName), 117 | 118 | ok. 119 | 120 | %% test that rust compile failure causes rebar3 compile failure. 121 | test_fails_compile(Config) -> 122 | #{priv_dir := PrivDir} = maps:from_list(Config), 123 | AppDir = filename:join(PrivDir, "fails_compile"), 124 | {error, _} = rebar_utils:sh("escript rebar3 compile", [ 125 | {cd, AppDir}, 126 | {use_stdout, true}, 127 | return_on_error 128 | ]), 129 | ok. 130 | 131 | %% test that rust test failure causes rebar3 test failure. 132 | test_fails_test(Config) -> 133 | #{priv_dir := PrivDir} = maps:from_list(Config), 134 | AppDir = filename:join(PrivDir, "fails_test"), 135 | {error, _} = rebar_utils:sh("escript rebar3 eunit", [ 136 | {cd, AppDir}, 137 | {use_stdout, true}, 138 | return_on_error 139 | ]), 140 | ok. 141 | 142 | %% check debug vs release builds 143 | test_release_debug(Config) -> 144 | #{priv_dir := PrivDir} = maps:from_list(Config), 145 | AppDir = filename:join(PrivDir, "release_debug"), 146 | Prefix = filename:join([AppDir, "priv", "crates", "build_type"]), 147 | DebugExeName = filename:join([ 148 | Prefix, 149 | "build_type" ++ 150 | case os:type() of 151 | {win32, _} -> ".exe"; 152 | {unix, _} -> "" 153 | end 154 | ]), 155 | {ok, _} = rebar_utils:sh("escript rebar3 compile", [{cd, AppDir}, {use_stdout, true}]), 156 | {ok, "debug"} = rebar_utils:sh(DebugExeName, [{cd, AppDir}, {use_stdout, true}]), 157 | 158 | ReleaseExeName = filename:join([ 159 | Prefix, 160 | "build_type" ++ 161 | case os:type() of 162 | {win32, _} -> ".exe"; 163 | {unix, _} -> "" 164 | end 165 | ]), 166 | {ok, _} = rebar_utils:sh("rebar3 as prod compile", [{cd, AppDir}, {use_stdout, true}]), 167 | {ok, "release"} = rebar_utils:sh(ReleaseExeName, [{cd, AppDir}, {use_stdout, true}]), 168 | 169 | ok. 170 | -------------------------------------------------------------------------------- /ct/rebar3_cargo_SUITE_data/.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | rebar.lock 3 | Cargo.lock 4 | priv/ 5 | cargo.hrl 6 | -------------------------------------------------------------------------------- /ct/rebar3_cargo_SUITE_data/fails_compile/Cargo.toml: -------------------------------------------------------------------------------- 1 | [workspace] 2 | members = ["rust_src/uhoh"] -------------------------------------------------------------------------------- /ct/rebar3_cargo_SUITE_data/fails_compile/rebar.config: -------------------------------------------------------------------------------- 1 | {plugins, [ 2 | rebar3_cargo 3 | ]}. 4 | 5 | {erl_opts, [debug_info]}. 6 | 7 | {provider_hooks, [ 8 | {pre, [{compile, {cargo, build}}]}, 9 | {post, [ 10 | {clean, {rust, clean}}, 11 | {eunit, {rust, test}} 12 | ]} 13 | ]}. 14 | -------------------------------------------------------------------------------- /ct/rebar3_cargo_SUITE_data/fails_compile/rust_src/uhoh/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "uhoh" 3 | version = "0.1.0" 4 | authors = ["Daniel Goertzen "] 5 | 6 | [dependencies] 7 | -------------------------------------------------------------------------------- /ct/rebar3_cargo_SUITE_data/fails_compile/rust_src/uhoh/src/main.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | println!(this doesn't compile because it is missing quotes); 3 | } 4 | -------------------------------------------------------------------------------- /ct/rebar3_cargo_SUITE_data/fails_compile/src/fails_compile.app.src: -------------------------------------------------------------------------------- 1 | {application, fails_compile, 2 | [{description, "An OTP library"}, 3 | {vsn, "0.1.0"}, 4 | {registered, []}, 5 | {applications, 6 | [kernel, 7 | stdlib 8 | ]}, 9 | {env,[]}, 10 | {modules, []}, 11 | 12 | {maintainers, []}, 13 | {licenses, []}, 14 | {links, []} 15 | ]}. 16 | -------------------------------------------------------------------------------- /ct/rebar3_cargo_SUITE_data/fails_compile/src/fails_compile.erl: -------------------------------------------------------------------------------- 1 | -module(fails_compile). 2 | 3 | %% API exports 4 | -export([]). 5 | 6 | %%==================================================================== 7 | %% API functions 8 | %%==================================================================== 9 | 10 | 11 | %%==================================================================== 12 | %% Internal functions 13 | %%==================================================================== 14 | -------------------------------------------------------------------------------- /ct/rebar3_cargo_SUITE_data/fails_test/Cargo.toml: -------------------------------------------------------------------------------- 1 | [workspace] 2 | members = ["rust_src/uhoh2"] -------------------------------------------------------------------------------- /ct/rebar3_cargo_SUITE_data/fails_test/rebar.config: -------------------------------------------------------------------------------- 1 | {plugins, [ 2 | rebar3_cargo 3 | ]}. 4 | 5 | {erl_opts, [debug_info]}. 6 | 7 | {provider_hooks, [ 8 | {pre, [{compile, {cargo, build}}]}, 9 | {post, [ 10 | {clean, {rust, clean}}, 11 | {eunit, {rust, test}} 12 | ]} 13 | ]}. 14 | -------------------------------------------------------------------------------- /ct/rebar3_cargo_SUITE_data/fails_test/rust_src/uhoh2/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "uhoh2" 3 | version = "0.1.0" 4 | authors = ["Daniel Goertzen "] 5 | 6 | [dependencies] 7 | -------------------------------------------------------------------------------- /ct/rebar3_cargo_SUITE_data/fails_test/rust_src/uhoh2/src/main.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | println!("Hello, world!"); 3 | } 4 | 5 | #[test] 6 | fn it_breaks() { 7 | assert!(false); 8 | } 9 | -------------------------------------------------------------------------------- /ct/rebar3_cargo_SUITE_data/fails_test/src/fails_test.app.src: -------------------------------------------------------------------------------- 1 | {application, fails_test, 2 | [{description, "An OTP library"}, 3 | {vsn, "0.1.0"}, 4 | {registered, []}, 5 | {applications, 6 | [kernel, 7 | stdlib 8 | ]}, 9 | {env,[]}, 10 | {modules, []}, 11 | 12 | {maintainers, []}, 13 | {licenses, []}, 14 | {links, []} 15 | ]}. 16 | -------------------------------------------------------------------------------- /ct/rebar3_cargo_SUITE_data/fails_test/src/fails_test.erl: -------------------------------------------------------------------------------- 1 | -module(fails_test). 2 | 3 | %% API exports 4 | -export([]). 5 | 6 | %%==================================================================== 7 | %% API functions 8 | %%==================================================================== 9 | 10 | 11 | %%==================================================================== 12 | %% Internal functions 13 | %%==================================================================== 14 | -------------------------------------------------------------------------------- /ct/rebar3_cargo_SUITE_data/release_debug/Cargo.toml: -------------------------------------------------------------------------------- 1 | [workspace] 2 | members = ["rust_src/build_type"] -------------------------------------------------------------------------------- /ct/rebar3_cargo_SUITE_data/release_debug/rebar.config: -------------------------------------------------------------------------------- 1 | {plugins, [ 2 | rebar3_cargo 3 | ]}. 4 | 5 | {erl_opts, [debug_info]}. 6 | 7 | {provider_hooks, [ 8 | {pre, [{compile, {cargo, build}}]}, 9 | {post, [ 10 | {compile, {cargo, build}}, 11 | {clean, {cargo, clean}}, 12 | {eunit, {cargo, test}} 13 | ]} 14 | ]}. 15 | -------------------------------------------------------------------------------- /ct/rebar3_cargo_SUITE_data/release_debug/rust_src/build_type/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "build_type" 3 | version = "0.1.0" 4 | authors = ["Daniel Goertzen "] 5 | 6 | [dependencies] 7 | -------------------------------------------------------------------------------- /ct/rebar3_cargo_SUITE_data/release_debug/rust_src/build_type/src/main.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | #[cfg(debug_assertions)] 3 | print!("debug"); 4 | 5 | #[cfg(not(debug_assertions))] 6 | print!("release"); 7 | } 8 | -------------------------------------------------------------------------------- /ct/rebar3_cargo_SUITE_data/release_debug/src/release_debug.app.src: -------------------------------------------------------------------------------- 1 | {application, release_debug, 2 | [{description, "An OTP library"}, 3 | {vsn, "0.1.0"}, 4 | {registered, []}, 5 | {applications, 6 | [kernel, 7 | stdlib 8 | ]}, 9 | {env,[]}, 10 | {modules, []}, 11 | 12 | {maintainers, []}, 13 | {licenses, []}, 14 | {links, []} 15 | ]}. 16 | -------------------------------------------------------------------------------- /ct/rebar3_cargo_SUITE_data/release_debug/src/release_debug.erl: -------------------------------------------------------------------------------- 1 | -module(release_debug). 2 | 3 | %% API exports 4 | -export([]). 5 | 6 | %%==================================================================== 7 | %% API functions 8 | %%==================================================================== 9 | 10 | 11 | %%==================================================================== 12 | %% Internal functions 13 | %%==================================================================== 14 | -------------------------------------------------------------------------------- /ct/rebar3_cargo_SUITE_data/test_nif_app/.cargo/config.toml: -------------------------------------------------------------------------------- 1 | [target.'cfg(target_os = "macos")'] 2 | rustflags = [ 3 | "-C", "link-arg=-undefined", 4 | "-C", "link-arg=dynamic_lookup", 5 | ] 6 | -------------------------------------------------------------------------------- /ct/rebar3_cargo_SUITE_data/test_nif_app/Cargo.toml: -------------------------------------------------------------------------------- 1 | [workspace] 2 | members = [ 3 | "rust_src/nifsys" 4 | ] 5 | -------------------------------------------------------------------------------- /ct/rebar3_cargo_SUITE_data/test_nif_app/rebar.config: -------------------------------------------------------------------------------- 1 | {plugins, [ 2 | rebar3_cargo 3 | ]}. 4 | 5 | {erl_opts, [debug_info]}. 6 | 7 | {provider_hooks, [ 8 | {pre, [{compile, {cargo, build}}]}, 9 | {post, [ 10 | {clean, {cargo, clean}}, 11 | {eunit, {cargo, test}} 12 | ]} 13 | ]}. 14 | -------------------------------------------------------------------------------- /ct/rebar3_cargo_SUITE_data/test_nif_app/rust_src/nifsys/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "nifsys" 5 | version = "0.0.2" 6 | dependencies = [ 7 | "rustler_sys 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", 8 | ] 9 | 10 | [[package]] 11 | name = "rustler_sys" 12 | version = "2.0.0" 13 | source = "registry+https://github.com/rust-lang/crates.io-index" 14 | dependencies = [ 15 | "unreachable 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", 16 | ] 17 | 18 | [[package]] 19 | name = "unreachable" 20 | version = "0.1.1" 21 | source = "registry+https://github.com/rust-lang/crates.io-index" 22 | dependencies = [ 23 | "void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", 24 | ] 25 | 26 | [[package]] 27 | name = "void" 28 | version = "1.0.2" 29 | source = "registry+https://github.com/rust-lang/crates.io-index" 30 | 31 | [metadata] 32 | "checksum rustler_sys 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3e2c6dd78a5cffd4294c4aca568522736b8a67f0e8ccdb6a55354dc933726bc4" 33 | "checksum unreachable 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "1f2ae5ddb18e1c92664717616dd9549dde73f539f01bd7b77c2edb2446bdff91" 34 | "checksum void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" 35 | -------------------------------------------------------------------------------- /ct/rebar3_cargo_SUITE_data/test_nif_app/rust_src/nifsys/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "nifsys" 3 | version = "0.0.2" 4 | authors = ["Daniel Goertzen "] 5 | 6 | [lib] 7 | name = "nifsys" 8 | crate-type = ["cdylib"] 9 | 10 | [dependencies] 11 | rustler = "0.22.0-rc.1" 12 | lazy_static = "1.2.0" 13 | -------------------------------------------------------------------------------- /ct/rebar3_cargo_SUITE_data/test_nif_app/rust_src/nifsys/src/lib.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] extern crate rustler; 2 | #[macro_use] extern crate lazy_static; 3 | 4 | use rustler::{Env, Term, NifResult, Encoder}; 5 | 6 | mod atoms { 7 | rustler_atoms! { 8 | atom an_atom; 9 | } 10 | } 11 | 12 | rustler_export_nifs! { 13 | "nifsys", 14 | [("static_atom", 0, static_atom), 15 | ("native_add", 2, native_add), 16 | ("tuple_add", 1, tuple_add)], 17 | None 18 | } 19 | 20 | fn static_atom<'a>(env: Env<'a>, _args: &[Term<'a>]) -> NifResult> { 21 | Ok(atoms::an_atom().encode(env)) 22 | } 23 | 24 | /// Add two integers. `native_add(A,B) -> A+B.` 25 | fn native_add<'a>(env: Env<'a>, args: &[Term<'a>]) -> NifResult> { 26 | let num1: i64 = args[0].decode()?; 27 | let num2: i64 = args[1].decode()?; 28 | 29 | Ok((num1 + num2).encode(env)) 30 | } 31 | 32 | #[derive(NifTuple)] 33 | struct AddTuple { 34 | e1: i32, 35 | e2: i32, 36 | } 37 | 38 | /// Add integers provided in a 2-tuple. `tuple_add({A,B}) -> A+B.` 39 | fn tuple_add<'a>(env: Env<'a>, args: &[Term<'a>]) -> NifResult> { 40 | let tuple: AddTuple = args[0].decode()?; 41 | 42 | Ok((tuple.e1 + tuple.e2).encode(env)) 43 | } 44 | 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /ct/rebar3_cargo_SUITE_data/test_nif_app/src/cargo.hrl: -------------------------------------------------------------------------------- 1 | -cargo_header_version 1. 2 | -ifndef(CARGO_LOAD_APP). 3 | -define(CARGO_LOAD_APP,test_nif_app). 4 | -endif. 5 | -ifndef(CARGO_HRL). 6 | -define(CARGO_HRL, 1). 7 | -define(load_nif_from_crate(__CRATE,__INIT),(fun()->__APP=?CARGO_LOAD_APP,__PATH=filename:join([code:priv_dir(__APP),"crates",__CRATE,__CRATE]),erlang:load_nif(__PATH,__INIT)end)()). 8 | -endif. 9 | -------------------------------------------------------------------------------- /ct/rebar3_cargo_SUITE_data/test_nif_app/src/nifsys.erl: -------------------------------------------------------------------------------- 1 | -module(nifsys). 2 | 3 | -include("cargo.hrl"). 4 | 5 | -export([static_atom/0,native_add/2, tuple_add/1]). 6 | -on_load(init/0). 7 | 8 | -include_lib("eunit/include/eunit.hrl"). 9 | 10 | init() -> 11 | ?load_nif_from_crate(nifsys, 0). 12 | 13 | static_atom() -> 14 | exit(nif_library_not_loaded). 15 | 16 | native_add(_X, _Y) -> 17 | exit(nif_library_not_loaded). 18 | 19 | tuple_add(_X) -> 20 | exit(nif_library_not_loaded). 21 | 22 | short_test_() -> 23 | [ ?_assertEqual('an_atom', static_atom()), 24 | ?_assertEqual(7, native_add(3,4)), 25 | ?_assertEqual(9, tuple_add({4,5}))]. 26 | -------------------------------------------------------------------------------- /ct/rebar3_cargo_SUITE_data/test_nif_app/src/test_nif_app.app.src: -------------------------------------------------------------------------------- 1 | {application, test_nif_app, 2 | [{description, "An OTP library"}, 3 | {vsn, "0.1.0"}, 4 | {registered, []}, 5 | {applications, 6 | [kernel, 7 | stdlib 8 | ]}, 9 | {env,[]}, 10 | {modules, []}, 11 | 12 | {maintainers, []}, 13 | {licenses, []}, 14 | {links, []} 15 | ]}. 16 | -------------------------------------------------------------------------------- /ct/rebar3_cargo_SUITE_data/test_nif_app/src/test_nif_app.erl: -------------------------------------------------------------------------------- 1 | -module(test_nif_app). 2 | 3 | %% API exports 4 | -export([]). 5 | 6 | %%==================================================================== 7 | %% API functions 8 | %%==================================================================== 9 | 10 | 11 | %%==================================================================== 12 | %% Internal functions 13 | %%==================================================================== 14 | -------------------------------------------------------------------------------- /ct/rebar3_cargo_SUITE_data/test_port_app/Cargo.toml: -------------------------------------------------------------------------------- 1 | [workspace] 2 | members = [ 3 | "rust_src/erl_comm" 4 | ] 5 | -------------------------------------------------------------------------------- /ct/rebar3_cargo_SUITE_data/test_port_app/rebar.config: -------------------------------------------------------------------------------- 1 | {plugins, [ 2 | rebar3_cargo 3 | ]}. 4 | 5 | {erl_opts, [debug_info]}. 6 | 7 | {provider_hooks, [ 8 | {pre, [{compile, {cargo, build}}]}, 9 | {post, [ 10 | {clean, {cargo, clean}}, 11 | {eunit, {cargo, test}} 12 | ]} 13 | ]}. 14 | -------------------------------------------------------------------------------- /ct/rebar3_cargo_SUITE_data/test_port_app/rust_src/erl_comm/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "byteorder" 5 | version = "0.4.2" 6 | source = "registry+https://github.com/rust-lang/crates.io-index" 7 | 8 | [[package]] 9 | name = "erl_comm" 10 | version = "0.1.0" 11 | dependencies = [ 12 | "byteorder 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", 13 | ] 14 | 15 | [metadata] 16 | "checksum byteorder 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "96c8b41881888cc08af32d47ac4edd52bc7fa27fef774be47a92443756451304" 17 | -------------------------------------------------------------------------------- /ct/rebar3_cargo_SUITE_data/test_port_app/rust_src/erl_comm/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "erl_comm" 3 | version = "0.1.0" 4 | authors = ["Daniel Goertzen "] 5 | 6 | [dependencies] 7 | byteorder = "0.4.2" 8 | -------------------------------------------------------------------------------- /ct/rebar3_cargo_SUITE_data/test_port_app/rust_src/erl_comm/src/main.rs: -------------------------------------------------------------------------------- 1 | extern crate byteorder; 2 | use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt}; 3 | use std::io::{self, stdin, stdout, Read, Write}; 4 | 5 | // complex.c 6 | 7 | fn foo(x: i32) -> i32 { 8 | x + 1 9 | } 10 | 11 | fn bar(y: i32) -> i32 { 12 | y * 2 13 | } 14 | 15 | 16 | // erl_comm.c 17 | 18 | fn read_cmd(buf: &mut [u8]) -> io::Result<&[u8]> { 19 | let len = r#try!(stdin().read_u16::()) as usize; 20 | let mut result = &mut buf[..len]; 21 | r#try!(stdin().read_exact(&mut result)); 22 | Ok(result) 23 | } 24 | 25 | fn write_cmd(buf: &[u8]) -> io::Result<()> { 26 | r#try!(stdout().write_u16::(buf.len() as u16)); 27 | r#try!(stdout().write_all(buf)); 28 | stdout().flush() 29 | } 30 | 31 | 32 | // port.c 33 | 34 | fn main_loop() -> io::Result<()> { 35 | let mut buf: [u8; 100] = unsafe { std::mem::uninitialized() }; 36 | 37 | loop { 38 | let res = { 39 | let cmd = r#try!(read_cmd(&mut buf[..])); 40 | match cmd[0] { 41 | 1 => foo(cmd[1] as i32), 42 | 2 => bar(cmd[1] as i32), 43 | _ => panic!("invalid input"), 44 | } 45 | }; 46 | buf[0] = res as u8; 47 | r#try!(write_cmd(&buf[..1])); 48 | } 49 | } 50 | 51 | #[allow(unused_must_use)] 52 | fn main() { 53 | main_loop(); // exit normally on any io error 54 | } 55 | -------------------------------------------------------------------------------- /ct/rebar3_cargo_SUITE_data/test_port_app/src/cargo.hrl: -------------------------------------------------------------------------------- 1 | -cargo_header_version 1. 2 | -ifndef(CARGO_LOAD_APP). 3 | -define(CARGO_LOAD_APP,test_port_app). 4 | -endif. 5 | -ifndef(CARGO_HRL). 6 | -define(CARGO_HRL, 1). 7 | -define(load_nif_from_crate(__CRATE,__INIT),(fun()->__APP=?CARGO_LOAD_APP,__PATH=filename:join([code:priv_dir(__APP),"crates",__CRATE,__CRATE]),erlang:load_nif(__PATH,__INIT)end)()). 8 | -endif. 9 | -------------------------------------------------------------------------------- /ct/rebar3_cargo_SUITE_data/test_port_app/src/complex1.erl: -------------------------------------------------------------------------------- 1 | -module(complex1). 2 | -export([start/0,start/1, stop/0, init/1]). 3 | -export([foo/1, bar/1]). 4 | 5 | -include("cargo.hrl"). 6 | -include_lib("eunit/include/eunit.hrl"). 7 | 8 | start() -> 9 | start(filename:join([code:priv_dir(?CARGO_LOAD_APP), "crates", "erl_comm", "erl_comm"])). 10 | 11 | start(ExtPrg) -> 12 | spawn(?MODULE, init, [ExtPrg]). 13 | stop() -> 14 | complex ! stop. 15 | 16 | foo(X) -> 17 | call_port({foo, X}). 18 | bar(Y) -> 19 | call_port({bar, Y}). 20 | 21 | call_port(Msg) -> 22 | complex ! {call, self(), Msg}, 23 | receive 24 | {complex, Result} -> 25 | Result 26 | end. 27 | 28 | init(ExtPrg) -> 29 | register(complex, self()), 30 | process_flag(trap_exit, true), 31 | Port = open_port({spawn, ExtPrg}, [{packet, 2}]), 32 | loop(Port). 33 | 34 | loop(Port) -> 35 | receive 36 | {call, Caller, Msg} -> 37 | Port ! {self(), {command, encode(Msg)}}, 38 | receive 39 | {Port, {data, Data}} -> 40 | Caller ! {complex, decode(Data)} 41 | end, 42 | loop(Port); 43 | stop -> 44 | Port ! {self(), close}, 45 | receive 46 | {Port, closed} -> 47 | exit(normal) 48 | end; 49 | {'EXIT', Port, _Reason} -> 50 | exit(port_terminated) 51 | end. 52 | 53 | encode({foo, X}) -> [1, X]; 54 | encode({bar, Y}) -> [2, Y]. 55 | 56 | decode([Int]) -> Int. 57 | 58 | short_test_() -> 59 | { setup, 60 | fun() -> start() end, 61 | fun(_) -> stop() end, 62 | [ ?_assertEqual(13, foo(12)), 63 | ?_assertEqual(24, bar(12)) ]}. 64 | -------------------------------------------------------------------------------- /ct/rebar3_cargo_SUITE_data/test_port_app/src/test_port_app.app.src: -------------------------------------------------------------------------------- 1 | {application, test_port_app, 2 | [{description, "An OTP library"}, 3 | {vsn, "0.1.0"}, 4 | {registered, []}, 5 | {applications, 6 | [kernel, 7 | stdlib 8 | ]}, 9 | {env,[]}, 10 | {modules, []}, 11 | 12 | {maintainers, []}, 13 | {licenses, []}, 14 | {links, []} 15 | ]}. 16 | -------------------------------------------------------------------------------- /ct/rebar3_cargo_SUITE_data/test_port_app/src/test_port_app.erl: -------------------------------------------------------------------------------- 1 | -module(test_port_app). 2 | 3 | %% API exports 4 | -export([]). 5 | 6 | %%==================================================================== 7 | %% API functions 8 | %%==================================================================== 9 | 10 | 11 | %%==================================================================== 12 | %% Internal functions 13 | %%==================================================================== 14 | -------------------------------------------------------------------------------- /rebar.config: -------------------------------------------------------------------------------- 1 | {project_plugins, [{erlfmt, "~> 1.0"}]}. 2 | 3 | {deps, [{cargo, "0.1.3"}]}. 4 | 5 | {eunit_tests, [rebar3_cargo]}. 6 | 7 | {ct_opts, [ 8 | {dir, "ct"} 9 | ]}. 10 | 11 | {dialyzer, [ 12 | {warnings, [unknown]} 13 | ]}. 14 | 15 | {erlfmt, [ 16 | check, 17 | verbose, 18 | {files, ["src/*", "ct/*.erl", "rebar.config"]} 19 | ]}. 20 | -------------------------------------------------------------------------------- /rebar.lock: -------------------------------------------------------------------------------- 1 | {"1.2.0", 2 | [{<<"cargo">>,{pkg,<<"cargo">>,<<"0.1.3">>},0}, 3 | {<<"jsx">>,{pkg,<<"jsx">>,<<"2.11.0">>},1}]}. 4 | [ 5 | {pkg_hash,[ 6 | {<<"cargo">>, <<"2A06177C3846B529BBB6FADB22C823D9806E033A7460EA6F2D37C2C23BB001C3">>}, 7 | {<<"jsx">>, <<"08154624050333919B4AC1B789667D5F4DB166DC50E190C4D778D1587F102EE0">>}]}, 8 | {pkg_hash_ext,[ 9 | {<<"cargo">>, <<"5482AC4BF6F087D6AB893A30C0E17F39BD6F6FAED8A4FB2D8D36548BBD6AFCC9">>}, 10 | {<<"jsx">>, <<"EED26A0D04D217F9EECEFFFB89714452556CF90EB38F290A27A4D45B9988F8C0">>}]} 11 | ]. 12 | -------------------------------------------------------------------------------- /rebar3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rusterlium/rebar3_cargo/d690d783a913c5585342f245600a1c9b1b928166/rebar3 -------------------------------------------------------------------------------- /src/internal.hrl: -------------------------------------------------------------------------------- 1 | -ifndef(REBAR3_CARGO_INTERNAL_HRL). 2 | -define(REBAR3_CARGO_INTERNAL_HRL, 1). 3 | 4 | -define(NAMESPACE, cargo). 5 | 6 | -endif. 7 | -------------------------------------------------------------------------------- /src/rebar3_cargo.app.src: -------------------------------------------------------------------------------- 1 | {application,rebar3_cargo, 2 | [{description,"A rebar plugin to build Rust crates"}, 3 | {vsn,"0.1.1"}, 4 | {registered,[]}, 5 | {applications,[kernel,stdlib,cargo]}, 6 | {env,[]}, 7 | {modules,[]}, 8 | {licenses,["Apache-2.0","MIT"]}, 9 | {links,[{"Github", 10 | "https://github.com/rusterlium/rebar3_cargo"}]}]}. 11 | -------------------------------------------------------------------------------- /src/rebar3_cargo.erl: -------------------------------------------------------------------------------- 1 | -module(rebar3_cargo). 2 | 3 | -ignore_xref(init/1). 4 | -export([init/1]). 5 | 6 | -spec init(rebar_state:t()) -> {ok, rebar_state:t()}. 7 | init(State0) -> 8 | {ok, State1} = rebar3_cargo_compile_prv:init(State0), 9 | {ok, State2} = rebar3_cargo_clean_prv:init(State1), 10 | {ok, State3} = rebar3_cargo_test_prv:init(State2), 11 | {ok, State4} = rebar3_cargo_compile_deps_prv:init(State3), 12 | {ok, State4}. 13 | -------------------------------------------------------------------------------- /src/rebar3_cargo_clean_prv.erl: -------------------------------------------------------------------------------- 1 | -module(rebar3_cargo_clean_prv). 2 | 3 | -export([ 4 | init/1, 5 | do/1, 6 | format_error/1 7 | ]). 8 | 9 | -include("internal.hrl"). 10 | 11 | -define(PROVIDER, clean). 12 | -define(DEPS, [{default, app_discovery}]). 13 | 14 | %% =================================================================== 15 | %% Public API 16 | %% =================================================================== 17 | -spec init(rebar_state:t()) -> {ok, rebar_state:t()}. 18 | init(State) -> 19 | Provider = providers:create([ 20 | % The 'user friendly' name of the task 21 | {name, ?PROVIDER}, 22 | {namespace, ?NAMESPACE}, 23 | % The module implementation of the task 24 | {module, ?MODULE}, 25 | % The task can be run by the user, always true 26 | {bare, true}, 27 | % The list of dependencies 28 | {deps, ?DEPS}, 29 | % How to use the plugin 30 | {example, "rebar3 cargo clean"}, 31 | % list of options understood by the plugin 32 | {opts, []}, 33 | {short_desc, "Clean Rust crates"}, 34 | {desc, "Clean Rust crates"} 35 | ]), 36 | {ok, rebar_state:add_provider(State, Provider)}. 37 | 38 | -spec format_error(any()) -> iolist(). 39 | format_error(Reason) -> 40 | io_lib:format("~p", [Reason]). 41 | 42 | -spec do(rebar_state:t()) -> {ok, rebar_state:t()} | {error, string()}. 43 | do(State) -> 44 | %% execute for each app 45 | [clean_app(App) || App <- rebar3_cargo_util:get_apps(State)], 46 | {ok, State}. 47 | 48 | clean_app(App) -> 49 | PrivDir = rebar3_cargo_util:get_priv_dir(App), 50 | CargoOpts = rebar3_cargo_opts:from_app(App), 51 | Cargo = rebar3_cargo_util:cargo_init(App, CargoOpts, false), 52 | catch cargo:clean(Cargo), 53 | 54 | %% delete priv/crates 55 | ok = rebar_file_utils:rm_rf(filename:join(PrivDir, "crates")), 56 | 57 | ok. 58 | -------------------------------------------------------------------------------- /src/rebar3_cargo_compile.erl: -------------------------------------------------------------------------------- 1 | -module(rebar3_cargo_compile). 2 | 3 | -export([ 4 | force_compile_app/2, 5 | compile_app/2 6 | ]). 7 | 8 | -spec force_compile_app(rebar_app_info:t(), rebar_state:t()) -> rebar_app_info:t(). 9 | force_compile_app(App, State) -> 10 | CargoOpts = rebar3_cargo_opts:from_app(App), 11 | rebar3_cargo_header:ensure(App), 12 | 13 | TargetApp = get_target_app(CargoOpts, App, State), 14 | do_compile_app(CargoOpts, App, TargetApp, State), 15 | patch_app(App, TargetApp). 16 | 17 | -spec compile_app(rebar_app_info:t(), rebar_state:t()) -> rebar_app_info:t(). 18 | compile_app(App, State) -> 19 | CargoOpts = rebar3_cargo_opts:from_app(App), 20 | rebar3_cargo_header:ensure(App), 21 | 22 | TargetApp = get_target_app(CargoOpts, App, State), 23 | 24 | case rebar3_cargo_opts:skip(CargoOpts) of 25 | true -> 26 | rebar_log:log(info, "Skipping cargo build of ~s", [rebar_app_info:name(App)]); 27 | _ -> 28 | do_compile_app(CargoOpts, App, TargetApp, State) 29 | end, 30 | 31 | patch_app(App, TargetApp). 32 | 33 | do_compile_app(CargoOpts, App, TargetApp, State) -> 34 | IsRelease = 35 | case rebar3_cargo_opts:mode(CargoOpts) of 36 | release -> 37 | true; 38 | debug -> 39 | false; 40 | auto -> 41 | lists:member(prod, rebar_state:current_profiles(State)) 42 | end, 43 | 44 | Cargo = rebar3_cargo_util:cargo_init(App, CargoOpts, IsRelease), 45 | Artifacts = cargo:build_all(Cargo), 46 | 47 | lists:foreach( 48 | fun(Artifact) -> 49 | do_crate(Artifact, IsRelease, TargetApp) 50 | end, 51 | Artifacts 52 | ). 53 | 54 | patch_app(App, TargetApp) -> 55 | AppName = binary_to_atom(rebar_app_info:name(TargetApp), utf8), 56 | ErlOpts = [{d, 'CARGO_LOAD_APP', AppName}], 57 | 58 | Opts = rebar_app_info:opts(App), 59 | 60 | ErlOpts1 = ErlOpts ++ rebar_opts:get(Opts, erl_opts, []), 61 | Opts1 = rebar_opts:set(Opts, erl_opts, ErlOpts1), 62 | 63 | rebar_app_info:opts(App, Opts1). 64 | 65 | get_target_app(CargoOpts, App, State) -> 66 | case rebar3_cargo_opts:load_from_app(CargoOpts) of 67 | undefined -> 68 | App; 69 | Other -> 70 | [OtherApp | _] = [ 71 | A 72 | || A <- rebar_state:project_apps(State), 73 | rebar_app_info:name(A) =:= atom_to_binary(Other, utf8) 74 | ], 75 | OtherApp 76 | end. 77 | 78 | do_crate(Artifact, IsRelease, App) -> 79 | Name = cargo_artifact:name(Artifact), 80 | _Version = cargo_artifact:version(Artifact), 81 | Files = cargo_artifact:filenames(Artifact), 82 | 83 | _Type = 84 | case IsRelease of 85 | true -> 86 | "release"; 87 | false -> 88 | "debug" 89 | end, 90 | 91 | PrivDir = rebar3_cargo_util:get_priv_dir(App), 92 | OutDir = filename:join([PrivDir, "crates", Name]), 93 | 94 | filelib:ensure_dir(filename:join([OutDir, "dummy"])), 95 | 96 | % TODO: Distinguish nif vs. other cases here? 97 | lists:foreach( 98 | fun(F) -> 99 | case filelib:is_regular(F) of 100 | true -> 101 | cp(F, OutDir); 102 | _ -> 103 | false 104 | end 105 | end, 106 | Files 107 | ), 108 | 109 | ok. 110 | 111 | -spec cp(file:filename_all(), file:name_all()) -> ok | {error, ignored}. 112 | cp(Src, DstDir) -> 113 | Extension = filename:extension(Src), 114 | Fname = filename:basename(Src, Extension), 115 | 116 | % Erlang's load_nif/2 is not prepared to read Mac OS's .dylib extension, 117 | % take a shortcut here and rename the extension to .so in that case 118 | Dst0 = maybe_rename_extension(os:type(), Fname, Extension), 119 | Dst1 = maybe_strip_lib(os:type(), Dst0), 120 | 121 | OutPath = filename:join([DstDir, Dst1]), 122 | 123 | rebar_api:info(" Copying ~s to ~s...", [Fname, OutPath]), 124 | 125 | file:delete(OutPath), 126 | 127 | case file:copy(Src, OutPath) of 128 | {ok, _} -> 129 | % Preserve file info 130 | {ok, SrcFileInfo} = file:read_file_info(Src), 131 | ok = file:write_file_info(OutPath, SrcFileInfo), 132 | ok; 133 | Error -> 134 | rebar_api:warn(" Failed to copy ~s: ~p", [Fname, Error]) 135 | end, 136 | ok. 137 | 138 | -spec maybe_rename_extension( 139 | OsType :: {unix | win32, atom()}, 140 | Name :: file:filename_all(), 141 | Extension :: file:filename_all() 142 | ) -> file:filename_all(). 143 | maybe_rename_extension({unix, darwin}, Name, <<".dylib">>) -> 144 | filename:flatten([Name, ".so"]); 145 | maybe_rename_extension(_, Name, Extension) -> 146 | filename:flatten([Name, Extension]). 147 | 148 | maybe_strip_lib({win32, _}, Name) -> 149 | Name; 150 | maybe_strip_lib(_, Name) -> 151 | case string:prefix(filename:flatten(Name), "lib") of 152 | nomatch -> 153 | filename:flatten(Name); 154 | Else -> 155 | Else 156 | end. 157 | -------------------------------------------------------------------------------- /src/rebar3_cargo_compile_deps_prv.erl: -------------------------------------------------------------------------------- 1 | -module(rebar3_cargo_compile_deps_prv). 2 | 3 | -export([ 4 | init/1, 5 | do/1, 6 | format_error/1 7 | ]). 8 | 9 | -include("internal.hrl"). 10 | 11 | -define(PROVIDER, 'build-all'). 12 | -define(DEPS, [{default, lock}]). 13 | 14 | %% =================================================================== 15 | %% Public API 16 | %% =================================================================== 17 | -spec init(rebar_state:t()) -> {ok, rebar_state:t()}. 18 | init(State) -> 19 | Provider = providers:create([ 20 | % The 'user friendly' name of the task 21 | {name, ?PROVIDER}, 22 | {namespace, ?NAMESPACE}, 23 | % The module implementation of the task 24 | {module, ?MODULE}, 25 | % The task can be run by the user, always true 26 | {bare, true}, 27 | % The list of dependencies 28 | {deps, ?DEPS}, 29 | % How to use the plugin 30 | {example, "rebar3 cargo build-all"}, 31 | % list of options understood by the plugin 32 | {opts, []}, 33 | {short_desc, "Compile all dependency Rust crates"}, 34 | {desc, "Compile all dependency Rust crates"} 35 | ]), 36 | {ok, rebar_state:add_provider(State, Provider)}. 37 | 38 | -spec format_error(any()) -> iolist(). 39 | format_error(Reason) -> 40 | io_lib:format("~p", [Reason]). 41 | 42 | -spec do(rebar_state:t()) -> {ok, rebar_state:t()} | {error, string()}. 43 | do(State) -> 44 | %% execute for each app 45 | AllDeps = rebar_state:all_deps(State), 46 | AllDeps1 = run_cargo(AllDeps, State), 47 | State1 = rebar_state:all_deps(State, AllDeps1), 48 | 49 | AllApps = rebar_state:project_apps(State1), 50 | AllApps1 = run_cargo(AllApps, State1), 51 | State2 = rebar_state:project_apps(State, AllApps1), 52 | 53 | {ok, State2}. 54 | 55 | run_cargo(Apps, State) -> 56 | lists:map( 57 | fun(App) -> 58 | case rebar3_cargo_util:is_cargo_app(App) of 59 | true -> 60 | rebar3_cargo_compile:force_compile_app(App, State); 61 | false -> 62 | App 63 | end 64 | end, 65 | Apps 66 | ). 67 | -------------------------------------------------------------------------------- /src/rebar3_cargo_compile_prv.erl: -------------------------------------------------------------------------------- 1 | -module(rebar3_cargo_compile_prv). 2 | 3 | -export([ 4 | init/1, 5 | do/1, 6 | format_error/1 7 | ]). 8 | 9 | -include("internal.hrl"). 10 | 11 | -define(PROVIDER, build). 12 | -define(DEPS, [{default, app_discovery}]). 13 | 14 | %% =================================================================== 15 | %% Public API 16 | %% =================================================================== 17 | -spec init(rebar_state:t()) -> {ok, rebar_state:t()}. 18 | init(State) -> 19 | Provider = providers:create([ 20 | % The 'user friendly' name of the task 21 | {name, ?PROVIDER}, 22 | {namespace, ?NAMESPACE}, 23 | % The module implementation of the task 24 | {module, ?MODULE}, 25 | % The task can be run by the user, always true 26 | {bare, true}, 27 | % The list of dependencies 28 | {deps, ?DEPS}, 29 | % How to use the plugin 30 | {example, "rebar3 cargo build"}, 31 | % list of options understood by the plugin 32 | {opts, []}, 33 | {short_desc, "Compile Rust crates"}, 34 | {desc, "Compile Rust crates"} 35 | ]), 36 | {ok, rebar_state:add_provider(State, Provider)}. 37 | 38 | -spec format_error(any()) -> iolist(). 39 | format_error(Reason) -> 40 | io_lib:format("~p", [Reason]). 41 | 42 | -spec do(rebar_state:t()) -> {ok, rebar_state:t()} | {error, string()}. 43 | do(State) -> 44 | %% execute for each app 45 | State1 = 46 | case rebar_state:current_app(State) of 47 | undefined -> 48 | rebar_api:info("No current app, using project apps", []), 49 | NewApps = [ 50 | rebar3_cargo_compile:compile_app(App, State) 51 | || App <- rebar_state:project_apps(State) 52 | ], 53 | rebar_state:project_apps(State, NewApps); 54 | AppInfo -> 55 | rebar_state:current_app(State, rebar3_cargo_compile:compile_app(AppInfo, State)) 56 | end, 57 | 58 | {ok, State1}. 59 | -------------------------------------------------------------------------------- /src/rebar3_cargo_header.erl: -------------------------------------------------------------------------------- 1 | -module(rebar3_cargo_header). 2 | 3 | -export([ 4 | ensure/1 5 | ]). 6 | 7 | -spec ensure(rebar_app_info:t()) -> ok. 8 | ensure(App) -> 9 | CurrentVersion = 1, 10 | Attr = cargo_header_version, 11 | 12 | OutDir = rebar_app_info:dir(App), 13 | OutPath = filename:join([OutDir, "src", "cargo.hrl"]), 14 | filelib:ensure_dir(OutPath), 15 | 16 | % Check the currently written version 17 | ExistingVersion = 18 | case epp:parse_file(OutPath, []) of 19 | {ok, Tree} -> 20 | ARes = erl_syntax_lib:analyze_forms(Tree), 21 | Attrs = proplists:get_value(attributes, ARes, []), 22 | proplists:get_value(Attr, Attrs, undefined); 23 | _Err -> 24 | undefined 25 | end, 26 | 27 | if 28 | ExistingVersion =:= undefined orelse ExistingVersion < CurrentVersion -> 29 | rebar_log:log( 30 | info, 31 | "Writing new cargo header file for ~s...", 32 | [rebar_app_info:name(App)] 33 | ), 34 | 35 | AppDefine = "CARGO_LOAD_APP", 36 | FuncDefine = "CARGO_HRL", 37 | 38 | %% erlfmt-ignore 39 | Hrl = [ 40 | "-", 41 | atom_to_list(Attr), 42 | " ", 43 | integer_to_list(CurrentVersion), 44 | ".\n", 45 | 46 | "-ifndef(", 47 | AppDefine, 48 | ").\n", 49 | "-define(", 50 | AppDefine, 51 | ",", 52 | rebar_app_info:name(App), 53 | ").\n", 54 | "-endif.\n" 55 | "-ifndef(", 56 | FuncDefine, 57 | ").\n", 58 | "-define(", 59 | FuncDefine, 60 | ", 1).\n", 61 | 62 | "-define(load_nif_from_crate(__CRATE,__INIT)," 63 | "(fun()->", 64 | "__APP=?", 65 | AppDefine, 66 | ",", 67 | "__PATH=filename:join([code:priv_dir(__APP),\"crates\",__CRATE,__CRATE])," 68 | "erlang:load_nif(__PATH,__INIT)" 69 | "end)()" 70 | ").\n", 71 | 72 | "-endif.\n" 73 | ], 74 | 75 | file:write_file(OutPath, Hrl); 76 | true -> 77 | rebar_log:log(debug, " Skip writing cargo header file", []) 78 | end. 79 | -------------------------------------------------------------------------------- /src/rebar3_cargo_opts.erl: -------------------------------------------------------------------------------- 1 | -module(rebar3_cargo_opts). 2 | 3 | -export([ 4 | from_app/1, 5 | from_state/1 6 | ]). 7 | 8 | -export([ 9 | mode/1, 10 | skip/1, 11 | load_from_app/1, 12 | src_dir/1 13 | ]). 14 | 15 | -export_type([ 16 | t/0 17 | ]). 18 | 19 | -define(DEFAULT_SRC_DIR, "."). 20 | -define(KEY, cargo_opts). 21 | 22 | -type mode() :: release | debug | auto. 23 | 24 | -record(opts, { 25 | mode :: mode(), 26 | skip :: boolean(), 27 | load_from_app :: atom(), 28 | src_dir :: filelib:file_path() 29 | }). 30 | 31 | -opaque t() :: #opts{}. 32 | 33 | -spec from_app(rebar_app_info:t()) -> t(). 34 | from_app(App) -> 35 | AppOpts = rebar_app_info:opts(App), 36 | Opts = 37 | case dict:find(?KEY, AppOpts) of 38 | {ok, O} -> 39 | O; 40 | _ -> 41 | [] 42 | end, 43 | 44 | from_opts(Opts). 45 | 46 | -spec from_state(rebar_state:t()) -> t(). 47 | from_state(State) -> 48 | CargoOpts = rebar_state:get(State, cargo_opts, []), 49 | from_opts(CargoOpts). 50 | 51 | from_opts(Opts) -> 52 | Release = proplists:get_value(release, Opts), 53 | Debug = proplists:get_value(debug, Opts), 54 | 55 | Mode = 56 | case {Release, Debug} of 57 | {true, _} -> 58 | release; 59 | {_, true} -> 60 | debug; 61 | _ -> 62 | auto 63 | end, 64 | 65 | #opts{ 66 | mode = Mode, 67 | skip = proplists:get_bool(skip, Opts), 68 | load_from_app = proplists:get_value(load_from_app, Opts), 69 | src_dir = proplists:get_value(src_dir, Opts, ?DEFAULT_SRC_DIR) 70 | }. 71 | 72 | -spec mode(t()) -> mode(). 73 | mode(#opts{mode = Mode}) -> Mode. 74 | 75 | -spec skip(t()) -> boolean(). 76 | skip(#opts{skip = Skip}) -> Skip. 77 | 78 | -spec load_from_app(t()) -> atom(). 79 | load_from_app(#opts{load_from_app = App}) -> App. 80 | 81 | -spec src_dir(t()) -> string(). 82 | src_dir(#opts{src_dir = SrcDir}) -> SrcDir. 83 | -------------------------------------------------------------------------------- /src/rebar3_cargo_test_prv.erl: -------------------------------------------------------------------------------- 1 | -module(rebar3_cargo_test_prv). 2 | 3 | -export([ 4 | init/1, 5 | do/1, 6 | format_error/1 7 | ]). 8 | 9 | -include("internal.hrl"). 10 | 11 | -define(PROVIDER, test). 12 | -define(DEPS, [{default, app_discovery}]). 13 | 14 | %% =================================================================== 15 | %% Public API 16 | %% =================================================================== 17 | -spec init(rebar_state:t()) -> {ok, rebar_state:t()}. 18 | init(State) -> 19 | Provider = providers:create([ 20 | % The 'user friendly' name of the task 21 | {name, ?PROVIDER}, 22 | {namespace, ?NAMESPACE}, 23 | % The module implementation of the task 24 | {module, ?MODULE}, 25 | % The task can be run by the user, always true 26 | {bare, true}, 27 | % The list of dependencies 28 | {deps, ?DEPS}, 29 | % How to use the plugin 30 | {example, "rebar3 cargo test"}, 31 | % list of options understood by the plugin 32 | {opts, []}, 33 | {short_desc, "Test Rust crates"}, 34 | {desc, "Test Rust crates"} 35 | ]), 36 | {ok, rebar_state:add_provider(State, Provider)}. 37 | 38 | -spec format_error(any()) -> iolist(). 39 | format_error(Reason) -> 40 | io_lib:format("~p", [Reason]). 41 | 42 | -spec do(rebar_state:t()) -> {ok, rebar_state:t()} | {error, string()}. 43 | do(State) -> 44 | %% execute for each app 45 | [test_app(App) || App <- rebar3_cargo_util:get_apps(State)], 46 | {ok, State}. 47 | 48 | test_app(App) -> 49 | CargoOpts = rebar3_cargo_opts:from_app(App), 50 | Cargo = rebar3_cargo_util:cargo_init(App, CargoOpts, false), 51 | cargo:test_all(Cargo). 52 | -------------------------------------------------------------------------------- /src/rebar3_cargo_util.erl: -------------------------------------------------------------------------------- 1 | -module(rebar3_cargo_util). 2 | 3 | -cargo_header_version(1). 4 | 5 | -export([ 6 | get_apps/1, 7 | get_priv_dir/1, 8 | cargo_init/3, 9 | is_cargo_app/1 10 | ]). 11 | 12 | -define(DEFAULT_TARGET_DIR, "target"). 13 | 14 | -spec get_apps(rebar_state:t()) -> [rebar_app_info:t()]. 15 | get_apps(State) -> 16 | Res = 17 | case rebar_state:current_app(State) of 18 | undefined -> 19 | rebar_state:project_apps(State); 20 | AppInfo -> 21 | [AppInfo] 22 | end, 23 | [App || App <- Res, is_cargo_app(App)]. 24 | 25 | -spec get_priv_dir(rebar_app_info:t()) -> file:filename_all(). 26 | get_priv_dir(App) -> 27 | %PrivDir = rebar_app_info:priv_dir(App), 28 | % ensure_dir/1 fails if priv not present 29 | % (ref https://github.com/erlang/rebar3/issues/1173) 30 | AppDir = rebar_app_info:dir(App), 31 | filename:join([AppDir, "priv"]). 32 | 33 | -spec cargo_init( 34 | App :: rebar_app_info:t(), 35 | CargoOpts :: rebar3_cargo_opts:t(), 36 | IsRelease :: boolean() 37 | ) -> cargo_opts:t(). 38 | cargo_init(App, _CargoOpts, IsRelease) -> 39 | cargo_opts:new(#{ 40 | path => get_src_dir(App), 41 | target_dir => list_to_binary( 42 | filename:join([rebar_app_info:out_dir(App), ?DEFAULT_TARGET_DIR]) 43 | ), 44 | release => IsRelease 45 | }). 46 | 47 | -spec get_src_dir(rebar_app_info:t()) -> filelib:path(). 48 | get_src_dir(App) -> 49 | Opts = rebar3_cargo_opts:from_app(App), 50 | AppDir = rebar_app_info:dir(App), 51 | filename:join(AppDir, rebar3_cargo_opts:src_dir(Opts)). 52 | 53 | -spec is_cargo_app(rebar_app_info:t()) -> boolean(). 54 | is_cargo_app(App) -> 55 | filelib:is_file(filename:join(get_src_dir(App), "Cargo.toml")). 56 | --------------------------------------------------------------------------------