File: Build.pm

package info (click to toggle)
openmprtl 0.20140926-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 4,864 kB
  • ctags: 6,936
  • sloc: ansic: 32,194; cpp: 15,269; perl: 3,893; asm: 1,802; makefile: 38
file content (283 lines) | stat: -rw-r--r-- 9,571 bytes parent folder | download
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# <copyright>
#    Copyright (c) 2013-2014 Intel Corporation.  All Rights Reserved.
#
#    Redistribution and use in source and binary forms, with or without
#    modification, are permitted provided that the following conditions
#    are met:
#
#      * Redistributions of source code must retain the above copyright
#        notice, this list of conditions and the following disclaimer.
#      * Redistributions in binary form must reproduce the above copyright
#        notice, this list of conditions and the following disclaimer in the
#        documentation and/or other materials provided with the distribution.
#      * Neither the name of Intel Corporation nor the names of its
#        contributors may be used to endorse or promote products derived
#        from this software without specific prior written permission.
#
#    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
#    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
#    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
#    A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
#    HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#    SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
#    LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
#    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
#    THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
#    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
#    OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# </copyright>
package Build;

use strict;
use warnings;

use Cwd qw{};

use LibOMP;
use tools;
use Uname;
use Platform ":vars";

my $host = Uname::host_name();
my $root = $ENV{ LIBOMP_WORK    };
my $tmp  = $ENV{ LIBOMP_TMP     };
my $out  = $ENV{ LIBOMP_EXPORTS };

my @jobs;
our $start = time();

# --------------------------------------------------------------------------------------------------
# Helper functions.
# --------------------------------------------------------------------------------------------------

# tstr -- Time string. Returns string "yyyy-dd-mm hh:mm:ss UTC".
sub tstr(;$) {
    my ( $time ) = @_;
    if ( not defined( $time ) ) {
        $time = time();
    }; # if
    my ( $sec, $min, $hour, $day, $month, $year ) = gmtime( $time );
    $month += 1;
    $year  += 1900;
    my $str = sprintf( "%04d-%02d-%02d %02d:%02d:%02d UTC", $year, $month, $day, $hour, $min, $sec );
    return $str;
}; # sub tstr

# dstr -- Duration string. Returns string "hh:mm:ss".
sub dstr($) {
    # Get time in seconds and format it as time in hours, minutes, seconds.
    my ( $sec ) = @_;
    my ( $h, $m, $s );
    $h   = int( $sec / 3600 );
    $sec = $sec - $h * 3600;
    $m   = int( $sec / 60 );
    $sec = $sec - $m * 60;
    $s   = int( $sec );
    $sec = $sec - $s;
    return sprintf( "%02d:%02d:%02d", $h, $m, $s );
}; # sub dstr

# rstr -- Result string.
sub rstr($) {
    my ( $rc ) = @_;
    return ( $rc == 0 ? "+++ Success +++" : "--- Failure ---" );
}; # sub rstr

sub shorter($;$) {
    # Return shorter variant of path -- either absolute or relative.
    my ( $path, $base ) = @_;
    my $abs = abs_path( $path );
    my $rel = rel_path( $path, $base );
    if ( $rel eq "" ) {
        $rel = ".";
    }; # if
    $path = ( length( $rel ) < length( $abs ) ? $rel : $abs );
    if ( $target_os eq "win" ) {
        $path =~ s{\\}{/}g;
    }; # if
    return $path;
}; # sub shorter

sub tee($$) {

    my ( $action, $file ) = @_;
    my $pid = 0;

    my $save_stdout = Symbol::gensym();
    my $save_stderr = Symbol::gensym();

    # --- redirect stdout ---
    STDOUT->flush();
    # Save stdout in $save_stdout.
    open( $save_stdout, ">&" . STDOUT->fileno() )
        or die( "Cannot dup filehandle: $!; stopped" );
    # Redirect stdout to tee or to file.
    if ( $tools::verbose ) {
        $pid = open( STDOUT, "| tee -a \"$file\"" )
            or die "Cannot open pipe to \"tee\": $!; stopped";
    } else {
        open( STDOUT, ">>$file" )
            or die "Cannot open file \"$file\" for writing: $!; stopped";
    }; # if

    # --- redirect stderr ---
    STDERR->flush();
    # Save stderr in $save_stderr.
    open( $save_stderr, ">&" . STDERR->fileno() )
        or die( "Cannot dup filehandle: $!; stopped" );
    # Redirect stderr to stdout.
    open( STDERR, ">&" . STDOUT->fileno() )
        or die( "Cannot dup filehandle: $!; stopped" );

    # Perform actions.
    $action->();

    # --- restore stderr ---
    STDERR->flush();
    # Restore stderr from $save_stderr.
    open( STDERR, ">&" . $save_stderr->fileno() )
        or die( "Cannot dup filehandle: $!; stopped" );
    # Close $save_stderr.
    $save_stderr->close() or die ( "Cannot close filehandle: $!; stopped" );

    # --- restore stdout ---
    STDOUT->flush();
    # Restore stdout from $save_stdout.
    open( STDOUT, ">&" . $save_stdout->fileno() )
        or die( "Cannot dup filehandle: $!; stopped" );
    # Close $save_stdout.
    $save_stdout->close() or die ( "Cannot close filehandle: $!; stopped" );

    # Wait for the child tee process, otherwise output of make and build.pl interleaves.
    if ( $pid != 0 ) {
        waitpid( $pid, 0 );
    }; # if

}; # sub tee

