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