9116869a2e77aaba1224a3203ace15f33e63581c
[openafs.git] / src / libafs / make_kbuild_makefile.pl
1 #!/usr/bin/perl
2 # make_kbuild_makefile.pl
3 # Generate a Makefile for use with the Linux 2.6+ kernel build system
4 #
5 # Usage: make_kbuild_makefile.pl ${KDIR} ${TARG} Makefiles...
6 #
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
14
15 # Produces ${KDIR}/Makefile, suitable for use with kbuild
16
17 use IO::File;
18
19
20 if (@ARGV < 3) {
21   die "Usage: $0 KDIR TARG Makefiles...\n";
22 }
23
24 ($KDIR, $TARG, @Makefiles) = @ARGV;
25 $TARG =~ s/\.k?o$//;
26
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";
35   $text = '';
36   while (<$F>) {
37     chomp;
38     $text .= $_;
39     next if $text =~ s/\\$/ /;                 ## Continuation
40     if ($text =~ /^#/) { $text = ''; next }    ## Comment
41     #print STDERR "<< $text\n";
42
43     $text =~ s/\$\((\w+)\)/$vars{$1}/g;        # Substitute variables
44     $text =~ s/\$\{(\w+)\}/$vars{$1}/g;
45     #print STDERR ">> $text\n";
46
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
52     }
53     elsif ($text =~ /^(\S+\.o):\s*(\S+\.c)/) {    ## Dependency
54       $deps{$1} = $2;
55     }
56     elsif ($text =~ /^(\S+\.o):\s*(\S+\.s)/) {    ## Dependency
57       $deps{$1} = $2;
58     }
59     $text = '';
60   }
61   $F->close();
62 }
63
64
65 $KDIR = "$vars{TOP_OBJDIR}/src/libafs/$KDIR";
66 @libafs_objs = (split(' ', $vars{AFSAOBJS}), split(' ', $vars{AFSNFSOBJS}));
67 @afspag_objs = (split(' ', $vars{AFSPAGOBJS}));
68
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";
71 while (<$MV>) {
72   s#AFS_component_version_number#$KDIR/AFS_component_version_number#g;
73   $MakefileVersion .= $_;
74 }
75 $MV->close();
76
77 if (! -d $KDIR) {
78   mkdir($KDIR, 0777) or die "$KDIR: $!\n";
79 }
80
81 %all_objs = map(($_ => 1), @libafs_objs, @afspag_objs);
82
83 foreach (keys %all_objs) {
84   die "No source known for $_\n" unless exists $deps{$_};
85   if($deps{$_} =~ /\.s$/) {
86      ($src = $_) =~ s/\.o$/.S/;
87   } else {
88      ($src = $_) =~ s/\.o$/.c/;
89   }
90   if (-e "$KDIR/$src" || -l "$KDIR/$src") {
91     unlink("$KDIR/$src") or die "$KDIR/$src: $!\n";
92   }
93   next unless $deps{$_} =~ m#/#;
94   symlink($deps{$_}, "$KDIR/$src") or die "$KDIR/$src: $!\n";
95 }
96
97 foreach $src (qw(h sys netinet)) {
98   if (-e "$KDIR/$src" || -l "$KDIR/$src") {
99     unlink("$KDIR/$src") or die "$KDIR/$src: $!\n";
100   }
101   symlink("$vars{LINUX_KERNEL_PATH}/include/linux", "$KDIR/$src")
102     or die "$KDIR/$src: $!\n";
103 }
104
105 $cflags = "$vars{CFLAGS} $vars{COMMON_INCLUDE}";
106 $cflags =~ s#-I(?!/)#-I$KDIR/#g;
107 $cflags =~ s/\s+/ \\\n /g;
108 $F = new IO::File("$KDIR/Makefile", O_WRONLY|O_CREAT|O_TRUNC, 0666)
109      or die "$KDIR/Makefile: $!\n";
110 foreach (sort keys %vars) {
111   next unless /^[AC]FLAGS_/;
112   print $F "$_ = $vars{$_}\n";
113 }
114 print $F "EXTRA_CFLAGS=$cflags\n";
115 print $F "obj-m := $TARG.o afspag.o\n";
116 print $F "$TARG-objs := ", join("\\\n $_", @libafs_objs), "\n";
117 print $F "afspag-objs := ", join("\\\n $_", @afspag_objs), "\n";
118 print $F "\n$MakefileVersion\n";
119 $F->close() or die "$KDIR/Makefile: $!\n";
120