D1X
D1X

Reputation: 5474

Makefile line `test -f `

I have a Makefile with the following line:

 $(CXX) $(CXXFLAGS) $(INCL) -c -o $@ `test -f '$<' || echo '$(SRCDIR)/'`$<

Can you explain me what this line does?

Note: The Makefile is taken from here: https://projects.coin-or.org/BuildTools/wiki/user-examples

That page specifies this:

The generic compilation rules (e.g., .c.o) might look somewhat complicated. But with this, the Makefile can also be used by people who work with the MS compilers (e.g., cl) under Cygwin, since those compilers don't understand the UNIX-type directory paths. In such a case, the CYGPATH_W will have been set to "cygpath -w" by configure, otherwise it is just "echo".

I am not going to work with MS compilers.

Upvotes: 0

Views: 2205

Answers (1)

Graeme Perrow
Graeme Perrow

Reputation: 57268

The test -f thing is a Unix-ism that will return true if the specified file exists and false if it does not. "$<" is the source file for a make rule. So test -f '$<' || echo '$(SRCDIR)/' will check for the existence of the source file and if it does not exist, will echo $(SRCDIR)/ followed by the name of the file. That will be the input to the actual compiler.

It's basically saying "if file X exists in the local directory, then compile it. Otherwise (that's what the || does), compile the file called X located in the directory $(SRCDIR)". This way you can put all your source in a subdirectory and then override certain files by putting new copies of them in the same directory as the Makefile.

Upvotes: 4

Related Questions