summaryrefslogtreecommitdiff
path: root/solenv/bin/modules/CreatePDBRelocators.pm
blob: 8f272fe896f8873b2c9c4b028996843c36e67879 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#*************************************************************************
#
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
#
# Copyright 2000, 2010 Oracle and/or its affiliates.
#
# OpenOffice.org - a multi-platform office productivity suite
#
# This file is part of OpenOffice.org.
#
# OpenOffice.org is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License version 3
# only, as published by the Free Software Foundation.
#
# OpenOffice.org is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License version 3 for more details
# (a copy is included in the LICENSE file that accompanied this code).
#
# You should have received a copy of the GNU Lesser General Public License
# version 3 along with OpenOffice.org.  If not, see
# <http://www.openoffice.org/license.html>
# for a copy of the LGPLv3 License.
#
#*************************************************************************

#*************************************************************************
#
# createPDBRelocators - create for pdb relocator files
#                       PDB relocator files are used to find debug infos
#                       for analysis of creash reports
#
# usage: create_pdb_relocators($inpath, $milestoneext, $pre);
#
#*************************************************************************

package CreatePDBRelocators;

use strict;
use File::Basename;
use SourceConfig;

sub new
{
    my $Object = shift;
    my $solarversion = shift;
    my $self = {};
    my @repositories;

    if (!defined ($solarversion)) {
        $solarversion = $ENV{SOLARVERSION};
    }
    if ( !$solarversion ) {
        print STDERR "can't determine SOLARVERSION.\n";
        exit (1);
    }

    $self->{SOLARVERSION} = $solarversion;

    my $SourceConfigObj = SourceConfig->new();
    @repositories = $SourceConfigObj->get_repositories();

    if (!scalar @repositories) {
        print STDERR "no repository and no working directory found.\n";
        exit (2);
    }
    $self->{REPOSITORIES} = \@repositories;
    bless($self, $Object);
    return $self;
}

sub create_pdb_relocators
{
    my $self = shift;
    my $inpath   = shift;
    my $milestoneext    = shift;
    my $pre      = shift;

    my $solarversion = $self->{SOLARVERSION};
    my $root_dir = "$solarversion/$inpath";

    # sanitize path
    $root_dir =~ s/\\/\//g;
    my $o =~ s/\\/\//g;
    my $premilestoneext = $milestoneext;
    if ( $pre ne "" ) {
        $premilestoneext = ~ s/^\.//;
        $premilestoneext = ".pre$premilestoneext";
    }
    my $pdb_dir = $root_dir . "/pdb$premilestoneext";
    my $pdb_so_dir = $root_dir . "/pdb$premilestoneext/so";

    # create pdb directories if necessary
    if ( ! -d $pdb_dir ) {
        if ( !mkdir($pdb_dir, 0775) ) {
            print STDERR "can't create directory '$pdb_dir'\n";
                return undef;
        }
    }
    if ( ! -d $pdb_so_dir ) {
        if ( !mkdir($pdb_so_dir, 0775) ) {
            print STDERR "can't create directory '$pdb_so_dir'\n";
                return undef;
        }
    }

    # collect files
    my @pdb_files;
    foreach my $repository (@{$self->{REPOSITORIES}}) {
        my $o = $self->{SOLARVERSION} . "/$repository";
        $repository =~ s/(.*?)\.(.*)/$1/;
        $self->collect_files( $o, $inpath, \@pdb_files);

        foreach (@pdb_files) {
            my $relocator = basename($_) . ".location";
            /$o\/(.*)/i;

            my $src_location = $1;

            my $location = "";
            my $target = "";
            if ( $src_location =~ /\/so\// )
            {
                $location = "../../../$repository$milestoneext/" . $src_location;
                $target = "$pdb_dir/so/$relocator";
            }
            else
            {
                $location = "../../$repository$milestoneext/" . $src_location;
                $target = "$pdb_dir/$relocator";
            }

            if ( !open(RELOCATOR, ">$target") ) {
                print STDERR "can't write file '$target'\n";
                return undef;
            }
            print RELOCATOR "$location\n";
            close(RELOCATOR);
        }
    }
    return 1;
}

sub collect_files_from_all_repositories
{
    my $self = shift;
    my ($platform, $filesref) = @_;
    my $repository;
    my $ret;
    foreach $repository (@{$self->{REPOSITORIES}}) {
        my $srcdir = $self->{SOLARVERSION} . "/$repository";
        $ret |= $self->collect_files ($srcdir, $platform, $filesref);
    }
    return $ret;
}

sub collect_files
{
    my $self = shift;
    my ($srcdir, $platform, $filesref) = @_;
    my $template = "$srcdir/*/$platform";
    if ( $ENV{GUI} eq "WNT" ) {
        # collect all pdb files on o:
        # regular glob does not work with two wildcard on WNT
        my @bin    = glob("$template/bin/*.pdb");
        my @bin_so = glob("$template/bin/so/*.pdb");
        # we are only interested in pdb files which are accompanied by
        # .exe or .dll which the same name
        foreach (@bin, @bin_so) {
            my $dir  = dirname($_);
            my $base = basename($_, ".pdb");
            my $exe = "$dir/$base.exe";
            my $dll = "$dir/$base.dll";
            if ( -e $exe || -e $dll ) {
                push(@$filesref, $_);
            }
        }
    }
    else {
        # collect all shared libraries on o:
        my @lib = glob("$template/lib/*.so*");
        my @lib_so = glob("$template/lib/so/*.so*");
        my @mac_lib = glob("$template/lib/*.dylib*");
        my @mac_lib_so = glob("$template/lib/so/*.dylib*");
        # collect all binary executables on o:
        my @bin = $self->find_binary_execs("$template/bin");
        my @bin_so = $self->find_binary_execs("$template/bin/so");
        @$filesref = (@lib, @lib_so, @mac_lib, @mac_lib_so, @bin, @bin_so);
    }
    return 1;
}

sub find_binary_execs
{
    my $self = shift;
    my $path = shift;
    my @files = glob("$path/*");
    my @execs = grep(-x $_, @files);
    my @elf_files = grep(`file $_` =~ /ELF/, @execs);
    my @MachO_files = grep(`file $_` =~ /Mach\-O/, @execs);
    return ( @elf_files, @MachO_files );
}

1; # required