Git Product home page Git Product logo

crubit's Introduction

Crubit: C++/Rust Bidirectional Interop Tool

Build status

NOTE: Crubit currently expects deep integration with the build system, and is difficult to deploy to environments dissimilar to Google's monorepo. We do not have our tooling set up to accept external contributions at this time.

Crubit is a bidirectional bindings generator for C++ and Rust, with the goal of integrating the C++ and Rust ecosystems. With Crubit, Rust can be used anywhere C++ can be, directly calling and being called by C++. Crubit does not require wrapping C++ or Rust libraries in "FFI-friendly", simplified APIs. Any C++ interface can be called or implemented by Rust code.

Current status: Crubit is aiming for an initial stable "MVP" version, comparable to bindgen and cbindgen. This will support basic datatypes like integers and pointers, (rust-movable) structs/unions/enums, and extern "C" functions.

Example

Consider the following C++ function:

extern "C" bool IsAbsPath(std::string_view path);

This function, if present in a header file which is processed by Crubit, becomes callable from Rust as if it were defined as:

pub fn IsAbsPath(path: std::string_view) -> bool {...}

There are some temporary restrictions on the API shape. For example, if path were a std::string, or a non-extern "C" function, it would not be callable from Rust directly via Crubit. (For example, std::string is not rust-movable.) These restrictions will be relaxed over time.

For actual copy-pastable examples on getting started with Crubit, see the examples/ subdirectory. examples/cpp includes code which actually calls C++ via Crubit, and a snapshot of what the generated interface looks like.

Building Crubit

$ apt install clang lld bazel
$ git clone [email protected]:google/crubit.git
$ cd crubit
$ bazel build --linkopt=-fuse-ld=/usr/bin/ld.lld //rs_bindings_from_cc:rs_bindings_from_cc_impl

Using a prebuilt LLVM tree

$ git clone https://github.com/llvm/llvm-project
$ cd llvm-project
$ CC=clang CXX=clang++ cmake -S llvm -B build -DLLVM_ENABLE_PROJECTS='clang' -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=install
$ cmake --build build -j
$ # wait...
$ cmake --install build
$ cd ../crubit
$ LLVM_INSTALL_PATH=../llvm-project/install bazel build //rs_bindings_from_cc:rs_bindings_from_cc_impl

crubit's People

Contributors

anforowicz avatar cramertj avatar cushon avatar d0k avatar danakj avatar durin42 avatar felipeamp avatar gigaroby avatar gribozavr avatar hlopko avatar kinu avatar kiwiwarmnfuzzy avatar kotlaja avatar krasimirgg avatar martinboehme avatar maskray avatar merrymeerkat avatar mforster avatar mmdriley avatar mvanbem-goog avatar not-an-aardvark avatar sam-mccall avatar scentini avatar slackito avatar ssbr avatar veluca93 avatar weiyi-t avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

crubit's Issues

Consider retiring the "Build with Bazel provided LLVM installation" instructions

For several reasons:

  1. The instructions as they stand, do not work. The commit hash declared in bazel/llvm.bzl is API incompatible with the crubit source code. See Instructions as written failure below.
  2. If the commit hash is updated to something recent, the build still fails. See Failure with updated commit below.
  3. Removing this option from the installation instructions will simplify them for users and contract the support surface.
  4. The bazel build of LLVM doesn't seem actively maintained. The Failure with updated commit at first glance looks to be a defect in LLVM's bazel build.

Instructions as written failure

dsankel@AP-PW015KJM:~/code/crubit$ bazel build --linkopt=-fuse-ld=/usr/bin/ld.lld //rs_bindings_from_cc:rs_bindings_from_cc_impl
DEBUG: Rule 'llvm-raw' indicated that a canonical reproducible form can be obtained by modifying arguments shallow_since = "1661922120 +0800"
DEBUG: Repository llvm-raw instantiated at:
  /home/dsankel/code/crubit/WORKSPACE:127:36: in <toplevel>
  /home/dsankel/code/crubit/bazel/llvm.bzl:61:23: in llvm_loader_repository_dependencies
