Skip to content

Commit e668300

Browse files
committed
feat(log): unit-registered event
1 parent d259e47 commit e668300

File tree

4 files changed

+96
-0
lines changed

4 files changed

+96
-0
lines changed

src/cargo/core/compiler/build_context/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ pub struct BuildContext<'a, 'gctx> {
7878
/// The dependency graph of units to compile.
7979
pub unit_graph: UnitGraph,
8080

81+
/// A map from unit to index.
82+
pub unit_to_index: HashMap<Unit, u64>,
83+
8184
/// Reverse-dependencies of documented units, used by the `rustdoc --scrape-examples` flag.
8285
pub scrape_units: Vec<Unit>,
8386

@@ -96,6 +99,7 @@ impl<'a, 'gctx> BuildContext<'a, 'gctx> {
9699
target_data: RustcTargetData<'gctx>,
97100
roots: Vec<Unit>,
98101
unit_graph: UnitGraph,
102+
unit_to_index: HashMap<Unit, u64>,
99103
scrape_units: Vec<Unit>,
100104
) -> CargoResult<BuildContext<'a, 'gctx>> {
101105
let all_kinds = unit_graph
@@ -116,6 +120,7 @@ impl<'a, 'gctx> BuildContext<'a, 'gctx> {
116120
target_data,
117121
roots,
118122
unit_graph,
123+
unit_to_index,
119124
scrape_units,
120125
all_kinds,
121126
})

src/cargo/ops/cargo_compile/mod.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ use annotate_snippets::{Group, Level, Origin};
6464
pub use compile_filter::{CompileFilter, FilterRule, LibRule};
6565

6666
pub(super) mod unit_generator;
67+
use itertools::Itertools as _;
6768
use unit_generator::UnitGenerator;
6869

6970
mod packages;
@@ -520,7 +521,23 @@ pub fn create_bcx<'a, 'gctx>(
520521
build_config.compile_time_deps_only,
521522
);
522523

524+
// unit_graph must be immutable after this point.
525+
let unit_graph = unit_graph;
526+
let units: Vec<_> = unit_graph.keys().sorted().collect();
527+
let unit_to_index: HashMap<_, _> = units
528+
.iter()
529+
.enumerate()
530+
.map(|(i, &unit)| (unit.clone(), i as u64))
531+
.collect();
523532
if let Some(logger) = logger {
533+
for (i, unit) in units.into_iter().enumerate() {
534+
logger.log(LogMessage::UnitRegistered {
535+
package_id: unit.pkg.package_id().to_spec(),
536+
target: (&unit.target).into(),
537+
mode: unit.mode,
538+
index: i as u64,
539+
});
540+
}
524541
let elapsed = ws.gctx().creation_time().elapsed().as_secs_f64();
525542
logger.log(LogMessage::UnitGraphFinished { elapsed });
526543
}
@@ -641,6 +658,7 @@ where `<compatible-ver>` is the latest version supporting rustc {rustc_version}"
641658
target_data,
642659
root_units,
643660
unit_graph,
661+
unit_to_index,
644662
scrape_units,
645663
)?;
646664

src/cargo/util/log_message.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,19 @@ pub enum LogMessage {
5959
/// Seconds elapsed from build start.
6060
elapsed: f64,
6161
},
62+
/// Emitted when a compilation unit is registered in the unit graph,
63+
/// right before [`LogMessage::UnitGraphFinished`] that Cargo finalizes
64+
/// the unit graph.
65+
UnitRegistered {
66+
/// Package ID specification.
67+
package_id: PackageIdSpec,
68+
/// Cargo target (lib, bin, example, etc.).
69+
target: Target,
70+
/// The compilation action this unit is for (check, build, test, etc.).
71+
mode: CompileMode,
72+
/// Unit index for compact reference in subsequent events.
73+
index: u64,
74+
},
6275
/// Emitted when a compilation unit starts.
6376
UnitStarted {
6477
/// Package ID specification.

tests/testsuite/build_analysis.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,66 @@ fn log_msg_unit_graph() {
535535
"run_id": "[..]T[..]Z-[..]",
536536
"timestamp": "[..]T[..]Z"
537537
},
538+
{
539+
"index": 0,
540+
"mode": "check",
541+
"package_id": "path+[ROOTURL]/foo/bar#0.0.0",
542+
"reason": "unit-registered",
543+
"run_id": "[..]T[..]Z-[..]",
544+
"target": {
545+
"kind": "lib",
546+
"name": "bar"
547+
},
548+
"timestamp": "[..]T[..]Z"
549+
},
550+
{
551+
"index": 1,
552+
"mode": "doc",
553+
"package_id": "path+[ROOTURL]/foo/bar#0.0.0",
554+
"reason": "unit-registered",
555+
"run_id": "[..]T[..]Z-[..]",
556+
"target": {
557+
"kind": "lib",
558+
"name": "bar"
559+
},
560+
"timestamp": "[..]T[..]Z"
561+
},
562+
{
563+
"index": 2,
564+
"mode": "doc",
565+
"package_id": "path+[ROOTURL]/foo#0.0.0",
566+
"reason": "unit-registered",
567+
"run_id": "[..]T[..]Z-[..]",
568+
"target": {
569+
"kind": "lib",
570+
"name": "foo"
571+
},
572+
"timestamp": "[..]T[..]Z"
573+
},
574+
{
575+
"index": 3,
576+
"mode": "build",
577+
"package_id": "path+[ROOTURL]/foo#0.0.0",
578+
"reason": "unit-registered",
579+
"run_id": "[..]T[..]Z-[..]",
580+
"target": {
581+
"kind": "build-script",
582+
"name": "build-script-build"
583+
},
584+
"timestamp": "[..]T[..]Z"
585+
},
586+
{
587+
"index": 4,
588+
"mode": "run-custom-build",
589+
"package_id": "path+[ROOTURL]/foo#0.0.0",
590+
"reason": "unit-registered",
591+
"run_id": "[..]T[..]Z-[..]",
592+
"target": {
593+
"kind": "build-script",
594+
"name": "build-script-build"
595+
},
596+
"timestamp": "[..]T[..]Z"
597+
},
538598
{
539599
"elapsed": "{...}",
540600
"reason": "unit-graph-finished",

0 commit comments

Comments
 (0)