xref: /aoo41x/main/solenv/bin/modules/installer/upx.pm (revision 9780544f)
1*9780544fSAndrew Rist#**************************************************************
2*9780544fSAndrew Rist#
3*9780544fSAndrew Rist#  Licensed to the Apache Software Foundation (ASF) under one
4*9780544fSAndrew Rist#  or more contributor license agreements.  See the NOTICE file
5*9780544fSAndrew Rist#  distributed with this work for additional information
6*9780544fSAndrew Rist#  regarding copyright ownership.  The ASF licenses this file
7*9780544fSAndrew Rist#  to you under the Apache License, Version 2.0 (the
8*9780544fSAndrew Rist#  "License"); you may not use this file except in compliance
9*9780544fSAndrew Rist#  with the License.  You may obtain a copy of the License at
10*9780544fSAndrew Rist#
11*9780544fSAndrew Rist#    http://www.apache.org/licenses/LICENSE-2.0
12*9780544fSAndrew Rist#
13*9780544fSAndrew Rist#  Unless required by applicable law or agreed to in writing,
14*9780544fSAndrew Rist#  software distributed under the License is distributed on an
15*9780544fSAndrew Rist#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*9780544fSAndrew Rist#  KIND, either express or implied.  See the License for the
17*9780544fSAndrew Rist#  specific language governing permissions and limitations
18*9780544fSAndrew Rist#  under the License.
19*9780544fSAndrew Rist#
20*9780544fSAndrew Rist#**************************************************************
21*9780544fSAndrew Rist
22*9780544fSAndrew 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";
71cdf0e10cSrcweir	push( @installer::globals::logfileinfo, $infoline);
72cdf0e10cSrcweir
73cdf0e10cSrcweir	if ($returnvalue)
74cdf0e10cSrcweir	{
75cdf0e10cSrcweir		$infoline = "WARNING: Could not successfully upx $filename! Using original file.\n";
76cdf0e10cSrcweir		push( @installer::globals::logfileinfo, $infoline);
77cdf0e10cSrcweir	}
78cdf0e10cSrcweir	else
79cdf0e10cSrcweir	{
80cdf0e10cSrcweir		$infoline = "SUCCESS: upx $filename!\n";
81cdf0e10cSrcweir		push( @installer::globals::logfileinfo, $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	{
100cdf0e10cSrcweir		$infoline = "\n\nWarning: This is an UPX product, but upx was not found in PATH!\n\n";
101cdf0e10cSrcweir		push( @installer::globals::logfileinfo, $infoline);
102cdf0e10cSrcweir	}
103cdf0e10cSrcweir	else
104cdf0e10cSrcweir	{
105cdf0e10cSrcweir		$infoline = "Using upx: $installer::globals::upxfile\n";
106cdf0e10cSrcweir		push( @installer::globals::logfileinfo, $infoline);
107cdf0e10cSrcweir
108cdf0e10cSrcweir		my $upxdirbase = installer::systemactions::create_directories("upx", $languagestringref);
109cdf0e10cSrcweir
110cdf0e10cSrcweir		if (! installer::existence::exists_in_array($upxdirbase, \@installer::globals::removedirs))
111cdf0e10cSrcweir		{
112cdf0e10cSrcweir			push(@installer::globals::removedirs, $upxdirbase);
113cdf0e10cSrcweir		}
114cdf0e10cSrcweir
115cdf0e10cSrcweir		for ( my $i = 0; $i <= $#{$filelist}; $i++ )
116cdf0e10cSrcweir		{
117cdf0e10cSrcweir			my $sourcefilename = ${$filelist}[$i]->{'sourcepath'};
118cdf0e10cSrcweir
119cdf0e10cSrcweir			if ( is_upx_candidate($sourcefilename, ${$filelist}[$i]) )
120cdf0e10cSrcweir			{
121cdf0e10cSrcweir				my $shortfilename = $sourcefilename;
122cdf0e10cSrcweir				installer::pathanalyzer::make_absolute_filename_to_relative_filename(\$shortfilename);
123cdf0e10cSrcweir
124cdf0e10cSrcweir				$infoline = "\nUpx: $shortfilename";
125cdf0e10cSrcweir				push( @installer::globals::logfileinfo, $infoline);
126cdf0e10cSrcweir
127cdf0e10cSrcweir				# copy file into directory for stripped libraries
128cdf0e10cSrcweir				my $onelanguage = ${$filelist}[$i]->{'specificlanguage'};
129cdf0e10cSrcweir
130cdf0e10cSrcweir				# files without language into directory "00"
131cdf0e10cSrcweir				if ($onelanguage eq "") { $onelanguage = "00"; }
132cdf0e10cSrcweir
133cdf0e10cSrcweir				my $upxdir = $upxdirbase . $installer::globals::separator . $onelanguage;
134cdf0e10cSrcweir				installer::systemactions::create_directory($upxdir);	# creating language specific subdirectories
135cdf0e10cSrcweir
136cdf0e10cSrcweir				my $destfilename = $upxdir . $installer::globals::separator . $shortfilename;
137cdf0e10cSrcweir				installer::systemactions::copy_one_file($sourcefilename, $destfilename);
138cdf0e10cSrcweir
139cdf0e10cSrcweir				# change sourcepath in files collector
140cdf0e10cSrcweir				${$filelist}[$i]->{'sourcepath'} = $destfilename;
141cdf0e10cSrcweir
142cdf0e10cSrcweir				# do upx on file
143cdf0e10cSrcweir				my $return = do_upx($destfilename);
144cdf0e10cSrcweir
145cdf0e10cSrcweir				# Using original file, if upx was not successful (no reason for error)
146cdf0e10cSrcweir				if ( $return ) { ${$filelist}[$i]->{'sourcepath'} = $sourcefilename; }
147cdf0e10cSrcweir			}
148cdf0e10cSrcweir		}
149cdf0e10cSrcweir	}
150cdf0e10cSrcweir}
151cdf0e10cSrcweir
152cdf0e10cSrcweir1;
153