Repository rule new_git_repository defined at:
  /home/dsankel/.cache/bazel/_bazel_dsankel/1360c4342c51417fe5124e0a1427d9ea/external/bazel_tools/tools/build_defs/repo/git.bzl:186:37: in <toplevel>
INFO: Analyzed target //rs_bindings_from_cc:rs_bindings_from_cc_impl (121 packages loaded, 9510 targets configured).
INFO: Found 1 target...
ERROR: /home/dsankel/code/crubit/lifetime_annotations/BUILD:26:11: Compiling lifetime_annotations/type_lifetimes.cc failed: (Exit 1): clang-16 failed: error executing command /home/dsankel/code/llvm-project/install/bin/clang-16 -U_FORTIFY_SOURCE -fstack-protector -Wall -Wthread-safety -Wself-assign -Wunused-but-set-parameter -Wno-free-nonheap-object -fcolor-diagnostics ... (remaining 88 arguments skipped)

Use --sandbox_debug to see verbose messages from the sandbox and retain the sandbox build root for debugging
lifetime_annotations/type_lifetimes.cc:742:40: error: cannot initialize a variable of type 'const clang::TemplateTypeParmDecl *' with an rvalue of type 'const TemplateTypeParmType *'
    const clang::TemplateTypeParmDecl* type_parm = targ->getReplacedParameter();
                                       ^           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
lifetime_annotations/type_lifetimes.cc:779:48: error: cannot initialize a variable of type 'const clang::TemplateTypeParmDecl *' with an rvalue of type 'const TemplateTypeParmType *'
            const clang::TemplateTypeParmDecl* type_parm =
                                               ^
2 errors generated.
Target //rs_bindings_from_cc:rs_bindings_from_cc_impl failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 318.474s, Critical Path: 66.47s
INFO: 2279 processes: 1038 internal, 1241 linux-sandbox.
FAILED: Build did NOT complete successfully

Failure with updated commit

Using commit 163bb6d64e5f1220777c3ec2a8b58c0666a74d91.

dsankel@AP-PW015KJM:~/code/crubit$ bazel build --linkopt=-fuse-ld=/usr/bin/ld.lld //rs_bindings_from_cc:rs_bindings_from_cc_impl
DEBUG: Rule 'llvm-raw' indicated that a canonical reproducible form can be obtained by modifying arguments shallow_since = "1669057075 -0500"
DEBUG: Repository llvm-raw instantiated at:
  /home/dsankel/code/crubit/WORKSPACE:127:36: in <toplevel>
  /home/dsankel/code/crubit/bazel/llvm.bzl:61:23: in llvm_loader_repository_dependencies
Repository rule new_git_repository defined at:
  /home/dsankel/.cache/bazel/_bazel_dsankel/1360c4342c51417fe5124e0a1427d9ea/external/bazel_tools/tools/build_defs/repo/git.bzl:186:37: in <toplevel>
ERROR: /home/dsankel/code/crubit/rs_bindings_from_cc/BUILD:62:10: error loading package '@llvm-project//llvm': cannot load '@bazel_skylib//rules:expand_template.bzl': no such file and referenced by '//rs_bindings_from_cc:rs_bindings_from_cc_impl'
ERROR: Analysis of target '//rs_bindings_from_cc:rs_bindings_from_cc_impl' failed; build aborted: Analysis failed
INFO: Elapsed time: 98.704s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (50 packages loaded, 154 targets configured)
    currently loading: @llvm-project//llvm ... (2 packages)

Ab

  • - _@114_https://github.com/jrandolf/grpc/pull/4Neither the name of Google LLC nor the names of its
    // contributors may be used to endorse or promote products derived from
    // this software without specific prior written permission.
    //
    // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.