All Projects → AndrewGaspar → Corrosion

AndrewGaspar / Corrosion

Licence: mit
Marrying Rust and CMake - Easy Rust and C/C++ Integration!

Programming Languages

c
50402 projects - #5 most used programming language
rust
11053 projects
cpp
1120 projects
cplusplus
227 projects

Projects that are alternatives of or similar to Corrosion

Reggae
Build system in D, Python, Ruby, Javascript or Lua
Stars: ✭ 141 (+33.02%)
Mutual labels:  cmake, build-tool, build-system
Arduino Cmake Ng
CMake-Based framework for Arduino platforms
Stars: ✭ 123 (+16.04%)
Mutual labels:  cmake, build-tool, build-system
Cbt
CBT - fun, fast, intuitive, compositional, statically checked builds written in Scala
Stars: ✭ 489 (+361.32%)
Mutual labels:  build-tool, build-system
Erlang.mk
A build tool for Erlang that just works.
Stars: ✭ 538 (+407.55%)
Mutual labels:  build-tool, build-system
Flubucore
A cross platform build and deployment automation system for building projects and executing deployment scripts using C# code.
Stars: ✭ 695 (+555.66%)
Mutual labels:  build-tool, build-system
Python Cmake Buildsystem
A cmake buildsystem for compiling Python
Stars: ✭ 317 (+199.06%)
Mutual labels:  cmake, build-system
Realize
Realize is the #1 Golang Task Runner which enhance your workflow by automating the most common tasks and using the best performing Golang live reloading.
Stars: ✭ 4,162 (+3826.42%)
Mutual labels:  build-tool, build-system
Crossbuild
🌍 multiarch cross compiling environments
Stars: ✭ 632 (+496.23%)
Mutual labels:  build-tool, build-system
BuildAMation
Build system and project generator for C/C++ desktop software development. Uses C# for build scripts. It is no longer under active development.
Stars: ✭ 13 (-87.74%)
Mutual labels:  build-tool, build-system
Cargo Make
Rust task runner and build tool.
Stars: ✭ 895 (+744.34%)
Mutual labels:  build-tool, cargo
Swift Llbuild
A low-level build system, used by Xcode and the Swift Package Manager
Stars: ✭ 836 (+688.68%)
Mutual labels:  build-tool, build-system
Hopp
Crazy rapid build system.
Stars: ✭ 24 (-77.36%)
Mutual labels:  build-tool, build-system
Cargo Lipo
Cargo subcommand to automatically create universal libraries for iOS.
Stars: ✭ 290 (+173.58%)
Mutual labels:  build-tool, cargo
makesure
Simple task/command runner with declarative goals and dependencies
Stars: ✭ 230 (+116.98%)
Mutual labels:  build-tool, build-system
Bake
A build system that lets you clone, build and run C/C++ projects with a single command
Stars: ✭ 434 (+309.43%)
Mutual labels:  build-tool, build-system
obs-docu
Official Open Build Service Documentation. Content gets reviewed and edited. Generated books are available at http://www.openbuildservice.org
Stars: ✭ 26 (-75.47%)
Mutual labels:  build-tool, build-system
Cmake Js
CMake.js - a Node.js native addon build tool
Stars: ✭ 610 (+475.47%)
Mutual labels:  cmake, build-system
Pi Builder
Extensible tool to build Arch Linux ARM for Raspberry Pi on x86_64 host using Docker
Stars: ✭ 31 (-70.75%)
Mutual labels:  build-tool, build-system
jagen
A software engineer's workspace manager and build systems wrapper
Stars: ✭ 32 (-69.81%)
Mutual labels:  build-tool, build-system
bob-build
Meta-build system using Blueprint and ninja
Stars: ✭ 24 (-77.36%)
Mutual labels:  build-tool, build-system

Corrosion

Build Status

Corrosion, formerly known as cmake-cargo, is a tool for integrating Rust into an existing CMake project. Corrosion is capable of importing executables, static libraries, and dynamic libraries from a crate.

Features

  • Automatic Import of Executable, Static, and Shared Libraries from Rust Crate
  • Easy Installation of Rust Executables
  • Trivially Link Rust Executables to C++ Libraries in Tree
  • Multi-Config Generator Support
  • Simple Cross-Compilation

Coming Soon

  • Automatic Generation of Rust Bindings (via bindgen) and C/C++ Bindings (via cbindgen)
  • Easy Install of Libraries

Sample Usage

cmake_minimum_required(VERSION 3.12)
project(MyCoolProject LANGUAGES CXX)

find_package(Corrosion REQUIRED)

corrosion_import_crate(MANIFEST_PATH rust-lib/Cargo.toml)

