xref: /trunk/main/solenv/bin/modules/installer/upx.pm (revision b274bc22)
19780544fSAndrew Rist#**************************************************************
29780544fSAndrew Rist#
39780544fSAndrew Rist#  Licensed to the Apache Software Foundation (ASF) under one
49780544fSAndrew Rist#  or more contributor license agreements.  See the NOTICE file
59780544fSAndrew Rist#  distributed with this work for additional information
69780544fSAndrew Rist#  regarding copyright ownership.  The ASF licenses this file
79780544fSAndrew Rist#  to you under the Apache License, Version 2.0 (the
89780544fSAndrew Rist#  "License"); you may not use this file except in compliance
99780544fSAndrew Rist#  with the License.  You may obtain a copy of the License at
109780544fSAndrew Rist#
119780544fSAndrew Rist#    http://www.apache.org/licenses/LICENSE-2.0
129780544fSAndrew Rist#
139780544fSAndrew Rist#  Unless required by applicable law or agreed to in writing,
149780544fSAndrew Rist#  software distributed under the License is distributed on an
159780544fSAndrew Rist#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
169780544fSAndrew Rist#  KIND, either express or implied.  See the License for the
179780544fSAndrew Rist#  specific language governing permissions and limitations
189780544fSAndrew Rist#  under the License.
199780544fSAndrew Rist#
209780544fSAndrew Rist#**************************************************************
219780544fSAndrew Rist
229780544fSAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweirpackage installer::upx;
25cdf0e10cSrcweir
26cdf0e10cSrcweiruse installer::converter;
27cdf0e10cSrcweiruse installer::existence;
28cdf0e10cSrcweiruse installer::globals;
29cdf0e10cSrcweiruse installer::logger;
30cdf0e10cSrcweiruse installer::pathanalyzer;
31cdf0e10cSrcweiruse installer::scriptitems;
32cdf0e10cSrcweiruse installer::systemactions;
33cdf0e10cSrcweir
34cdf0e10cSrcweir#####################################################################
35cdf0e10cSrcweir# Checking whether a file has to be stripped
36cdf0e10cSrcweir#####################################################################
37cdf0e10cSrcweir
38cdf0e10cSrcweirsub is_upx_candidate
39cdf0e10cSrcweir{
40cdf0e10cSrcweir	my ( $filename, $onefile ) = @_;
41cdf0e10cSrcweir
42cdf0e10cSrcweir	my $useupx = 0;
43cdf0e10cSrcweir
44cdf0e10cSrcweir	if (( $filename =~ /\.so\s*$/ ) ||
45cdf0e10cSrcweir		( $filename =~ /\.dll\s*$/ ) ||
46cdf0e10cSrcweir		( $filename =~ /\.exe\s*$/ ) ||
47cdf0e10cSrcweir		( $filename =~ /\.bin\s*$/ ))
48cdf0e10cSrcweir	{
49cdf0e10cSrcweir		my $styles = "";
50cdf0e10cSrcweir		if ( $onefile->{'Styles'} ) { $styles = $onefile->{'Styles'}; }
51cdf0e10cSrcweir		if ( ! ( $styles =~ /\bDONT_UPX\b/ )) { $useupx = 1; }
52cdf0e10cSrcweir	}
53cdf0e10cSrcweir
54cdf0e10cSrcweir	return $useupx;
55cdf0e10cSrcweir}
56cdf0e10cSrcweir
57cdf0e10cSrcweir#####################################################################
58cdf0e10cSrcweir# Checking whether a file has to be stripped
59cdf0e10cSrcweir#####################################################################
60cdf0e10cSrcweir
61cdf0e10cSrcweirsub do_upx
62cdf0e10cSrcweir{
63cdf0e10cSrcweir	my ( $filename ) = @_;
64cdf0e10cSrcweir
65cdf0e10cSrcweir	my $compression = "9";
66cdf0e10cSrcweir	my $systemcall = $installer::globals::upxfile . " -" . $compression . " " . $filename;
67cdf0e10cSrcweir
68cdf0e10cSrcweir	my $returnvalue = system($systemcall);
69cdf0e10cSrcweir
70cdf0e10cSrcweir	my $infoline = "Systemcall: $systemcall\n";
71*b274bc22SAndre Fischer	$installer::logger::Lang->print($infoline);
72cdf0e10cSrcweir
73cdf0e10cSrcweir	if ($returnvalue)
74cdf0e10cSrcweir	{
75cdf0e10cSrcweir		$infoline = "WARNING: Could not successfully upx $filename! Using original file.\n";
76*b274bc22SAndre Fischer		$installer::logger::Lang->print($infoline);
77cdf0e10cSrcweir	}
78cdf0e10cSrcweir	else
79cdf0e10cSrcweir	{
80cdf0e10cSrcweir		$infoline = "SUCCESS: upx $filename!\n";
81*b274bc22SAndre Fischer		$installer::logger::Lang->print($infoline);
82cdf0e10cSrcweir	}
83cdf0e10cSrcweir
84cdf0e10cSrcweir	return $returnvalue;
85cdf0e10cSrcweir}
86cdf0e10cSrcweir
87cdf0e10cSrcweir#####################################################################
88cdf0e10cSrcweir# Using upx to decrease file size
89cdf0e10cSrcweir#####################################################################
90cdf0e10cSrcweir
91cdf0e10cSrcweirsub upx_on_libraries
92cdf0e10cSrcweir{
93cdf0e10cSrcweir	my ( $filelist, $languagestringref) = @_;
94cdf0e10cSrcweir
95cdf0e10cSrcweir	installer::logger::include_header_into_logfile("UPX'ing files:");
96cdf0e10cSrcweir	my $infoline = "";
97cdf0e10cSrcweir
98cdf0e10cSrcweir	if ( ! $installer::globals::upx_in_path )
99cdf0e10cSrcweir	{
100*b274bc22SAndre Fischer		$installer::logger::Lang->print("\n");
101*b274bc22SAndre Fischer		$installer::logger::Lang->print("\n");
102*b274bc22SAndre Fischer		$installer::logger::Lang->print("Warning: This is an UPX product, but upx was not found in PATH!\n");
103*b274bc22SAndre Fischer		$installer::logger::Lang->print("\n");
104cdf0e10cSrcweir	}
105cdf0e10cSrcweir	else
106cdf0e10cSrcweir	{
107cdf0e10cSrcweir		$infoline = "Using upx: $installer::globals::upxfile\n";
108*b274bc22SAndre Fischer		$installer::logger::Lang->print($infoline);
109cdf0e10cSrcweir
110cdf0e10cSrcweir		my $upxdirbase = installer::systemactions::create_directories("upx", $languagestringref);
111cdf0e10cSrcweir
112cdf0e10cSrcweir		if (! installer::existence::exists_in_array($upxdirbase, \@installer::globals::removedirs))
113cdf0e10cSrcweir		{
114cdf0e10cSrcweir			push(@installer::globals::removedirs, $upxdirbase);
115cdf0e10cSrcweir		}
116cdf0e10cSrcweir
117cdf0e10cSrcweir		for ( my $i = 0; $i <= $#{$filelist}; $i++ )
118cdf0e10cSrcweir		{
119cdf0e10cSrcweir			my $sourcefilename = ${$filelist}[$i]->{'sourcepath'};
120cdf0e10cSrcweir
121cdf0e10cSrcweir			if ( is_upx_candidate($sourcefilename, ${$filelist}[$i]) )
122cdf0e10cSrcweir			{
123cdf0e10cSrcweir				my $shortfilename = $sourcefilename;
124cdf0e10cSrcweir				installer::pathanalyzer::make_absolute_filename_to_relative_filename(\$shortfilename);
125cdf0e10cSrcweir
126*b274bc22SAndre Fischer				$installer::logger::Lang->print("\n");
127*b274bc22SAndre Fischer				$installer::logger::Lang->printf("Upx: %s", $shortfilename);
128cdf0e10cSrcweir
129cdf0e10cSrcweir				# copy file into directory for stripped libraries
130cdf0e10cSrcweir				my $onelanguage = ${$filelist}[$i]->{'specificlanguage'};
131cdf0e10cSrcweir
132cdf0e10cSrcweir				# files without language into directory "00"
133cdf0e10cSrcweir				if ($onelanguage eq "") { $onelanguage = "00"; }
134cdf0e10cSrcweir
135cdf0e10cSrcweir				my $upxdir = $upxdirbase . $installer::globals::separator . $onelanguage;
136cdf0e10cSrcweir				installer::systemactions::create_directory($upxdir);	# creating language specific subdirectories
137cdf0e10cSrcweir
138cdf0e10cSrcweir				my $destfilename = $upxdir . $installer::globals::separator . $shortfilename;
139cdf0e10cSrcweir				installer::systemactions::copy_one_file($sourcefilename, $destfilename);
140cdf0e10cSrcweir
141cdf0e10cSrcweir				# change sourcepath in files collector
142cdf0e10cSrcweir				${$filelist}[$i]->{'sourcepath'} = $destfilename;
143cdf0e10cSrcweir
144cdf0e10cSrcweir				# do upx on file
145cdf0e10cSrcweir				my $return = do_upx($destfilename);
146cdf0e10cSrcweir
147cdf0e10cSrcweir				# Using original file, if upx was not successful (no reason for error)
148cdf0e10cSrcweir				if ( $return ) { ${$filelist}[$i]->{'sourcepath'} = $sourcefilename; }
149cdf0e10cSrcweir			}
150cdf0e10cSrcweir		}
151cdf0e10cSrcweir	}
152cdf0e10cSrcweir}
153cdf0e10cSrcweir
154cdf0e10cSrcweir1;
155