Skip to content

Commit

Permalink
Rollup merge of #84029 - drahnr:master, r=petrochenkov
Browse files Browse the repository at this point in the history
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
  • Loading branch information
JohnTitor authored Jul 2, 2021
2 parents 851c82e + 67e6a81 commit 45470a3
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 3 deletions.
4 changes: 4 additions & 0 deletions compiler/rustc_expand/src/proc_macro_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,10 @@ impl server::FreeFunctions for Rustc<'_> {
fn track_env_var(&mut self, var: &str, value: Option<&str>) {
self.sess.env_depinfo.borrow_mut().insert((Symbol::intern(var), value.map(Symbol::intern)));
}

fn track_path(&mut self, path: &str) {
self.sess.file_depinfo.borrow_mut().insert(Symbol::intern(path));
}
}

impl server::TokenStream for Rustc<'_> {
Expand Down
16 changes: 13 additions & 3 deletions compiler/rustc_interface/src/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,18 @@ use rustc_passes::{self, hir_stats, layout_test};
use rustc_plugin_impl as plugin;
use rustc_query_impl::Queries as TcxQueries;
use rustc_resolve::{Resolver, ResolverArenas};
use rustc_serialize::json;
use rustc_session::config::{CrateType, Input, OutputFilenames, OutputType, PpMode, PpSourceMode};
use rustc_session::lint;
use rustc_session::output::{filename_for_input, filename_for_metadata};
use rustc_session::search_paths::PathKind;
use rustc_session::Session;
use rustc_span::symbol::{Ident, Symbol};
use rustc_span::FileName;
use rustc_trait_selection::traits;
use rustc_typeck as typeck;
use tracing::{info, warn};

use rustc_serialize::json;
use tempfile::Builder as TempFileBuilder;
use tracing::{info, warn};

use std::any::Any;
use std::cell::RefCell;
Expand Down Expand Up @@ -594,6 +594,16 @@ fn write_out_deps(
.map(|fmap| escape_dep_filename(&fmap.name.prefer_local().to_string()))
.collect();

// Account for explicitly marked-to-track files
// (e.g. accessed in proc macros).
let file_depinfo = sess.parse_sess.file_depinfo.borrow();
let extra_tracked_files = file_depinfo.iter().map(|path_sym| {
let path = PathBuf::from(&*path_sym.as_str());
let file = FileName::from(path);
escape_dep_filename(&file.prefer_local().to_string())
});
files.extend(extra_tracked_files);

if let Some(ref backend) = sess.opts.debugging_opts.codegen_backend {
files.push(backend.to_string());
}
Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_session/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ pub struct ParseSess {
pub reached_eof: Lock<bool>,
/// Environment variables accessed during the build and their values when they exist.
pub env_depinfo: Lock<FxHashSet<(Symbol, Option<Symbol>)>>,
/// File paths accessed during the build.
pub file_depinfo: Lock<FxHashSet<Symbol>>,
/// All the type ascriptions expressions that have had a suggestion for likely path typo.
pub type_ascription_path_suggestions: Lock<FxHashSet<Span>>,
/// Whether cfg(version) should treat the current release as incomplete
Expand Down Expand Up @@ -165,6 +167,7 @@ impl ParseSess {
symbol_gallery: SymbolGallery::default(),
reached_eof: Lock::new(false),
env_depinfo: Default::default(),
file_depinfo: Default::default(),
type_ascription_path_suggestions: Default::default(),
assume_incomplete_release: false,
proc_macro_quoted_spans: Default::default(),
Expand Down
1 change: 1 addition & 0 deletions library/proc_macro/src/bridge/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ macro_rules! with_api {
FreeFunctions {
fn drop($self: $S::FreeFunctions);
fn track_env_var(var: &str, value: Option<&str>);
fn track_path(path: &str);
},
TokenStream {
fn drop($self: $S::TokenStream);
Expand Down
14 changes: 14 additions & 0 deletions library/proc_macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1234,3 +1234,17 @@ pub mod tracked_env {
value
}
}

/// Tracked access to additional files.
#[unstable(feature = "track_path", issue = "73921")]
pub mod tracked_path {

/// Track a file explicitly.
///
/// Commonly used for tracking asset preprocessing.
#[unstable(feature = "track_path", issue = "73921")]
pub fn path<P: AsRef<str>>(path: P) {
let path: &str = path.as_ref();
crate::bridge::client::FreeFunctions::track_path(path);
}
}
13 changes: 13 additions & 0 deletions src/test/run-make/track-path-dep-info/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
-include ../../run-make-fulldeps/tools.mk

# FIXME(eddyb) provide `HOST_RUSTC` and `TARGET_RUSTC`
# instead of hardcoding them everywhere they're needed.
ifeq ($(IS_MUSL_HOST),1)
ADDITIONAL_ARGS := $(RUSTFLAGS)
endif

all:
# Proc macro
$(BARE_RUSTC) $(ADDITIONAL_ARGS) --out-dir $(TMPDIR) macro_def.rs
EXISTING_PROC_MACRO_ENV=1 $(RUSTC) --emit dep-info macro_use.rs
$(CGREP) "emojis.txt:" < $(TMPDIR)/macro_use.d
1 change: 1 addition & 0 deletions src/test/run-make/track-path-dep-info/emojis.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
👾👾👾👾👾👾
11 changes: 11 additions & 0 deletions src/test/run-make/track-path-dep-info/macro_def.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#![feature(track_path)]
#![crate_type = "proc-macro"]

extern crate proc_macro;
use proc_macro::*;

#[proc_macro]
pub fn access_tracked_paths(_: TokenStream) -> TokenStream {
tracked_path::path("emojis.txt");
TokenStream::new()
}
6 changes: 6 additions & 0 deletions src/test/run-make/track-path-dep-info/macro_use.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#[macro_use]
extern crate macro_def;

access_tracked_paths!();

fn main() {}

0 comments on commit 45470a3

Please sign in to comment.