Search code examples
c++linuxparsingdependenciesscons

Scons doesn't calculate correct 'ParseDepends' or 'Ignore' as I specified


I wish to tell scons, when I changed one header file, don't recompile my source file(This is just my test!) I've got hello.c file:

#ifdef FOO
#include"~/headers/n.h"
#endif
#include<stdio.h>
int main(){
  printf("hello\n");
  return 2;
}

And my SConstruct file is:

obj=Object('hello.c')
SideEffect('hello.d',obj)
ParseDepends('hello.d')
Program('hello',obj)

You see, I don't define any "FOO", so the hello.c file doesn't use my .h file at all. I also expect that ParseDepends will be able to read C-preprocessor command as to ignore my #include "n.h" because there's no "FOO" definied.

But when running scons, and then change n.h file, running scons again will trigger rebuild of hello.c

Then I tried to use 'Ignore' statement as below:

hello_obj=Object('hello.c')
hello=Program(hello_obj)
Ignore(hello_obj,'n.h')

Well I got same test result: change inside n.h is not ignored by scons! Why?


Solution

  • Use Ignore(). Quoting its documentation,

    Sometimes it makes sense to not rebuild a program, even if a dependency file changes. In this case, you would tell SCons specifically to ignore a dependency

    In your case, here is a complete SConstruct (I changed your C program to to say #include "headers/n.h"):

    obj=Object('hello.c')
    Ignore(obj, 'headers/n.h')
    Program('hello',obj)
    

    Here is a shell session that proves it works:

    $ scons -Q -c
    Removed hello.o
    Removed hello
    $ scons -Q 
    gcc -o hello.o -c hello.c
    gcc -o hello hello.o
    $ echo hello >> headers/n.h 
    $ scons -Q 
    scons: `.' is up to date.