xref: /trunk/main/solenv/bin/modules/packager/work.pm (revision cdf0e10c)
1#*************************************************************************
2#
3# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4#
5# Copyright 2000, 2010 Oracle and/or its affiliates.
6#
7# OpenOffice.org - a multi-platform office productivity suite
8#
9# This file is part of OpenOffice.org.
10#
11# OpenOffice.org is free software: you can redistribute it and/or modify
12# it under the terms of the GNU Lesser General Public License version 3
13# only, as published by the Free Software Foundation.
14#
15# OpenOffice.org is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18# GNU Lesser General Public License version 3 for more details
19# (a copy is included in the LICENSE file that accompanied this code).
20#
21# You should have received a copy of the GNU Lesser General Public License
22# version 3 along with OpenOffice.org.  If not, see
23# <http://www.openoffice.org/license.html>
24# for a copy of the LGPLv3 License.
25#
26#*************************************************************************
27
28
29package packager::work;
30
31use packager::exiter;
32use packager::existence;
33use packager::files;
34use packager::globals;
35
36###########################################
37# Setting global variables
38###########################################
39
40sub set_global_variable
41{
42	my $compiler = $ENV{'OUTPATH'};
43
44	if ( $ENV{'PROEXT'} ) { $compiler = $compiler . $ENV{'PROEXT'}; }
45
46	$packager::globals::compiler = $compiler;
47}
48
49#############################################################################
50# Converting a string list with separator $listseparator
51# into an array
52#############################################################################
53
54sub convert_stringlist_into_array
55{
56	my ( $includestringref, $listseparator ) = @_;
57
58	my @newarray = ();
59	my $first;
60	my $last = ${$includestringref};
61
62	while ( $last =~ /^\s*(.+?)\Q$listseparator\E(.+)\s*$/)	# "$" for minimal matching
63	{
64		$first = $1;
65		$last = $2;
66		push(@newarray, "$first");
67	}
68
69	push(@newarray, "$last");
70
71	return \@newarray;
72}
73
74###########################################
75# Generating a list of package calls
76# corresponding to the package list
77###########################################
78
79sub create_package_todos
80{
81	my ( $packagelist ) = @_;
82
83	my @targets = ();	# only used, if the build server is not used
84
85	for ( my $i = 0; $i <= $#{$packagelist}; $i++ )
86	{
87		my $line = ${$packagelist}[$i];
88
89		if ( $line =~ /^\s*\#/ ) { next; }	# comment line
90
91		if ( $line =~ /^\s*(\w+?)\s+(\S+?)\s+(\S+?)\s+(\w+?)\s*$/ )
92		{
93			my $product = $1;
94			my $compilerlist = $2;
95			my $languagelist = $3;
96			my $target = $4;
97
98			$product =~ s/\s//g;
99			$compilerlist =~ s/\s//g;
100			$languagelist =~ s/\s//g;
101			$target =~ s/\s//g;
102
103			my $compilers = convert_stringlist_into_array(\$compilerlist, ",");
104
105			# is the compiler of this "build" part of the compiler list in pack.lst ?
106
107			if ( packager::existence::exists_in_array($packager::globals::compiler, $compilers) )
108			{
109				# products are separated in pack.lst by "|"
110
111				my $languagesets = convert_stringlist_into_array(\$languagelist, "\|");
112
113				# now all information is available to create the targets for the systemcalls
114
115				for ( my $j = 0; $j <= $#{$languagesets}; $j++ )
116				{
117					my $languagestring = ${$languagesets}[$j];
118					$languagestring =~ s/\,/\_/g;	# comma in pack.lst becomes "_" in dmake command
119
120					my $target = $target . "_" . $languagestring;
121					push(@targets, $target);
122
123					my $insertline = $target . "\n";
124					push( @packager::globals::logfileinfo, $insertline);
125				}
126			}
127		}
128	}
129
130	return \@targets;
131}
132
133###########################################
134# Executing the generated system calls
135###########################################
136
137sub execute_system_calls
138{
139	my ( $targets ) = @_;
140
141	for ( my $i = 0; $i <= $#{$targets}; $i++ )
142	{
143		my $systemcall = "dmake " . ${$targets}[$i];
144
145		my $infoline = "Packager: $systemcall\n";
146		print $infoline;
147		push( @packager::globals::logfileinfo, $infoline);
148
149		my $returnvalue = system($systemcall);
150
151		$infoline = "Packager finished: $systemcall\n";
152		print $infoline;
153		push( @packager::globals::logfileinfo, $infoline);
154
155		if ( $returnvalue )
156		{
157			$infoline = "\nERROR: Packager $systemcall\n";
158			print $infoline;
159			push( @packager::globals::logfileinfo, $infoline);
160			if (!($packager::globals::ignoreerrors)) { packager::exiter::exit_program("ERROR: Packing not successful : $systemcall", "execute_system_calls"); }
161		}
162	}
163}
164
165##############################################################
166# Starting the build server with the generated system calls
167##############################################################
168
169sub start_build_server
170{
171	my ( $targets ) = @_;
172
173	# preparing the directory structure
174
175	my $prj = $ENV{PRJ};				# for example "..";
176	my $platform = $ENV{INPATH};		# wntmsci10.pro, unxsols4.pro
177	my $platformpath = $prj . $packager::globals::separator . $platform;
178	if ( ! -d $platformpath ) { packager::files::create_directory($miscpath); }
179	my $miscpath = $platformpath  . $packager::globals::separator . "misc";
180	if ( ! -d $miscpath ) { packager::files::create_directory($miscpath); }
181	$miscpath = $miscpath  . $packager::globals::separator . "temp";
182	if ( -d $miscpath ) { packager::files::remove_complete_directory($miscpath); }	# removing old files !
183	if ( ! -d $miscpath ) { packager::files::create_directory($miscpath); }
184
185	my $prjroot = ".." . $packager::globals::separator . ".." . $packager::globals::separator . ".." . $packager::globals::separator . ".."; # platform/misc/temp/uniquetempdir
186
187	my $makefilepath = $prj . $packager::globals::separator . "util" . $packager::globals::separator . "makefile.mk";
188
189	if ( ! $ENV{'PRJNAME'} ) { packager::exiter::exit_program("ERROR: Environment variable PRJNAME not set!", "do_broadcast"); }
190	my $prjname = $ENV{PRJNAME};
191
192	my $pkgformat = $ENV{PKGFORMAT};
193
194	my $prjdep = $prjname . "\\" . "util";	# always windows like path
195    my @targetdirs;
196	my @targetlines = ();
197	# iterating over all targets
198	for ( my $i = 0; $i <= $#{$targets}; $i++ )
199	{
200		my $target = ${$targets}[$i];
201		my $tempdir = $miscpath . $packager::globals::separator . $target;
202		$tempdir = packager::files::create_unique_directory ($tempdir);
203        @targetlines=();
204	    push( @targetlines, "\ngenerated_target : $target\n\n");	# to be included into the makefile.mk
205
206		if ( defined $pkgformat ) {
207	    	push( @targetlines, "\n$target : ".'$$@{$(PKGFORMAT:^".")}'."\n\n");	# to be included into the makefile.mk
208		}
209
210		generate_makefile($tempdir, $makefilepath, $prjroot, $target, \@targetlines);
211
212		do_broadcast($tempdir, $prjname, $prj, $platform, $prjdep);
213        push @targetdirs, $tempdir;
214	}
215}
216
217##############################################################
218# Generating the makefile in the temporary directory
219##############################################################
220
221sub generate_makefile
222{
223	my ( $tempdir, $makefilepath, $prjroot, $target, $targetlines_ref ) = @_;
224
225	my $makefile = packager::files::read_file($makefilepath);
226
227	my @targetlines = ();
228	push( @targetlines, @{$targetlines_ref});	# to be included into the makefile.mk
229
230	$prjroot = $prjroot . "\n";
231
232	my $uniquename = $tempdir;
233	get_filename_from_path(\$uniquename);
234	$uniquename = $uniquename . "\n";
235
236	my $counter = 0;
237	my $increase = 1;
238
239	for ( my $i = 0; $i <= $#{$makefile}; $i++ )
240	{
241		if ( ${$makefile}[$i] =~ /^\s*TARGET\s*=.*/ ) { ${$makefile}[$i] = "TARGET=" . $uniquename; }	# setting the new project root
242
243		if ( ${$makefile}[$i] =~ /^\s*PRJ\s*=.*/ ) { ${$makefile}[$i] = "PRJ=" . $prjroot; }	# setting the new project root
244
245		if ( ${$makefile}[$i] =~ /^\s*\.INCLUDE[\t ]*:[\t ]*target.mk[\t ]*$/ ) { $increase = 0; }	# no more increase of the counter
246
247		if ( $increase ) { $counter++; }
248	}
249
250	splice(@{$makefile}, $counter, 0, @targetlines);	# including the new target lines at position $counter
251
252	my $newmakefilepath = $tempdir . $packager::globals::separator . "makefile.mk";
253	packager::files::save_file($newmakefilepath, $makefile);
254}
255
256##############################################################
257# Generating the broadcasts for the build server
258##############################################################
259
260sub do_broadcast
261{
262    use  File::Temp;
263
264	my ( $tempdir, $prjname, $prj, $platform, $prjdep ) = @_;
265
266	# Syntax:  cmd_bcst -s 18 "Version;Environment;Project;Verzeichnis;Restriction[;Abhaengigkeit1][;Abhaengigkeit n]..."
267	# Example: cmd_bcst -s 18 "SRC680;wntmsci10.pro;instsetoo_native;;instsetoo_native\bla1;instsetoo_native\util"
268
269	if ( ! $ENV{'WORK_STAMP'} ) { packager::exiter::exit_program("ERROR: Environment variable WORK_STAMP not set!", "do_broadcast"); }
270	my $workstamp = $ENV{WORK_STAMP};
271	my $cwsworkstamp = $ENV{CWS_WORK_STAMP};
272
273	my $prjdir = $tempdir;
274	$prjdir =~ s/$prj/$prjname/;
275	$prjdir =~ s/\//\\/g;					# convert to windows path syntax
276
277    my $tempfiledir = $ENV{TMP};
278    $tempfiledir = $tempdir if ( ! defined $tempfiledir );
279    my ( $tmpfile_handle, $tmpfile_name ) = mkstemp( $tempfiledir . $packager::globals::separator . "packagerXXXXX");
280    if ( ! $tmpfile_handle ) {
281        packager::exiter::exit_program("ERROR: Couldn't open temporary file \"$tmpfile_name\"!", "do_broadcast");
282    }
283    if (defined($cwsworkstamp)) {
284        print $tmpfile_handle "\"$cwsworkstamp;$platform;$prjname;$prjdir;nobase;$prjdep\"";
285        print "to tmpfile: \"$cwsworkstamp;$platform;$prjname;$prjdir;nobase;$prjdep\"\n";
286    }
287    else {
288        print $tmpfile_handle "\"$workstamp;$platform;$prjname;$prjdir;nobase;$prjdep\"";
289        print "to tmpfile: \"$workstamp;$platform;$prjname;$prjdir;nobase;$prjdep\"\n";
290    }
291    close $tmpfile_handle;
292	my $returnvalue = system("cmd_bcst -s 18 \@$tmpfile_name");
293	print "cmd_bcst -s 18 \@$tmpfile_name\n";
294    unlink "$tmpfile_name";
295
296	if ( $returnvalue )	# an error occured
297	{
298		if (!($packager::globals::ignoreerrors)) { packager::exiter::exit_program("ERROR: Packing not successful!", "do_broadcast"); }
299	}
300}
301
302##############################################################
303# Returning the name of file or directory from complete path
304##############################################################
305
306sub get_filename_from_path
307{
308	my ($longfilenameref) = @_;
309
310	if ( $packager::globals::isunix )
311	{
312		if ( $$longfilenameref =~ /^.*\/(\S.+\S?)/ )
313		{
314			$$longfilenameref = $1;
315		}
316	}
317
318	if ( $packager::globals::iswin )
319	{
320		if ( $$longfilenameref =~ /^.*\\(\S.+\S?)/ )
321		{
322			$$longfilenameref = $1;
323		}
324	}
325}
326
3271;
328