Merge pull request #433 from alercah/rerun

Support conditional execution by Cargo.
This commit is contained in:
Markus Westerlind 2019-01-19 22:08:48 +01:00 committed by GitHub
commit becdc9bf19
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 0 deletions

View File

@ -96,6 +96,22 @@ impl Configuration {
self
}
/// If true, print `rerun-if-changed` directives to standard output, so that
/// Cargo will only rerun the build script if any of the processed
/// `.lalrpop` files are changed. This option is independent of
/// [`force_build`], although it would be usual to set [`force_build`] and
/// [`emit_rerun_directives`] at the same time.
///
/// While many build scripts will want to set this to `true`, the default is
/// false, because emitting any rerun directives to Cargo will cause the
/// script to only be rerun when Cargo thinks it is needed. This could lead
/// to hard-to-find bugs if other parts of the build script do not emit
/// directives correctly, or need to be rerun unconditionally.
pub fn emit_rerun_directives(&mut self, val: bool) -> &mut Configuration {
self.session.emit_rerun_directives = val;
self
}
/// If true, emit comments into the generated code. This makes the
/// generated code significantly larger. Default is false.
pub fn emit_comments(&mut self, val: bool) -> &mut Configuration {

View File

@ -98,6 +98,7 @@ fn process_file_into(
rs_file: &Path,
report_file: &Path,
) -> io::Result<()> {
session.emit_rerun_directive(lalrpop_file);
if session.force_build || try!(needs_rebuild(&lalrpop_file, &rs_file)) {
log!(
session,

View File

@ -37,6 +37,9 @@ pub struct Session {
pub out_dir: Option<path::PathBuf>,
/// Emit `rerun-if-changed` directives for Cargo
pub emit_rerun_directives: bool,
/// Emit comments in generated code explaining the states and so
/// forth.
pub emit_comments: bool,
@ -94,6 +97,7 @@ impl Session {
in_dir: None,
out_dir: None,
force_build: false,
emit_rerun_directives: false,
emit_comments: false,
emit_whitespace: true,
emit_report: false,
@ -120,6 +124,7 @@ impl Session {
in_dir: None,
out_dir: None,
force_build: false,
emit_rerun_directives: false,
emit_comments: false,
emit_whitespace: true,
emit_report: false,
@ -150,6 +155,16 @@ impl Session {
{
self.log.log(level, message)
}
pub fn emit_rerun_directive(&self, path: &path::Path) {
if self.emit_rerun_directives {
if let Some(display) = path.to_str() {
println!("cargo:rerun-if-changed={}", display)
} else {
println!("cargo:warning=LALRPOP is unable to inform Cargo that {} is a dependency because its filename cannot be represented in UTF-8. This is probably because it contains an unpaired surrogate character on Windows. As a result, your build script will not be rerun when it changes.", path.to_string_lossy());
}
}
}
}
impl Default for Session {