2 # make_kbuild_makefile.pl
3 # Generate a Makefile for use with the Linux 2.6+ kernel build system
5 # Usage: make_kbuild_makefile.pl ${KDIR} ${TARG} Makefiles...
7 # The specified makefiles will be scanned for variable values
8 # The module ${TARG} will be built in ${TOP_SRCDIR}/src/libafs/${KDIR}.
9 # It will include objects listed in ${AFSAOBJS} and ${AFSNONFSOBJS}
10 # Appropriate source files for each object will be symlinked into ${KDIR}
11 # EXTRA_CFLAGS will be set to ${CFLAGS} ${COMMON_INCLUDE}
13 # Produces ${KDIR}/Makefile, suitable for use with kbuild
19 die "Usage: echo objects... | $0 KDIR TARG Makefiles...\n";
22 ($KDIR, $TARG, @Makefiles) = @ARGV;
25 ## Read in all of the Makefiles given on the command line
26 ## Our ultimate goal is to find the correct source file for each object.
27 ## We make the following assumptions:
28 ## - Every variable is defined before it is used.
29 ## - Each of our objects has exactly one dependency, which is the name
30 ## of the source file that needs to be symlinked into $KDIR
31 foreach $mf (@Makefiles) {
32 $F = new IO::File($mf, O_RDONLY) or die "$mf: $!\n";
37 next if $text =~ s/\\$/ /; ## Continuation
38 if ($text =~ /^#/) { $text = ''; next } ## Comment
39 #print STDERR "<< $text\n";
41 $text =~ s/\$\((\w+)\)/$vars{$1}/g; # Substitute variables
42 $text =~ s/\$\{(\w+)\}/$vars{$1}/g;
43 #print STDERR ">> $text\n";
45 if ($text =~ /^\s*(\S+)\s*=/) { ## Variable definition
46 ($key, $value) = ($1, $');
47 $value =~ s/^\s*//; # Remove leading and
48 $value =~ s/\s*$//; # trailing whitespace
49 $vars{$key} = $value; # Store it
51 elsif ($text =~ /^(\S+\.o):\s*(\S+\.c)/) { ## Dependency
60 $KDIR = "$vars{TOP_OBJDIR}/src/libafs/$KDIR";
61 @objects = (split(' ', $vars{AFSAOBJS}), split(' ', $vars{AFSNONFSOBJS}));
63 $MV = new IO::File("$vars{TOP_OBJDIR}/src/config/Makefile.version", O_RDONLY)
64 or die "$vars{TOP_OBJDIR}/src/config/Makefile.version: $!\n";
66 s#AFS_component_version_number#$KDIR/AFS_component_version_number#g;
67 $MakefileVersion .= $_;
72 mkdir($KDIR, 0777) or die "$KDIR: $!\n";
77 ($src = $_) =~ s/\.o$/.c/;
78 die "No source known for $_\n" unless exists $deps{$_};
79 if (-e "$KDIR/$src" || -l "$KDIR/$src") {
80 unlink("$KDIR/$src") or die "$KDIR/$src: $!\n";
82 next unless $deps{$_} =~ m#/#;
83 symlink($deps{$_}, "$KDIR/$src") or die "$KDIR/$src: $!\n";
86 foreach $src (qw(h sys netinet)) {
87 if (-e "$KDIR/$src" || -l "$KDIR/$src") {
88 unlink("$KDIR/$src") or die "$KDIR/$src: $!\n";
90 symlink("$vars{LINUX_KERNEL_PATH}/include/linux", "$KDIR/$src")
91 or die "$KDIR/$src: $!\n";
94 $cflags = "$vars{CFLAGS} $vars{COMMON_INCLUDE}";
95 $cflags =~ s#-I(?!/)#-I$KDIR/#g;
96 $cflags =~ s/\s+/ \\\n /g;
97 $F = new IO::File("$KDIR/Makefile", O_WRONLY|O_CREAT|O_TRUNC, 0666)
98 or die "$KDIR/Makefile: $!\n";
99 print $F "EXTRA_CFLAGS=$cflags\n";
100 print $F "obj-m := $TARG.o\n";
101 print $F "$TARG-objs := ", join("\\\n $_", @objects), "\n";
102 print $F "\n$MakefileVersion\n";
103 $F->close() or die "$KDIR/Makefile: $!\n";