in Makefiles, how to test for gcc's --with-ld option?

762 views Asked by At

We're trying to develop an as-portable-as-possible Makefile...

Neither uname nor uname -v is definitive in one suite of cases...

which ld is also unhelpful, as both linkers are present...

I imagine we could just parse output of gcc -v for '--with-ld=/usr/bin/ld', then test the features/version of that linker. But is the best way to do this?

What are 'Best Practices' here? Can gcc be queried more cleanly - from within a Makefile - for its linker options?

2

There are 2 answers

6
Eugeniu Rosca On

The first thing that comes to my mind (tested with GNU make and clearmake):

# redirect gcc -v to stdout && count the number of occurrences
GCC_WITH_LD     := $(shell gcc -v 2>&1 | grep -c "\--with-ld")

ifeq ($(GCC_WITH_LD),0)
$(shell echo --with-ld NOT FOUND 1>&2) # print to stderr
# exit using error directive?
else
$(shell echo --with-ld FOUND 1>&2) # print to stderr
endif

mytarget:
        @echo myjobs
0
andy_js On

The option you're looking for is -print-prog-name:

andy@Andrews-Mac-Pro ~ % gcc -print-prog-name=ld
/Library/Developer/CommandLineTools/usr/bin/ld

Then in the Makefile you can set LD with:

LD := $(shell gcc -print-prog-name=ld)

This also works on Solaris.