1#! /usr/bin/perl
2#
3# Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
4#
5# This program is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License, version 2.0,
7# as published by the Free Software Foundation.
8#
9# This program is also distributed with certain software (including
10# but not limited to OpenSSL) that is licensed under separate terms,
11# as designated in a particular file or component or in included license
12# documentation.  The authors of MySQL hereby grant you an additional
13# permission to link the program and your derivative works with the
14# separately licensed software that they have included with MySQL.
15#
16# This program is distributed in the hope that it will be useful,
17# but WITHOUT ANY WARRANTY; without even the implied warranty of
18# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19# GNU General Public License, version 2.0, for more details.
20#
21# You should have received a copy of the GNU General Public License
22# along with this program; if not, write to the Free Software
23# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
24#
25
26#
27# Take the given GCC command line and run it with all absolute paths
28# changed to relative paths. This makes sure that no part of the build
29# path leaks into the .o files, which it normally would through the
30# contents of __FILE__. (Debug information is also affected, but that
31# is already fixed through -fdebug-prefix-map=.)
32#
33# A more elegant solution would be -ffile-prefix-map=, but this is
34# not currently supported in GCC; see
35# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=70268.
36#
37
38use strict;
39use warnings;
40use Cwd;
41
42my $cwd = getcwd();
43
44my @newarg = ($ARGV[0]);
45for my $i (1..$#ARGV) {
46	my $arg = $ARGV[$i];
47	if ($arg =~ /-I(.+)$/) {
48		$arg = '-I' . relativize($1, $cwd);
49	} elsif ($arg =~ /^\//) {
50		$arg = relativize($arg, $cwd);
51	}
52	push @newarg, $arg;
53}
54
55exec(@newarg);
56
57# /a/b/c/foo from /a/b/d = ../c/foo
58sub relativize {
59	my ($dir1, $dir2) = @_;
60
61	if ($dir1 !~ /^\//) {
62		# Not an absolute path.
63		return $dir1;
64	}
65
66	if (! -e $dir1) {
67		print STDERR "Unknown file/directory $dir1.\n";
68		return $dir1;
69	}
70	# Resolve symlinks and such, because getcwd() does.
71	$dir1 = Cwd::abs_path($dir1);
72
73	if ($dir1 =~ /^\/(lib|tmp|usr)/) {
74		# Not related to our source code.
75		return $dir1;
76	}
77
78	if ($dir1 eq $dir2) {
79		return ".";
80	}
81
82	my (@dir1_components) = split /\//, $dir1;
83	my (@dir2_components) = split /\//, $dir2;
84
85	# Remove common leading components.
86	while (scalar @dir1_components > 0 && scalar @dir2_components > 0 &&
87	       $dir1_components[0] eq $dir2_components[0]) {
88		shift @dir1_components;
89		shift @dir2_components;
90	}
91
92	my $ret = "";
93	for my $i (0..$#dir2_components) {
94		$ret .= '../';
95	}
96	$ret .= join('/', @dir1_components);
97
98	# print STDERR "[$dir1] from [$dir2] => [$ret]\n";
99
100	return $ret;
101}
102
103