Bootstrap 12.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
#!/usr/bin/perl -w
#
# Bootstrap
#
# Script to export a given BK source tree into a separate directory
# and create the source distribution to be used for all binary builds
#
# Use the "--help" option for more info!
#
# written by Lenz Grimmer <lenz@mysql.com>
#

13
use Cwd;
14 15 16
use Getopt::Long;
Getopt::Long::Configure ("bundling");

17
# Include helper functions
18 19 20 21 22 23 24 25 26 27 28
$LOGGER= "$ENV{HOME}/bin/logger.pm";
if (-f $LOGGER)
{
	do "$LOGGER";
}
else
{
	die "ERROR: $LOGGER cannot be found!\n";
}

# Some predefined settings
29
$build_command= "BUILD/compile-dist";
30 31
$PWD= cwd();
$opt_docdir= $PWD . "/mysqldoc";
unknown's avatar
unknown committed
32
$opt_archive_log= undef;
33
$opt_build_command= undef;
34 35
$opt_changelog= undef;
$opt_delete= undef;
36
$opt_directory= $PWD;
37 38 39 40
$opt_dry_run= undef;
$opt_export_only= undef;
$opt_help= $opt_verbose= 0;
$opt_log= undef;
unknown's avatar
unknown committed
41
$opt_mail= "build\@mysql.com";
42
$opt_pull= undef;
unknown's avatar
unknown committed
43
$opt_revision= undef;
44 45 46 47
$opt_suffix= "";
$opt_test= undef;
$opt_skip_check= undef;
$opt_skip_manual= undef;
48
$opt_win_dist= undef;
unknown's avatar
unknown committed
49
$opt_quiet= undef;
50
$version= "unknown";
51
$major=$minor=$release=0;
52 53

GetOptions(
unknown's avatar
unknown committed
54
  "archive-log|a",
55
	"build-command|b=s",
56 57 58 59 60 61 62 63 64
	"changelog|c:s",
	"directory|d=s",
	"delete",
	"docdir=s",
	"dry-run",
	"export-only|e",
	"help|h",
	"log|l:s",
	"mail|m=s",
65
	"pull|p",
66 67 68 69 70
	"revision|r=s",
	"skip-check|s",
	"skip-manual",
	"suffix=s",
	"test|t",
71
	"verbose|v",
unknown's avatar
unknown committed
72
	"win-dist|w",
73
	"quiet|q",
74 75
) || print_help("");

unknown's avatar
unknown committed
76 77 78 79 80 81 82 83 84 85 86
#
# Override predefined build command
#
if (defined $opt_build_command)
{
	$build_command= $opt_build_command;
}

print_help("") if ($opt_help);
defined($REPO=$ARGV[0]) || print_help("Please enter the BK repository to be used!");

87 88 89 90 91 92 93 94 95
#
# Override predefined Log file name
#
if (defined $opt_log)
{
	if ($opt_log ne "")
	{
		if ($opt_log =~ /^\/.*/)
		{
96
			$LOGFILE= $opt_log;
97 98 99
		}
		else
		{
100
			$LOGFILE= $PWD . "/" . $opt_log;
101 102 103
		}
	}
}
104 105

$LOGFILE= $PWD . "/Bootstrap-" . $REPO . ".log" unless ($LOGFILE);
106

107 108 109 110 111 112
&logger("Starting build");
&abort("The directory \"$REPO\" could not be found!") if (!-d $REPO);
&logger("Using $REPO as the BK parent repository");
system ("bk help > /dev/null") == 0 or &abort("Cannot execute BitKeeper binary!");
system ("bk root $REPO > /dev/null 2>&1") == 0 or &abort("$REPO does not seem to be a valid BK repository!");

113
if (($opt_directory ne $PWD) && (!-d $opt_directory && !$opt_dry_run))
114 115 116 117
{
	&abort("Could not find target directory \"$opt_directory\"!");
}

118
&logger("Logging to $LOGFILE") if (defined $opt_log);
119

