xref: /trunk/main/solenv/bin/modules/installer/upx.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
28package installer::upx;
29
30use installer::converter;
31use installer::existence;
32use installer::globals;
33use installer::logger;
34use installer::pathanalyzer;
35use installer::scriptitems;
36use installer::systemactions;
37
38#####################################################################
39# Checking whether a file has to be stripped
40#####################################################################
41
42sub is_upx_candidate
43{
44	my ( $filename, $onefile ) = @_;
45
46	my $useupx = 0;
47
48	if (( $filename =~ /\.so\s*$/ ) ||
49		( $filename =~ /\.dll\s*$/ ) ||
50		( $filename =~ /\.exe\s*$/ ) ||
51		( $filename =~ /\.bin\s*$/ ))
52	{
53		my $styles = "";
54		if ( $onefile->{'Styles'} ) { $styles = $onefile->{'Styles'}; }
55		if ( ! ( $styles =~ /\bDONT_UPX\b/ )) { $useupx = 1; }
56	}
57
58	return $useupx;
59}
60
61#####################################################################
62# Checking whether a file has to be stripped
63#####################################################################
64
65sub do_upx
66{
67	my ( $filename ) = @_;
68
69	my $compression = "9";
70	my $systemcall = $installer::globals::upxfile . " -" . $compression . " " . $filename;
71
72	my $returnvalue = system($systemcall);
73
74	my $infoline = "Systemcall: $systemcall\n";
75	push( @installer::globals::logfileinfo, $infoline);
76
77	if ($returnvalue)
78	{
79		$infoline = "WARNING: Could not successfully upx $filename! Using original file.\n";
80		push( @installer::globals::logfileinfo, $infoline);
81	}
82	else
83	{
84		$infoline = "SUCCESS: upx $filename!\n";
85		push( @installer::globals::logfileinfo, $infoline);
86	}
87
88	return $returnvalue;
89}
90
91#####################################################################
92# Using upx to decrease file size
93#####################################################################
94
95sub upx_on_libraries
96{
97	my ( $filelist, $languagestringref) = @_;
98
99	installer::logger::include_header_into_logfile("UPX'ing files:");
100	my $infoline = "";
101
102	if ( ! $installer::globals::upx_in_path )
103	{
104		$infoline = "\n\nWarning: This is an UPX product, but upx was not found in PATH!\n\n";
105		push( @installer::globals::logfileinfo, $infoline);
106	}
107	else
108	{
109		$infoline = "Using upx: $installer::globals::upxfile\n";
110		push( @installer::globals::logfileinfo, $infoline);
111
112		my $upxdirbase = installer::systemactions::create_directories("upx", $languagestringref);
113
114		if (! installer::existence::exists_in_array($upxdirbase, \@installer::globals::removedirs))
115		{
116			push(@installer::globals::removedirs, $upxdirbase);
117		}
118
119		for ( my $i = 0; $i <= $#{$filelist}; $i++ )
120		{
121			my $sourcefilename = ${$filelist}[$i]->{'sourcepath'};
122
123			if ( is_upx_candidate($sourcefilename, ${$filelist}[$i]) )
124			{
125				my $shortfilename = $sourcefilename;
126				installer::pathanalyzer::make_absolute_filename_to_relative_filename(\$shortfilename);
127
128				$infoline = "\nUpx: $shortfilename";
129				push( @installer::globals::logfileinfo, $infoline);
130
131				# copy file into directory for stripped libraries
132				my $onelanguage = ${$filelist}[$i]->{'specificlanguage'};
133
134				# files without language into directory "00"
135				if ($onelanguage eq "") { $onelanguage = "00"; }
136
137				my $upxdir = $upxdirbase . $installer::globals::separator . $onelanguage;
138				installer::systemactions::create_directory($upxdir);	# creating language specific subdirectories
139
140				my $destfilename = $upxdir . $installer::globals::separator . $shortfilename;
141				installer::systemactions::copy_one_file($sourcefilename, $destfilename);
142
143				# change sourcepath in files collector
144				${$filelist}[$i]->{'sourcepath'} = $destfilename;
145
146				# do upx on file
147				my $return = do_upx($destfilename);
148
149				# Using original file, if upx was not successful (no reason for error)
150				if ( $return ) { ${$filelist}[$i]->{'sourcepath'} = $sourcefilename; }
151			}
152		}
153	}
154}
155
1561;
157