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