Marcel Hlopko | e8f1c4e | 2021-07-28 18:12:49 +0000 | [diff] [blame] | 1 | # Crubit: C++/Rust Bidirectional Interop Tool |
| 2 | |
Marcel Hlopko | d391661 | 2022-06-03 02:25:50 -0700 | [diff] [blame] | 3 | [](https://buildkite.com/bazel/crubit) |
| 4 | |
Devin Jeanpierre | 8bc1cd0 | 2023-11-30 00:55:38 -0800 | [diff] [blame] | 5 | Crubit is a bidirectional bindings generator for C++ and Rust. |
Marcel Hlopko | e8f1c4e | 2021-07-28 18:12:49 +0000 | [diff] [blame] | 6 | |
Marcel Hlopko | ac46bf9 | 2022-03-25 15:27:22 +0000 | [diff] [blame] | 7 | Please don't use, this is an experiment and we don't yet know where will it take |
Devin Jeanpierre | d7add54 | 2023-04-03 14:40:23 -0700 | [diff] [blame] | 8 | us. There will be breaking changes without warning. Unfortunately, we can't |
| 9 | take contributions at this point. |
| 10 | |
Jing Lu | dd2ed2e | 2023-07-14 06:47:10 -0700 | [diff] [blame] | 11 | Crubit allows for C++ code and Rust code to call each other without manually |
| 12 | wrapping the APIs in an FFI-friendly interop layer. For example, a C++ function |
| 13 | like this: |
Devin Jeanpierre | d7add54 | 2023-04-03 14:40:23 -0700 | [diff] [blame] | 14 | |
| 15 | ```c++ |
| 16 | bool IsAbsPath(std::string_view path); |
| 17 | ``` |
| 18 | |
| 19 | ... becomes callable from Rust as if it were defined as: |
| 20 | |
| 21 | ```rs |
| 22 | pub fn IsAbsPath(path: std::string_view) -> bool {...} |
| 23 | ``` |
| 24 | |
| 25 | Crubit automatically generates ABI-compatible bindings for structs (which can be |
| 26 | passed both by value and by reference), functions, and methods, for a large |
| 27 | variety of types. (Trivial types, nontrivial types, templated types, etc.) |
Dmitri Gribenko | 184a80a | 2022-05-23 14:55:43 -0700 | [diff] [blame] | 28 | |
| 29 | ## Building Crubit |
| 30 | |
| 31 | ``` |
| 32 | $ apt install clang lld bazel |
| 33 | $ git clone git@github.com:google/crubit.git |
| 34 | $ cd crubit |
| 35 | $ bazel build --linkopt=-fuse-ld=/usr/bin/ld.lld //rs_bindings_from_cc:rs_bindings_from_cc_impl |
Matthew Riley | 03a01e3 | 2022-05-24 10:16:04 -0700 | [diff] [blame] | 36 | ``` |
| 37 | |
| 38 | ### Using a prebuilt LLVM tree |
| 39 | |
| 40 | ``` |
| 41 | $ git clone https://github.com/llvm/llvm-project |
| 42 | $ cd llvm-project |
Lukasz Anforowicz | 4c77264 | 2022-05-30 11:11:02 -0700 | [diff] [blame] | 43 | $ CC=clang CXX=clang++ cmake -S llvm -B build -DLLVM_ENABLE_PROJECTS='clang' -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=install |
Matthew Riley | 03a01e3 | 2022-05-24 10:16:04 -0700 | [diff] [blame] | 44 | $ cmake --build build -j |
| 45 | $ # wait... |
Lukasz Anforowicz | 4c77264 | 2022-05-30 11:11:02 -0700 | [diff] [blame] | 46 | $ cmake --install build |
Matthew Riley | 03a01e3 | 2022-05-24 10:16:04 -0700 | [diff] [blame] | 47 | $ cd ../crubit |
Lukasz Anforowicz | 4c77264 | 2022-05-30 11:11:02 -0700 | [diff] [blame] | 48 | $ LLVM_INSTALL_PATH=../llvm-project/install bazel build //rs_bindings_from_cc:rs_bindings_from_cc_impl |
| 49 | ``` |