summaryrefslogtreecommitdiff
path: root/bin/DependencyGenerator/DependencyEditor.pm
blob: 72f1c693e7bfc6471f49f8b7553436c7ef268c46 (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package DependencyEditor;

# ************************************************************
# Description   : Edits existing dependencies.
# Author        : Chad Elliott
# Create Date   : 2/10/2002
# ************************************************************

# ************************************************************
# Pragmas
# ************************************************************

use strict;
use FileHandle;
use File::Basename;

use DependencyGenerator;
use ObjectGeneratorFactory;

# ************************************************************
# Subroutine Section
# ************************************************************

sub new {
  my($class) = shift;
  my($self)  = bless {
                     }, $class;
  return $self;
}


sub process {
  my($self)    = shift;
  my($output)  = shift;
  my($type)    = shift;
  my($cpp)     = shift;
  my($macros)  = shift;
  my($ipaths)  = shift;
  my($replace) = shift;
  my($files)   = shift;
  my($status)  = 0;
  my(@exclude) = ();
  my(@options) = ();

  ## Determine which include files to exclude based on the directory
  if (lc(basename($cpp)) eq 'cl' || lc(basename($cpp)) eq 'cl.exe') {
    push(@options, '/MD');
    if (defined $ENV{INCLUDE}) {
      foreach my $exc (split(';', $ENV{INCLUDE})) {
        $exc =~ s/\\/\\\\\\\\/g;
        push(@exclude, $exc);
      }
    }
  }
  else {
    @exclude = ('/usr/','/opt/', '/var/');
  }

  ## Back up the original file and receive the contents
  my(@contents) = ();
  if (-r $output) {
    if (!$self->backup($output, \@contents)) {
      print STDERR "ERROR: Unable to backup $output\n";
      return 1;
    }
  }

  ## Write out the new file
  my($fh) = new FileHandle();
  if (open($fh, ">$output")) {
    foreach my $line (@contents) {
      if ($line =~ /DO NOT DELETE THIS LINE/) {
        last;
      }
      print $fh $line;
    }

    print $fh "# DO NOT DELETE THIS LINE -- " . basename($0) . " uses it.\n" .
              "# DO NOT PUT ANYTHING AFTER THIS LINE, IT WILL GO AWAY.\n\n";

    my($dep) = new DependencyGenerator($cpp, $macros, \@options, $ipaths);
    my($objgen) = ObjectGeneratorFactory::create($type);
    foreach my $file (sort @$files) {
      my(@objects) = $objgen->process($file);
      print $fh $dep->process($file, \@objects,
                              \@exclude, $replace, $type) . "\n";
    }

    print $fh "# IF YOU PUT ANYTHING HERE IT WILL GO AWAY\n";
    close($fh);
  }
  else {
    print STDERR "ERROR: Unable to open $output for output\n";
    $status++;
  }
  return $status;
}


sub backup {
  my($self)     = shift;
  my($source)   = shift;
  my($contents) = shift;
  my($status)   = 0;
  my($fh)       = new FileHandle();
  my($backup)   = "$source.bak";

  unlink($backup);
  if (open($fh, $source)) {
    my($oh) = new FileHandle();
    if (open($oh, ">$backup")) {
      $status = 1;
      while(<$fh>) {
        print $oh $_;
        push(@$contents, $_);
      }
      close($oh);

      ## Set file permission
      my(@buf) = stat($source);
      if (defined $buf[8] && defined $buf[9]) {
        utime($buf[8], $buf[9], $backup);
      }
    }
    close($fh);
  }
  return $status;
}


1;