Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Collect C/C++ include directories from deps #2

Merged
merged 2 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "conan2"
version = "0.1.0"
version = "0.1.1"
description = "Pulls the C/C++ library linking flags from Conan dependencies"
authors = ["Sergey Kvachonok <[email protected]>"]
edition = "2021"
Expand Down
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,20 @@ fn main() {
.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:

```rust
use conan2::ConanInstall;

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

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

metadata.emit();
```
63 changes: 55 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,27 @@
//! .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:
//!
//! ```no_run
//! use conan2::ConanInstall;
//!
//! let metadata = ConanInstall::new().run().parse();
//!
//! for path in metadata.include_paths() {
//! // Add "-I{path}" to CXXFLAGS or something.
//! }
//!
//! metadata.emit();
//! ```

#![deny(missing_docs)]

use std::collections::BTreeSet;
use std::io::{BufRead, Cursor, Write};
use std::path::{Path, PathBuf};
use std::process::{Command, Output};
Expand Down Expand Up @@ -97,7 +115,12 @@ pub struct ConanInstall {
pub struct ConanOutput(Output);

/// Build script instructions for Cargo
pub struct CargoInstructions(Vec<u8>);
pub struct CargoInstructions {
/// Raw build script output
out: Vec<u8>,
/// C include paths collected from the packages
includes: BTreeSet<PathBuf>,
}

/// Conan dependency graph as a JSON-based tree structure
struct ConanDependencyGraph(Value);
Expand Down Expand Up @@ -304,32 +327,47 @@ impl CargoInstructions {
/// Gets the Cargo instruction lines as bytes.
#[must_use]
pub fn as_bytes(&self) -> &[u8] {
&self.0
&self.out
}

/// Gets the C/C++ include directory paths for all dependencies.
#[must_use]
pub fn include_paths(&self) -> Vec<PathBuf> {
self.includes.iter().cloned().collect()
}

/// Creates a new empty Cargo instructions list.
fn new() -> CargoInstructions {
CargoInstructions(Vec::with_capacity(1024))
CargoInstructions {
out: Vec::with_capacity(1024),
includes: BTreeSet::new(),
}
}

/// Adds `cargo:warning={message}` instruction.
fn warning(&mut self, message: &str) {
writeln!(self.0, "cargo:warning={message}").unwrap();
writeln!(self.out, "cargo:warning={message}").unwrap();
}

/// Adds `cargo:rerun-if-env-changed={val}` instruction.
fn rerun_if_env_changed(&mut self, val: &str) {
writeln!(self.0, "cargo:rerun-if-env-changed={val}").unwrap();
writeln!(self.out, "cargo:rerun-if-env-changed={val}").unwrap();
}

/// Adds `cargo:rustc-link-lib={lib}` instruction.
fn rustc_link_lib(&mut self, lib: &str) {
writeln!(self.0, "cargo:rustc-link-lib={lib}").unwrap();
writeln!(self.out, "cargo:rustc-link-lib={lib}").unwrap();
}

/// Adds `cargo:rustc-link-search={path}` instruction.
fn rustc_link_search(&mut self, path: &str) {
writeln!(self.0, "cargo:rustc-link-search={path}").unwrap();
writeln!(self.out, "cargo:rustc-link-search={path}").unwrap();
}

/// Adds `cargo:include={path}` instruction.
fn include(&mut self, path: &str) {
writeln!(self.out, "cargo:include={path}").unwrap();
self.includes.insert(path.into());
}
}

Expand Down Expand Up @@ -411,7 +449,16 @@ impl ConanDependencyGraph {
}
};

// 4. Recursively visit dependency component requirements.
// 4. Emit "cargo:include=DIR" metadata for Rust dependencies.
if let Some(Value::Array(includedirs)) = component.get("includedirs") {
for include in includedirs {
if let Value::String(include) = include {
cargo.include(include);
}
}
};

// 5. Recursively visit dependency component requirements.
if let Some(Value::Array(requires)) = component.get("requires") {
for requirement in requires {
if let Value::String(req_comp_name) = requirement {
Expand Down
7 changes: 6 additions & 1 deletion tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ fn run_conan_install() {
assert!(output.is_success());
assert_eq!(output.status_code(), 0);

output.parse().emit();
let cargo = output.parse();
let includes = cargo.include_paths();

assert!(includes.len() > 3);

cargo.emit();
}

#[test]
Expand Down
Loading