| 1 |
# Copyright (C) 2008-2010 Modestas Vainius <[email protected]> |
| 2 |
# |
| 3 |
# This program is free software: you can redistribute it and/or modify |
| 4 |
# it under the terms of the GNU General Public License as published by |
| 5 |
# the Free Software Foundation, either version 3 of the License, or |
| 6 |
# (at your option) any later version. |
| 7 |
# |
| 8 |
# This program is distributed in the hope that it will be useful, |
| 9 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
# GNU General Public License for more details. |
| 12 |
# |
| 13 |
# You should have received a copy of the GNU General Public License |
| 14 |
# along with this program. If not, see <http://www.gnu.org/licenses/> |
| 15 |
|
| 16 |
package Debian::PkgKde::SymbolsHelper::CompileTest; |
| 17 |
|
| 18 |
use strict; |
| 19 |
use warnings; |
| 20 |
use File::Temp qw(tempdir); |
| 21 |
use File::Spec; |
| 22 |
use Dpkg::ErrorHandling; |
| 23 |
|
| 24 |
sub new { |
| 25 |
my ($cls, $compiler, $lib) = @_; |
| 26 |
|
| 27 |
my $tmpdir = tempdir(); |
| 28 |
my $sourcefile = "testcomp"; |
| 29 |
my $out = "testcomp"; |
| 30 |
my $cmd; |
| 31 |
|
| 32 |
error("Unable to create a temporary directory for test compilation") unless $tmpdir; |
| 33 |
|
| 34 |
if ($compiler =~ /gcc/) { |
| 35 |
$sourcefile .= ".c"; |
| 36 |
} elsif ($compiler =~ /g\+\+/) { |
| 37 |
$sourcefile .= ".cpp"; |
| 38 |
} else { |
| 39 |
error("Unrecognized compiler: $compiler"); |
| 40 |
} |
| 41 |
$sourcefile = File::Spec::catfile($tmpdir, $sourcefile); |
| 42 |
|
| 43 |
if ($lib) { |
| 44 |
$cmd = "$compiler -shared -fPIC"; |
| 45 |
$out .= ".so"; |
| 46 |
} else { |
| 47 |
$cmd = "$compiler"; |
| 48 |
} |
| 49 |
$out = File::Spec::catfile($tmpdir, $out); |
| 50 |
|
| 51 |
my $self = bless { tmpdir => $tmpdir, |
| 52 |
sourcefile => $sourcefile, out => $out }, $cls; |
| 53 |
$self->set_cmd($cmd); |
| 54 |
return $self; |
| 55 |
} |
| 56 |
|
| 57 |
sub set_cmd { |
| 58 |
my ($self, $cmd) = @_; |
| 59 |
$self->{cmd} = "$cmd $self->{sourcefile} -o $self->{out}"; |
| 60 |
} |
| 61 |
|
| 62 |
sub compile { |
| 63 |
my ($self, $sourcecode) = @_; |
| 64 |
|
| 65 |
open(SOURCE, ">", $self->{sourcefile}) |
| 66 |
or error("Unable to open temporary source file for writing: $self->{sourcefile}"); |
| 67 |
print SOURCE $sourcecode |
| 68 |
or error("Unable to write to temporary source file $self->{sourcefile}"); |
| 69 |
close(SOURCE); |
| 70 |
|
| 71 |
system($self->{cmd}) == 0 or error("Compilation failed: $self->{cmd}"); |
| 72 |
return $self->get_output_file(); |
| 73 |
} |
| 74 |
|
| 75 |
sub get_output_file { |
| 76 |
my $self = shift; |
| 77 |
return (-f $self->{out}) ? $self->{out} : undef; |
| 78 |
} |
| 79 |
|
| 80 |
sub rm { |
| 81 |
my $self = shift; |
| 82 |
system("rm -rf $self->{tmpdir}") == 0 or error("Unable to delete temporary directory: $self->{tmpdir}"); |
| 83 |
} |
| 84 |
|
| 85 |
1; |