Bug 17304

Summary: More flexible support for building klds outside a kernel tree needed
Product: Base System Reporter: John E. Hein <jhein>
Component: kernAssignee: Warner Losh <imp>
Status: Closed FIXED    
Severity: Affects Only Me    
Priority: Normal    
Version: Unspecified   
Hardware: Any   
OS: Any   
Attachments:
Description Flags
file.diff none

Description John E. Hein 2000-03-10 19:20:01 UTC
 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.

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}
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;
}
Comment 1 Warner Losh freebsd_committer freebsd_triage 2000-03-13 21:41:15 UTC
Responsible Changed
From-To: freebsd-bugs->imp

I'll take point on this 
Comment 2 Warner Losh freebsd_committer freebsd_triage 2000-05-27 05:29:05 UTC
State Changed
From-To: open->closed

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.