From owner-freebsd-questions@freebsd.org Wed Mar 21 21:05:09 2018 Return-Path: Delivered-To: freebsd-questions@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 2CA9FF66FF0 for ; Wed, 21 Mar 2018 21:05:09 +0000 (UTC) (envelope-from news@mips.inka.de) Received: from mail.inka.de (quechua.inka.de [IPv6:2a04:c9c7:0:1073:217:a4ff:fe3b:e77c]) (using TLSv1.2 with cipher DHE-RSA-AES128-SHA (128/128 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id A874687FE5 for ; Wed, 21 Mar 2018 21:05:08 +0000 (UTC) (envelope-from news@mips.inka.de) Received: from localhost by mail.inka.de with local-rmail id 1eykuw-0003Nc-Ee; Wed, 21 Mar 2018 22:05:06 +0100 Received: from lorvorc.mips.inka.de (localhost [127.0.0.1]) by lorvorc.mips.inka.de (8.15.2/8.15.2) with ESMTP id w2LL4od8018185 for ; Wed, 21 Mar 2018 22:04:50 +0100 (CET) (envelope-from news@lorvorc.mips.inka.de) Received: (from news@localhost) by lorvorc.mips.inka.de (8.15.2/8.15.2/Submit) id w2LL4on7018184 for freebsd-questions@freebsd.org; Wed, 21 Mar 2018 22:04:50 +0100 (CET) (envelope-from news) To: freebsd-questions@freebsd.org From: Christian Weisgerber Newsgroups: list.freebsd.questions Subject: Re: "Portable" conditionalization of Makefiles Date: Wed, 21 Mar 2018 21:04:50 -0000 (UTC) Message-ID: References: <99925.1521593119@segfault.tristatelogic.com> User-Agent: slrn/1.0.3 (FreeBSD) X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.25 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 21 Mar 2018 21:05:09 -0000 On 2018-03-21, "Ronald F. Guilmette" 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