Hello,
For a project a need to use cargo as a library to compile some runtime dll that will be injected to a process. I checked cargo task and cargo make, but didn't return what I needed so I went with cargo.
Also I know API is not stable and can change.
The issue is I have been doing some tests and everything was fine until I included dashmap. Dashmap build complains about not having the -Z unstable-options
flag must also be passed to enable the flag check-cfg
.
I checked with the CLI and it works so I'm missing something for sure.
Here is the code, to prevent name clashing in questions I qualified all imports.
fn generate_compile_options<S: AsRef<str>>(context: &cargo::GlobalContext,
profile: S,
feature_list: &[S],
packages: cargo::ops::Packages) -> cargo::CargoResult<cargo::ops::CompileOptions> {
let mut build_config = cargo::core::compiler::BuildConfig::new(
context,
None,
false,
&[], // TODO: Nice to have for future?
cargo::core::compiler::CompileMode::Build,
)?;
build_config.requested_profile = cargo::util::interning::InternedString::new(profile.as_ref());
let mut features = BTreeSet::default();
for feature in feature_list {
features.insert(cargo::core::summary::FeatureValue::new(
cargo::util::interning::InternedString::new(feature.as_ref()))
);
}
let cli_features = cargo::core::resolver::CliFeatures {
features: std::rc::Rc::new(features),
all_features: false,
uses_default_features: false,
};
Ok(cargo::ops::CompileOptions {
build_config,
cli_features,
spec: packages,
filter: cargo::ops::CompileFilter::Default {
required_features_filterable: false,
},
target_rustdoc_args: None,
target_rustc_args: None,
target_rustc_crate_types: None,
rustdoc_document_private_items: false,
honor_rust_version: None,
})
}
fn main() -> Result<(), Error> {
let module_path = std::path::PathBuf::from("./plugins/Cargo.toml");
let context = cargo::util::context::GlobalContext::default().unwrap();
let workspace = cargo::core::Workspace::new(&std::fs::canonicalize(module_path).unwrap(), &context).unwrap();
let compile_options = generate_compile_options(
&context,
"release",
&vec![],
cargo::ops::Packages::All,
).unwrap();
let result = cargo::ops::compile(&workspace, &compile_options).unwrap();
}
Any ideas?