New URL for NEMO forge!   http://forge.nemo-ocean.eu

Since March 2022 along with NEMO 4.2 release, the code development moved to a self-hosted GitLab.
This present forge is now archived and remained online for history.
Compile.pm in vendors/lib/FCM/System/Make/Build/Task – NEMO

source: vendors/lib/FCM/System/Make/Build/Task/Compile.pm @ 10669

Last change on this file since 10669 was 10669, checked in by nicolasmartin, 5 years ago

Import latest FCM release from Github into the repository for testing

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