summaryrefslogtreecommitdiff
path: root/Build-tools/Do-rpm
blob: 0f423feb921e8151cee73988244fc9922589aa60 (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
#!/usr/bin/perl -w
#
# Do-rpm - compile RPM packages out of a source tarball and move the
# resulting RPM packages into the current directory.
#
# The script currently assumes the following environment (which should exist
# like that, if the Do-compile script was used to build the binary
# distribution)
#
# - there must be a source distribution (mysql-<version>.tar.gz)
#   in the current directory
# - there must be a spec file (mysql-<version>.spec) in the directory
#   $HOME/<hostname>/mysql-<version>/support-files/
#
# Use the "--help" option for more info!
#
# written by Lenz Grimmer <lenz@mysql.com>
#

use Cwd;
use File::Basename;
use File::Copy;
use Getopt::Long;
Getopt::Long::Configure ("bundling");
use Sys::Hostname;

$opt_cc= undef;
$opt_cflags= undef;
$opt_clean= undef;
$opt_cxx= undef;
$opt_cxxflags= undef;
$opt_dry_run= undef;
$opt_help= undef;
$opt_log= undef;
$opt_mail= "";
$opt_verbose= undef;

$MAJOR= $MINOR= $RELEASE= 0;

GetOptions(
	"cc=s",
	"cflags=s",
	"clean|c",
	"cxx=s",
	"cxxflags=s",
	"dry-run|t",
	"help|h",
	"log|l:s",
	"mail|m=s",
	"verbose|v",
) || &print_help;

defined($VERSION=$ARGV[0]) || print_help("Please provide the MySQL version!");

# Include helper functions
$PWD= cwd();
$LOGGER= "$PWD/logger.pm";
if (-f "$LOGGER")
{
	do "$LOGGER";
}
else
{
	die "ERROR: $LOGGER cannot be found!\n";
}

#
# Override predefined Log file name
#
if (defined $opt_log)
{
	if ($opt_log ne "")
	{
		if ($opt_log =~ /^\/.*/)
		{
			$LOGFILE= $opt_log;
		}
		else
		{
			$LOGFILE= $PWD . "/" . $opt_log;
		}
	}
}

($MAJOR, $MINOR, $RELEASE)= split(/\./, $VERSION);
$HOST= hostname();
$HOST=~ /^([^.-]*)/;
$HOST= $1;
$LOGFILE= "$PWD/Logs/Do-rpm-$HOST-$MAJOR.$MINOR.log";

&print_help("") if ($opt_help);

#
# Newer RPM versions ship with a separate tool "rpmbuild" to build RPMs
#
if (-x "/usr/bin/rpmbuild")
{
	$RPM= "/usr/bin/rpmbuild";
}
else
{
	$RPM= "/bin/rpm";
}

if ($RPM)
{
	&logger("Found rpm binary: $RPM");
}
else
{
	&abort("Unable to find RPM binary!");
}

#
# determine some RPM settings for this host
#
chomp($RPMARCH= `$RPM --eval "%{_arch}" 2> /dev/null`);
chomp($RPMDIR= `$RPM --eval "%{_rpmdir}" 2> /dev/null`);
chomp($SOURCEDIR= `$RPM --eval "%{_sourcedir}" 2> /dev/null`);
chomp($SPECDIR= `$RPM --eval "%{_specdir}" 2> /dev/null`);
chomp($SRCRPMDIR= `$RPM --eval "%{_srcrpmdir}" 2> /dev/null`);

$SOURCEFILE= "mysql-$VERSION.tar.gz";
$SPECFILE= "$PWD/$HOST/mysql-$VERSION/support-files/mysql-$VERSION.spec";

&logger("Starting RPM build of MySQL-$VERSION on $HOST");

foreach $file ($SOURCEFILE, $SPECFILE)
{
	&abort("Unable to find $file!") unless (-f "$file");
}

#
# Install source and spec file
#
&logger("Copying SOURCE and SPEC file to build directories.");
copy($SOURCEFILE, $SOURCEDIR)
or &abort("Unable to copy $SOURCEFILE to $SOURCEDIR!");
copy($SPECFILE, $SPECDIR)
or &abort("Unable to copy $SPECFILE to $SPECDIR!");

#
# Set environment variables - these are being used in the
# official MySQL RPM spec file
#
&logger("Setting special build environment variables")
if ($opt_cc) or ($opt_cflags) or ($opt_cxxflags) or ($opt_cxx);
$ENV{MYSQL_BUILD_CC}=$opt_cc if ($opt_cc);
$ENV{MYSQL_BUILD_CFLAGS}=$opt_cflags if ($opt_cflags);
$ENV{MYSQL_BUILD_CXXFLAGS}=$opt_cxxflags if ($opt_cxxflags);
$ENV{MYSQL_BUILD_CXX}=$opt_cxx if ($opt_cxx);

#
# Build the RPMs
#
$command= "$RPM";
$command.= " -v" if ($opt_verbose);
$command.= " -ba";
$command.= " --clean" if $opt_clean;
$command.= " $SPECDIR/";
$command.= basename($SPECFILE);
&logger("Building RPM.");
&run_command($command, "Error while building the RPMs!"); 

#
# Move the resulting RPMs into the pwd
#
$command= "mv";
$command.= " -v " if ($opt_verbose);
$command.= "$SRCRPMDIR/MySQL*$VERSION*.src.rpm $PWD";
&run_command($command, "Error moving source RPM!");

$command= "mv";
$command.= " -v " if ($opt_verbose);
$command.= "$RPMDIR/$RPMARCH/MySQL*$VERSION*.$RPMARCH.rpm $PWD";
&run_command($command, "Error moving binary RPMs!");

#
# Clean up
#
if ($opt_clean)
{
	&logger("Removing spec file and source package");
	unlink("$SPECDIR/" . basename($SPECFILE));
	unlink("$SOURCEDIR/$SOURCEFILE");
}

&logger("SUCCESS: RPM files successfully created.") if (!$opt_dry_run);
exit 0;

sub print_help
{
	my $message= $_[0];
	if ($message ne "")
	{
		print "\n";
		print "ERROR: $message\n\n}";
	}
	print <<EOF;

Usage: Do-rpm <options> <version>

Creates a binary RPM package out of a MySQL source distribution and moves the
resulting RPMs into the current directory.  <version> is the MySQL version
number (e.g. 4.0.11-gamma)

Options:

    --cc=<compiler>           Use <compiler> to compile C code
    --ccflags=<flags>         Use special C compiler flags
    --cxx=<compiler>          Use <compiler> to compile C++ code
    --cxxflags=<flags>        Use special C++ compiler flags
-c, --clean                   Clean up after the build
-t, --dry-run                 Dry run without executing
-h, --help                    Print this help
-l, --log[=<filename>]        Write a log file [to <filename>]
                              (default is "$LOGFILE")
-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
-v, --verbose                 Verbose execution

EOF
	exit 1;
}