From nobody@FreeBSD.org  Sat Jan 19 10:55:34 2002
Return-Path: <nobody@FreeBSD.org>
Received: from freefall.freebsd.org (freefall.FreeBSD.org [216.136.204.21])
	by hub.freebsd.org (Postfix) with ESMTP id 0C5AF37B404
	for <freebsd-gnats-submit@FreeBSD.org>; Sat, 19 Jan 2002 10:55:30 -0800 (PST)
Received: (from nobody@localhost)
	by freefall.freebsd.org (8.11.6/8.11.6) id g0JItUh29643;
	Sat, 19 Jan 2002 10:55:30 -0800 (PST)
	(envelope-from nobody)
Message-Id: <200201191855.g0JItUh29643@freefall.freebsd.org>
Date: Sat, 19 Jan 2002 10:55:30 -0800 (PST)
From: Kevin Walsh <walsh@cs.duke.edu>
To: freebsd-gnats-submit@FreeBSD.org
Subject: bsd.kmod.mk fails due to bug in make with .PATH and exists() function
X-Send-Pr-Version: www-1.0
X-GNATS-Notify: bde

>Number:         34062
>Category:       bin
>Synopsis:       bsd.kmod.mk fails due to bug in make with .PATH and exists() function
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    ru
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Sat Jan 19 11:00:01 PST 2002
>Closed-Date:    Thu Oct 02 14:35:55 PDT 2003
>Last-Modified:  Thu Oct 02 14:35:55 PDT 2003
>Originator:     Kevin Walsh
>Release:        4.3-RELEASE
>Organization:
Duke University
>Environment:
FreeBSD rack27.cs.duke.edu 4.4-RELEASE FreeBSD 4.4-RELEASE #0: Fri Nov  2 16:21:54 EST 2001     priya@rack26.cs.duke.edu:/usr/research/playground/priya/fbsd/4.4-RELEASE/compile/DUMMY  i386

>Description:
When attempting to compile the 4.4-RELEASE kernel modules ('make modules' from the compile/DUMMY directory) make fails with the error:
===> libmchain
"/usr/share/mk/bsd.kmod.mk", line 10: "can't find kernel source tree"
*** Error code 1

A tour of libmchain/Makefile and the bsd.kmod.mk file reveals the following bug.
If a path (in this case /blah/blah/../../kern) is specified in the .PATH (as it is in libmchain/Makefile), then the .exists() function will fail when searching for the exact same path if a trailing '/' is left on (in this case, bsd.kmod.mk searches for /blah/blah/../../kern/).

This bug is too obvious and problematic to be real. I ran into it on my very first FreeBSD compile ever (and in multiple places, too!)
>How-To-Repeat:
Do 'make modules'.
Or run 'make' in a directory with the following Makefile:
# Makefile -- this will break the FreeBSD make (pmake???) and
# illustrates what is wrong with the kernel makefiles

_FOO=/usr/local/bin/

# this one works fine
.if exists(${_FOO})
.else
.error "first try -- _FOO does not exist: ${_FOO}"
.endif

# this also works fine
.PATH: /usr/local
.if exists(${_FOO})
.else
.error "second try -- _FOO does not exist: ${_FOO}"
.endif

# this one fails
.PATH: /usr/local/bin
.if exists(${_FOO})
.else
.error "third try -- _FOO does not exist: ${_FOO}"
.endif

all:

>Fix:
Workaround 1: fix all of the makefiles to never search for a directory with a trailing '/' character.
Workaround 2: fix all of the module makefiles to never include '../../kern'.

Fix: Change the function Dir_FindFile(name,path) in usr.bin/make/dir.c
so that (1) there is a check to see if 'name' is absolute BEFORE failing, and/or (2) trailing slashes are recognized and handled appropriately.
>Release-Note:
>Audit-Trail:

From: Bruce Evans <bde@zeta.org.au>
To: Kevin Walsh <walsh@cs.duke.edu>
Cc: <freebsd-gnats-submit@FreeBSD.ORG>
Subject: Re: misc/34062: bsd.kmod.mk fails due to bug in make with .PATH and
 exists() function
