cc-rs icon indicating copy to clipboard operation
cc-rs copied to clipboard

README.md: Add note that rerun-if-changed is not emitted.

Open EvanTheB opened this issue 3 years ago • 4 comments

Issue #230 is the issue for this, this patch just documents the current behaviour.

EvanTheB avatar Jul 27 '22 23:07 EvanTheB

Customarily the decision whether or not a package gets rebuilt depends on whether or not any of the files in corresponding cargo package --list [--allow-dirty] output are modified. Customarily you would make your .c sources to be a part of the package, so that emitting explicit cargo:rerun-if-changed directives wouldn't be normally required. I suppose question is if it's some unusual case, then it might be more appropriate to discuss the specifics of when it would be actually required to emit the directives.

dot-asm avatar Jul 31 '22 23:07 dot-asm

There might be some misunderstanding on my[?] side. You'd use cargo:rerun-if-changed in case you find that rebuilding upon any change in cargo package --list excessive. But either way, the suggested "Note that cc-rs does not automatically tell cargo to rebuild when the c files are changed" still doesn't adequately reflect the current state of affairs. I mean in most common situation cargo will follow modification timestamps without explicit if-changed directives.

dot-asm avatar Aug 01 '22 18:08 dot-asm

I need to recheck as I dont understand this very well. I had a c file in my src/ folder, which does show up in cargo package --list. However modifying that file did not cause a rebuild. Searching the issue brought me to the mentioned issue, and adding rerun-if-changed made the behaviour what I expect (rebuild when I modify the c file). There might be something else at play.

Because pkg_config is emitting rerun-if commands, the default behaviour you mention may not be as expected. Its not clear what is the 'right' way to solve this. But it would have saved me half a day of debugging to have this mentioned in the cc docs somehow...

extern crate cc;

fn main() {
    // pkg_config is needed only to pick up the include path for log.c to use.
    // libflashrom-sys tells cargo how to link to libflashrom.
    let flashrom = pkg_config::Config::new()
        .cargo_metadata(false)
        .probe("flashrom")
        .unwrap();
    let mut log_c = cc::Build::new();
    log_c.file("src/log.c");
    for p in flashrom.include_paths {
        log_c.include(p);
    }
    log_c.compile("log.o");
    println!("cargo:rerun-if-changed=src/log.c");
}

EvanTheB avatar Aug 03 '22 00:08 EvanTheB

Because pkg_config is emitting rerun-if commands, the default behaviour you mention may not be as expected.

Bingo! If you have one rerun-if, you need all of them. And that's what the note should say instead of "cargo never tells." I suppose it would be appropriate to mention the possibility of build-dependency crates adding rerun-if without you realizing it in advance...

dot-asm avatar Aug 03 '22 09:08 dot-asm