#build-script #build #package-manager #conan #ffi #linker

build conan2

Pulls the C/C++ library linking flags from Conan dependencies

5 releases

0.1.4 Nov 28, 2024
0.1.3 Nov 27, 2024
0.1.2 Aug 28, 2024
0.1.1 Feb 15, 2024
0.1.0 Oct 13, 2023

#120 in Build Utils

Download history 151/week @ 2024-08-28 24/week @ 2024-09-04 21/week @ 2024-09-11 42/week @ 2024-09-18 26/week @ 2024-09-25 34/week @ 2024-10-02 119/week @ 2024-10-09 125/week @ 2024-10-16 188/week @ 2024-10-23 112/week @ 2024-10-30 26/week @ 2024-11-06 70/week @ 2024-11-13 71/week @ 2024-11-20 313/week @ 2024-11-27 102/week @ 2024-12-04

559 downloads per month
Used in dotlottie-rs

MIT license

26KB
342 lines

conan2-rs

Introduction

conan2-rs is a Cargo build script wrapper of the Conan C/C++ package manager (version 2.0 only).

It automatically pulls the C/C++ library linking flags from Conan dependencies and passes them to rustc.

Adding C/C++ dependencies using Conan

The simplest way to add C/C++ dependencies to a Rust project using Conan is to add a plain conanfile.txt file as follows:

[requires]
libxml2/2.11.4
openssl/3.1.3
zlib/1.3

Example usage

Add conan2 to the Cargo.toml build dependencies section:

[build-dependencies]
conan2 = "0.1"

Add the following lines to the project build.rs script to invoke conan install and pass the Conan dependency information to Cargo automatically:

use conan2::ConanInstall;

fn main() {
    ConanInstall::new().run().parse().emit();
}

The most commonly used build_type Conan setting will be defined automatically depending on the current Cargo build profile: Debug or Release.

The Conan executable is assumed to be named conan unless the CONAN environment variable is set to override.

An example Rust crate using conan2-rs to link Conan dependencies can also be found in the project repository.

Advanced usage

Automatic Conan profile inference from Cargo target

Using custom Conan profiles with names derived from the Cargo target information and a reduced output verbosity level:

use conan2::{ConanInstall, ConanVerbosity};

fn main() {
    let target_os = std::env::var("CARGO_CFG_TARGET_OS").unwrap();
    let target_arch = std::env::var("CARGO_CFG_TARGET_ARCH").unwrap();
    let conan_profile = format!("{}-{}", target_os, target_arch);

    ConanInstall::new()
        .profile(&conan_profile)
        .build("missing")
        .verbosity(ConanVerbosity::Error) // Silence Conan warnings
        .run()
        .parse()
        .emit();
}

Automatic Conan profile creation

Creating a custom default Conan profile on the fly with zero configuration:

use conan2::{ConanInstall, ConanVerbosity};

ConanInstall::new()
    .profile("cargo")
    .detect_profile() // Run `conan profile detect --exist-ok` for the above
    .run()
    .parse()
    .emit();

Using separate host and build profiles

Using different values for --profile:host and --profile:build arguments of conan install command:

use conan2::{ConanInstall, ConanVerbosity};

ConanInstall::new()
    .host_profile("cargo-host")
    .build_profile("cargo-build")
    .run()
    .parse()
    .emit();

Getting C/C++ include paths from Conan dependencies

To use the list of include paths, do the following after parsing the conan install output:

use conan2::ConanInstall;

let metadata = ConanInstall::new().run().parse();

for path in metadata.include_paths() {
    // Add "-I{path}" to CXXFLAGS or something.
}

metadata.emit();

Dependencies

~0.5–1MB
~20K SLoC