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 ${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 TARG Makefiles...\n";
24 ($KDIR, $TARG, @Makefiles) = @ARGV;
27 ## Read in all of the Makefiles given on the command line
28 ## Our ultimate goal is to find the correct source file for each object.
29 ## We make the following assumptions:
30 ## - Every variable is defined before it is used.
31 ## - Each of our objects has exactly one dependency, which is the name
32 ## of the source file that needs to be symlinked into $KDIR
33 foreach $mf (@Makefiles) {
34 $F = new IO
::File
($mf, O_RDONLY
) or die "$mf: $!\n";
39 next if $text =~ s/\\$/ /; ## Continuation
40 if ($text =~ /^#/) { $text = ''; next } ## Comment
41 #print STDERR "<< $text\n";
43 $text =~ s/\$\((\w+)\)/$vars{$1}/g; # Substitute variables
44 $text =~ s/\$\{(\w+)\}/$vars{$1}/g;
45 #print STDERR ">> $text\n";
47 if ($text =~ /^\s*(\S+)\s*=/) { ## Variable definition
48 ($key, $value) = ($1, $');
49 $value =~ s/^\s*//; # Remove leading and
50 $value =~ s/\s*$//; # trailing whitespace
51 $vars{$key} = $value; # Store it
53 elsif ($text =~ /^(\S+\.o):\s*(\S+\.c)/) { ## Dependency
56 elsif ($text =~ /^(\S+\.o):\s*(\S+\.s)/) { ## Dependency
65 $KDIR = "$vars{TOP_OBJDIR}/src/libafs/$KDIR";
66 @libafs_objs = (split(' ', $vars{AFSAOBJS}), split(' ', $vars{AFSNFSOBJS}));
67 @afspag_objs = (split(' ', $vars{AFSPAGOBJS}));
69 $MV = new IO::File("$vars{TOP_OBJDIR}/src/config/Makefile.version", O_RDONLY)
70 or die "$vars{TOP_OBJDIR}/src/config/Makefile.version: $!\n";
72 s#AFS_component_version_number#$KDIR/AFS_component_version_number#g;
73 $MakefileVersion .= $_;
78 mkdir($KDIR, 0777) or die "$KDIR: $!\n";
81 %all_objs = map(($_ => 1), @libafs_objs, @afspag_objs);
83 foreach (keys %all_objs) {
84 die "No source known for $_\n" unless exists $deps{$_};
85 if($deps{$_} =~ /\.s$/) {
86 ($src = $_) =~ s/\.o$/.S/;
88 ($src = $_) =~ s/\.o$/.c/;
90 if (-e "$KDIR/$src" || -l "$KDIR/$src") {
91 unlink("$KDIR/$src") or die "$KDIR/$src: $!\n";
93 next unless $deps{$_} =~ m#/#;
94 symlink($deps{$_}, "$KDIR/$src") or die "$KDIR/$src: $!\n";
97 %remap = ('h
' => 'linux
', 'netinet
' => 'linux
', 'sys
' => 'linux
');
98 if (-f "$vars{LINUX_KERNEL_PATH}/include/linux/types.h") {
99 foreach $src (keys %remap) {
100 if (-e "$KDIR/$src" || -l "$KDIR/$src") {
101 unlink("$KDIR/$src") or die "$KDIR/$src: $!\n";
103 symlink("$vars{LINUX_KERNEL_PATH}/include/linux", "$KDIR/$src")
104 or die "$KDIR/$src: $!\n";
107 foreach $src (keys %remap) {
108 system ('rm
', '-rf
', "$KDIR/$src"); # too crude?
109 mkdir("$KDIR/$src", 0777) or die "$KDIR/$src: $!\n";
113 @include_dirs = map { /^\// ? $_ : "$KDIR/$_" }
114 split /[\s\\]*-I/, $vars{COMMON_INCLUDE};
115 push @include_dirs, "$vars{TOP_SRCDIR}/../include/rx", "$vars{TOP_SRCDIR}/rx";
118 $content = new IO::File($src, O_RDONLY) or die "$src: $!\n";
122 if (/^\s*#\s*include\s*[<"](?:\.\.\/)?([^\/>"]*)(.*?)[>"]/) {
124 if (exists $seen{$inc}) {
126 } elsif (exists $remap{$1} && $2 !~ /.\//) {
127 $H = new IO::File("$KDIR/$inc", O_WRONLY|O_CREAT|O_TRUNC, 0666)
128 or die "$KDIR/$inc: $!\n";
129 print $H "#include <linux$2>\n";
130 $H->close() or die "$KDIR/$inc: $!\n";
132 for $dir (@include_dirs) {
133 if (-f "$dir/$inc") {
134 push @q, "$dir/$inc";
139 if ($1 =~ /^(arpa|asm|.*fs|i?net|kern|ksys|linux|mach|rpc|scsi|vm)$/
141 # Safe to ignore silently.
143 warn "Ignoring $_ ($inc not found)\n";
147 } elsif (/^\s*#\s*include/) {
148 warn "Ignoring $_ (unrecognized syntax)\n";
154 $cflags = "$vars{CFLAGS} $vars{COMMON_INCLUDE}";
155 $cflags =~ s#-I(?!/)#-I$KDIR/#g;
156 $cflags =~ s/\s+/ \\\n /g;
157 $F = new IO::File("$KDIR/Makefile", O_WRONLY|O_CREAT|O_TRUNC, 0666)
158 or die "$KDIR/Makefile: $!\n";
159 foreach (sort keys %vars) {
160 next unless /^[AC]FLAGS_/;
161 print $F "$_ = $vars{$_}\n";
163 print $F "EXTRA_CFLAGS=$cflags\n";
164 print $F "obj-m := $TARG.o afspag.o\n";
165 print $F "$TARG-objs := ", join("\\\n $_", @libafs_objs), "\n";
166 print $F "afspag-objs := ", join("\\\n $_", @afspag_objs), "\n";
167 print $F "\n$MakefileVersion\n";
168 $F->close() or die "$KDIR/Makefile: $!\n";