1 | # ------------------------------------------------------------------------------ |
---|
2 | # Copyright (C) 2006-2021 British Crown (Met Office) & Contributors. |
---|
3 | # |
---|
4 | # This file is part of FCM, tools for managing and building source code. |
---|
5 | # |
---|
6 | # FCM is free software: you can redistribute it and/or modify |
---|
7 | # it under the terms of the GNU General Public License as published by |
---|
8 | # the Free Software Foundation, either version 3 of the License, or |
---|
9 | # (at your option) any later version. |
---|
10 | # |
---|
11 | # FCM is distributed in the hope that it will be useful, |
---|
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | # GNU General Public License for more details. |
---|
15 | # |
---|
16 | # You should have received a copy of the GNU General Public License |
---|
17 | # along with FCM. If not, see <http://www.gnu.org/licenses/>. |
---|
18 | # ------------------------------------------------------------------------------ |
---|
19 | use strict; |
---|
20 | use warnings; |
---|
21 | |
---|
22 | # ------------------------------------------------------------------------------ |
---|
23 | package FCM::System::Make::Build::FileType; |
---|
24 | use base qw{FCM::Class::CODE}; |
---|
25 | |
---|
26 | use Text::ParseWords qw{shellwords}; |
---|
27 | |
---|
28 | # Creates the class. |
---|
29 | __PACKAGE__->class( |
---|
30 | { id => '$', |
---|
31 | file_ext => '$', |
---|
32 | file_pat => '$', |
---|
33 | file_she => '$', |
---|
34 | source_analyse_always => '$', |
---|
35 | source_analyse_dep_of => '%', |
---|
36 | source_analyse_more => '&', |
---|
37 | source_analyse_more_init => '&', |
---|
38 | source_to_targets => '&', |
---|
39 | target_deps_filter => '&', |
---|
40 | target_file_ext_of => '%', |
---|
41 | target_file_name_option_of => '%', |
---|
42 | task_class_of => '%', |
---|
43 | task_of => '%', |
---|
44 | util => '&', |
---|
45 | }, |
---|
46 | { init => \&_init, |
---|
47 | action_of => { |
---|
48 | (map {my $key = $_; ($key => sub {$_[0]->{$key}})} |
---|
49 | qw{ |
---|
50 | id |
---|
51 | file_ext |
---|
52 | file_pat |
---|
53 | file_she |
---|
54 | source_analyse_always |
---|
55 | target_file_ext_of |
---|
56 | target_file_name_option_of |
---|
57 | task_of |
---|
58 | } |
---|
59 | ), |
---|
60 | source_analyse => \&_source_analyse, |
---|
61 | source_analyse_deps => sub {keys(%{$_[0]->{source_analyse_dep_of}})}, |
---|
62 | source_to_targets => sub {$_[0]->{source_to_targets}->(@_)}, |
---|
63 | target_deps_filter => sub {$_[0]->{target_deps_filter}->(@_)}, |
---|
64 | }, |
---|
65 | }, |
---|
66 | ); |
---|
67 | |
---|
68 | # Initialises some attributes. |
---|
69 | sub _init { |
---|
70 | my ($attrib_ref) = @_; |
---|
71 | while (my ($key, $class) = each(%{$attrib_ref->{task_class_of}})) { |
---|
72 | $attrib_ref->{util}->class_load($class); |
---|
73 | $attrib_ref->{task_of}{$key} |
---|
74 | = $class->new({util => $attrib_ref->{util}}); |
---|
75 | } |
---|
76 | } |
---|
77 | |
---|
78 | # Reads information according to the $source. |
---|
79 | sub _source_analyse { |
---|
80 | my ($attrib_ref, $source) = @_; |
---|
81 | my %no_dep_of; |
---|
82 | my %dep_type_of |
---|
83 | = map {($_ => 1)} keys(%{$attrib_ref->{source_analyse_dep_of}}); |
---|
84 | while (my $type = each(%dep_type_of)) { |
---|
85 | my $key = 'no-dep.' . $type; |
---|
86 | if ($source->get_prop_of($key)) { |
---|
87 | for my $v (shellwords($source->get_prop_of($key))) { |
---|
88 | if ($v eq '*') { |
---|
89 | delete($dep_type_of{$type}); |
---|
90 | } |
---|
91 | else { |
---|
92 | $no_dep_of{$type}{$v} = 1; |
---|
93 | } |
---|
94 | } |
---|
95 | } |
---|
96 | } |
---|
97 | if (!keys(%dep_type_of) && !$attrib_ref->{source_analyse_always}) { |
---|
98 | return; |
---|
99 | } |
---|
100 | my $path = $source->get_path(); |
---|
101 | my $handle = $attrib_ref->{util}->file_load_handle($path); |
---|
102 | |
---|
103 | my @dep_types = keys(%dep_type_of) |
---|
104 | ? keys(%dep_type_of) : (_source_analyse_deps($attrib_ref)); |
---|
105 | my (%dep_of, %info_of, %state); |
---|
106 | $attrib_ref->{source_analyse_more_init}->(\%info_of, \%state); |
---|
107 | LINE: |
---|
108 | while (my $line = readline($handle)) { |
---|
109 | chomp($line); |
---|
110 | TYPE: |
---|
111 | for my $type (@dep_types) { |
---|
112 | my ($item, $can_analyse_more) |
---|
113 | = $attrib_ref->{source_analyse_dep_of}{$type}->($line); |
---|
114 | if ($item) { |
---|
115 | $dep_of{$type}{$item} = 1; |
---|
116 | if ($can_analyse_more) { |
---|
117 | last TYPE; |
---|
118 | } |
---|
119 | else { |
---|
120 | next LINE; |
---|
121 | } |
---|
122 | } |
---|
123 | } |
---|
124 | $attrib_ref->{source_analyse_more}->($line, \%info_of, \%state); |
---|
125 | } |
---|
126 | |
---|
127 | close($handle); |
---|
128 | $source->set_info_of(\%info_of); |
---|
129 | while (my ($type, $hash_ref) = each(%dep_of)) { |
---|
130 | while (my $item = each(%{$hash_ref})) { |
---|
131 | if (!exists($no_dep_of{$type}{$item})) { |
---|
132 | push(@{$source->get_deps()}, [$item, $type]); |
---|
133 | } |
---|
134 | } |
---|
135 | } |
---|
136 | } |
---|
137 | |
---|
138 | 1; |
---|
139 | __END__ |
---|
140 | |
---|
141 | =head1 NAME |
---|
142 | |
---|
143 | FCM::System::Make::Build::FileType |
---|
144 | |
---|
145 | =head1 SYNOPSIS |
---|
146 | |
---|
147 | use FCM::System::Make::Build::FileType; |
---|
148 | my $file_type_util = FCM::System::Make::Build::FileType->new(\%attrib); |
---|
149 | $file_type_util->source_analyse($handle); |
---|
150 | |
---|
151 | =head1 DESCRIPTION |
---|
152 | |
---|
153 | An abstract class to implement the shared methods for gathering information to |
---|
154 | build different types of source files. |
---|
155 | |
---|
156 | =head1 METHODS |
---|
157 | |
---|
158 | =over 4 |
---|
159 | |
---|
160 | =item $class->new(\%attrib) |
---|
161 | |
---|
162 | Creates and returns a new instance. |
---|
163 | |
---|
164 | =item $instance->id() |
---|
165 | |
---|
166 | Returns the recommended ID of this file type. |
---|
167 | |
---|
168 | =item $instance->file_ext() |
---|
169 | =item $instance->file_pat() |
---|
170 | =item $instance->file_she() |
---|
171 | |
---|
172 | Returns the recommended file name extension, file name pattern and file she-bang |
---|
173 | line pattern of this file type. |
---|
174 | |
---|
175 | =item $instance->source_analyse($source) |
---|
176 | |
---|
177 | Analysis $source for dependencies and other information. Add or modify items in |
---|
178 | @{$source->get_deps()} and %{$source->get_info_of()}. |
---|
179 | |
---|
180 | =item $instance->source_analyse_deps() |
---|
181 | |
---|
182 | Returns a list containing the possible dependency types. |
---|
183 | |
---|
184 | =item $instance->source_analyse_always() |
---|
185 | |
---|
186 | Returns true if $instance->source_analyse($handle,\@dep_types) can read |
---|
187 | information other than dependencies. |
---|
188 | |
---|
189 | =item $instance->source_to_targets($source,\%prop_of) |
---|
190 | |
---|
191 | Using the information in $source, creates and returns the contexts of a list of |
---|
192 | suitable build targets. Where appropriate, the %prop_of should contain a mapping |
---|
193 | of the names of the properties used by this method and their values. |
---|
194 | |
---|
195 | =item $instance->target_deps_filter($target) |
---|
196 | |
---|
197 | This may modify @{$target->get_deps()} in place based on values in |
---|
198 | %{$target->get_prop_of()}. This method is normally implemented by sub-classes. |
---|
199 | |
---|
200 | =item $instance->target_file_ext_of() |
---|
201 | |
---|
202 | Returns a HASH reference containing a map between the named types of file |
---|
203 | extensions used by the $instance->source_to_targets($source,\%prop_of) method |
---|
204 | and their default values. |
---|
205 | |
---|
206 | =item $instance->target_file_name_option_of() |
---|
207 | |
---|
208 | Returns a HASH reference containing a map between the named types of files |
---|
209 | used by the $instance->source_to_targets($source,\%prop_of) method |
---|
210 | and their default settings for other file naming options. |
---|
211 | |
---|
212 | =item $instance->task_of() |
---|
213 | |
---|
214 | Returns a HASH reference containing a map between the named tasks for this file |
---|
215 | type and their implementation objects. Each task should have a |
---|
216 | $task->main($target) method to update a target and optionally a $task->prop_of() |
---|
217 | method to return a HASH reference containing a map between the named properties |
---|
218 | used by the task and their default values. |
---|
219 | |
---|
220 | =back |
---|
221 | |
---|
222 | =head1 COPYRIGHT |
---|
223 | |
---|
224 | Copyright (C) 2006-2021 British Crown (Met Office) & Contributors. |
---|
225 | |
---|
226 | =cut |
---|