"Portable" conditionalization of Makefiles

Trond Endrestøl Trond.Endrestol at fagskolen.gjovik.no
Wed Mar 21 07:29:47 UTC 2018


On Tue, 20 Mar 2018 17:45-0700, Ronald F. Guilmette wrote:

> 
> 
> I have a pile of (mostly) C code that I wrote myself over the past
> several years.  I developed it on FreeBSD but have always thought
> that it would be Nice if it compiled and ran also on Linux.
> 
> I just spent about a day editing the various files to make it all
> compile and link OK on Linux.  So that part is all done now.  But
> here's the one remaining problem:
> 
> There's a chance that I may distribute this stuff someday.  When and
> if I do, I'd like to be able to tell people to "just run make" in the
> top-level directory, regardless of whether they are on Linux or *BSD.
> (I -could- just tell people to use gmake if they are on *BSD, but I'd
> rather not.)
> 
> 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.
> 
> Of course, these two make programs have implemented different, and
> apparently incompatable syntaxes for conditionalization... with the
> FreeBSD make using directives like ifeq/else/endif and GNU make using
> directives like .if/.else/.endif
> 
> I *really* don't want to use any mechanism which builds the Makefile
> on the fly, like configure/autoconf and friends.  (I have always felt
> that those things were abominations... basically elephant guns, often
> used ridiculously to kill mere gnats.)
> 
> It occurs to me that I can't have been the first person to have come
> up against this problem.  But I don't know that solution.  Is there
> already a well known and widely used motif or convention for how to
> code up conditional parts of Makefiles in such a way that they will
> do what's needed, conditionally, when run through -either- GNU Make
> or FreeBSD make?

While not a perfect solution, how about using BSDmakefile for use with 
make(1) on the BSDs, and GNUmakefile for use with GNU Make on 
GNU/Linux? Maybe you can couple this with a Makefile.commom to handle 
the common bits.

Should a user run gmake on BSD under these conditions, they might get 
a surprise.

-- 
Trond.


More information about the freebsd-questions mailing list