aboutsummaryrefslogtreecommitdiff
path: root/final/runtime/tools/lib/Build.pm
blob: cf671569e0296dadc5c8cd23138640f871bfcfe3 (plain)
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
#
#//===----------------------------------------------------------------------===//
#//
#//                     The LLVM Compiler Infrastructure
#//
#// This file is dual licensed under the MIT and the University of Illinois Open
#// Source Licenses. See LICENSE.txt for details.
#//
#//===----------------------------------------------------------------------===//
#
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;