Prevent -I/usr/include during Linux kernel module compilation.
[lufs.git] / kernel / Linux / prepmod.in
index 41f52cb..b3c93cf 100755 (executable)
@@ -27,12 +27,9 @@ use Getopt::Long;
 
 my $basedir='@datadir@/lufs';
 my $vardir='@localstatedir@/lib/lufs';
-my $includedir='@includedir@'; # '/lufs' is required by C '#include'
 $basedir=~s#\$\Q{prefix}\E#'@prefix@';#ge;
 $vardir=~s#\$\Q{prefix}\E#'@prefix@';#ge;
 $vardir="" if $vardir=~/^@/;
-$includedir=~s#\$\Q{prefix}\E#'@prefix@';#ge;
-$includedir="" if $includedir=~/^@/;
 sub srcdir { my($uname_r)=@_; $basedir."/".($uname_r lt "2.5" ? "2.4" : "2.5"); }
 my $modbindir=$basedir."/modbin";
 my @sources=qw(proc.c inode.c dir.c file.c symlink.c);
@@ -42,7 +39,6 @@ my $kernel;
 my $prebuild;
 use vars qw($kernel_gcc_args);
 $kernel_gcc_args="";
-$kernel_gcc_args="-I$includedir" if $includedir;
 
 my $lufsmnt_bin;
 if ($0 eq "lufsmnt" || $0=~m#/lufsmnt$#) {
@@ -125,11 +121,14 @@ print STDERR "Running kernel version: $uname_r (base version $uname_r_base)\n" i
 my $moduledir="/lib/modules/$uname_r$uname_smp/kernel/fs/lufs";
 print STDERR "Destination module directory: $moduledir\n" if !$quiet;
 
-do { $kernel||=$_ if -d $_; } for ("/lib/modules/$uname_r/build");
-do { $kernel||=$_ if -d $_; } for ("/usr/src/kernel-headers-$uname_r");
-do { $kernel||=$_ if -d $_; } for ("/usr/src/linux-$uname_r");
-do { $kernel||=$_ if -d $_; } for ("/usr/src/linux-$uname_r_base");
-do { $kernel||=$_ if -d $_; } for ("/usr/src/linux");
+my @kernel_paths=(
+                               "/lib/modules/$uname_r/build",
+                               "/usr/src/kernel-headers-$uname_r",
+                               "/usr/src/linux-$uname_r",
+                               "/usr/src/linux-$uname_r_base",
+                               "/usr/src/linux",
+                               );
+do { $kernel||=$_ if -d $_; } for (@kernel_paths);
 if (!$kernel) {
        print STDERR "Failed to find kernel headers for $uname_r\n" if !$kernel && !$quiet;
        }
@@ -156,7 +155,13 @@ for (<$modbindir/*-$uname_r*/*.o>,<$modbindir/*-${uname_r_base}*/*.o>,<$modbindi
                        if _system "/bin/rm -rf $moduledir; /bin/mkdir -p $moduledir; /bin/ln -s $_ $moduledir/lufs.o";
        _pass 1;
        }
-confess "lufs module not loaded: Try running $basedir/prepmod to see more.";
+confess "lufs module not loaded: Try running $basedir/prepmod to see more." if $quiet;
+confess "Failed to prepare lufs.o module for your Linux kernel $uname_r.\n"
+               .($kernel ? "Detected Linux kernel sources \"$kernel\" do not appear to be valid.\n"
+                               : "No Linux kernel sources for your running kernel were found.\n")
+               ."Please install kernel-source-x.y.z.i386.rpm or kernel-headers_x.y.z_i386.deb.\n"
+               ."The following directory paths were search (first existing directory used):\n"
+               .join("",map("\t\t$_\n",@kernel_paths));
 
 
 sub build_gcc
@@ -181,7 +186,7 @@ my($kernel,$uname_r,$destmodule)=@_;
 
        my $config=_readfile "$kernel/.config","optional";
        my $autoconf=_readfile "$kernel/include/linux/autoconf.h","optional";
-       $cmdline.=" -DMODVERSIONS -include $kernel/include/linux/modversions.h"
+       $cmdline.=" -DMODVERSIONS -include $kernel/include/linux/version.h -include $kernel/include/linux/modversions.h"
                        if 0
                                        || $config=~/^CONFIG_MODVERSIONS=y\b/m
                                        || $autoconf=~/^#define CONFIG_MODVERSIONS\b/m
@@ -198,7 +203,7 @@ my($kernel,$uname_r,$destmodule)=@_;
 
 sub build_make
 {
-my($kernel,$uname_r,$destmodule)=@_;
+my($kernel,$uname_r,$destmodule,%args)=@_;
 
        print STDERR "Using kernel sources: $kernel\n" if !$quiet;
        confess "Kernel sources $kernel do not contain 'Rules.make' file" if ! -f $kernel."/Rules.make";
@@ -207,6 +212,14 @@ my($kernel,$uname_r,$destmodule)=@_;
        do { $kdebug=$_ if !/^@/; } for ('@KDEBUG_FLAGS@');
        my $predir=srcdir $uname_r;
        $predir=$ENV{"PWD"}."/$predir" if $predir!~m#^/#;
+       # Do not use existing '$kernel/tmp_include_depends' or '$kernel/.depend'
+       # as it may contain non-existing pathnames:
+       _system "make -C $kernel dep" if !$args{"nodep"};
+       # Workaround a bug in at least Red Hat 2.4.18-18.8.0
+       for ("$kernel/include/linux/modversions.h") {
+               _system "cp -p $_ $_-orig" if ! -f "$_-orig";
+               _writefile $_,"#include <linux/version.h>\t/* lufs */\n"._readfile $_;
+               }
        my $cmdline="make -C $kernel"
                        ." SUBDIRS=\"$predir\" modules"
                        ." EXTRA_CFLAGS=\"$kernel_gcc_args\""
@@ -221,7 +234,7 @@ my($kernel,$uname_r,$destmodule)=@_;
 
 sub build
 {
-my($kernel,$uname_r,$destmodule)=@_;
+my($kernel,$uname_r,$destmodule,%args)=@_;
 
        # Debian uname(1) does not support '-p'.
        my $arch=_readfile "uname -p|" || _readfile "uname -m|";
@@ -234,9 +247,9 @@ my($kernel,$uname_r,$destmodule)=@_;
                }
        my $r;
        if (-f "$kernel/Rules.make" && -f "$kernel/.config")
-               { $r=build_make $kernel,$uname_r,$destmodule; }
+               { $r=build_make $kernel,$uname_r,$destmodule,%args; }
        else
-               { $r=build_gcc $kernel,$uname_r,$destmodule; }
+               { $r=build_gcc $kernel,$uname_r,$destmodule,%args; }
        _system "rm -f $kernel/.config"
                        if !$single_config;
        return $r;
@@ -245,11 +258,11 @@ my($kernel,$uname_r,$destmodule)=@_;
 
 sub prebuild_kernel
 {
-my($dir,$vendor,$uname_r)=@_;
+my($dir,$vendor,$uname_r,%args)=@_;
 
        confess "Unrecognized vendor for dir: $dir" if !$vendor;
        confess "Unrecognized uname_r for dir: $dir" if !$uname_r;
-       confess "Failed to build $dir" if !build $dir,$uname_r,$modbindir."/lufs-$vendor-$uname_r.o";
+       confess "Failed to build $dir" if !build $dir,$uname_r,$modbindir."/lufs-$vendor-$uname_r.o",%args;
 }
 
 
@@ -274,6 +287,9 @@ my($rpm)=@_;
        $dir or confess "Kernel source tree not found in: $rpm";
        _system "cp -p $dir/include/linux/rhconfig.h $dir/include/linux/rhconfig.h-orig"
                                if -f "$dir/include/linux/rhconfig.h";
+       # Do not use existing '$dir/tmp_include_depends' or '$dir/.depend'
+       # as it may contain non-existing pathnames:
+       _system "make -C $dir dep";
        for my $smp ("","smp") {
                my @archs=qw(i386 i586 i686 athlon);
                for my $arch (@archs) {
@@ -306,8 +322,6 @@ $defines
  #define __module__smp
 RHCONFIG_H_NOBOOT_EOF
                                }
-                       _system "make -C $dir dep"
-                                       if ! -f "$dir/tmp_include_depends" && ! -f "$dir/.depend";
                        # Mandrake packages have no configs/ and they have just that '.config' file.
                        my $single_config=-f "$dir/.config";
                        if (!$single_config) {
@@ -315,7 +329,7 @@ RHCONFIG_H_NOBOOT_EOF
                                next if ! -f $spec_config;
                                _system "ln -s $spec_config $dir/.config";
                                }
-                       prebuild_kernel $dir,$vendor,$uname_r.$smp.".".$arch;
+                       prebuild_kernel $dir,$vendor,$uname_r.$smp.".".$arch,"nodep"=>1;
                        _system "rm -f $dir/.config"
                                        if !$single_config;
                        }