xref: /trunk/main/solenv/bin/relocate (revision cdf0e10c)
1*cdf0e10cSrcweir:
2*cdf0e10cSrcweir    eval 'exec perl -S $0 ${1+"$@"}'
3*cdf0e10cSrcweir        if 0;
4*cdf0e10cSrcweir
5*cdf0e10cSrcweir#*************************************************************************
6*cdf0e10cSrcweir#
7*cdf0e10cSrcweir#    This tool makes it easy to cleanly re-locate a
8*cdf0e10cSrcweir# build, eg. after you have copied or moved it to a new
9*cdf0e10cSrcweir# path. It tries to re-write all the hard-coded path logic
10*cdf0e10cSrcweir# internally.
11*cdf0e10cSrcweir#
12*cdf0e10cSrcweir#*************************************************************************
13*cdf0e10cSrcweir#
14*cdf0e10cSrcweir# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
15*cdf0e10cSrcweir#
16*cdf0e10cSrcweir# Copyright 2000, 2010 Oracle and/or its affiliates.
17*cdf0e10cSrcweir#
18*cdf0e10cSrcweir# OpenOffice.org - a multi-platform office productivity suite
19*cdf0e10cSrcweir#
20*cdf0e10cSrcweir# This file is part of OpenOffice.org.
21*cdf0e10cSrcweir#
22*cdf0e10cSrcweir# OpenOffice.org is free software: you can redistribute it and/or modify
23*cdf0e10cSrcweir# it under the terms of the GNU Lesser General Public License version 3
24*cdf0e10cSrcweir# only, as published by the Free Software Foundation.
25*cdf0e10cSrcweir#
26*cdf0e10cSrcweir# OpenOffice.org is distributed in the hope that it will be useful,
27*cdf0e10cSrcweir# but WITHOUT ANY WARRANTY; without even the implied warranty of
28*cdf0e10cSrcweir# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29*cdf0e10cSrcweir# GNU Lesser General Public License version 3 for more details
30*cdf0e10cSrcweir# (a copy is included in the LICENSE file that accompanied this code).
31*cdf0e10cSrcweir#
32*cdf0e10cSrcweir# You should have received a copy of the GNU Lesser General Public License
33*cdf0e10cSrcweir# version 3 along with OpenOffice.org.  If not, see
34*cdf0e10cSrcweir# <http://www.openoffice.org/license.html>
35*cdf0e10cSrcweir# for a copy of the LGPLv3 License.
36*cdf0e10cSrcweir#
37*cdf0e10cSrcweir#*************************************************************************
38*cdf0e10cSrcweir
39*cdf0e10cSrcweirsub sniff_set($)
40*cdf0e10cSrcweir{
41*cdf0e10cSrcweir    my $build_dir = shift;
42*cdf0e10cSrcweir    my ($dirhandle, $fname);
43*cdf0e10cSrcweir
44*cdf0e10cSrcweir    opendir ($dirhandle, $build_dir) || die "Can't open $build_dir";
45*cdf0e10cSrcweir    while ($fname = readdir ($dirhandle)) {
46*cdf0e10cSrcweir	$fname =~ /[Ss]et.sh$/ && last;
47*cdf0e10cSrcweir    }
48*cdf0e10cSrcweir    closedir ($dirhandle);
49*cdf0e10cSrcweir
50*cdf0e10cSrcweir    return $fname;
51*cdf0e10cSrcweir}
52*cdf0e10cSrcweir
53*cdf0e10cSrcweirsub sed_file($$$)
54*cdf0e10cSrcweir{
55*cdf0e10cSrcweir    my ($old_fname, $function, $state) = @_;
56*cdf0e10cSrcweir    my $tmp_fname = "$old_fname.new";
57*cdf0e10cSrcweir    my $old_file;
58*cdf0e10cSrcweir    my $new_file;
59*cdf0e10cSrcweir
60*cdf0e10cSrcweir    open ($old_file, $old_fname) || die "Can't open $old_fname: $!";
61*cdf0e10cSrcweir    open ($new_file, ">$tmp_fname") || die "Can't open $tmp_fname: $!";
62*cdf0e10cSrcweir
63*cdf0e10cSrcweir    while (<$old_file>) {
64*cdf0e10cSrcweir	my $value = &$function($state, $_);
65*cdf0e10cSrcweir	print $new_file $value;
66*cdf0e10cSrcweir    }
67*cdf0e10cSrcweir
68*cdf0e10cSrcweir    close ($new_file) || die "Failed to close $tmp_fname: $!";
69*cdf0e10cSrcweir    close ($old_file) || die "Failed to close $old_fname: $!";
70*cdf0e10cSrcweir
71*cdf0e10cSrcweir    rename $tmp_fname, $old_fname || die "Failed to replace $old_fname: $!";
72*cdf0e10cSrcweir}
73*cdf0e10cSrcweir
74*cdf0e10cSrcweirsub rewrite_value($$)
75*cdf0e10cSrcweir{
76*cdf0e10cSrcweir    my ($state, $value) = @_;
77*cdf0e10cSrcweir
78*cdf0e10cSrcweir    $value =~ s/$state->{'old_root'}/$state->{'new_root'}/g;
79*cdf0e10cSrcweir    $value =~ s/$state->{'win32_old_root'}/$state->{'win32_new_root'}/g;
80*cdf0e10cSrcweir
81*cdf0e10cSrcweir    return $value;
82*cdf0e10cSrcweir}
83*cdf0e10cSrcweir
84*cdf0e10cSrcweirsub rewrite_set($$$)
85*cdf0e10cSrcweir{
86*cdf0e10cSrcweir    my $new_root = shift;
87*cdf0e10cSrcweir    my $old_root = shift;
88*cdf0e10cSrcweir    my $set = shift;
89*cdf0e10cSrcweir    my $tmp;
90*cdf0e10cSrcweir    my %state;
91*cdf0e10cSrcweir
92*cdf0e10cSrcweir    print "   $set\n";
93*cdf0e10cSrcweir
94*cdf0e10cSrcweir# unix style
95*cdf0e10cSrcweir    $state{'old_root'} = $old_root;
96*cdf0e10cSrcweir    $state{'new_root'} = $new_root;
97*cdf0e10cSrcweir# win32 style
98*cdf0e10cSrcweir    $tmp = $old_root;
99*cdf0e10cSrcweir    $tmp =~ s/\//\\\\\\\\\\\\\\\\/g;
100*cdf0e10cSrcweir    $state{'win32_old_root'} = $tmp;
101*cdf0e10cSrcweir    $tmp = $new_root;
102*cdf0e10cSrcweir    $tmp =~ s/\//\\\\\\\\/g;
103*cdf0e10cSrcweir    $state{'win32_new_root'} = $tmp;
104*cdf0e10cSrcweir
105*cdf0e10cSrcweir    sed_file ("$new_root/$set", \&rewrite_value, \%state);
106*cdf0e10cSrcweir
107*cdf0e10cSrcweir    my $tcsh_set = $set;
108*cdf0e10cSrcweir    $tcsh_set =~ s/\.sh$//;
109*cdf0e10cSrcweir
110*cdf0e10cSrcweir    print "   $tcsh_set\n";
111*cdf0e10cSrcweir
112*cdf0e10cSrcweir    sed_file ("$new_root/$tcsh_set", \&rewrite_value, \%state);
113*cdf0e10cSrcweir}
114*cdf0e10cSrcweir
115*cdf0e10cSrcweirsub find_old_root($$)
116*cdf0e10cSrcweir{
117*cdf0e10cSrcweir    my $new_root = shift;
118*cdf0e10cSrcweir    my $set = shift;
119*cdf0e10cSrcweir    my $fname = "$new_root/$set";
120*cdf0e10cSrcweir    my $old_root;
121*cdf0e10cSrcweir    my $file;
122*cdf0e10cSrcweir
123*cdf0e10cSrcweir    open ($file, $fname) || die "Can't open $fname: $!";
124*cdf0e10cSrcweir
125*cdf0e10cSrcweir    while (<$file>) {
126*cdf0e10cSrcweir	if (/\s*([^=]+)\s*=\s*\"([^\"]+)\"/) {
127*cdf0e10cSrcweir	    my ($name, $value) = ($1, $2);
128*cdf0e10cSrcweir
129*cdf0e10cSrcweir	    if ($name eq 'SRC_ROOT') {
130*cdf0e10cSrcweir		$old_root = $value;
131*cdf0e10cSrcweir		last;
132*cdf0e10cSrcweir	    }
133*cdf0e10cSrcweir	}
134*cdf0e10cSrcweir    }
135*cdf0e10cSrcweir
136*cdf0e10cSrcweir    close ($file) || die "Failed to close $fname: $!";
137*cdf0e10cSrcweir
138*cdf0e10cSrcweir    return $old_root;
139*cdf0e10cSrcweir}
140*cdf0e10cSrcweir
141*cdf0e10cSrcweirsub rewrite_product_deps($$$)
142*cdf0e10cSrcweir{
143*cdf0e10cSrcweir    my $new_root = shift;
144*cdf0e10cSrcweir    my $product_path = shift;
145*cdf0e10cSrcweir    my $old_root = shift;
146*cdf0e10cSrcweir
147*cdf0e10cSrcweir    my $path = "$new_root/$product_path/misc";
148*cdf0e10cSrcweir    my $misc_dir;
149*cdf0e10cSrcweir    opendir ($misc_dir, $path) || return;
150*cdf0e10cSrcweir    my $name;
151*cdf0e10cSrcweir    while ($name = readdir ($misc_dir)) {
152*cdf0e10cSrcweir# Should try re-writing these - but perhaps this would
153*cdf0e10cSrcweir# screw with timestamps ?
154*cdf0e10cSrcweir    if ($name =~ m/\.dpcc$/ || $name =~ m/\.dpslo$/ || $name =~ m/\.dpobj$/) {
155*cdf0e10cSrcweir		unlink ("$path/$name");
156*cdf0e10cSrcweir	}
157*cdf0e10cSrcweir    }
158*cdf0e10cSrcweir    closedir ($misc_dir);
159*cdf0e10cSrcweir}
160*cdf0e10cSrcweir
161*cdf0e10cSrcweirsub rewrite_dpcc($$)
162*cdf0e10cSrcweir{
163*cdf0e10cSrcweir    my $new_root = shift;
164*cdf0e10cSrcweir    my $old_root = shift;
165*cdf0e10cSrcweir
166*cdf0e10cSrcweir    my $top_dir;
167*cdf0e10cSrcweir    my $idx = 0;
168*cdf0e10cSrcweir    opendir ($top_dir, $new_root) || die "Can't open $new_root: $!";
169*cdf0e10cSrcweir    my $name;
170*cdf0e10cSrcweir    while ($name = readdir ($top_dir)) {
171*cdf0e10cSrcweir	my $sub_dir;
172*cdf0e10cSrcweir	opendir ($sub_dir, "$new_root/$name") || next;
173*cdf0e10cSrcweir	my $sub_name;
174*cdf0e10cSrcweir	while ($sub_name = readdir ($sub_dir)) {
175*cdf0e10cSrcweir	    if ($sub_name =~ /\.pro$/) {
176*cdf0e10cSrcweir		$idx || print "\n    ";
177*cdf0e10cSrcweir		if ($idx++ == 6) {
178*cdf0e10cSrcweir		    $idx = 0;
179*cdf0e10cSrcweir		}
180*cdf0e10cSrcweir		print "$name ";
181*cdf0e10cSrcweir		rewrite_product_deps ($new_root, "$name/$sub_name", $old_root);
182*cdf0e10cSrcweir	    }
183*cdf0e10cSrcweir	}
184*cdf0e10cSrcweir	closedir ($sub_dir);
185*cdf0e10cSrcweir    }
186*cdf0e10cSrcweir    closedir ($top_dir);
187*cdf0e10cSrcweir}
188*cdf0e10cSrcweir
189*cdf0e10cSrcweirsub rewrite_bootstrap($$)
190*cdf0e10cSrcweir{
191*cdf0e10cSrcweir    my $new_root = shift;
192*cdf0e10cSrcweir    my $old_root = shift;
193*cdf0e10cSrcweir
194*cdf0e10cSrcweir    print "   bootstrap\n";
195*cdf0e10cSrcweir
196*cdf0e10cSrcweir    my %state;
197*cdf0e10cSrcweir    $state{'old_root'} = $old_root;
198*cdf0e10cSrcweir    $state{'new_root'} = $new_root;
199*cdf0e10cSrcweir
200*cdf0e10cSrcweir    my $rewrite = sub { my $state = shift; my $value = shift;
201*cdf0e10cSrcweir			$value =~ s/$state->{'old_root'}/$state->{'new_root'}/g;
202*cdf0e10cSrcweir			return $value; };
203*cdf0e10cSrcweir    sed_file ("$new_root/bootstrap", $rewrite, \%state);
204*cdf0e10cSrcweir    `chmod +x $new_root/bootstrap`;
205*cdf0e10cSrcweir}
206*cdf0e10cSrcweir
207*cdf0e10cSrcweirfor $a (@ARGV) {
208*cdf0e10cSrcweir    if ($a eq '--help' || $a eq '-h') {
209*cdf0e10cSrcweir	print "relocate: syntax\n";
210*cdf0e10cSrcweir	print "  relocate /path/to/new/ooo/source_root\n";
211*cdf0e10cSrcweir    }
212*cdf0e10cSrcweir}
213*cdf0e10cSrcweir
214*cdf0e10cSrcweir$OOO_BUILD = shift (@ARGV) || die "Pass path to relocated source tree";
215*cdf0e10cSrcweirsubstr ($OOO_BUILD, 0, 1) eq '/' || die "relocate requires absolute paths";
216*cdf0e10cSrcweir
217*cdf0e10cSrcweirmy $set;
218*cdf0e10cSrcweir
219*cdf0e10cSrcweir$set = sniff_set($OOO_BUILD) || die "Can't find env. set";
220*cdf0e10cSrcweir$OLD_ROOT = find_old_root($OOO_BUILD, $set);
221*cdf0e10cSrcweir
222*cdf0e10cSrcweirprint "Relocate: $OLD_ROOT -> $OOO_BUILD\n";
223*cdf0e10cSrcweir
224*cdf0e10cSrcweirprint "re-writing environment:\n";
225*cdf0e10cSrcweir
226*cdf0e10cSrcweirrewrite_set($OOO_BUILD, $OLD_ROOT, $set);
227*cdf0e10cSrcweirrewrite_bootstrap($OOO_BUILD, $OLD_ROOT);
228*cdf0e10cSrcweir
229*cdf0e10cSrcweirprint "re-writing dependencies:\n";
230*cdf0e10cSrcweir
231*cdf0e10cSrcweirrewrite_dpcc($OOO_BUILD, $OLD_ROOT);
232*cdf0e10cSrcweir
233*cdf0e10cSrcweirprint "done.\n";
234