6 releases (stable)

Uses old Rust 2015

1.1.2 Jul 21, 2019
1.1.1 Jun 14, 2018
1.0.0 Feb 22, 2018
0.1.1 Dec 18, 2017

#12 in #project

Download history 15/week @ 2020-12-24 22/week @ 2020-12-31 29/week @ 2021-01-07 14/week @ 2021-01-14 18/week @ 2021-01-21 7/week @ 2021-01-28 21/week @ 2021-02-04 37/week @ 2021-02-11 18/week @ 2021-02-18 2/week @ 2021-02-25 3/week @ 2021-03-04 13/week @ 2021-03-11 23/week @ 2021-03-18 33/week @ 2021-03-25 21/week @ 2021-04-01 26/week @ 2021-04-08

102 downloads per month

MIT license

21KB
514 lines

Rust libaries as Xcode projects

Generates Xcode project files from Cargo.toml allowing use of Rust libraries in Mac Cocoa applications without leaving Xcode.

Requirements

  • Rust (tested with 1.26)
  • Xcode (tested with 9.2 and 10.0b)

Once the Xcode project file is generated, cargo-xcode is no longer needed.

Installation

cargo install cargo-xcode

Usage

TL;DR: Run cargo xcode and use the generated project files as subprojects in other Xcode projects.

This tool will generate Rust-specific project files for all binaries and C-compatible libraries in Cargo workspace. The generated Xcode projects are not suitable for standalone use, and are supposed to be used only as subprojects of regular Mac Xcode projects (Xcode can nest projects).

  1. If you don't have an existing Cocoa Mac app project yet, create one in Xcode (normal ObjC or Swift app). This will be called your "parent project" in later steps.

  2. If your Rust project is a library, edit Cargo.toml and add crate-type = ["lib", "staticlib"] in the [lib] section. Only libraries of type "staticlib" or "cdylib" are used (leave "lib" type for compatibility with Rust libraries and tests).

  3. In the same directory as Cargo.toml (or root of a Cargo workspace) run:

    cargo xcode
    

    This will generate <rust-project-name>.xcodeproj. Don't open it yet!

  4. Open your parent project (from step 1) in Xcode and add <rust-project-name>.xcodeproj to the workspace (drag the file into the parent project's sidebar). You should see the Rust project embedded in your parent project. If the Rust project appears empty in the sidebar, close all Xcode projects and re-open only the parent project.

  5. In your parent project's target's Build Phases, in Link Binary With Libraries phase, you can now add Rust libraries from the workspace.

  6. If you're linking with static Rust libraries, also link your executables/libraries with libresolv.dylib (without it Xcode won't find _res_9_init required by Rust's stdlib).

Features

It's better than just launching cargo build from a script:

  • Configures Cargo to follow Xcode's Debug and Release configurations.
  • Configures Cargo to use Xcode's standard build folder.
  • Makes Xcode aware of dependencies and rebuild Rust code when needed.
  • Xcode's "Clean build folder" also cleans Cargo's target dir.
  • Supports Cargo workspaces and multiple targets per crate.

Limitations

Only 64-bit Intel Mac target is supported at the moment (i.e. no cross compilation).

Rust binaries are exported as command-line tools. This tool intentionally does not make app bundles. If you want to build a Mac GUI app, create one as ObjC or Swift project in Xcode and run Rust code from a Rust static library.

Dependencies

~1.1–1.9MB
~42K SLoC