1#
2# fixwin32mak.pl ::: Apache/Win32 maintanace program
3#
4# This program, launched from the build/ directory, replaces all nasty absoulute paths
5# in the win32 .mak files with the appropriate relative root.
6#
7# Run this program prior to committing or packaging any newly exported make files.
8
9use Cwd;
10use IO::File;
11use File::Find;
12
13$root = cwd;
14# ignore our own direcory (allowing us to move into any parallel tree)
15$root =~ s|^.:(.*)?$|cd "$1|;
16$root =~ s|/|\\\\|g;
17$altroot = $root;
18$altroot =~ s| ".:| "|;
19print "Stripping " . $root . " and " . $altroot . "\n";
20find(\&fixcwd, '.');
21
22# Given this pattern that disregarded the RECURSE flag...
23#
24# !IF "$(RECURSE)" == "0"
25#
26# ALL : "$(OUTDIR)\mod_charset_lite.so"
27#
28# !ELSE
29#
30# ALL : "libhttpd - Win32 Release" "libaprutil - Win32 Release" "libapr - Win32 Release" "$(OUTDIR)\mod_charset_lite.so"
31#
32# !ENDIF
33#...
34# DS_POSTBUILD_DEP=$(INTDIR)\postbld.dep
35#...
36# ALL : $(DS_POSTBUILD_DEP)
37#
38# $(DS_POSTBUILD_DEP) : "libhttpd - Win32 Release" "libaprutil - Win32 Release" "libapr - Win32 Release" "$(OUTDIR)\mod_charset_lite.so"
39#
40# we will axe the final ALL : clause,
41# strip all but the final element from $(DS_POSTBUILD_DEP) : clause
42# move the DS_POSTBUILD_DEP assignment above the IF (for true ALL : targets)
43# and in pass 2, append the $(DS_POSTBUILD_DEP) to the valid ALL : targets
44
45
46sub fixcwd {
47    if (m|.mak$|) {
48        $thisroot = $File::Find::dir;
49        $thisroot =~ s|^./(.*)$|$1|;
50        $thisroot =~ s|/|\\\\|g;
51        $thisroot = $root . "\\\\" . $thisroot;
52        $thisaltroot = $altroot . "\\\\" . $thisroot;
53        $oname = $_;
54        $tname = '.#' . $_;
55        $verchg = 0;
56        $postdep = 0;
57        $srcfl = new IO::File $_, "r" || die;
58        $dstfl = new IO::File $tname, "w" || die;
59        while ($src = <$srcfl>) {
60            if ($src =~ m|^DS_POSTBUILD_DEP=.+$|) {
61                $postdepval = $src;
62            }
63            if ($src =~ s|^ALL : \$\(DS_POSTBUILD_DEP\)||) {
64                $postdep = -1;
65                $verchg = -1;
66                $src = <$srcfl>;
67                $src = <$srcfl> if ($src =~ m|^$|);
68            }
69            if ($postdep) {
70                $src =~ s|^(\$\(DS_POSTBUILD_DEP\)) :.+(\"[^\"]+\")$|"$1" : $2|;
71            }
72            if ($src =~ m|^\s*($root[^\"]*)\".*$|) {
73                $orig = $thisroot;
74            } elsif ($src =~ m|^\s*($altroot[^\"]*)\".*$|) {
75                $orig = $thisaltroot;
76            }
77            if (defined($orig)) {
78                $repl = "cd \".";
79                while (!($src =~ s|$orig|$repl|)) {
80                   if (!($orig =~ s|^(.*)\\\\[^\\]+$|$1|)) {
81                       break;
82                   }
83                   $repl .= "\\..";
84                }
85                print "Replaced " . $orig . " with " . $repl . "\n";
86                $verchg = -1;
87                undef $orig;
88            }
89            # With modern LINK.EXE linkers, there is a different LINK for
90            # each platform, and it's determined by the file path.  Best
91            # that here, after we compiled the code to the default CPU,
92            # that we also link here to the default CPU.  Omitting the
93            # /machine spec from the .dsp was not enough, MSVC put it back.
94            #
95            if ($src =~ s#^(LINK32_FLAGS=.*) /machine:(x|IX|I3)86 #$1 #) {
96                $verchg = -1;
97            }
98            print $dstfl $src;
99        }
100        undef $srcfl;
101        undef $dstfl;
102        if ($verchg) {
103            if ($postdep) {
104                $srcfl = new IO::File $tname, "r" || die;
105                $dstfl = new IO::File $oname, "w" || die;
106                while ($src = <$srcfl>) {
107                    if ($src =~ m|^INTDIR=|) {
108                        print $dstfl $src;
109                        $src = $postdepval;
110                    }
111                    $src =~ s|^(ALL : .+)$|$1 "\$\(DS_POSTBUILD_DEP\)"|;
112                    print $dstfl $src;
113                }
114                undef $srcfl;
115                undef $dstfl;
116                unlink $tname || die;
117                print "Corrected post-dependency within " . $oname . " in " . $File::Find::dir . "\n";
118            }
119            else {
120                unlink $oname || die;
121                rename $tname, $oname || die;
122                print "Corrected absolute paths within " . $oname . " in " . $File::Find::dir . "\n";
123            }
124        }
125        else {
126            unlink $tname;
127        }
128        $dname = $oname;
129        $dname =~ s/.mak$/.dsp/;
130        @dstat = stat($dname);
131        @ostat = stat($oname);
132        if ($ostat[9] && $dstat[9] && ($ostat[9] != $dstat[9])) {
133            @onames = ($oname);
134            utime $dstat[9], $dstat[9], @onames;
135            print "Touched datestamp for " . $oname . " in " . $File::Find::dir . "\n";
136        }
137        $oname =~ s/.mak$/.dep/;
138        $verchg = 0;
139        $srcfl = new IO::File $oname, "r" || die;
140        $dstfl = new IO::File $tname, "w" || die;
141        while ($src = <$srcfl>) {
142            if (($src =~ m/^\t"(\.\.\\)+(apr|apr-util|apr-iconv)\\.*"\\/) ||
143                ($src =~ m/^\t{\$\(INCLUDE\)}".*"\\/)) {
144                $verchg = -1;
145            }
146            else {
147                print $dstfl $src;
148            }
149        }
150        undef $srcfl;
151        undef $dstfl;
152        if ($verchg) {
153            unlink $oname || die;
154            rename $tname, $oname || die;
155            print "Stripped external dependencies from " . $oname . " in " . $File::Find::dir . "\n";
156        }
157        else {
158            unlink $tname || die;
159        }
160        @ostat = stat($oname);
161        if ($ostat[9] && $dstat[9] && ($ostat[9] != $dstat[9])) {
162            @onames = ($oname);
163            utime $dstat[9], $dstat[9], @onames;
164            print "Touched datestamp for " . $oname . " in " . $File::Find::dir . "\n";
165        }
166    }
167}
168