add_executable(cpp-exe main.cpp)
target_link_libraries(cpp-exe PUBLIC rust-lib)

Documentation

Table of Contents

Installation

There are two fundamental installation methods that are supported by Corrosion - installation as a CMake package or using it as a subdirectory in an existing CMake project. Corrosion strongly recommends installing the package, either via a package manager or manually using cmake's installation facilities.

Installation will pre-build all of Corrosion's native tooling, meaning that configuring any project which uses Corrosion is much faster. Using Corrosion as a subdirectory will result in the native tooling for Corrosion to be re-built every time you configure a new build directory, which could be a non-trivial cost for some projects. It also may result in issues with large, complex projects with many git submodules that each individually may use Corrosion. This can unnecessarily exacerbate diamond dependency problems that wouldn't otherwise occur using an externally installed Corrosion.

Package Manager

Coming soon...

CMake Install

After using a package manager, the next recommended way to use Corrosion is to install it as a package using CMake. This means you won't need to rebuild Corrosion's tooling every time you generate a new build directory. Installation also solves the diamond dependency problem that often comes with git submodules or other primitive dependency solutions.

First, download and install Corrosion:

git clone https://github.com/AndrewGaspar/corrosion.git
# Optionally, specify -DCMAKE_INSTALL_PREFIX=<target-install-path>. You can install Corrosion anyway
cmake -Scorrosion -Bbuild -DCMAKE_BUILD_TYPE=Release
cmake --build build --config Release
# This next step may require sudo or admin privileges if you're installing to a system location,
# which is the default.
cmake --install build --config Release

You'll want to ensure that the install directory is available in your PATH or CMAKE_PREFIX_PATH environment variable. This is likely to already be the case by default on a Unix system, but on Windows it will install to C:\Program Files (x86)\Corrosion by default, which will not be in your PATH or CMAKE_PREFIX_PATH by default.

Once Corrosion is installed and you've ensured the package is avilable in your PATH, you can use it from your own project like any other package from your CMakeLists.txt:

find_package(Corrosion REQUIRED)

FetchContent

If installation is difficult or not feasible in your environment, you can use the FetchContent module to include Corrosion. This will download Corrosion and use it as if it were a subdirectory at configure time.

In your CMakeLists.txt:

include(FetchContent)

FetchContent_Declare(
    Corrosion
    GIT_REPOSITORY https://github.com/AndrewGaspar/corrosion.git
    GIT_TAG origin/master # Optionally specify a version tag or branch here
)

FetchContent_MakeAvailable(Corrosion)

Subdirectory

Corrosion can also be used directly as a subdirectory. This solution may work well for small projects, but it's discouraged for large projects with many dependencies, especially those which may themselves use Corrosion. Either copy the Corrosion library into your source tree, being sure to preserve the LICENSE file, or add this repository as a git submodule:

git submodule add https://github.com/AndrewGaspar/corrosion.git

From there, using Corrosion is easy. In your CMakeLists.txt:

add_subdirectory(path/to/corrosion)

Usage

Options

All of the following variables are evaluated automatically in most cases. In typical cases you shouldn't need to alter any of these.

  • Rust_TOOLCHAIN:STRING - Specify a named rustup toolchain to use. Changes to this variable resets all other options. Default: If the first-found rustc is a rustup proxy, then the default rustup toolchain (see rustup show) is used. Otherwise, the variable is unset by default.
  • Rust_ROOT:STRING - CMake provided. Path to a Rust toolchain to use. This is an alternative if you want to select a specific Rust toolchain, but it's not managed by rustup. Default: Nothing

Advanced

  • Rust_COMPILER:STRING - Path to an actual rustc. If set to a rustup proxy, it will be replaced by a path to an actual rustc. Default: The rustc in the first-found toolchain, either from rustup, or from a toolchain available in the user's PATH.
  • Rust_CARGO:STRING - Path to an actual cargo. Default: the cargo installed next to ${Rust_COMPILER}.
  • Rust_CARGO_TARGET:STRING - The default target triple to build for. Alter for cross-compiling. Default: On Visual Studio Generator, the matching triple for CMAKE_VS_PLATFORM_NAME. Otherwise, the default target triple reported by ${Rust_COMPILER} --version --verbose.

Developer/Maintainer Options

