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} Makefiles...
7 # The specified makefiles will be scanned for variable values
8 # The libafs.ko module will be built in ${TOP_SRCDIR}/src/libafs/${KDIR}.
9 # It will include objects listed in ${AFSAOBJS} and ${AFSNFSOBJS}
10 # The afspag.ko module will be built from objects listed in ${AFSPAGOBJS}.
11 # Appropriate source files for each object will be symlinked into ${KDIR}
12 # EXTRA_CFLAGS will be set to ${CFLAGS} ${COMMON_INCLUDE}
13 # Any CFLAGS_* and AFLAGS_* variables will be copied
15 # Produces ${KDIR}/Makefile, suitable for use with kbuild
21 die "Usage: $0 KDIR Makefiles...\n";
24 ($KDIR, @Makefiles) = @ARGV;
26 ## Read in all of the Makefiles given on the command line
27 ## Our ultimate goal is to find the correct source file for each object.
28 ## We make the following assumptions:
29 ## - Every variable is defined before it is used.
30 ## - Each of our objects has exactly one dependency, which is the name
31 ## of the source file that needs to be symlinked into $KDIR
32 foreach $mf (@Makefiles) {
33 $F = new IO::File($mf, O_RDONLY) or die "$mf: $!\n";
38 next if $text =~ s/\\$/ /; ## Continuation
39 if ($text =~ /^#/) { $text = ''; next } ## Comment
40 #print STDERR "<< $text\n";
42 $text =~ s/\$\((\w+)\)/$vars{$1}/g; # Substitute variables
43 $text =~ s/\$\{(\w+)\}/$vars{$1}/g;
44 #print STDERR ">> $text\n";
46 if ($text =~ /^\s*(\S+)\s*=/) { ## Variable definition
47 ($key, $value) = ($1, $');
48 $value =~ s/^\s*//; # Remove leading and
49 $value =~ s/\s*$//; # trailing whitespace
50 $vars{$key} = $value; # Store it
52 elsif ($text =~ /^(\S+\.o):\s*(\S+\.c)/) { ## Dependency
55 elsif ($text =~ /^(\S+\.o):\s*(\S+\.s)/) { ## Dependency
64 $KDIR = "$vars{TOP_OBJDIR}/src/libafs/$KDIR";
65 @libafs_objs = (split(' ', $vars{AFSAOBJS}), split(' ', $vars{AFSNFSOBJS}));
66 @afspag_objs = (split(' ', $vars{AFSPAGOBJS}));
68 $MV = new IO::File("$vars{TOP_OBJDIR}/src/config/Makefile.version", O_RDONLY)
69 or die "$vars{TOP_OBJDIR}/src/config/Makefile.version: $!\n";
71 s#AFS_component_version_number#$KDIR/AFS_component_version_number#g;
72 $MakefileVersion .= $_;
77 mkdir($KDIR, 0777) or die "$KDIR: $!\n";
80 %all_objs = map(($_ => 1), @libafs_objs, @afspag_objs);
82 foreach (keys %all_objs) {
83 die "No source known for $_\n" unless exists $deps{$_};
84 if($deps{$_} =~ /\.s$/) {
85 ($src = $_) =~ s/\.o$/.S/;
87 ($src = $_) =~ s/\.o$/.c/;
89 if (-e "$KDIR/$src" || -l "$KDIR/$src") {
90 unlink("$KDIR/$src") or die "$KDIR/$src: $!\n";
92 next unless $deps{$_} =~ m#/#;
93 symlink($deps{$_}, "$KDIR/$src") or die "$KDIR/$src: $!\n";
96 foreach $src (qw(h sys netinet)) {
97 if (-e "$KDIR/$src" || -l "$KDIR/$src") {
98 unlink("$KDIR/$src") or die "$KDIR/$src: $!\n";
100 symlink("$vars{LINUX_KERNEL_PATH}/include/linux", "$KDIR/$src")
101 or die "$KDIR/$src: $!\n";
104 $cflags = "$vars{CFLAGS} $vars{COMMON_INCLUDE}";
105 $cflags =~ s#-I(?!/)#-I$KDIR/#g;
106 $cflags =~ s/\s+/ \\\n /g;
107 $F = new IO::File("$KDIR/Makefile", O_WRONLY|O_CREAT|O_TRUNC, 0666)
108 or die "$KDIR/Makefile: $!\n";
109 foreach (sort keys %vars) {
110 next unless /^[AC]FLAGS_/;
111 print $F "$_ = $vars{$_}\n";
113 print $F "EXTRA_CFLAGS=$cflags\n";
114 print $F "obj-m := libafs.o afspag.o\n";
115 print $F "libafs-objs := ", join("\\\n $_", @libafs_objs), "\n";
116 print $F "afspag-objs := ", join("\\\n $_", @afspag_objs), "\n";
117 print $F "\n$MakefileVersion\n";
118 $F->close() or die "$KDIR/Makefile: $!\n";