From jhein@timing.com  Fri Mar 10 11:14:03 2000
Return-Path: <jhein@timing.com>
Received: from timingpdc.timing.com (timingpdc.timing.com [206.168.13.194])
	by hub.freebsd.org (Postfix) with ESMTP id C89B837BD47
	for <FreeBSD-gnats-submit@freebsd.org>; Fri, 10 Mar 2000 11:14:00 -0800 (PST)
	(envelope-from jhein@timing.com)
Received: from Brain.timing.com ([206.168.13.195]) by timingpdc.timing.com
          (Post.Office MTA v3.1.2 release (PO205-101c)
          ID# 103-49575U100L2S100) with ESMTP id AAA617;
          Fri, 10 Mar 2000 12:14:34 -0700
Received: (from jhein@localhost)
	by Brain.timing.com (8.9.3/8.9.3) id MAA32389;
	Fri, 10 Mar 2000 12:13:47 -0700 (MST)
	(envelope-from jhein)
Message-Id: <200003101913.MAA32389@Brain.timing.com>
Date: Fri, 10 Mar 2000 12:13:47 -0700 (MST)
From: "John E. Hein" <jhein@timing.com>
Reply-To: jhein@timing.com
To: FreeBSD-gnats-submit@freebsd.org
Subject: patch for bsd.kmod.mk to support more flexible compilation of KLDs via $KERN
X-Send-Pr-Version: 3.2

>Number:         17304
>Category:       kern
>Synopsis:       More flexible support for building klds outside a kernel tree needed
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    imp
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Fri Mar 10 11:20:01 PST 2000
>Closed-Date:    Fri May 26 22:29:05 MDT 2000
>Last-Modified:  Fri May 26 22:30:20 MDT 2000
>Originator:     John E. Hein
>Release:        FreeBSD 4.0 (osreldate: 400016)
>Organization:
Timing Solutions Corp.
>Environment:

 Desired target OS:

 FreeBSD demo.ts 4.0-TSC-20000301 FreeBSD 4.0-TSC-20000301 #0: Fri Mar  3 18:24:00 MST 2000     imp@marvin.timing.com:/net/daffy/u7/homes/imp/tsc/FreeBSD-tsc-4/sys/compile/MINNIE.TS  i386

 System on which building of KLD is done:

 Building from any FreeBSD version != desired target OS:


>Description:

 bsd.kmod.mk does not support having a FreeBSD source tree in a location
  other than the default /usr/src very well.  There are 2 problems with
  this:
   - the machine you compile on is not exactly the same version of FreeBSD
      as the desired target machine.
   - you can't keep your own KLD sources outside the FreeBSD source
      tree easily.  Thus people tend to pollute their FreeBSD source
      tree with their own KLD sources.

 The included patch supports more flexible compilation on non-target machines
  & compilation of KLD sources located outside the FreeBSD source tree.


>How-To-Repeat:

 The following example will not compile on a recent 4.0 machine
  (such as osreldate 400016).  It DOES compile on a November snapshot
  of 4.0 (osreldate 400011).  With my patch (below), you can point
  $KERN to the FreeBSD source tree of your choice (which may or
  may not be located where your KLD is) and you can compile
  successfully.

 In addition, this example will not compile if it is NOT in the
  proper place actually within the FreeBSD source tree.  The patch below
  allows the user to put his KLD anywhere and simply tell the makefile
  where to find the desired FreeBSD sources.  Thus he can compile
  his KLD for any desired FreeBSD version from any machine he wishes.


 Here's the Makefile:

/* Makefile */
SRCS = test.c

.include <bsd.kmod.mk>


 Here's the test.c source code:

/* test.c */
#include <sys/cdefs.h>
#include <sys/types.h>

#ifdef _KERNEL
#warning "_KERNEL defined... we are using a recent bsd.kmod.mk"
#else
#warning "_KERNEL NOT defined... we're not using the latest OS stuff"
#endif

#include <i386/isa/isa_device.h>

void              testfunc (void);
void
testfunc (void)
{
/*
 * example of trying to use stuff that is gone in newer versions of the
 *  4.x source tree
 *
 * This DID compile on an old November snapshot of 4.0 (400011)
 *
 * This does NOT compile on a recent November snapshot of 4.0 (400016)
 *  since the id_alive field no longer exists
 *
 *
 * This is just an example to demonstrate why one might
 *  want to compile for a version of FreeBSD other than
 *  the installed version of the current machine.  Let's say
 *  we have a compile system based on a certain version of
 *  FreeBSD and we want to upgrade it, but not before we can be
 *  sure that we can make our KLDs compile.
 */
    struct isa_device isa_dev;

#warning "This should not compile for newer versions of 4.x"
    isa_dev.id_alive = 1;
}


>Fix:

 Following is the proposed patch to bsd.kmod.mk :

 It allows the KLD developer to specify $KERN which points
  to the sys/kern directory of his desired target FreeBSD version.
  So he would compile his KLD with
   'make KERN=/the/location/of/my/desired/target/FreeBSD/version/sys/kern'

 Note that this does not break buildworld.  In fact, it does not
  change the default behavior at all unless KERN is specified (and
  it's not in any of the default buildworld makefiles).

 In addition, this allows that your KLD source need not be in the
  FreeBSD tree to compile.  It can be anywhere (such as the location
  of your company's driver repository).

@@ -175,15 +175,24 @@
 # found in the search are likely to cause problems.  If nothing is found,
 # then the links default to /usr/include and /usr/include/machine.
 ${_ILINKS}:
-       @set +x; for up in ../.. ../../.. ; do \
+       @set +x; for up in ${ILINK_SEARCH_DIRS}; do \
                case ${.TARGET} in \
                machine) \
-                       testpath=${.CURDIR}/$$up/${MACHINE_ARCH}/include ; \
-                       path=${.CURDIR}/$$up/${MACHINE_ARCH}/include ; \
+                       if [ "${ILINK_SEARCH_DEFAULT}" = "1" ]; then \
+                               testpath=${.CURDIR}/$$up/${MACHINE_ARCH}/include ; \
+                       else \
+                               testpath=$$up/${MACHINE_ARCH}/include ; \
+                       fi ; \
+                       path=$$testpath ; \
                        defaultpath=/usr/include/machine ;; \
                @) \
-                       testpath=${.CURDIR}/$$up/sys ; \
-                       path=${.CURDIR}/$$up ; \
+                       if [ "${ILINK_SEARCH_DEFAULT}" = "1" ]; then \
+                               testpath=${.CURDIR}/$$up/sys ; \
+                               path=${.CURDIR}/$$up ; \
+                       else \
+                               testpath=$$up ; \
+                               path=$$up ; \
+                       fi ; \
                        defaultpath=/usr/include ;; \
                esac ; \
                if [ -d $$testpath ] ; then break ; fi ; \
@@ -261,10 +270,17 @@
        ${KMODUNLOAD} ${KMOD}
 .endif
 
+.if !defined(KERN)
 .if exists(${.CURDIR}/../../kern)
 KERN=  ${.CURDIR}/../../kern
 .else
 KERN=  ${.CURDIR}/../../sys/kern
+.endif
+ILINK_SEARCH_DEFAULT=1
+ILINK_SEARCH_DIRS=     ../.. ../../..
+.else
+ILINK_SEARCH_DEFAULT=0
+ILINK_SEARCH_DIRS=     ${KERN}/..
 .endif
 
 .for _src in ${SRCS:Mopt_*.h}

>Release-Note:
>Audit-Trail:
Responsible-Changed-From-To: freebsd-bugs->imp 
Responsible-Changed-By: imp 
Responsible-Changed-When: Mon Mar 13 14:41:15 MST 2000 
Responsible-Changed-Why:  
I'll take point on this 
State-Changed-From-To: open->closed 
State-Changed-By: imp 
State-Changed-When: Fri May 26 22:29:05 MDT 2000 
State-Changed-Why:  
Much better support for this is now in the tree.  There are some cross version 
issues, but they cannot be easily avoided.  Older builds on newer kernels work 
great, and usually vice versa, but not always. 
>Unformatted:

This is also desirable for building a kld that needs something not
installed in the system.  It is also desirable when building on ones
desktop which might not exactly match the target machine.

A better patch would be
Index: bsd.kmod.mk
===================================================================
RCS file: /base/FreeBSD-tsc-3/share/mk/bsd.kmod.mk,v
retrieving revision 1.1.1.1
diff -u -r1.1.1.1 bsd.kmod.mk
--- bsd.kmod.mk	1999/08/30 18:47:19	1.1.1.1
+++ bsd.kmod.mk	2000/03/13 21:07:07
@@ -239,15 +239,15 @@
 	ln -s $$path ${.TARGET}
 .else
 ${_ILINKS}:
-	@set +x; for up in ../.. ../../.. ; do \
+	@set +x; for up in ${_ILINK_SEARCH_DIRS}; do \
 		case ${.TARGET} in \
 		machine) \