120 121 122 123 124
#
# Pull recent changes first
#
if ($opt_pull)
{
125 126
	&bk_pull("$REPO");
	&bk_pull("$opt_docdir") unless ($opt_skip_manual);
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
#
# Use a temporary name until we know the version number
#
$target_dir= $opt_directory . "/mysql-" . $$ . "-" . time() . ".tmp";
&logger("Using temporary directory $target_dir");
&abort("Target directory $target_dir already exists!") if (-d $target_dir && !$opt_dry_run);

#
# Export the BK tree
#
$command= "bk export ";
$command.= "-r " . $opt_revision . " " if $opt_revision;
$command.= "-v " if ($opt_verbose || defined $opt_log);
$command.= $REPO . " " . $target_dir;
&logger("Exporting $REPO");
&run_command($command, "Could not create $target_dir!");

#
# Make sure we can write all files
#
$command= "find $target_dir -type f -print0 | xargs --null chmod u+w";
&run_command($command, "Failed to fix file permissions!");

#
# Try to obtain version number from newly extracted configure.in
#
$CONF="$target_dir/configure.in";
&abort("Could not find \"$CONF\" to determine version!") if (!-f $CONF && !$opt_dry_run);

#
# The following can only be done, if the tree has actually been
# exported - it cannot be performed in a dry run.
#
if (!$opt_dry_run)
{
	open (CONF, $CONF) or &abort("Unable to open \"$CONF\": $!");
	@conf= <CONF>;
	close CONF;

	foreach (@conf)
	{
		m/^AM_INIT_AUTOMAKE\(mysql, ([1-9]\.[0-9]{1,2}\.[0-9]{1,2}.*)\)/;
    $version= $1;
172
		($major, $minor, $release) = split(/\./,$version);
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
	}
	&logger("Found version string: $version");

	#
	# Add suffix to version string and write out the modified file
	#
	if ($opt_suffix)
	{
		$opt_suffix= "-" . &ymd() if ($opt_suffix eq "YMD");

		&logger("Replacing $version with $version$opt_suffix");
		foreach (@conf)
		{
				s/^AM_INIT_AUTOMAKE.*/AM_INIT_AUTOMAKE\(mysql, $version$opt_suffix\)/;
		}
		open(CONF,">$CONF") or &abort("Unable to open \"$CONF\": $!");
		print CONF @conf;
		close(CONF);
	}
}

#
# Rename directory according to the version number found in configure.in
# of the extracted tree (plus suffix, if requested)
#
$temp_name= $target_dir;
$target_dir= $opt_directory . "/mysql-" . $version . $opt_suffix . "-build";
if (-d $target_dir)
{
	&logger("Target directory $target_dir already exists!");
	if ($opt_delete)
	{
		&logger("Deleting $target_dir...");
		$command= "rm ";
		$command.= "-v " if ($opt_verbose || defined $opt_log);
		$command.= "$target_dir";
		&run_command($command, "Could not delete $target_dir!");
	}
	else
	{
213 214 215 216
		# Get the time stamp of "configure.in"
		@stat= stat("$target_dir/configure.in");
		my $mtime= $stat[9];
		my ($sec,$min,$hour,$mday,$mon,$year) = localtime($mtime);
unknown's avatar
unknown committed
217
		my $mtime= sprintf("%04d-%02d-%02d-%02d:%02d", $year+1900, $mon+1, $mday, $hour, $min);
218 219

		&logger("Renaming $target_dir to $target_dir-$mtime");
220 221
		$command= "mv ";
		$command.= "-v " if ($opt_verbose || defined $opt_log);
222
		$command.= "$target_dir $target_dir-$mtime";
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
		&run_command($command, "Could not rename $target_dir!");
	}
}

&logger("Renaming temporary directory to $target_dir");
$command= "mv ";
$command.= "-v " if ($opt_verbose || defined $opt_log);
$command.= "$temp_name $target_dir";
&run_command($command, "Could not rename $temp_name!");

#
# Add a ChangeLog (make dist will pick it up automatically)
#
if (defined $opt_changelog)
{
	#
	# Use some magic to obtain the correct ChangeSet number that identifies
	# the last tagged ChangeSet (this relies heavily on our current tagging
	# practice!)
	#
	my $revision= "";
	if ($opt_changelog eq "last")
	{
		if (!$opt_revision)
		{
unknown's avatar
unknown committed
248
			$revision= `bk changes -t -d':REV:::TAG:' -n $REPO | grep mysql-$major.$minor | head -1 | cut -f1 -d ":"`;
249 250 251 252 253 254 255 256 257 258 259 260
		}
		else
		{
			$revision= `bk changes -r..$opt_revision -t -d':REV:' -n $REPO | head -2 | tail -1`;
		}
		chomp($revision);
		$opt_changelog= $revision;
	}

	$msg= "Adding $target_dir/ChangeLog";
	$msg.= " (down to revision $opt_changelog)" if $opt_changelog ne "";
	&logger($msg);
261
	$command= "bk changes -v";
262 263 264 265 266 267 268
	$command.= " -r" if ($opt_changelog ne "" || $opt_revision);
	$command.= $opt_changelog if $opt_changelog ne "";
	$command.= ".." if ($opt_changelog ne "" && !$opt_revision);
	$command.= ".." . $opt_revision if $opt_revision;
	$command.= " " . $REPO . " > $target_dir/ChangeLog";
	&logger($command);
	# We cannot use run_command here because of output redirection
269
	unless ($opt_dry_run)
270 271 272 273 274 275 276 277
	{
		system($command) == 0 or &abort("Could not create $target_dir/ChangeLog!");
	}
}

#
# Add the latest manual from the mysqldoc tree
#
278
unless ($opt_skip_manual)
279
{
280
	&logger("Updating manual files");
281 282
	foreach $file qw/internals manual reservedwords/
	{
283 284
		system ("bk cat $opt_docdir/Docs/$file.texi > $target_dir/Docs/$file.texi") == 0
		or &abort("Could not update $file.texi in $target_dir/Docs/!");
285
	}
286 287 288 289 290

	&run_command("rm -f $target_dir/Docs/Images/Makefile*",
 	  "Could not remove Makefiles in $target_dir/Docs/Images/!");
	&run_command("cp $opt_docdir/Docs/Images/*.* $target_dir/Docs/Images",
	  "Could not copy image files in $target_dir/Docs/Images/!");
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
}

#
# Abort here, if we just wanted to export the tree
#
if ($opt_export_only)
{
	&logger("SUCCESS: Export finished successfully.");
	exit 0;
}

#
# Enter the target directory first
#
&logger("Entering $target_dir");
if (!$opt_dry_run)
{
	chdir($target_dir) or &abort("Cannot chdir to $target_dir: $!");
}

#
# Now build the source distribution
#
unknown's avatar
unknown committed
314
&logger("Compiling...");
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
$command= $build_command;
&run_command($command, "Compilation failed!");

#
# Testing the built binary by running "make test" (optional)
#
if ($opt_test)
{
	&logger ("Running test suite");
	$command= "make test";
	&run_command($command, "\"make test\" failed!");
}

#
# Pack it all up
#
&logger("Creating source distribution");
$command= "make dist";
&run_command($command, "make dist failed!");

#
336 337 338 339 340
# Package the Windows source
#
if ($opt_win_dist)
{
	&logger ("Creating Windows source package");
unknown's avatar
unknown committed
341
	$command= "./scripts/make_win_src_distribution --tar --zip";
342 343 344 345 346
	&run_command($command, "make_win_src_distribution failed!");
}

#
# Run "make distcheck" to verify the source archive
347 348 349 350 351 352 353 354 355 356 357 358
#
if (!$opt_skip_check)
{
	&logger ("Checking source distribution");
	$command= "make distcheck";
	&run_command($command, "make distcheck failed!");
}

#
# All done when we came down here
#
&logger("SUCCESS: Build finished successfully.") if (!$opt_dry_run);
unknown's avatar
unknown committed
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373

#
# Move the log file into the Log dir of the target dir
#
if ($opt_archive_log)
{
  my $logdir= $target_dir . "/Logs";
  &logger("Moving $LOGFILE to $logdir");
  mkdir "$logdir" if (! -d $logdir);
  $command= "mv ";
  $command.= "-v " if ($opt_verbose || defined $opt_log);
  $command.= "$LOGFILE $logdir";
  &run_command($command, "Could not move $LOGFILE to $logdir!");
}

374 375
exit 0;

376 377 378 379 380 381 382 383 384 385 386 387
#
# Run a BK pull on the given BK tree
#
sub bk_pull
{
  my $bk_tree= $_[0];
	&logger("Updating BK tree $bk_tree to latest ChangeSet first");
  chdir ($bk_tree) or &abort("Could not chdir to $bk_tree!");
	&run_command("bk pull", "Could not update $bk_tree!");
  chdir ($PWD) or &abort("Could not chdir to $PWD!");
}

388 389 390 391 392 393 394 395 396 397 398 399
#
# Print the help text message (with an optional message on top)
#
sub print_help
{
	my $message= $_[0];
	if ($message ne "")
	{
		print "\n";
		print "ERROR: $message\n";
	}
	print <<EOF;
400

401 402
Usage: Bootstrap [options] <bk repository>

403 404 405
Creates a MySQL source distribution to be used for the release builds.

It checks out (exports) a clear-text version of the given local BitKeeper
406
repository, creates and adds a Changelog file (if requested), adds the
407 408 409
latest manual files from the mysqldoc BK tree and builds a source
distribution (*.tar.gz) file. Optionally, the test suite and the
distribution check can be run before the source archive is being created.
410 411 412

Options:

unknown's avatar
unknown committed
413 414
-a, --archive-log          Move the log file into the Logs directory of
                           the exported tree after a successful build
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
-b, --build-command=<cmd>  Use <cmd> to compile the sources before packing
                           the distribution.
                           (default is "$build_command")
-c, --changelog[=<rev>]    Add a ChangeLog [down to revision <rev>]
                           This will automatically be included in the source
                           distribution. To get a ChangeLog down to the last
                           tagged Changeset, simply use "last" as the revision
                           number.
--delete                   Delete an already existing distribution directory
                           in the target directory instead of renaming it.
-d, --directory=<dir>      Specify the target directory
                           (default is "$opt_directory")
--docdir=<dir>             Use the MySQL documentation BK tree located
                           in <dir>
                           (default is "$opt_docdir")
--dry-run                  Dry run without executing
-e, --export-only          Just export (and add the ChangeLog, if requested),
                           do not build or test the source distribution
-h, --help                 Print this help message
-l, --log[=<filename>]     Write a log file [to <filename>]
unknown's avatar
unknown committed
435
                           (default is "./Bootstrap-<bk repository>.log")
436 437 438 439
-m, --mail=<address>       Mail a failure report to the given address (and
                           include a log file snippet, if logging is enabled)
                           Note that the \@-Sign needs to be quoted!
                           Example: --mail=user\\\@domain.com
unknown's avatar
unknown committed
440
                           Default: build\@mysql.com
unknown's avatar
unknown committed
441
-q, --quiet                Be quiet
442
-p, --pull                 Update the source BK trees before building
443 444 445 446 447 448 449 450 451 452
-r, --revision=<rev>       Export the tree as of revision <rev>
                           (default is up to the latest revision)
-s, --skip-check           Skip checking the distribution with "make distcheck"
--skip-manual              Skip updating the manual from the mysqldoc tree
--suffix=<suffix>          Append <suffix> to the version number in
                           configure.in. Using the special suffix "YMD" will
                           add the current date as the suffix
                           (e.g. "-20020518").
-t, --test                 Run the test suite after build
-v, --verbose              Be verbose
453
-w, --win-dist             Also make Windows source distribution
454 455 456 457 458 459 460 461

Example:

    Bootstrap -c last -v -l -- mysql-4.0

EOF
	exit 1;
}