1

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?

DrLou
  • 649
  • 5
  • 21
  • make doesn't have any special built-in gcc-feature-testing magic if that's the question. – Etan Reisner Jun 10 '15 at 16:48
  • It certainly makes sense to do this check globally per Makefile, not per each target. – Eugeniu Rosca Jun 10 '15 at 16:58
  • `which` is not universally available anyway. There are Linux distributions out there that don't install it by default. Is there some particular reason you're doing this from a Makefile? I think you'll make things much easier on yourself by using some more powerful framework. Possibly autoconf+automake, possibly some other similar tool, possibly some custom configuration script. –  Jun 10 '15 at 18:54
  • 1
    Actually, what are you going to be doing with the linker anyway? There are exceptions, but most linker invocations by far should be going through the compiler. Maybe you have some good scenario where you really need to call the linker directly, but do check if you can just use `$(CC)` for both compilation and linking. –  Jun 10 '15 at 19:05
  • Which version of Solaris are you targeting? GNU Make is available on later versions as `gmake`, and in my experience it's easier to just use GNU make on Solaris - and I've been coding on Solaris for over 20 years. – Andrew Henle Jun 10 '15 at 22:20

2 Answers2

0

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
Eugeniu Rosca
  • 5,177
  • 16
  • 45
  • This does depend on GNU-make extensions that are not available in all make versions... – Chris Dodd Jun 10 '15 at 18:46
  • I tested it with GNU make and clearmake (clearcase Make) and both behaved fine. – Eugeniu Rosca Jun 10 '15 at 18:49
  • @chatraed How about the BSDs? I know their make does support capturing command output, but I'm pretty sure they use a different syntax. –  Jun 10 '15 at 18:58
  • @hvd: I think the initial request of the "as-portable-as-possible Makefile" refers to the linux system and not to the make flavors. But I could be wrong. We should ask DrLou. – Eugeniu Rosca Jun 10 '15 at 19:06
  • @chatraed Considering the OP tagged the question "solaris", I don't think the OP's project will run on Linux exclusively. :) –  Jun 10 '15 at 19:07
  • @hvd: That is loosely coupled with my idea. linux->unix. – Eugeniu Rosca Jun 10 '15 at 19:14
0

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.

andy_js
  • 71
  • 1
  • 2