From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from pigeon.gentoo.org ([208.92.234.80] helo=lists.gentoo.org) by finch.gentoo.org with esmtp (Exim 4.60) (envelope-from ) id 1SM4za-0001AJ-Vg for garchives@archives.gentoo.org; Sun, 22 Apr 2012 22:10:51 +0000 Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id C06C3E095F; Sun, 22 Apr 2012 22:10:35 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) by pigeon.gentoo.org (Postfix) with ESMTP id 8BEE9E092F for ; Sun, 22 Apr 2012 22:09:53 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by smtp.gentoo.org (Postfix) with ESMTP id 04A5B1B4024 for ; Sun, 22 Apr 2012 22:09:53 +0000 (UTC) X-Virus-Scanned: by amavisd-new using ClamAV at gentoo.org X-Spam-Flag: NO X-Spam-Score: -1.618 X-Spam-Level: X-Spam-Status: No, score=-1.618 tagged_above=-999 required=5.5 tests=[AWL=-0.870, BAYES_00=-1.9, RCVD_NUMERIC_HELO=1.164, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001, T_RP_MATCHES_RCVD=-0.01] autolearn=no Received: from smtp.gentoo.org ([127.0.0.1]) by localhost (smtp.gentoo.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id Cjh_Eb4nj4jP for ; Sun, 22 Apr 2012 22:09:45 +0000 (UTC) Received: from plane.gmane.org (plane.gmane.org [80.91.229.3]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id 8ADF81B4044 for ; Sun, 22 Apr 2012 22:09:44 +0000 (UTC) Received: from list by plane.gmane.org with local (Exim 4.69) (envelope-from ) id 1SM4yR-0005nU-5y for gentoo-dev@gentoo.org; Mon, 23 Apr 2012 00:09:39 +0200 Received: from 109.176.201.97 ([109.176.201.97]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Mon, 23 Apr 2012 00:09:39 +0200 Received: from slong by 109.176.201.97 with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Mon, 23 Apr 2012 00:09:39 +0200 X-Injected-Via-Gmane: http://gmane.org/ To: gentoo-dev@lists.gentoo.org From: Steven J Long Subject: [gentoo-dev] Re: .LIBPATTERNS harmful? Date: Sun, 22 Apr 2012 23:13:28 +0100 Organization: Friendly-Coders Message-ID: References: <201204221117.39554.vapier@gentoo.org> Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-dev@lists.gentoo.org Reply-to: gentoo-dev@lists.gentoo.org Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Complaints-To: usenet@dough.gmane.org X-Gmane-NNTP-Posting-Host: 109.176.201.97 X-Archives-Salt: f7107fc8-ed15-46f5-8f72-581b99769255 X-Archives-Hash: 345c6dbbe75166a8379aa262ebde7907 Mike Frysinger wrote: > On Sunday 22 April 2012 00:44:11 Steven J Long wrote: >> I can find nothing overriding it in portage, which makes sense, since in >> general one cannot know if the package in question uses gmake >> .LIBPATTERNS to link to locally-built libs. However I can't help thinking >> of it as harmful for a package manager, since a command like ld would be >> given a parameter of say, /usr/lib/libfoo.so, not -lfoo, meaning LDFLAGS >> would be irrelevant for its lookup. > > .LIBPATTERNS only matters if you specify the -lfoo in the dependency, and > then link in via an automatic make variable. > Indeed. But that is accepted, conventional usage of make: it's why $+ exists, for example. > e.g. this: > $ cat Makefile > all: test > test: -lm > $ echo 'main(){}' > test.c > $ make > cc test.c /usr/lib/libm.so -o test > > so the easy answer is: don't add -lfoo flags as dependencies to make > targets. if you want to have something link in a library, do: > $ cat Makefile > all: test > test: LDLIBS += -lm > $ make > cc test.c -lm -o test > The problem with target-specific variables, is that they also apply to any prerequisites (and their prerequisites..) that get updated. While LDLIBS is only used for linking final executables, it's not a general solution, since it's possible for an object file to depend on a built executable (eg if the binary is used to build an input file, or is a test program, or happens to be a prerequisite of another target in the chain): $ echo 'main(){}' > bar.c $ echo 'const char * foo(void) { return "fubar"; }' > foo.c $ printf '%s\n' '#include ' 'const char * foo(void);' 'int main(){' ' printf ("Result: %s\n", foo()); }' > test.c $ cat Makefile test: foo.o -lm foo.o: bar $ make cc bar.c -o bar cc -c -o foo.o foo.c cc test.c foo.o /usr/lib/libm.so -o test vs: $ cat Makefile test: foo.o test: LDLIBS += -lm foo.o: bar $ make cc bar.c -lm -o bar cc -c -o foo.o foo.c cc test.c foo.o -lm -o test Furthermore, if we apply the method iteratively (let's say bar uses pthreads) and add: bar: LDLIBS += -lpthread ..then we get: cc bar.c -lm -lpthread -o bar ..meaning that test's lib/s will be searched before those specified by bar. For the gallery, this is all a no-no in general: just because a lib should be linked into one object, does not mean it should link into another, unless it's explicitly been specified. Libs are searched in the order they appear on the command-line, so test's libs would interpose symbols in bar's link. All this makes the method package-specific, so it has to be done (and tested) on a case-by-case basis, and imo liable to random breakage due to interposition on prerequisite linkages. Worse, the value is unpredictable, since it varies according to which targets are being updated during the make run (and have thus added to LDLIBS for their prerequisites.) While this might not matter so much for distro-builds, it can mess up development builds. In testing how to fix this, I found running: make .LIBPATTERNS= ..gave me: make: *** No rule to make target `-lpthread', needed by `bar'. Stop. So I just added a .PHONY line: $ cat Makefile test: foo.o -lm foo.o: bar bar: -lpthread .PHONY: -lm -lpthread ..and oddly enough, I found this alone, was enough to disable the expansion of those libs: $ make cc bar.c -lpthread -o bar cc -c -o foo.o foo.c cc test.c foo.o -lm -o test I got the same result with: make .LIBPATTERNS:= so using both seems like the best general solution, since we're then guaranteed make will do no libname substitution, and we can use conventional -lfoo deps for external libs. >> I'd hope upstream would accept them, since it makes cross-development >> easier. (One definitely does not want make expanding -lname to a library >> in /lib or /usr/lib in that case, and it's better to error out if the >> library can't be found than link to host libs.) > > i've seen this usage in only one or two packages before. and when i > notified the respective upstream, they weren't really doing it on purpose, > so a simple patch (like i showed above) they were fine with merging. Thanks, that's exactly the kind of knowledge I don't have, and it's good to know that no-one really wants -lfoo looked up and substituted by make. Adding -lname specified prereqs to .PHONY is a simple fix, although if they're internal there won't be any lookup done by make. Personally I think that's a good thing, but I don't know how it'd affect things; for instance a package building okay now with a dep on an internal -lfoo which is expanded to a target which can be built. But as you state, you've not seen it done on purpose (and it would mess up cross-compiles) so I guess the fix there is for the internal lib to be specified as a filename, libfoo.so or .a (and perhaps -L .)? In any event, we seem to agree that we don't want .LIBPATTERNS expansions happening. Personally, I feel setting .LIBPATTERNS empty by default, either via make command-line (or setenv unless the package is marked as needing the expansions) would be 'correct', in that it would pick up potential problems straightaway, but I don't have the knowledge to assess the consequences. It wouldn't affect packages using the LDLIBS+= target-specific setting, at least, but would break packages using -lfoo prerequisites via automatic variables, which haven't been patched to use LDLIBS+= or add .PHONY deps. Might be something to consider for ebuild-developer mode, so new ebuilds don't come in with the potential for borked linkage. A case-by-case fix would be to add .LIBPATTERNS:= to the makefile when adding .PHONY deps. Anyhow, thanks for discussion and sharing your know-how; it means I now know how to handle it in our builds at least (.PHONY for external libs specified as -lfoo, .LIBPATTERNS set empty, filenames for internal libs and -L params for their directories.) Regards, Steve. -- #friendly-coders -- Where everybody knows your nickname ;-)