summaryrefslogtreecommitdiffstats
path: root/src/build/tools/builddriver
blob: c476f6cb4e1fd05cebbaccab7fefe5ead865c967 (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
#!/usr/bin/perl
# IBM_PROLOG_BEGIN_TAG
# This is an automatically generated prolog.
#
# $Source: src/build/tools/builddriver $
#
# OpenPOWER HostBoot Project
#
# COPYRIGHT International Business Machines Corp. 2011,2014
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied. See the License for the specific language governing
# permissions and limitations under the License.
#
# IBM_PROLOG_END_TAG

# This script builds and publishes drivers.
# It does:
# 1) creates a git repository
# 2) checks out from the powerhal repository
# 3) builds
# 4) tags the driver
#
# This assumes:
# 1) you have current GSA credentials
# 2) you are in powerhal/writers GSA group
# 3) git is in your path
# 4) you are running this on gfwr801 or other suitable build machine

use strict;
use Getopt::Long;
use Cwd;

my $POWERHAL_HOME = "/gsa/rchgsa/projects/p/powerhal";
my $POWERHAL_REPO = $POWERHAL_HOME."/git";
my $DEFAULT_DRIVERS_HOME = $POWERHAL_HOME."/drivers";
my $DEFAULT_PUBLISH_HOME = $POWERHAL_HOME."/builds";
my $BUILD_SCRIPT = "src/build/tools/build";

my $FIRST_SPIN_NAME = "a";
my $DEFAULT_RELEASE ="110";
my $DEFAULT_BRANCH = "devel";


# Constructs a build name in the format bMMDDx_YYWW.rrr
# Example: b0126a_1104.110
sub constructBuildName
{
    my ($spin, $release) = @_;

    # TODO Rewrite this to avoid the date call and build it in perl?
    my $dateCmd = "/bin/date +b%m%d".$spin."_%y%W.".$release;

    my $buildName = `$dateCmd`;
    chomp $buildName;

    return $buildName;
}

# Return the build name to use
# This looks in the $driverHome for existing spins on this date and updates the spin name accordingly
sub getBuildName
{
    my ($driverHome, $release) = @_;

    my $driverNamePattern = constructBuildName(".", $release);
    my $dir;
    opendir ($dir, $driverHome) or die "Cannot open $driverHome.";
    my @matches = sort grep { /^$driverNamePattern/ } readdir ($dir);
    closedir ($dir);

    my $buildName;
    if (scalar(@matches) > 0)
    {
        print "Existing spins for this build date and release:\n";
        foreach (@matches)
        {
            print "\t".$_."\n";
        }

        my $lastSpin = $matches[-1];
        print "Last spin was $lastSpin\n";
        $lastSpin =~ /^b\d\d\d\d([a-z])/ or die "Found driver matches, but spin name match failed.";
        $lastSpin = $1;
        die "Too many spins today!!!" if ($lastSpin eq "z");
        $lastSpin++;
        $buildName = constructBuildName($lastSpin, $release);
    }
    else
    {
        print "First spin for this build date and release.\n";
        $buildName = constructBuildName($FIRST_SPIN_NAME, $release);
    }

    print "This build will be: $buildName\n";

    return $buildName;
}

# Creates a directory with the $dir name 
# Creates a git repository
# Adds $remoteRepository as the remote repository
# Checks out $branch
# Creates a tag $tag
# Pushes the tag to the remote repository
sub createRepo
{
    my ($dir, $remoteRepository, $branch, $tag) = @_;
    die "createRepo: dir is required" unless (defined $dir);
    die "createRepo: remoteRepository is required" unless (defined $remoteRepository);
    die "createRepo: branch is required" unless (defined $branch);
    die "createRepo: tag is required" unless (defined $tag);

    mkdir $dir or die "Cannot mkdir $dir";
    chdir $dir or die "Cannot chdir to $dir";

    my $cmd = "git init";
    system "$cmd";
    my $rc = $?;
    die "Failed running '$cmd' with rc $rc" unless ($rc == 0);

    $cmd = "git remote add origin ".$remoteRepository;
    system "$cmd";
    $rc = $?;
    die "Failed running '$cmd' with rc $rc" unless ($rc == 0);

    $cmd = "git fetch origin";
    system "$cmd";
    $rc = $?;
    die "Failed running '$cmd' with rc $rc" unless ($rc == 0);

    $cmd = "git checkout -b ".$branch." origin/".$branch;
    system "$cmd";
    $rc = $?;
    die "Failed running '$cmd' with rc $rc" unless ($rc == 0);

    $cmd = "git tag -a -m \"Build ".$tag."\" ".$tag;
    system "$cmd";
    $rc = $?;
    die "Failed running '$cmd' with rc $rc" unless ($rc == 0);

    $cmd = "git push --tags origin";
    system "$cmd";
    $rc = $?;
    die "Failed running '$cmd' with rc $rc" unless ($rc == 0);
}

# Builds the driver
# Assumes the current working directory is the root of the git repository
sub build
{
    die "Cannot find build script \"$BUILD_SCRIPT\"" unless (-e $BUILD_SCRIPT);

    system "$BUILD_SCRIPT | /usr/bin/tee build.log";
    my $rc = $?;
    print "Compile output in ".cwd()."/build.log\n";
    die "Compile failed with rc $rc" unless ($rc == 0);
}

sub publish
{
    my ($dir) = @_;
    die "publish: dir is required" unless (defined $dir);

    mkdir $dir or die "Cannot mkdir $dir";
    
    system "cp -p img/*.lid img/*.ruhx img/*.lidhdr $dir";
    my $rc = $?;
    die "Could not copy lids to $dir" unless ($rc == 0);

    mkdir $dir."/include" or die "Cannot mkdir include";
    mkdir $dir."/include/hal" or die "Cannot mkdir include/hal";
    system "cp -p src/include/hal/hal.h obj/doxygen/html/hal_8h.html obj/doxygen/html/*.css $dir"."/include/hal/";
    $rc = $?;
    die "Could not copy hal.h and doxygen files" unless ($rc == 0);
}


# main
my $startTime = time();
my $currentTime = localtime();
print "Build starting at $currentTime.\n";

my $driversHome = $DEFAULT_DRIVERS_HOME;
my $release = $DEFAULT_RELEASE;
my $remoteRepository = $POWERHAL_REPO;
my $branch = $DEFAULT_BRANCH;
my $tag; # Default tag is the build name
my $publishHome = $DEFAULT_PUBLISH_HOME;

GetOptions("home=s"     => \$driversHome,
           "release=s"  => \$release,
           "remote=s"   => \$remoteRepository,
           "branch=s"   => \$branch,
           "tag=s"      => \$tag,
           "publishhome=s" => \$publishHome);

die "Unsupported parameters: @ARGV" unless (scalar(@ARGV) == 0);

print "Drivers home = $driversHome\n";
print "Release = $release\n";
print "Remote repository = $remoteRepository\n";
print "Branch = $branch\n";

my $buildName = getBuildName($driversHome, $release);
print "Build name = $buildName\n";

$tag = (defined $tag) ? $tag : $buildName;
print "Tag = $tag\n";
print "Publish home = $publishHome\n";


# This changes the current working directory to the new repository
createRepo($driversHome."/".$buildName, $remoteRepository, $branch, $tag);
build();
publish($publishHome."/".$buildName);

$currentTime = localtime();
print "Build $buildName completed successfully at $currentTime.\n";
print "Duration: ".(time() - $startTime)." seconds\n";

OpenPOWER on IntegriCloud