These options are not used in the course of normal Corrosion usage, but are used to configure how Corrosion is built and installed. Only applies to Corrosion builds and subdirectory uses.

  • CORROSION_DEV_MODE:BOOL - Indicates that Corrosion is being actively developed. Default: OFF if Corrosion is a subdirectory, ON if it is the top-level project
  • CORROSION_BUILD_TESTS:BOOL - Build the Corrosion tests. Default: Off if Corrosion is a subdirectory, ON if it is the top-level project
  • CORROSION_GENERATOR_EXECUTABLE:STRING - Specify a path to the corrosion-generator executable. This is to support scenarios where it's easier to build corrosion-generator outside of the normal bootstrap path, such as in the case of package managers that make it very easy to import Rust crates for fully reproducible, offline builds.
  • CORROSION_INSTALL_EXECUTABLE:BOOL - Controls whetehr corrosion-generator is installed with the package. Default: ON with CORROSION_GENERATOR_EXECUTABLE unset, otherwise OFF

Importing C-Style Libraries Written in Rust

Corrosion makes it completely trivial to import a crate into an existing CMake project. Consider a project called rust2cpp with the following file structure:

rust2cpp/
    rust/
        src/
            lib.rs
        Cargo.lock
        Cargo.toml
    CMakeLists.txt
    main.cpp

This project defines a simple Rust lib crate, like so, in rust2cpp/rust/Cargo.toml:

[package]
name = "rust-lib"
version = "0.1.0"
authors = ["Andrew Gaspar <[email protected]>"]
license = "MIT"
edition = "2018"

[dependencies]

[lib]
crate-type=["staticlib"]

In addition to "staticlib", you can also use "cdylib". In fact, you can define both with a single crate and switch between which is used using the standard BUILD_SHARED_LIBS variable.

This crate defines a simple crate called rust-lib. Importing this crate into your CMakeLists.txt is trivial:

# Note: you must have already included Corrosion for `corrosion_import_crate` to be available. See # the `Installation` section above.

corrosion_import_crate(MANIFEST_PATH rust/Cargo.toml)

Now that you've imported the crate into CMake, all of the executables, static libraries, and dynamic libraries defined in the Rust can be directly referenced. So, merely define your C++ executable as normal in CMake and add your crate's library using target_link_libraries:

add_executable(cpp-exe main.cpp)
target_link_libraries(cpp-exe PUBLIC rust-lib)

That's it! You're now linking your Rust library to your C++ library.

Generate Bindings to Rust Library Automatically

Currently, you must manually declare bindings in your C or C++ program to the exported routines and types in your Rust project. You can see boths sides of this in the Rust code and in the C++ code.

Integration with cbindgen is planned for the future.

Importing Libraries Written in C and C++ Into Rust

TODO

Cross Compiling

Corrosion attempts to support cross-compiling as generally as possible, though not all configurations are tested. Cross-compiling is explicitly supported in the following scenarios.

In all cases, you will need to install the standard library for the Rust target triple. When using Rustup, you can use it to install the target standard library:

rustc target add <target-rust-triple>

If the target triple is automatically derived, Corrosion will print the target during configuration. For example:

-- Rust Target: aarch64-linux-android

Windows-to-Windows

Corrosion supports cross-compiling between arbitrary Windows architectures using the Visual Studio Generator. For example, to cross-compile for ARM64 from any platform, simply set the -A architecture flag:

cmake -S. -Bbuild-arm64 -A ARM64
cmake --build build-arm64

Linux-to-Linux

In order to cross-compile on Linux, you will need to install a cross-compiler. For example, on Ubuntu, to cross compile for 64-bit Little-Endian PowerPC Little-Endian, install g++-powerpc64le-linux-gnu from apt-get:

sudo apt install g++-powerpc64le-linux-gnu

Currently, Corrosion does not automatically determine the target triple while cross-compiling on Linux, so you'll need to specify a matching Rust_CARGO_TARGET.

cmake -S. -Bbuild-ppc64le -DRust_CARGO_TARGET=powerpc64le-unknown-linux-gnu -DCMAKE_CXX_COMPILER=powerpc64le-linux-gnu-g++
cmake --build build-ppc64le

Android

Cross-compiling for Android is supported on all platforms with the Makefile and Ninja generators, and the Rust target triple will automatically be selected. The CMake cross-compiling instructions for Android apply here. For example, to build for ARM64:

cmake -S. -Bbuild-android-arm64 -GNinja -DCMAKE_SYSTEM_NAME=Android -DCMAKE_ANDROID_NDK=/path/to/android-ndk-rxxd -DCMAKE_ANDROID_ARCH_ABI=arm64-v8a

Important note: The Android SDK ships with CMake 3.10 at newest, which Android Studio will prefer over any CMake you've installed locally. CMake 3.10 is insufficient for using Corrosion, which requires a minimum of CMake 3.12. If you're using Android Studio to build your project, follow the instructions in the Android Studio documentation for using a specific version of CMake.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].