sub log_it($$@) {
    my ( $title, $format, @args ) = @_;
    my $message  = sprintf( $format, @args );
    my $progress = cat_file( $tmp, sprintf( "%s-%s.log", $target_platform, Uname::host_name() ) );
    if ( $title ne "" and $message ne "" ) {
        my $line = sprintf( "%-15s : %s\n", $title, $message );
        info( $line );
        write_file( $progress, tstr() . ": " . $line, -append => 1 );
    } else {
        write_file( $progress, "\n", -append => 1 );
    }; # if
}; # sub log_it

sub progress($$@) {
    my ( $title, $format, @args ) = @_;
    log_it( $title, $format, @args );
}; # sub progress

sub summary() {
    my $total   = @jobs;
    my $success = 0;
    my $finish = time();
    foreach my $job ( @jobs ) {
        my ( $build_dir, $rc ) = ( $job->{ build_dir }, $job->{ rc } );
        progress( rstr( $rc ), "%s", $build_dir );
        if ( $rc == 0 ) {
            ++ $success;
        }; # if
    }; # foreach $job
    my $failure = $total - $success;
    progress( "Successes",      "%3d of %3d", $success, $total );
    progress( "Failures",       "%3d of %3d", $failure, $total );
    progress( "Time elapsed",   "  %s", dstr( $finish - $start ) );
    progress( "Overall result", "%s", rstr( $failure ) );
    return $failure;
}; # sub summary

# --------------------------------------------------------------------------------------------------
# Worker functions.
# --------------------------------------------------------------------------------------------------

sub init() {
    make_dir( $tmp );
}; # sub init

sub clean(@) {
    # Clean directories.
    my ( @dirs ) = @_;
    my $exit = 0;
    # Mimisc makefile -- print a command.
    print( "rm -f -r " . join( " ", map( shorter( $_ ) . "/*", @dirs ) ) . "\n" );
    $exit =
        execute(
            [ $^X, cat_file( $ENV{ LIBOMP_WORK }, "tools", "clean-dir.pl" ), @dirs ],
            -ignore_status => 1,
            ( $tools::verbose ? () : ( -stdout => undef, -stderr => "" ) ),
        );
    return $exit;
}; # sub clean

sub make($$$) {
    # Change dir to build one and run make.
    my ( $job, $clean, $marker ) = @_;
    my $dir      = $job->{ build_dir };
    my $makefile = $job->{ makefile };
    my $args     = $job->{ make_args };
    my $cwd      = Cwd::cwd();
    my $width    = -10;

    my $exit;
    $dir = cat_dir( $tmp, $dir );
    make_dir( $dir );
    change_dir( $dir );

    my $actions =
        sub {
            my $start = time();
            $makefile = shorter( $makefile );
            print( "-" x 79, "\n" );
            printf( "%${width}s: %s\n", "Started",   tstr( $start ) );
            printf( "%${width}s: %s\n", "Root dir",  $root );
            printf( "%${width}s: %s\n", "Build dir", shorter( $dir, $root ) );
            printf( "%${width}s: %s\n", "Makefile",  $makefile );
            print( "-" x 79, "\n" );
            {
                # Use shorter LIBOMP_WORK to have shorter command lines.
                # Note: Some tools may not work if current dir is changed.
                local $ENV{ LIBOMP_WORK } = shorter( $ENV{ LIBOMP_WORK } );
                $exit =
                    execute(
                        [
                            "make",
                            "-r",
                            "-f", $makefile,
                            "arch=" . $target_arch,
                            "marker=$marker",
                            @$args
                        ],
                        -ignore_status => 1
                    );
                if ( $clean and $exit == 0 ) {
                    $exit = clean( $dir );
                }; # if
            }
            my $finish = time();
            print( "-" x 79, "\n" );
            printf( "%${width}s: %s\n", "Finished", tstr( $finish ) );
            printf( "%${width}s: %s\n", "Elapsed", dstr( $finish - $start ) );
            printf( "%${width}s: %s\n", "Result", rstr( $exit ) );
            print( "-" x 79, "\n" );
            print( "\n" );
        }; # sub
    tee( $actions, "build.log" );

    change_dir( $cwd );

    # Save completed job to be able print summary later.
    $job->{ rc } = $exit;
    push( @jobs, $job );

    return $exit;

}; # sub make

1;