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 | package FCM::System::Make::Build::Task::Preprocess; |
---|
23 | use base qw{FCM::Class::CODE}; |
---|
24 | |
---|
25 | use FCM::Context::Event; |
---|
26 | use FCM::System::Exception; |
---|
27 | use FCM::System::Make::Build::Task::Share qw{_props_to_opts}; |
---|
28 | use File::Spec::Functions qw{catfile}; |
---|
29 | use Text::ParseWords qw{shellwords}; |
---|
30 | |
---|
31 | use FCM::System::Exception; |
---|
32 | |
---|
33 | my $E = 'FCM::System::Exception'; |
---|
34 | |
---|
35 | __PACKAGE__->class( |
---|
36 | {name => '$', prop_of => '%', util => '&'}, |
---|
37 | {action_of => {main => \&_main, prop_of => sub {$_[0]->{prop_of}}}}, |
---|
38 | ); |
---|
39 | |
---|
40 | sub _main { |
---|
41 | my ($attrib_ref, $target) = @_; |
---|
42 | my $NAME = $attrib_ref->{name}; |
---|
43 | my $P = sub {$target->get_prop_of($_[0])}; |
---|
44 | my @paths = @{$target->get_info_of('paths')}; |
---|
45 | my @include_paths |
---|
46 | = map {catfile(($_ eq $paths[0] ? q{.} : $_), 'include')} @paths; |
---|
47 | my %opt_of = ( |
---|
48 | D => $P->($NAME . '.flag-define'), |
---|
49 | I => $P->($NAME . '.flag-include'), |
---|
50 | ); |
---|
51 | my @command = ( |
---|
52 | shellwords($P->($NAME)), |
---|
53 | shellwords($P->($NAME . '.flags')), |
---|
54 | _props_to_opts($opt_of{D}, shellwords($P->($NAME . '.defs'))), |
---|
55 | _props_to_opts($opt_of{I}, @include_paths), |
---|
56 | _props_to_opts($opt_of{I}, shellwords($P->($NAME . '.include-paths'))), |
---|
57 | $target->get_path_of_source(), |
---|
58 | ); |
---|
59 | my %value_of = %{$attrib_ref->{util}->shell_simple(\@command)}; |
---|
60 | if ($value_of{rc}) { |
---|
61 | return $E->throw( |
---|
62 | $E->SHELL, {command_list => \@command, %value_of}, $value_of{e}, |
---|
63 | ); |
---|
64 | } |
---|
65 | $attrib_ref->{util}->event( |
---|
66 | FCM::Context::Event->MAKE_BUILD_SHELL_OUT, undef, $value_of{e}, |
---|
67 | ); |
---|
68 | $value_of{o} ||= q{}; |
---|
69 | $attrib_ref->{util}->file_save($target->get_path(), $value_of{o}); |
---|
70 | $target; |
---|
71 | } |
---|
72 | |
---|
73 | # ------------------------------------------------------------------------------ |
---|
74 | 1; |
---|
75 | __END__ |
---|
76 | |
---|
77 | =head1 NAME |
---|
78 | |
---|
79 | FCM::System::Make::Build::Task::Preprocess |
---|
80 | |
---|
81 | =head1 SYNOPSIS |
---|
82 | |
---|
83 | use FCM::System::Make::Build::Task::Preprocess; |
---|
84 | my $build_task = FCM::System::Make::Build::Task::Preprocess->new(\%attrib); |
---|
85 | $build_task->main($target); |
---|
86 | |
---|
87 | =head1 DESCRIPTION |
---|
88 | |
---|
89 | Invokes the preprocessor on the target source to generate the target. |
---|
90 | |
---|
91 | =head1 METHODS |
---|
92 | |
---|
93 | =over 4 |
---|
94 | |
---|
95 | =item $class->new(\%attrib) |
---|
96 | |
---|
97 | Creates and returns a new instance. %attrib should contain: |
---|
98 | |
---|
99 | =over 4 |
---|
100 | |
---|
101 | =item {name} |
---|
102 | |
---|
103 | The property name of the preprocessor. |
---|
104 | |
---|
105 | =item {prop_of} |
---|
106 | |
---|
107 | A HASH to map the property names and their default values. |
---|
108 | |
---|
109 | =item {util} |
---|
110 | |
---|
111 | An instance of L<FCM::Util|FCM::Util>. |
---|
112 | |
---|
113 | =back |
---|
114 | |
---|
115 | =item $instance->main($target) |
---|
116 | |
---|
117 | Invokes the preprocessor shell command on the target source to generate the |
---|
118 | target. |
---|
119 | |
---|
120 | =item $instance->prop_of() |
---|
121 | |
---|
122 | Returns the HASH that maps the property names (used by this task) to their |
---|
123 | default values. |
---|
124 | |
---|
125 | =back |
---|
126 | |
---|
127 | =head1 COPYRIGHT |
---|
128 | |
---|
129 | (C) Crown copyright Met Office. All rights reserved. |
---|
130 | |
---|
131 | =cut |
---|