-			testpath=${.CURDIR}/$$up/${MACHINE_ARCH}/include ; \
-			path=${.CURDIR}/$$up/${MACHINE_ARCH}/include ; \
+			testpath=$$up/${MACHINE_ARCH}/include ; \
+			path=$$testpath ; \
 			defaultpath=/usr/include/machine ;; \
 		@) \
-			testpath=${.CURDIR}/$$up/sys ; \
-			path=${.CURDIR}/$$up ; \
+			testpath=$$up/sys ; \
+			path=$$up ; \
 			defaultpath=/usr/include ;; \
 		esac ; \
 		if [ -d $$testpath ] ; then break ; fi ; \
@@ -311,10 +311,15 @@
 	${MODUNLOAD} -n ${KMOD}
 .endif
 
+.if !(defined(KERN) && exists(${KERN}))
 .if exists(${.CURDIR}/../../kern)
 KERN=	${.CURDIR}/../../kern
 .else
 KERN=	${.CURDIR}/../../sys/kern
+.endif
+_ILINK_SEARCH_DIRS=	${.CURDIR}/../.. ${.CURDIR}/../../..
+.else
+_ILINK_SEARCH_DIRS=	${KERN}/..
 .endif
 
 vnode_if.h:	${KERN}/vnode_if.sh ${KERN}/vnode_if.src
