1 | #------------------------------------------------------------------------------- |
---|
2 | # (C) British Crown Copyright 2006-17 Met Office. |
---|
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::NS; |
---|
24 | use base qw{FCM::Class::CODE}; |
---|
25 | |
---|
26 | use FCM::Context::Make::Build; # for FCM::Context::Make::Build::Target |
---|
27 | use FCM::System::Make::Build::Task::Archive; |
---|
28 | use FCM::System::Make::Build::Task::Install; |
---|
29 | use File::Spec::Functions qw{catfile}; |
---|
30 | |
---|
31 | my $ID = '/'; |
---|
32 | |
---|
33 | my %TARGET_FILE_EXT_OF = (a => '.a', etc => '.etc'); |
---|
34 | |
---|
35 | my %TASK_CLASS_OF = ( |
---|
36 | 'archive' => 'FCM::System::Make::Build::Task::Archive', |
---|
37 | 'install' => 'FCM::System::Make::Build::Task::Install', |
---|
38 | ); |
---|
39 | |
---|
40 | # Creates the class. |
---|
41 | __PACKAGE__->class( |
---|
42 | { id => {isa => '$', default => $ID}, |
---|
43 | target_file_ext_of => {isa => '%', default => {%TARGET_FILE_EXT_OF}}, |
---|
44 | target_file_name_option_of => '%', |
---|
45 | task_class_of => {isa => '%', default => {%TASK_CLASS_OF}}, |
---|
46 | shared_util_of => '%', |
---|
47 | task_of => '%', |
---|
48 | util => '&', |
---|
49 | }, |
---|
50 | { init => \&_init, |
---|
51 | action_of => { |
---|
52 | (map {my $key = $_; ($key => sub {$_[0]->{$key}})} |
---|
53 | qw{id target_file_ext_of target_file_name_option_of task_of} |
---|
54 | ), |
---|
55 | ns_targets_deps => sub {('o')}, |
---|
56 | ns_targets => \&_ns_targets, |
---|
57 | }, |
---|
58 | }, |
---|
59 | ); |
---|
60 | |
---|
61 | # Initialises some attributes. |
---|
62 | sub _init { |
---|
63 | my ($attrib_ref) = @_; |
---|
64 | while (my ($key, $class) = each(%{$attrib_ref->{task_class_of}})) { |
---|
65 | $attrib_ref->{util}->class_load($class); |
---|
66 | $attrib_ref->{task_of}{$key} |
---|
67 | = $class->new({util => $attrib_ref->{util}}); |
---|
68 | } |
---|
69 | } |
---|
70 | |
---|
71 | # Returns a list of targets for a given build source. |
---|
72 | sub _ns_targets { |
---|
73 | my ($attrib_ref, $targets_ref, $prop_hash_ref) = @_; |
---|
74 | my %target_of; |
---|
75 | TARGET: |
---|
76 | for my $target (@{$targets_ref}) { |
---|
77 | my @ns_targets; |
---|
78 | for ( |
---|
79 | [sub {!$_[0]->get_type()} , \&_ns_target_new_etc], |
---|
80 | [sub {$_[0]->get_category() eq 'o'}, \&_ns_target_new_lib], |
---|
81 | ) { |
---|
82 | my ($test, $new) = @{$_}; |
---|
83 | if ($test->($target)) { |
---|
84 | my $ns_iter = $attrib_ref->{util}->ns_iter( |
---|
85 | $target->get_ns(), $attrib_ref->{util}->NS_ITER_UP, |
---|
86 | ); |
---|
87 | $ns_iter->(); # discard |
---|
88 | while (defined(my $ns = $ns_iter->())) { |
---|
89 | my $ns_target = $new->($ns, $prop_hash_ref); |
---|
90 | my $key = $ns_target->get_key(); |
---|
91 | if (!exists($target_of{$key})) { |
---|
92 | $target_of{$key} = $ns_target; |
---|
93 | } |
---|
94 | push( |
---|
95 | @{$target_of{$key}->get_deps()}, |
---|
96 | [$target->get_key(), $target->get_category()], |
---|
97 | ); |
---|
98 | } |
---|
99 | next TARGET; |
---|
100 | } |
---|
101 | } |
---|
102 | } |
---|
103 | values(%target_of); |
---|
104 | } |
---|
105 | |
---|
106 | # Returns a new etc target for building data files in a namespace. |
---|
107 | sub _ns_target_new_etc { |
---|
108 | my ($ns, $prop_hash_ref) = @_; |
---|
109 | my $DOT_ETC = $prop_hash_ref->{etc}; |
---|
110 | my $TARGET = 'FCM::Context::Make::Build::Target'; |
---|
111 | $TARGET->new( |
---|
112 | { category => $TARGET->CT_ETC, |
---|
113 | dep_policy_of => {'etc' => $TARGET->POLICY_CAPTURE}, |
---|
114 | key => ($ns ? catfile($ns, $DOT_ETC) : $DOT_ETC), |
---|
115 | ns => $ns, |
---|
116 | task => 'install', |
---|
117 | } |
---|
118 | ); |
---|
119 | } |
---|
120 | |
---|
121 | # Returns a new archive target for building an object library for a namespace. |
---|
122 | sub _ns_target_new_lib { |
---|
123 | my ($ns, $prop_hash_ref) = @_; |
---|
124 | my $NAME = 'libo' . $prop_hash_ref->{a}; # FIXME: libo hard-coded |
---|
125 | my $TARGET = 'FCM::Context::Make::Build::Target'; |
---|
126 | $TARGET->new( |
---|
127 | { category => $TARGET->CT_LIB, |
---|
128 | dep_policy_of => {'o' => $TARGET->POLICY_CAPTURE}, |
---|
129 | info_of => {paths => [], deps => {o => []}}, |
---|
130 | key => ($ns ? catfile($ns, $NAME) : $NAME), |
---|
131 | ns => $ns, |
---|
132 | task => 'archive', |
---|
133 | } |
---|
134 | ); |
---|
135 | } |
---|
136 | |
---|
137 | #------------------------------------------------------------------------------- |
---|
138 | 1; |
---|
139 | __END__ |
---|
140 | |
---|
141 | =head1 NAME |
---|
142 | |
---|
143 | FCM::System::Make::Build::FileType::NS |
---|
144 | |
---|
145 | =head1 SYNOPSIS |
---|
146 | |
---|
147 | use FCM::System::Make::Build::FileType::NS; |
---|
148 | my $file_type_util = FCM::System::Make::Build::FileType->new(\%attrib); |
---|
149 | $file_type_util->ns_targets($m_ctx, $ctx, @targets); |
---|
150 | |
---|
151 | =head1 DESCRIPTION |
---|
152 | |
---|
153 | Generates name space level targets. |
---|
154 | |
---|
155 | =head1 METHODS |
---|
156 | |
---|
157 | =over 4 |
---|
158 | |
---|
159 | =item $class->new(\%attrib) |
---|
160 | |
---|
161 | Creates and returns a new instance. |
---|
162 | |
---|
163 | =item $instance->id() |
---|
164 | |
---|
165 | Returns the recommended ID of this file type. |
---|
166 | |
---|
167 | =item $instance->ns_targets(\@targets,\%prop_of) |
---|
168 | |
---|
169 | Using the information in the original list of targets, creates and returns the |
---|
170 | contexts of a list of extra targets based on the name spaces of the original |
---|
171 | list. In the current settings, a target with no type (i.e. a data file target) |
---|
172 | will generate a C<.etc> target for the container name spaces; a target in the |
---|
173 | C<o> category will generate a C<libo.a> target for the container name spaces. |
---|
174 | |
---|
175 | =item $instance->ns_targets_deps() |
---|
176 | |
---|
177 | Returns a list of dependency types used by |
---|
178 | $instance->ns_targets(\@targets,\%prop_of). |
---|
179 | |
---|
180 | =item $instance->target_file_ext_of() |
---|
181 | |
---|
182 | Returns a HASH reference containing a map between the named types of file |
---|
183 | extensions used by the $instance->ns_targets(\@targets,\%prop_of) method |
---|
184 | and their default values. |
---|
185 | |
---|
186 | =item $instance->target_file_name_option_of() |
---|
187 | |
---|
188 | Returns a HASH reference containing a map between the named types of files |
---|
189 | used by the $instance->source_to_targets($source,\%prop_of) method |
---|
190 | and their default settings for other file naming options. |
---|
191 | |
---|
192 | =item $instance->task_of() |
---|
193 | |
---|
194 | Returns a HASH reference containing a map between the named tasks for this file |
---|
195 | type and their implementation objects. Each task should have a |
---|
196 | $task->main($target) method to update a target and optionally a $task->prop_of() |
---|
197 | method to return a HASH reference containing a map between the named properties |
---|
198 | used by the task and their default values. |
---|
199 | |
---|
200 | =back |
---|
201 | |
---|
202 | =head1 TODO |
---|
203 | |
---|
204 | The configuration in this module is a bit hard coded. It can do with a refactor. |
---|
205 | |
---|
206 | =head1 COPYRIGHT |
---|
207 | |
---|
208 | (C) Crown copyright Met Office. All rights reserved. |
---|
209 | |
---|
210 | =cut |
---|