Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 21 Mar 2018 21:04:50 -0000 (UTC)
From:      Christian Weisgerber <naddy@mips.inka.de>
To:        freebsd-questions@freebsd.org
Subject:   Re: "Portable" conditionalization of Makefiles
Message-ID:  <slrnpb5i7i.h9a.naddy@lorvorc.mips.inka.de>
References:  <99925.1521593119@segfault.tristatelogic.com>

Next in thread | Previous in thread | Raw E-Mail | Index | Archive | Help
On 2018-03-21, "Ronald F. Guilmette" <rfg@tristatelogic.com> wrote:

> So anyway, the problem is that on Linux, I have to link in some different
> libraries to make the stuff work.  Specifically, I have to add -lresolv
> to the link command.  But that's a no-go for FreeBSD, whose linker
> will rightly complain about the missing library if it sees that extra
> option.
>
> Obviously, I need to conditionalize some small bits of my Makefile, but
> I need to do that in a way that will cause -neither- GNU Make nor FreeBSD
> make to barf all over everything.

There is no standard syntax for Makefile conditionals and none that
is in common between GNU make and FreeBSD's make.

You can set variables based on the value of other variables in a way
that is similar to conditionals:

------------------->
OPSYS=FreeBSD
LIBRESOLV_Linux=-lresolv
LIBRESOLV_FreeBSD=
LIBRESOLV=$(LIBRESOLV_$(OPSYS))

prog:
	@echo cc -o prog prog.o $(LIBRESOLV)
<-------------------

But you still need people to set at least one variable (here OPSYS)
either by editing the Makefile or overriding the value from the
command line:

$ make OPSYS=Linux


It is possible to recursively call make from within a Makefile, so
you could use a trick like this to call make again with a distinguishing
variable set to, say, the output of uname:

------------------->
LIBRESOLV_Linux=-lresolv
LIBRESOLV_FreeBSD=
LIBRESOLV=$(LIBRESOLV_$(OPSYS))

all:
	$(MAKE) OPSYS=`uname` prog

prog:
	@echo cc -o prog prog.o $(LIBRESOLV)
<-------------------

-- 
Christian "naddy" Weisgerber                          naddy@mips.inka.de



Want to link to this message? Use this URL: <http://docs.FreeBSD.org/cgi/mid.cgi?slrnpb5i7i.h9a.naddy>