Date: Sun, 20 Jan 2002 14:59:13 +1100 (EST)

 On Sat, 19 Jan 2002, Kevin Walsh wrote:
 
 > >Release:        4.3-RELEASE
 
 > >Description:
 > When attempting to compile the 4.4-RELEASE kernel modules ('make modules' from the compile/DUMMY directory) make fails with the error:
 > ===> libmchain
 > "/usr/share/mk/bsd.kmod.mk", line 10: "can't find kernel source tree"
 > *** Error code 1
 >
 > A tour of libmchain/Makefile and the bsd.kmod.mk file reveals the following bug.
 > If a path (in this case /blah/blah/../../kern) is specified in the .PATH (as it is in libmchain/Makefile), then the .exists() function will fail when searching for the exact same path if a trailing '/' is left on (in this case, bsd.kmod.mk searches for /blah/blah/../../kern/).
 >
 > This bug is too obvious and problematic to be real. I ran into it on my very first FreeBSD compile ever (and in multiple places, too!)
 
 kmod.mk was "fixed" (your Workaround 1) in -current (rev.1.86) and
 4.4-release (rev.1.82.2.7) to work around the bug.  The trailing slash
 was used to force a check for a directory (or maybe to follow a symlink
 named kern), and removing it allows plain files to match.
 
 > >How-To-Repeat:
 > Do 'make modules'.
 > Or run 'make' in a directory with the following Makefile:
 > # Makefile -- this will break the FreeBSD make (pmake???) and
 > # illustrates what is wrong with the kernel makefiles
 > [... good example]
 
 This actually illustrates something that is wrong with FreeBSD make
 (pmake???).
 
 > >Fix:
 > Workaround 1: fix all of the makefiles to never search for a directory with a trailing '/' character.
 > Workaround 2: fix all of the module makefiles to never include '../../kern'.
 >
 > Fix: Change the function Dir_FindFile(name,path) in usr.bin/make/dir.c
 > so that (1) there is a check to see if 'name' is absolute BEFORE failing, and/or (2) trailing slashes are recognized and handled appropriately.
 
 I have used the following since a few hours after kmod.mk was "fixed".  The
 fixer and the MAINTAINER didn't respond to mails asking for a proper fix.
 I didn't commit this because I don't really understand the code and suspect
 that there is more to the bug than this.
 
 %%
 Index: dir.c
 ===================================================================
 RCS file: /home/ncvs/src/usr.bin/make/dir.c,v
 retrieving revision 1.17
 diff -u -2 -r1.17 dir.c
 --- dir.c	25 Apr 2001 14:45:36 -0000	1.17
 +++ dir.c	25 Apr 2001 22:47:54 -0000
 @@ -764,4 +764,9 @@
  	    hits += 1;
  	    return (file);
 +#if 0
 +	/*
 +	 * XXX: this breaks finding names like "foo/", "foo//" and even
 +	 * "foo/." when the directory "foo" has been found previously.
 +	 */
  	} else if (hasSlash) {
  	    /*
 @@ -780,4 +785,5 @@
  		return ((char *) NULL);
  	    }
 +#endif
  	}
      }
 %%%
 
 Bruce
 
Responsible-Changed-From-To: freebsd-bugs->ru 
Responsible-Changed-By: kris 
Responsible-Changed-When: Sat Jul 12 17:33:28 PDT 2003 
Responsible-Changed-Why:  
ru has done a lot of work on the mk files recently 

http://www.freebsd.org/cgi/query-pr.cgi?pr=34062 
State-Changed-From-To: open->closed 
State-Changed-By: ru 
State-Changed-When: Thu Oct 2 14:31:06 PDT 2003 
State-Changed-Why:  
Fixed in 5.1-CURRENT, src/usr.bin/make/dir.c,v 1.32. 
Reverted the change to kmod.mk to work around this bug. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=34062 
>Unformatted:
