Search code examples
linuxmakefileincludedev-null

Why include /dev/null in makefile?


I ran across a line in a makefile. The line was:

-include $(wildcard makefile.mk) /dev/null

I don't understand the purpose of the /dev/null in the include line. Is it intentional or is it a mistake? Perhaps they intended to send the output there? I'm still rather new to makefiles, can someone help me make sense of this?


Solution

  • I think it is used to ensure that, even if the wildcard expands to nothing at all, the include will result in at least one file - /dev/null. Maybe I can explain it like this... imagine you had a hypothetical compilation like this

    INCLUDES=$(wildcard /usr/inc*)
    CC -I INCLUDES prog.c -o prog
    

    If your (admittedly strange) environment didn't have anything in /usr/include, your compilation command would expand to

    CC -I prog.c -o prog
    

    which is likely to give an error. So the /dev/null addition would maybe avoid that.

    I am not saying it is necessary, I am just saying I think that is maybe its purpose. It is similar to when programmers use

    grep pattern file /dev/null
    

    rather than the more modern

    grep -H pattern file
    

    because it is a file that will definitely exist, and as such grep will receive two files as arguments and will therefore tell you the name of the file it found the pattern in - exactly as if you had specified -H.

    Another example is the format command on Solaris. It gives you a lovely list of all the attached disks, but then sits there hanging till you choose one - which us not ideal if you want to parse the list from a script. Here again, you can use /dev/null and it will list the disks and exit if you do

    format < /dev/null