1*b1cdbd2cSJim Jagielski#**************************************************************
2*b1cdbd2cSJim Jagielski#
3*b1cdbd2cSJim Jagielski#  Licensed to the Apache Software Foundation (ASF) under one
4*b1cdbd2cSJim Jagielski#  or more contributor license agreements.  See the NOTICE file
5*b1cdbd2cSJim Jagielski#  distributed with this work for additional information
6*b1cdbd2cSJim Jagielski#  regarding copyright ownership.  The ASF licenses this file
7*b1cdbd2cSJim Jagielski#  to you under the Apache License, Version 2.0 (the
8*b1cdbd2cSJim Jagielski#  "License"); you may not use this file except in compliance
9*b1cdbd2cSJim Jagielski#  with the License.  You may obtain a copy of the License at
10*b1cdbd2cSJim Jagielski#
11*b1cdbd2cSJim Jagielski#    http://www.apache.org/licenses/LICENSE-2.0
12*b1cdbd2cSJim Jagielski#
13*b1cdbd2cSJim Jagielski#  Unless required by applicable law or agreed to in writing,
14*b1cdbd2cSJim Jagielski#  software distributed under the License is distributed on an
15*b1cdbd2cSJim Jagielski#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*b1cdbd2cSJim Jagielski#  KIND, either express or implied.  See the License for the
17*b1cdbd2cSJim Jagielski#  specific language governing permissions and limitations
18*b1cdbd2cSJim Jagielski#  under the License.
19*b1cdbd2cSJim Jagielski#
20*b1cdbd2cSJim Jagielski#**************************************************************
21*b1cdbd2cSJim Jagielski
22*b1cdbd2cSJim Jagielski
23*b1cdbd2cSJim Jagielski
24*b1cdbd2cSJim Jagielski#*************************************************************************
25*b1cdbd2cSJim Jagielski#
26*b1cdbd2cSJim Jagielski# createPDBRelocators - create for pdb relocator files
27*b1cdbd2cSJim Jagielski#                       PDB relocator files are used to find debug infos
28*b1cdbd2cSJim Jagielski#                       for analysis of creash reports
29*b1cdbd2cSJim Jagielski#
30*b1cdbd2cSJim Jagielski# usage: create_pdb_relocators($inpath, $milestoneext, $pre);
31*b1cdbd2cSJim Jagielski#
32*b1cdbd2cSJim Jagielski#*************************************************************************
33*b1cdbd2cSJim Jagielski
34*b1cdbd2cSJim Jagielskipackage CreatePDBRelocators;
35*b1cdbd2cSJim Jagielski
36*b1cdbd2cSJim Jagielskiuse strict;
37*b1cdbd2cSJim Jagielskiuse File::Basename;
38*b1cdbd2cSJim Jagielskiuse SourceConfig;
39*b1cdbd2cSJim Jagielski
40*b1cdbd2cSJim Jagielskisub new
41*b1cdbd2cSJim Jagielski{
42*b1cdbd2cSJim Jagielski    my $Object = shift;
43*b1cdbd2cSJim Jagielski    my $solarversion = shift;
44*b1cdbd2cSJim Jagielski    my $workdir;
45*b1cdbd2cSJim Jagielski    my $relworkdir;
46*b1cdbd2cSJim Jagielski    my $self = {};
47*b1cdbd2cSJim Jagielski    my @basedirs;
48*b1cdbd2cSJim Jagielski    my @repos;
49*b1cdbd2cSJim Jagielski
50*b1cdbd2cSJim Jagielski    if (!defined ($solarversion)) {
51*b1cdbd2cSJim Jagielski        $solarversion = $ENV{SOLARVERSION};
52*b1cdbd2cSJim Jagielski    }
53*b1cdbd2cSJim Jagielski    if ( !$solarversion ) {
54*b1cdbd2cSJim Jagielski	    print STDERR "can't determine SOLARVERSION.\n";
55*b1cdbd2cSJim Jagielski        exit (1);
56*b1cdbd2cSJim Jagielski    }
57*b1cdbd2cSJim Jagielski
58*b1cdbd2cSJim Jagielski    $self->{SOLARVERSION} = $solarversion;
59*b1cdbd2cSJim Jagielski
60*b1cdbd2cSJim Jagielski    $workdir = $ENV{WORKDIR};
61*b1cdbd2cSJim Jagielski    if ( !$workdir ) {
62*b1cdbd2cSJim Jagielski	    print STDERR "can't determine WORKDIR.\n";
63*b1cdbd2cSJim Jagielski        exit (1);
64*b1cdbd2cSJim Jagielski    }
65*b1cdbd2cSJim Jagielski
66*b1cdbd2cSJim Jagielski    if ( $workdir =~ /^$solarversion/ ) {
67*b1cdbd2cSJim Jagielski        $relworkdir = $workdir;
68*b1cdbd2cSJim Jagielski        $relworkdir =~ s/^$solarversion\///;
69*b1cdbd2cSJim Jagielski    } else {
70*b1cdbd2cSJim Jagielski        print STDERR "ERROR: workdir outside $solarversion unsupported\n";
71*b1cdbd2cSJim Jagielski        exit (2);
72*b1cdbd2cSJim Jagielski    }
73*b1cdbd2cSJim Jagielski    my $SourceConfigObj = SourceConfig->new();
74*b1cdbd2cSJim Jagielski    @repos = $SourceConfigObj->get_repositories();
75*b1cdbd2cSJim Jagielski    if ( defined $ENV{UPDMINOREXT} ) {
76*b1cdbd2cSJim Jagielski        foreach my $onedir ( @repos ) {
77*b1cdbd2cSJim Jagielski            push( @basedirs, $onedir.$ENV{UPDMINOREXT} );
78*b1cdbd2cSJim Jagielski        }
79*b1cdbd2cSJim Jagielski    }
80*b1cdbd2cSJim Jagielski    # basdirs is repositories (dmake) + workdir (gnu make)
81*b1cdbd2cSJim Jagielski    push(@basedirs, $relworkdir);
82*b1cdbd2cSJim Jagielski    if (!scalar @basedirs) {
83*b1cdbd2cSJim Jagielski        print STDERR "no basedir and no working directory found.\n";
84*b1cdbd2cSJim Jagielski        exit (2);
85*b1cdbd2cSJim Jagielski    }
86*b1cdbd2cSJim Jagielski    $self->{BASEDIRS} = \@basedirs;
87*b1cdbd2cSJim Jagielski    bless($self, $Object);
88*b1cdbd2cSJim Jagielski    return $self;
89*b1cdbd2cSJim Jagielski}
90*b1cdbd2cSJim Jagielski
91*b1cdbd2cSJim Jagielskisub create_pdb_relocators
92*b1cdbd2cSJim Jagielski{
93*b1cdbd2cSJim Jagielski    my $self = shift;
94*b1cdbd2cSJim Jagielski    my $inpath   = shift;
95*b1cdbd2cSJim Jagielski    my $milestoneext    = shift;
96*b1cdbd2cSJim Jagielski    my $pre      = shift;
97*b1cdbd2cSJim Jagielski
98*b1cdbd2cSJim Jagielski    my $solarversion = $self->{SOLARVERSION};
99*b1cdbd2cSJim Jagielski    my $root_dir = "$solarversion/$inpath";
100*b1cdbd2cSJim Jagielski
101*b1cdbd2cSJim Jagielski    # sanitize path
102*b1cdbd2cSJim Jagielski    $root_dir =~ s/\\/\//g;
103*b1cdbd2cSJim Jagielski    my $o =~ s/\\/\//g;
104*b1cdbd2cSJim Jagielski	my $premilestoneext = $milestoneext;
105*b1cdbd2cSJim Jagielski	if ( $pre ne "" ) {
106*b1cdbd2cSJim Jagielski		$premilestoneext = ~ s/^\.//;
107*b1cdbd2cSJim Jagielski		$premilestoneext = ".pre$premilestoneext";
108*b1cdbd2cSJim Jagielski	}
109*b1cdbd2cSJim Jagielski	my $pdb_dir = $root_dir . "/pdb$premilestoneext";
110*b1cdbd2cSJim Jagielski	my $pdb_so_dir = $root_dir . "/pdb$premilestoneext/so";
111*b1cdbd2cSJim Jagielski
112*b1cdbd2cSJim Jagielski    # create pdb directories if necessary
113*b1cdbd2cSJim Jagielski    if ( ! -d $pdb_dir ) {
114*b1cdbd2cSJim Jagielski	    if ( !mkdir($pdb_dir, 0775) ) {
115*b1cdbd2cSJim Jagielski		    print STDERR "can't create directory '$pdb_dir'\n";
116*b1cdbd2cSJim Jagielski                return undef;
117*b1cdbd2cSJim Jagielski        }
118*b1cdbd2cSJim Jagielski    }
119*b1cdbd2cSJim Jagielski    if ( ! -d $pdb_so_dir ) {
120*b1cdbd2cSJim Jagielski	    if ( !mkdir($pdb_so_dir, 0775) ) {
121*b1cdbd2cSJim Jagielski		    print STDERR "can't create directory '$pdb_so_dir'\n";
122*b1cdbd2cSJim Jagielski                return undef;
123*b1cdbd2cSJim Jagielski        }
124*b1cdbd2cSJim Jagielski    }
125*b1cdbd2cSJim Jagielski
126*b1cdbd2cSJim Jagielski    # collect files
127*b1cdbd2cSJim Jagielski    foreach my $basedir (@{$self->{BASEDIRS}}) {
128*b1cdbd2cSJim Jagielski        my @pdb_files;
129*b1cdbd2cSJim Jagielski        my $o = $self->{SOLARVERSION} . "/$basedir";
130*b1cdbd2cSJim Jagielski        $basedir =~ s/(.*?)\.(.*)/$1/;
131*b1cdbd2cSJim Jagielski		$self->collect_files( $o, $inpath, \@pdb_files);
132*b1cdbd2cSJim Jagielski
133*b1cdbd2cSJim Jagielski		foreach (@pdb_files) {
134*b1cdbd2cSJim Jagielski			my $relocator = basename($_) . ".location";
135*b1cdbd2cSJim Jagielski			/$o\/(.*)/i;
136*b1cdbd2cSJim Jagielski
137*b1cdbd2cSJim Jagielski			my $src_location = $1;
138*b1cdbd2cSJim Jagielski
139*b1cdbd2cSJim Jagielski			my $location = "";
140*b1cdbd2cSJim Jagielski			my $target = "";
141*b1cdbd2cSJim Jagielski			if ( $src_location =~ /\/so\// )
142*b1cdbd2cSJim Jagielski			{
143*b1cdbd2cSJim Jagielski				$location = "../../../$basedir$milestoneext/" . $src_location;
144*b1cdbd2cSJim Jagielski				$target = "$pdb_dir/so/$relocator";
145*b1cdbd2cSJim Jagielski			}
146*b1cdbd2cSJim Jagielski			else
147*b1cdbd2cSJim Jagielski			{
148*b1cdbd2cSJim Jagielski				$location = "../../$basedir$milestoneext/" . $src_location;
149*b1cdbd2cSJim Jagielski				$target = "$pdb_dir/$relocator";
150*b1cdbd2cSJim Jagielski			}
151*b1cdbd2cSJim Jagielski
152*b1cdbd2cSJim Jagielski			if ( !open(RELOCATOR, ">$target") ) {
153*b1cdbd2cSJim Jagielski				print STDERR "can't write file '$target'\n";
154*b1cdbd2cSJim Jagielski				return undef;
155*b1cdbd2cSJim Jagielski			}
156*b1cdbd2cSJim Jagielski			print RELOCATOR "$location\n";
157*b1cdbd2cSJim Jagielski			close(RELOCATOR);
158*b1cdbd2cSJim Jagielski		}
159*b1cdbd2cSJim Jagielski    }
160*b1cdbd2cSJim Jagielski    return 1;
161*b1cdbd2cSJim Jagielski}
162*b1cdbd2cSJim Jagielski
163*b1cdbd2cSJim Jagielskisub collect_files_from_all_basedirs
164*b1cdbd2cSJim Jagielski{
165*b1cdbd2cSJim Jagielski	my $self = shift;
166*b1cdbd2cSJim Jagielski	my ($platform, $filesref) = @_;
167*b1cdbd2cSJim Jagielski	my $basedir;
168*b1cdbd2cSJim Jagielski	my $ret;
169*b1cdbd2cSJim Jagielski	foreach $basedir (@{$self->{BASEDIRS}}) {
170*b1cdbd2cSJim Jagielski		my $srcdir = $self->{SOLARVERSION} . "/$basedir";
171*b1cdbd2cSJim Jagielski		$ret |= $self->collect_files ($srcdir, $platform, $filesref);
172*b1cdbd2cSJim Jagielski	}
173*b1cdbd2cSJim Jagielski	return $ret;
174*b1cdbd2cSJim Jagielski}
175*b1cdbd2cSJim Jagielski
176*b1cdbd2cSJim Jagielskisub collect_files
177*b1cdbd2cSJim Jagielski{
178*b1cdbd2cSJim Jagielski    my $self = shift;
179*b1cdbd2cSJim Jagielski    my ($srcdir, $platform, $filesref) = @_;
180*b1cdbd2cSJim Jagielski    my $template = "$srcdir/*/$platform";
181*b1cdbd2cSJim Jagielski    my $template2 = "$srcdir/LinkTarget";
182*b1cdbd2cSJim Jagielski	if ( $ENV{GUI} eq "WNT" ) {
183*b1cdbd2cSJim Jagielski        # collect all pdb files on o:
184*b1cdbd2cSJim Jagielski        # regular glob does not work with two wildcard on WNT
185*b1cdbd2cSJim Jagielski        my @bin    = glob("$template/bin/*.pdb");
186*b1cdbd2cSJim Jagielski        my @bin_so = glob("$template/bin/so/*.pdb");
187*b1cdbd2cSJim Jagielski        my @workdir = glob("$template2/*/*.pdb");
188*b1cdbd2cSJim Jagielski        # we are only interested in pdb files which are accompanied by
189*b1cdbd2cSJim Jagielski        # .exe or .dll which the same name
190*b1cdbd2cSJim Jagielski        foreach (@bin, @bin_so, @workdir) {
191*b1cdbd2cSJim Jagielski            my $dir  = dirname($_);
192*b1cdbd2cSJim Jagielski            my $base = basename($_, ".pdb");
193*b1cdbd2cSJim Jagielski            my $exe = "$dir/$base.exe";
194*b1cdbd2cSJim Jagielski            my $dll = "$dir/$base.dll";
195*b1cdbd2cSJim Jagielski            if ( -e $exe || -e $dll ) {
196*b1cdbd2cSJim Jagielski                push(@$filesref, $_);
197*b1cdbd2cSJim Jagielski            }
198*b1cdbd2cSJim Jagielski        }
199*b1cdbd2cSJim Jagielski    }
200*b1cdbd2cSJim Jagielski    else {
201*b1cdbd2cSJim Jagielski        # collect all shared libraries on o:
202*b1cdbd2cSJim Jagielski        my @lib = glob("$template/lib/*.so*");
203*b1cdbd2cSJim Jagielski        my @workdir_lib = glob("$template2/Library/*.so*");
204*b1cdbd2cSJim Jagielski        my @lib_so = glob("$template/lib/so/*.so*");
205*b1cdbd2cSJim Jagielski        my @mac_lib = glob("$template/lib/*.dylib*");
206*b1cdbd2cSJim Jagielski        my @mac_workdir_lib = glob("$template2/Library/*.dylib*");
207*b1cdbd2cSJim Jagielski        my @mac_lib_so = glob("$template/lib/so/*.dylib*");
208*b1cdbd2cSJim Jagielski        # collect all binary executables on o:
209*b1cdbd2cSJim Jagielski        my @bin = $self->find_binary_execs("$template/bin");
210*b1cdbd2cSJim Jagielski        my @workdir_bin = $self->find_binary_execs("$template2/Executable");
211*b1cdbd2cSJim Jagielski        my @bin_so = $self->find_binary_execs("$template/bin/so");
212*b1cdbd2cSJim Jagielski        push(@$filesref, (@lib, @lib_so, @workdir_lib, @mac_lib, @mac_workdir_lib, @mac_lib_so, @bin, @workdir_bin, @bin_so));
213*b1cdbd2cSJim Jagielski    }
214*b1cdbd2cSJim Jagielski    return 1;
215*b1cdbd2cSJim Jagielski}
216*b1cdbd2cSJim Jagielski
217*b1cdbd2cSJim Jagielskisub find_binary_execs
218*b1cdbd2cSJim Jagielski{
219*b1cdbd2cSJim Jagielski    my $self = shift;
220*b1cdbd2cSJim Jagielski    my $path = shift;
221*b1cdbd2cSJim Jagielski    my @files = glob("$path/*");
222*b1cdbd2cSJim Jagielski    my @execs = grep(-x $_, @files);
223*b1cdbd2cSJim Jagielski    my @elf_files = grep(`file $_` =~ /ELF/, @execs);
224*b1cdbd2cSJim Jagielski    my @MachO_files = grep(`file $_` =~ /Mach\-O/, @execs);
225*b1cdbd2cSJim Jagielski    return ( @elf_files, @MachO_files );
226*b1cdbd2cSJim Jagielski}
227*b1cdbd2cSJim Jagielski
228*b1cdbd2cSJim Jagielski1; # required
229*b1cdbd2cSJim Jagielski
230