summaryrefslogtreecommitdiff
path: root/docs/tutorials/006/fix.Makefile
blob: e99c194114a07d3844e3c5c12bbb50a6b397206e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/perl

	# Open the Makefile that has been mangled by 'make depend'
	# and suck it into a perl array.
open(IF,"<Makefile") || die;
@makefile = <IF>;
close(IF);

	# Now open our .depend file and a temporary Makefile.
	# We'll split the original Makefile between these two.
open(DF,">.depend") || die;
open(MF,">Makefile.tmp") || die;

	# For each line we read out of the original file...
foreach (@makefile) {

	# If we're into the dependency section, write the line
	# into the .depend file.
	#
	if( $depend ) {
		print DF $_;
	}
	else {
		# If we haven't gotten to the dependency section yet
		# then see if the current line is the separator that
		# "make depend" causes to be inserted.
		#
		if( m/^\Q# DO NOT DELETE THIS LINE -- g++dep uses it.\E/ ) {

			# If so, change our "mode" and skip this line.
			++$depend;
			next;
		}

		# Also skip the "include .depend" that we insert.  If we
		# don't do this, it is possible to have a bunch of these
		# inserted into the output when we read an unmangled Makefile
		next if( m/^include .depend/ );

		# Print the non-dependency info to the temporary Makefile
		print MF $_;
	}
}

# Tell our new Makefile to include the dependency file
print MF "include .depend\n";

# Close the two output files...
close(DF);
close(MF);

# Unlink (remove) the original Makefile and rename our
# temporary file.  There's obviously room for error checking
# here but we've got the Makefile checked into some revision
# control system anyway.  Don't we?

unlink("Makefile");
rename("Makefile.tmp","Makefile");

exit(0);