2 eval 'exec perl -S $0 ${1+"$@"}'
5 # This file is part of the LibreOffice project.
7 # This Source Code Form is subject to the terms of the Mozilla Public
8 # License, v. 2.0. If a copy of the MPL was not distributed with this
9 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
11 # This file incorporates work covered by the following license notice:
13 # Licensed to the Apache Software Foundation (ASF) under one or more
14 # contributor license agreements. See the NOTICE file distributed
15 # with this work for additional information regarding copyright
16 # ownership. The ASF licenses this file to you under the Apache
17 # License, Version 2.0 (the "License"); you may not use this file
18 # except in compliance with the License. You may obtain a copy of
19 # the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #*************************************************************************
23 # This tool makes it easy to cleanly re-locate a
24 # build, eg. after you have copied or moved it to a new
25 # path. It tries to re-write all the hard-coded path logic
31 my $build_dir = shift;
32 my ($dirhandle, $fname);
34 opendir ($dirhandle, $build_dir) || die "Can't open $build_dir";
35 while ($fname = readdir ($dirhandle)) {
36 $fname =~ /Set.sh$/ && last;
38 closedir ($dirhandle);
45 my ($old_fname, $function, $state) = @_;
46 my $tmp_fname = "$old_fname.new";
50 open ($old_file, $old_fname) || die "Can't open $old_fname: $!";
51 open ($new_file, ">$tmp_fname") || die "Can't open $tmp_fname: $!";
54 my $value = &$function($state, $_);
55 print $new_file $value;
58 close ($new_file) || die "Failed to close $tmp_fname: $!";
59 close ($old_file) || die "Failed to close $old_fname: $!";
61 rename $tmp_fname, $old_fname || die "Failed to replace $old_fname: $!";
66 my ($state, $value) = @_;
68 $value =~ s/$state->{'old_root'}/$state->{'new_root'}/g;
69 $value =~ s/$state->{'win32_old_root'}/$state->{'win32_new_root'}/g;
85 $state{'old_root'} = $old_root;
86 $state{'new_root'} = $new_root;
89 $tmp =~ s/\//\\\\\\\\\\\\\\\\/g;
90 $state{'win32_old_root'} = $tmp;
92 $tmp =~ s/\//\\\\\\\\/g;
93 $state{'win32_new_root'} = $tmp;
95 sed_file ("$new_root/$set", \&rewrite_value, \%state);
100 my $new_root = shift;
102 my $fname = "$new_root/$set";
106 open ($file, $fname) || die "Can't open $fname: $!";
109 if (/\s*([^=]+)\s*=\s*\"([^\"]+)\"/) {
110 my ($name, $value) = ($1, $2);
112 $env_keys{$name} = $value;
116 close ($file) || die "Failed to close $fname: $!";
121 sub sed_file_no_touch($$$)
123 my ($new_root, $old_root, $file) = @_;
126 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
127 $atime,$mtime,$ctime,$blksize,$blocks) = stat ($file);
129 open ($fin, $file) || die "Can't open $fin: $!";
130 open ($fout, ">$file.sed.bak") || die "Can't open $file.sed.bak: $!";
132 s/$old_root/$new_root/g;
137 rename ("$file.sed.bak", $file);
138 # print "rename $file.sed.bak to $file\n";
140 utime $atime, $mtime, $file;
143 sub sed_no_touch_recursive ($$$)
145 my ($new_root, $old_root, $dir) = @_;
147 opendir ($dh, $dir) || die "Can't open dir: $dir: $!";
148 while (my $entry = readdir ($dh)) {
149 $entry =~ /^\./ && next;
150 my $path = "$dir/$entry";
151 sed_no_touch_recursive ($new_root, $old_root, $path) if (-d $path);
152 sed_file_no_touch ($new_root, $old_root, $path) if (-f $path);
157 sub rewrite_product_deps($$$)
159 my $new_root = shift;
160 my $product_path = shift;
161 my $old_root = shift;
163 my $path = "$new_root/$product_path/misc";
165 opendir ($misc_dir, $path) || return;
167 while ($name = readdir ($misc_dir)) {
168 # Should try re-writing these - but perhaps this would
169 # screw with timestamps ?
170 if ($name =~ m/\.dpcc$/ || $name =~ m/\.dpslo$/ || $name =~ m/\.dpobj$/) {
171 sed_file_no_touch ($new_root, $old_root, "$path/$name");
174 closedir ($misc_dir);
179 my $new_root = shift;
180 my $old_root = shift;
184 opendir ($top_dir, $new_root) || die "Can't open $new_root: $!";
186 while ($name = readdir ($top_dir)) {
188 opendir ($sub_dir, "$new_root/$name") || next;
190 while ($sub_name = readdir ($sub_dir)) {
191 if ($sub_name =~ /\.pro$/) {
197 rewrite_product_deps ($new_root, "$name/$sub_name", $old_root);
206 sub rewrite_symlinks($$)
208 my $new_root = shift;
209 my $old_root = shift;
212 opendir ($dirh, $new_root);
213 while (my $ent = readdir ($dirh)) {
214 $ent =~ /^\./ && next;
215 my $link = "$new_root/$ent";
217 my $target = readlink ($link);
218 my $newtarget = $target;
219 $newtarget =~ s/$old_root/$new_root/;
220 if ($target =~ m/$new_root/) {
221 print STDERR "skip correct link $target\n";
222 } elsif ($newtarget eq $target) {
223 print STDERR "unusual - possibly stale link: $target\n";
224 if ($target =~ m/\/clone\//) { die "failed to rename link"; }
226 print "Re-write link $target to $newtarget\n";
228 symlink ($newtarget, $link);
234 sub rewrite_bootstrap($$)
236 my $new_root = shift;
237 my $old_root = shift;
239 print " bootstrap\n";
242 $state{'old_root'} = $old_root;
243 $state{'new_root'} = $new_root;
245 my $rewrite = sub { my $state = shift; my $value = shift;
246 $value =~ s/$state->{'old_root'}/$state->{'new_root'}/g;
248 sed_file ("$new_root/bootstrap", $rewrite, \%state);
249 `chmod +x $new_root/bootstrap`;
253 if ($a eq '--help' || $a eq '-h') {
254 print "relocate: syntax\n";
255 print " relocate /path/to/new/ooo/source_root\n";
259 $OOO_BUILD = shift (@ARGV) || die "Pass path to relocated source tree";
260 substr ($OOO_BUILD, 0, 1) eq '/' || die "relocate requires absolute paths";
264 $set = sniff_set($OOO_BUILD) || die "Can't find env. set";
265 my $env_keys = read_set ($OOO_BUILD, $set);
266 $OLD_ROOT = $env_keys->{'SRC_ROOT'};
267 my $solver = $env_keys->{SOLARVER} . "/" . $env_keys->{INPATH};
269 print "Relocate: $OLD_ROOT -> $OOO_BUILD\n";
270 if ($OLD_ROOT eq $OOO_BUILD) {
271 print "nothing to do\n";
275 print "re-writing symlinks\n";
276 rewrite_symlinks($OOO_BUILD, $OLD_ROOT);
278 print "re-writing dependencies:\n";
279 rewrite_dpcc($OOO_BUILD, $OLD_ROOT);
281 if (-d "$solver/workdir/Dep") {
282 print "re-writing new dependencies:\n";
283 sed_no_touch_recursive ($OOO_BUILD, $OLD_ROOT, "$solver/workdir/Dep");
286 print "re-writing environment:\n";
287 rewrite_set($OOO_BUILD, $OLD_ROOT, $set);
288 rewrite_bootstrap($OOO_BUILD, $OLD_ROOT);