File: Create.pm

package info (click to toggle)
aspell 0.60.8.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,336 kB
  • sloc: cpp: 24,378; sh: 12,340; perl: 1,924; ansic: 1,661; makefile: 852; sed: 16
file content (128 lines) | stat: -rw-r--r-- 4,029 bytes parent folder | download | duplicates (5)
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
# This file is part of The New Aspell
# Copyright (C) 2001-2002 by Kevin Atkinson under the GNU LGPL
# license version 2.0 or 2.1.  You should have received a copy of the
# LGPL license along with this library if you did not you can find it
# at http://www.gnu.org/.

package MkSrc::Create;

BEGIN {
  use Exporter;
  our @ISA = qw(Exporter);
  our @EXPORT = qw(create_cc_file create_file);
}

use strict;
use warnings;
no warnings qw(uninitialized);
no locale;

use MkSrc::Util;
use MkSrc::Info;

=head1 MKSrc::Create

=over

=item create_cc_file PARMS

Create a source file.

  Required Parms: type, dir, name, data
   Boolean Parms: header, cxx
  Optional Parms: namespace (required if cxx), pre_ext, accum

=item create_file FILENAME DATA

Writes DATA to FILENAME but only if DATA differs from the content of
the file and the string:

    Automatically generated file.

is present in the existing file if it already exists.

=back

=cut

sub create_cc_file ( % );

sub create_file ( $ $ );

sub create_cc_file ( % )  {
  my (%p) = @_;
  $p{name} = $p{data}{name} unless exists $p{name};
  $p{ext} = $p{cxx} ? ($p{header} ? 'hpp' : 'cpp') : 'h';
  my $body;
  my %accum = exists $p{accum} ? (%{$p{accum}}) : ();
  foreach my $d (@{$p{data}{data}}) {
    next unless exists $info{$d->{type}}{proc}{$p{type}};
    $body .= $info{$d->{type}}{proc}{$p{type}}->($d, \%accum);
  }
  return unless length($body) > 0;
  my $file = <<'---';
/* Automatically generated file.  Do not edit directly. */

/* This file is part of The New Aspell
 * Copyright (C) 2001-2002 by Kevin Atkinson under the GNU LGPL
 * license version 2.0 or 2.1.  You should have received a copy of the
 * LGPL license along with this library if you did not you can find it
 * at http://www.gnu.org/.                                              */

---
  my $hm = "ASPELL_". to_upper($p{name})."__".to_upper($p{ext});
  $file .= "#ifndef $hm\n#define $hm\n\n" if $p{header} && $p{name} ne 'errors';
  # hack to avoid including aerror_* symbols as two different type of symbols
  $file .= "#if !defined($hm) && !defined(ASPELL_ASPELL__H)\n#define $hm\n\n" if $p{header} && $p{name} eq 'errors';
  $file .= "#include \"aspell.h\"\n" if $p{type} eq 'cxx';
  $file .= "#include \"settings.h\"\n" if $p{type} eq 'native_impl' && $p{name} eq 'errors';
  $file .= "#include \"gettext.h\"\n" if $p{type} eq 'native_impl' && $p{name} eq 'errors';
  $file .= cmap {"#include <$_>\n"} sort keys %{$accum{sys_headers}};
  $file .= cmap {"#include \"".to_lower($_).".hpp\"\n"} sort keys %{$accum{headers}};
  $file .= "\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n" if $p{header} && !$p{cxx};
  $file .= join('', grep {defined $_} @{$accum{prefix}});
  $file .= "\nnamespace $p{namespace} {\n\n" if $p{cxx};
  if (defined $info{forward}{proc}{$p{type}}) {
    my @types = sort {$a->{name} cmp $b->{name}} (values %{$accum{types}});
    $file .= cmap {$info{forward}{proc}{$p{type}}->($_)} @types;
  }
  $file .= "\n";
  $file .= $body;
  $file .= join('', grep {defined $_} @{$accum{suffix}});
  $file .= "\n\n}\n\n" if $p{cxx};
  $file .= "#ifdef __cplusplus\n}\n#endif\n" if $p{header} && !$p{cxx};
  $file .= "#endif /* $hm */\n" if $p{header};
  create_file $p{dir}.'/'.to_lower($p{name}).$p{pre_ext}.'.'.$p{ext}, $file;
}

my $MKMK;

sub create_file ( $ $ ) {
  my ($filename, $to_write) = @_;
  unless (defined $MKMK) {
    open $MKMK, ">auto" or die;
  }
  local $/ = undef;
  my $existing = '';
  open F,"../$filename" and $existing=<F>;
  if ($to_write eq $existing) {
    print $MKMK "\$(srcdir)/$filename ";
    print "File \"$filename\" unchanged.\n";
  } elsif (length $existing > 0 && $existing !~ /Automatically generated file\./) {
    print "Will not write over \"$filename\".\n";
  } else {
    print $MKMK "\$(srcdir)/$filename ";
    print "Creating \"$filename\".\n";
    open F, ">../$filename" or die;
    print F $to_write;
  }
}

END {
  if (defined $MKMK) {
    print $MKMK ": \$(srcdir)/auto/auto ;\n";
    #print $MKMK "\ttouch \$@\n";
  }
}

1;