Skip to content

Commit 45470a3

Browse files
authored
Rollup merge of #84029 - drahnr:master, r=petrochenkov
add `track_path::path` fn for usage in `proc_macro`s Adds a way to declare a dependency on external files without including them, to either re-trigger the build of a file as well as covering the use case of including dependencies within the `rustc` invocation, such that tools like `sccache`/`cachepot` are able to handle references to external files which are not included. Ref #73921
2 parents 851c82e + 67e6a81 commit 45470a3

File tree

9 files changed

+66
-3
lines changed

9 files changed

+66
-3
lines changed

compiler/rustc_expand/src/proc_macro_server.rs

+4
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,10 @@ impl server::FreeFunctions for Rustc<'_> {
411411
fn track_env_var(&mut self, var: &str, value: Option<&str>) {
412412
self.sess.env_depinfo.borrow_mut().insert((Symbol::intern(var), value.map(Symbol::intern)));
413413
}
414+
415+
fn track_path(&mut self, path: &str) {
416+
self.sess.file_depinfo.borrow_mut().insert(Symbol::intern(path));
417+
}
414418
}
415419

416420
impl server::TokenStream for Rustc<'_> {

compiler/rustc_interface/src/passes.rs

+13-3
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,18 @@ use rustc_passes::{self, hir_stats, layout_test};
2828
use rustc_plugin_impl as plugin;
2929
use rustc_query_impl::Queries as TcxQueries;
3030
use rustc_resolve::{Resolver, ResolverArenas};
31+
use rustc_serialize::json;
3132
use rustc_session::config::{CrateType, Input, OutputFilenames, OutputType, PpMode, PpSourceMode};
3233
use rustc_session::lint;
3334
use rustc_session::output::{filename_for_input, filename_for_metadata};
3435
use rustc_session::search_paths::PathKind;
3536
use rustc_session::Session;
3637
use rustc_span::symbol::{Ident, Symbol};
38+
use rustc_span::FileName;
3739
use rustc_trait_selection::traits;
3840
use rustc_typeck as typeck;
39-
use tracing::{info, warn};
40-
41-
use rustc_serialize::json;
4241
use tempfile::Builder as TempFileBuilder;
42+
use tracing::{info, warn};
4343

4444
use std::any::Any;
4545
use std::cell::RefCell;
@@ -594,6 +594,16 @@ fn write_out_deps(
594594
.map(|fmap| escape_dep_filename(&fmap.name.prefer_local().to_string()))
595595
.collect();
596596

597+
// Account for explicitly marked-to-track files
598+
// (e.g. accessed in proc macros).
599+
let file_depinfo = sess.parse_sess.file_depinfo.borrow();
600+
let extra_tracked_files = file_depinfo.iter().map(|path_sym| {
601+
let path = PathBuf::from(&*path_sym.as_str());
602+
let file = FileName::from(path);
603+
escape_dep_filename(&file.prefer_local().to_string())
604+
});
605+
files.extend(extra_tracked_files);
606+
597607
if let Some(ref backend) = sess.opts.debugging_opts.codegen_backend {
598608
files.push(backend.to_string());
599609
}

compiler/rustc_session/src/parse.rs

+3
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ pub struct ParseSess {
133133
pub reached_eof: Lock<bool>,
134134
/// Environment variables accessed during the build and their values when they exist.
135135
pub env_depinfo: Lock<FxHashSet<(Symbol, Option<Symbol>)>>,
136+
/// File paths accessed during the build.
137+
pub file_depinfo: Lock<FxHashSet<Symbol>>,
136138
/// All the type ascriptions expressions that have had a suggestion for likely path typo.
137139
pub type_ascription_path_suggestions: Lock<FxHashSet<Span>>,
138140
/// Whether cfg(version) should treat the current release as incomplete
@@ -165,6 +167,7 @@ impl ParseSess {
165167
symbol_gallery: SymbolGallery::default(),
166168
reached_eof: Lock::new(false),
167169
env_depinfo: Default::default(),
170+
file_depinfo: Default::default(),
168171
type_ascription_path_suggestions: Default::default(),
169172
assume_incomplete_release: false,
170173
proc_macro_quoted_spans: Default::default(),

library/proc_macro/src/bridge/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ macro_rules! with_api {
5555
FreeFunctions {
5656
fn drop($self: $S::FreeFunctions);
5757
fn track_env_var(var: &str, value: Option<&str>);
58+
fn track_path(path: &str);
5859
},
5960
TokenStream {
6061
fn drop($self: $S::TokenStream);

library/proc_macro/src/lib.rs

+14
Original file line numberDiff line numberDiff line change
@@ -1234,3 +1234,17 @@ pub mod tracked_env {
12341234
value
12351235
}
12361236
}
1237+
1238+
/// Tracked access to additional files.
1239+
#[unstable(feature = "track_path", issue = "73921")]
1240+
pub mod tracked_path {
1241+
1242+
/// Track a file explicitly.
1243+
///
1244+
/// Commonly used for tracking asset preprocessing.
1245+
#[unstable(feature = "track_path", issue = "73921")]
1246+
pub fn path<P: AsRef<str>>(path: P) {
1247+
let path: &str = path.as_ref();
1248+
crate::bridge::client::FreeFunctions::track_path(path);
1249+
}
1250+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
-include ../../run-make-fulldeps/tools.mk
2+
3+
# FIXME(eddyb) provide `HOST_RUSTC` and `TARGET_RUSTC`
4+
# instead of hardcoding them everywhere they're needed.
5+
ifeq ($(IS_MUSL_HOST),1)
6+
ADDITIONAL_ARGS := $(RUSTFLAGS)
7+
endif
8+
9+
all:
10+
# Proc macro
11+
$(BARE_RUSTC) $(ADDITIONAL_ARGS) --out-dir $(TMPDIR) macro_def.rs
12+
EXISTING_PROC_MACRO_ENV=1 $(RUSTC) --emit dep-info macro_use.rs
13+
$(CGREP) "emojis.txt:" < $(TMPDIR)/macro_use.d
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
👾👾👾👾👾👾
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#![feature(track_path)]
2+
#![crate_type = "proc-macro"]
3+
4+
extern crate proc_macro;
5+
use proc_macro::*;
6+
7+
#[proc_macro]
8+
pub fn access_tracked_paths(_: TokenStream) -> TokenStream {
9+
tracked_path::path("emojis.txt");
10+
TokenStream::new()
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#[macro_use]
2+
extern crate macro_def;
3+
4+
access_tracked_paths!();
5+
6+
fn main() {}

0 commit comments

Comments
 (0)