|
| 1 | +// Copyright 2017 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution and at |
| 3 | +// http://rust-lang.org/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
| 10 | + |
| 11 | +use std::collections::HashMap; |
| 12 | + |
| 13 | +use cargo::core::{Package as CargoPackage, PackageId, Resolve, Target, Workspace}; |
| 14 | +use cargo::ops::{self, OutputMetadataOptions, Packages}; |
| 15 | +use cargo::util::CargoResult; |
| 16 | + |
| 17 | +/// Metadata version copied from `cargo_output_metadata.rs`. TODO: Remove |
| 18 | +/// when Cargo API will expose more information regarding output metadata. |
| 19 | +const VERSION: u32 = 1; |
| 20 | + |
| 21 | +/// Holds the information how exactly the build will be performed for a given |
| 22 | +/// workspace with given, specified features. |
| 23 | +/// **TODO:** Use it to schedule an analysis build instead of relying on Cargo |
| 24 | +/// invocations. |
| 25 | +#[derive(Debug)] |
| 26 | +pub struct Plan { |
| 27 | + // TODO: Implement/add inter-(package) target dep queue |
| 28 | + // with args/envs per-target/package |
| 29 | + pub metadata: Metadata |
| 30 | +} |
| 31 | + |
| 32 | +pub fn create_plan(ws: &Workspace) -> CargoResult<Plan> { |
| 33 | + // TODO: Fill appropriately |
| 34 | + let options = OutputMetadataOptions { |
| 35 | + features: vec![], |
| 36 | + no_default_features: false, |
| 37 | + all_features: false, |
| 38 | + no_deps: false, |
| 39 | + version: VERSION, |
| 40 | + }; |
| 41 | + |
| 42 | + let metadata = metadata_full(ws, &options)?; |
| 43 | + Ok(Plan { metadata: metadata.into() }) |
| 44 | +} |
| 45 | + |
| 46 | +/// Targets and features for a given package in the dep graph. |
| 47 | +#[derive(Debug)] |
| 48 | +pub struct Package { |
| 49 | + pub id: PackageId, |
| 50 | + pub targets: Vec<Target>, |
| 51 | + pub features: HashMap<String, Vec<String>>, |
| 52 | +} |
| 53 | + |
| 54 | +impl From<CargoPackage> for Package { |
| 55 | + fn from(pkg: CargoPackage) -> Package { |
| 56 | + Package { |
| 57 | + id: pkg.package_id().clone(), |
| 58 | + targets: pkg.targets().iter().map(|x| x.clone()).collect(), |
| 59 | + features: pkg.summary().features().clone() |
| 60 | + } |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +/// Provides inter-package dependency graph and available packages' info in the |
| 65 | +/// workspace scope, along with workspace members and root package ids. |
| 66 | +#[derive(Debug)] |
| 67 | +pub struct Metadata { |
| 68 | + packages: HashMap<PackageId, Package>, |
| 69 | + resolve: Resolve, |
| 70 | + members: Vec<PackageId>, |
| 71 | + root: Option<PackageId> |
| 72 | +} |
| 73 | + |
| 74 | +impl From<ExportInfo> for Metadata { |
| 75 | + fn from(info: ExportInfo) -> Metadata { |
| 76 | + // ExportInfo with deps information will always have `Some` resolve |
| 77 | + let MetadataResolve { resolve, root } = info.resolve.unwrap(); |
| 78 | + |
| 79 | + let packages: HashMap<PackageId, Package> = info.packages |
| 80 | + .iter() |
| 81 | + .map(|x| x.to_owned().into()) |
| 82 | + .map(|pkg: Package| (pkg.id.clone(), pkg)) // TODO: Can I borrow key from member of value? |
| 83 | + .collect(); |
| 84 | + |
| 85 | + Metadata { |
| 86 | + packages, |
| 87 | + resolve, |
| 88 | + members: info.workspace_members, |
| 89 | + root, |
| 90 | + } |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +// TODO: Copied for now from Cargo, since it's not fully exposed in the API. |
| 95 | +// Remove when appropriate members are exposed. |
| 96 | +#[derive(Debug)] |
| 97 | +pub struct ExportInfo { |
| 98 | + pub packages: Vec<CargoPackage>, |
| 99 | + pub workspace_members: Vec<PackageId>, |
| 100 | + pub resolve: Option<MetadataResolve>, |
| 101 | + pub target_directory: String, |
| 102 | + pub version: u32, |
| 103 | +} |
| 104 | + |
| 105 | +#[derive(Debug)] |
| 106 | +pub struct MetadataResolve { |
| 107 | + pub resolve: Resolve, |
| 108 | + pub root: Option<PackageId>, |
| 109 | +} |
| 110 | + |
| 111 | +fn metadata_full(ws: &Workspace, |
| 112 | + opt: &OutputMetadataOptions) -> CargoResult<ExportInfo> { |
| 113 | + let specs = Packages::All.into_package_id_specs(ws)?; |
| 114 | + let deps = ops::resolve_ws_precisely(ws, |
| 115 | + None, |
| 116 | + &opt.features, |
| 117 | + opt.all_features, |
| 118 | + opt.no_default_features, |
| 119 | + &specs)?; |
| 120 | + let (packages, resolve) = deps; |
| 121 | + |
| 122 | + let packages = packages.package_ids() |
| 123 | + .map(|i| packages.get(i).map(|p| p.clone())) |
| 124 | + .collect::<CargoResult<Vec<_>>>()?; |
| 125 | + |
| 126 | + Ok(ExportInfo { |
| 127 | + packages: packages, |
| 128 | + workspace_members: ws.members().map(|pkg| pkg.package_id().clone()).collect(), |
| 129 | + resolve: Some(MetadataResolve{ |
| 130 | + resolve: resolve, |
| 131 | + root: ws.current_opt().map(|pkg| pkg.package_id().clone()), |
| 132 | + }), |
| 133 | + target_directory: ws.target_dir().display().to_string(), |
| 134 | + version: VERSION, |
| 135 | + }) |
| 136 | +} |
0 commit comments