Merge pull request #224 from DOCGroup/jwillemsen-patch-1
[MPC.git] / modules / Depgen / DependencyEditor.pm
blobfbc3af9829cc81289e411ed0cbb0808a7347fc45
1 package DependencyEditor;
3 # ************************************************************
4 # Description : Edits existing dependencies.
5 # Author : Chad Elliott
6 # Create Date : 2/10/2002
7 # ************************************************************
9 # ************************************************************
10 # Pragmas
11 # ************************************************************
13 use strict;
14 use FileHandle;
16 use DependencyGenerator;
18 # ************************************************************
19 # Subroutine Section
20 # ************************************************************
22 sub new {
23 return bless {}, $_[0];
27 sub process {
28 my($self, $output, $type, $noinline, $macros,
29 $ipaths, $replace, $exclude, $files,
30 $append) = @_;
32 ## Back up the original file and receive the contents
33 my $contents;
34 if (-s $output) {
35 $contents = [];
36 if (!$self->backup($output, $contents, $append)) {
37 print STDERR "ERROR: Unable to backup $output\n";
38 return 1;
42 ## Write out the contents of the file
43 my $fh = new FileHandle();
44 if (open($fh, ">$output")) {
45 if (defined $contents) {
46 foreach my $line (@$contents) {
47 print $fh $line;
51 if (!$append) {
52 ## Write out the new dependency marker
53 print $fh "# DO NOT DELETE THIS LINE -- depgen.pl uses it.\n",
54 "# DO NOT PUT ANYTHING AFTER THIS LINE, IT WILL GO AWAY.\n\n";
56 else {
57 ## Write start append comment
58 print $fh "# DO NOT DELETE THIS LINE -- depgen.pl appended ",
59 "the following.\n",
60 "# APPENDED DEPENDENCY RULES " ,
61 "by depgen.pl.\n\n";
64 ## Generate the new dependencies and write them to the file
65 my $dep = new DependencyGenerator($macros, $ipaths, $replace,
66 $type, $noinline,
67 $exclude);
69 ## Sort the files so the dependencies are reproducible
70 foreach my $file (sort @$files) {
71 ## In some situations we may be passed a directory as part of an
72 ## option. If it is an unknown option, we may think the directory
73 ## needs to be part of the dependencies when it should not.
74 print $fh $dep->process($file), "\n" if (!-d $file);
77 ## Write out the end of the block warning
78 print $fh "# IF YOU PUT ANYTHING HERE IT WILL GO AWAY\n";
79 close($fh);
81 else {
82 print STDERR "ERROR: Unable to open $output for output\n";
83 return 1;
86 return 0;
90 sub backup {
91 my($self, $source, $contents, $append) = @_;
92 my $status;
93 my $fh = new FileHandle();
94 my $backup = "$source.bak";
96 ## Back up the file. While doing so, keep track of the contents of the
97 ## file and keep everything except the old dependencies or keep
98 ## everything if appending.
99 my $search_string;
100 if (!$append) {
101 $search_string = 'DO NOT DELETE';
103 else {
104 $search_string = 'IF YOU PUT ANYTHING HERE IT WILL GO AWAY';
107 if (open($fh, $source)) {
108 my $oh = new FileHandle();
109 if (open($oh, ">$backup")) {
110 my $record = 1;
111 $status = 1;
112 while(<$fh>) {
113 print $oh $_;
114 if ($record) {
115 if (index($_, $search_string) >= 0) {
116 $record = undef;
118 else {
119 push(@$contents, $_);
123 close($oh);
125 ## Set file permission so that the backup has the same permissions
126 ## as the original file.
127 my @buf = stat($source);
128 if (defined $buf[8] && defined $buf[9]) {
129 utime($buf[8], $buf[9], $backup);
132 close($fh);
134 return $status;