Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 16 May 2012 22:27:10 +0200
From:      Erik Cederstrand <erik@cederstrand.dk>
To:        toolchain@freebsd.org
Subject:   [patch] Add "-iremap src:dst" option to gcc
Message-ID:  <21DCECE3-64F2-4D50-879A-5F9EA6E442BD@cederstrand.dk>

next in thread | raw e-mail | index | archive | help

--Apple-Mail=_66DC85C9-BA2A-4669-9DD5-E0AB01E81FCA
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain;
	charset=us-ascii

Hi folks,

I found this patch by Joerg Sonnenberger to NetBSD's gcc which adds an =
"-iremap src:dst" option to gcc =
(http://www.mail-archive.com/source-changes-full@netbsd.org/msg08590.html)=
.

The option rewrites the prefix to __FILE__ which is stored in the object =
file. My use-case is to make __FILE__ a relative path instead of an =
absolute one, e.g. "src/foo/bar.c" instead of =
"/home/erik/freebsd/gcc_iremap/src/foo/bar.c". This is one of the =
requisites to create identical binaries from source code in different =
locations on my system.

The option can be used in e.g. make.conf as:

CFLAGS +=3D -iunwrap=3D${.IMPSRC}:src

Patch against HEAD is attached. Is anyone willing to review this and =
possibly commit?

Thanks,
Erik


--Apple-Mail=_66DC85C9-BA2A-4669-9DD5-E0AB01E81FCA
Content-Disposition: attachment;
	filename=iremap.diff
Content-Type: application/octet-stream;
	name="iremap.diff"
Content-Transfer-Encoding: 7bit

Index: contrib/gcc/doc/cpp.texi
===================================================================
--- contrib/gcc/doc/cpp.texi    (revision 235514)
+++ contrib/gcc/doc/cpp.texi    (working copy)
@@ -4167,6 +4167,7 @@
 @c man begin SYNOPSIS
 cpp [@option{-D}@var{macro}[=@var{defn}]@dots{}] [@option{-U}@var{macro}]
     [@option{-I}@var{dir}@dots{}] [@option{-iquote}@var{dir}@dots{}]
+    [@option{-iremap}@var{src}:@var{dst}]
     [@option{-W}@var{warn}@dots{}]
     [@option{-M}|@option{-MM}] [@option{-MG}] [@option{-MF} @var{filename}]
     [@option{-MP}] [@option{-MQ} @var{target}@dots{}]
Index: contrib/gcc/doc/gcc.1
===================================================================
--- contrib/gcc/doc/gcc.1       (revision 235514)
+++ contrib/gcc/doc/gcc.1       (working copy)
@@ -379,7 +379,8 @@
 \&\fB\-u\fR \fIsymbol\fR
 .IP "\fIDirectory Options\fR" 4
 .IX Item "Directory Options"
-\&\fB\-B\fR\fIprefix\fR  \fB\-I\fR\fIdir\fR  \fB\-iquote\fR\fIdir\fR  \fB\-L\fR\fIdir\fR
+\&\fB\-B\fR\fIprefix\fR  \fB\-I\fR\fIdir\fR  \fB\-iquote\fR\fIdir\fR  
+\fB\-iremap\fR\fIsrc:dst\fR  \fB\-L\fR\fIdir\fR
 \&\fB\-specs=\fR\fIfile\fR  \fB\-I\- \-\-sysroot=\fR\fIdir\fR
 .IP "\fITarget Options\fR" 4
 .IX Item "Target Options"
@@ -6165,6 +6166,11 @@
 \&\f(CW\*(C`#include\ "\f(CIfile\f(CW"\*(C'\fR; they are not searched for
 \&\f(CW\*(C`#include\ <\f(CIfile\f(CW>\*(C'\fR, before all directories specified by
 \&\fB\-I\fR and before the standard system directories.
+.IP "\fB\-iremap\fR \fIsrc:dst\fR" 4
+.IX Item "-iremap src:dst"
+Replace the prefix \fIsrc\fR in \fI__FILE__\fR with \fIdst\fR at expansion time.
+This option can be specified more than once. Processing stops at the first match.
+Processing stops at the first match.
 .IP "\fB\-fdollars\-in\-identifiers\fR" 4
 .IX Item "-fdollars-in-identifiers"
 Accept \fB$\fR in identifiers.
Index: contrib/gcc/doc/cppopts.texi
===================================================================
--- contrib/gcc/doc/cppopts.texi        (revision 235514)
+++ contrib/gcc/doc/cppopts.texi        (working copy)
@@ -506,6 +506,12 @@
 @xref{Search Path}.
 @end ifset
 
+@item -iremap @var{src}:@var{dst}
+@opindex iremap
+Replace the prefix @var{src} in __FILE__ with @var{dst} at expansion time.
+This option can be specified more than once.  Processing stops at the first
+match.
+
 @item -fdollars-in-identifiers
 @opindex fdollars-in-identifiers
 @anchor{fdollars-in-identifiers}
Index: contrib/gcc/doc/invoke.texi
===================================================================
--- contrib/gcc/doc/invoke.texi (revision 235514)
+++ contrib/gcc/doc/invoke.texi (working copy)
@@ -360,7 +360,8 @@
 
 @item Directory Options
 @xref{Directory Options,,Options for Directory Search}.
-@gccoptlist{-B@var{prefix}  -I@var{dir}  -iquote@var{dir}  -L@var{dir}
+@gccoptlist{-B@var{prefix}  -I@var{dir}  -iquote@var{dir}  
+-iremap@var{src}:@var{dst} -L@var{dir}
 -specs=@var{file}  -I- --sysroot=@var{dir}}
 
 @item Target Options
@@ -6406,6 +6407,12 @@
 "@var{file}"}; they are not searched for @samp{#include <@var{file}>},
 otherwise just like @option{-I}.
 
+@item -iremap @var{src}:@var{dst}
+@opindex iremap
+Replace the prefix @var{src} in __FILE__ with @var{dst} at expansion time.
+This option can be specified more than once.  Processing stops at the first
+match.
+
 @item -L@var{dir}
 @opindex L
 Add directory @var{dir} to the list of directories to be searched
Index: contrib/gcc/doc/cpp.1
===================================================================
--- contrib/gcc/doc/cpp.1       (revision 235514)
+++ contrib/gcc/doc/cpp.1       (working copy)
@@ -649,6 +649,11 @@
 \&\f(CW\*(C`#include\ "\f(CIfile\f(CW"\*(C'\fR; they are not searched for
 \&\f(CW\*(C`#include\ <\f(CIfile\f(CW>\*(C'\fR, before all directories specified by
 \&\fB\-I\fR and before the standard system directories.
+.IP "\fB\-iremap\fR \fIsrc:dst\fR" 4
+.IX Item "-iremap src:dst"
+Replace the prefix \fIsrc\fR in \fI__FILE__\fR with \fIdst\fR at expansion time.
+This option can be specified more than once. Processing stops at the first match.
+Processing stops at the first match.
 .IP "\fB\-fdollars\-in\-identifiers\fR" 4
 .IX Item "-fdollars-in-identifiers"
 Accept \fB$\fR in identifiers.
Index: contrib/gcc/gcc.h
===================================================================
--- contrib/gcc/gcc.h   (revision 235514)
+++ contrib/gcc/gcc.h   (working copy)
@@ -49,7 +49,7 @@
   || !strcmp (STR, "idirafter") || !strcmp (STR, "iprefix") \
   || !strcmp (STR, "iwithprefix") || !strcmp (STR, "iwithprefixbefore") \
   || !strcmp (STR, "iquote") || !strcmp (STR, "isystem") \
-  || !strcmp (STR, "isysroot") \
+  || !strcmp (STR, "iremap") || !strcmp (STR, "isysroot") \
   || !strcmp (STR, "-param") || !strcmp (STR, "specs") \
   || !strcmp (STR, "MF") || !strcmp (STR, "MT") || !strcmp (STR, "MQ"))
 
Index: contrib/gcc/c.opt
===================================================================
--- contrib/gcc/c.opt   (revision 235514)
+++ contrib/gcc/c.opt   (working copy)
@@ -781,6 +781,10 @@
 C ObjC C++ ObjC++ Joined Separate
 -iprefix <path>        Specify <path> as a prefix for next two options
 
+iremap
+C ObjC C++ ObjC++ Joined Separate
+-iremap <src:dst>  Convert <src> to <dst> if it occurs as prefix in __FILE__.
+
 isysroot
 C ObjC C++ ObjC++ Joined Separate
 -isysroot <dir>        Set <dir> to be the system root directory
Index: contrib/gcc/c-opts.c
===================================================================
--- contrib/gcc/c-opts.c        (revision 235514)
+++ contrib/gcc/c-opts.c        (working copy)
@@ -169,6 +169,7 @@
     case OPT_isysroot:
     case OPT_isystem:
     case OPT_iquote:
+    case OPT_iremap:
       error ("missing path after %qs", opt);
       break;
 
@@ -839,6 +840,10 @@
       add_path (xstrdup (arg), QUOTE, 0, true);
       break;
 
+    case OPT_iremap:
+      add_cpp_remap_path (arg);
+      break;
+
     case OPT_isysroot:
       sysroot = arg;
       break;
Index: contrib/gcclibs/libcpp/macro.c
===================================================================
--- contrib/gcclibs/libcpp/macro.c      (revision 235514)
+++ contrib/gcclibs/libcpp/macro.c      (working copy)
@@ -107,6 +107,61 @@
   "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
 };
 
+static size_t remap_pairs;
+static char **remap_src;
+static char **remap_dst;
+
+void
+add_cpp_remap_path (const char *arg)
+{
+   const char *arg_dst;
+   size_t len;
+
+   arg_dst = strchr(arg, ':');
+   if (arg_dst == NULL) {
+       fprintf(stderr, "Invalid argument for -iremap");
+       exit(1);
+   }
+   len = arg_dst - arg;
+   ++arg_dst;
+
+   remap_src = xrealloc(remap_src, sizeof(char *) * (remap_pairs + 1));
+   remap_dst = xrealloc(remap_dst, sizeof(char *) * (remap_pairs + 1));
+
+   remap_src[remap_pairs] = xmalloc(len + 1);
+   memcpy(remap_src[remap_pairs], arg, len);
+   remap_src[remap_pairs][len] = '\0';
+   remap_dst[remap_pairs] = xstrdup(arg_dst);
+   ++remap_pairs;
+}
+
+static const char *
+cpp_remap_file (const char *arg, char **tmp_name)
+{
+   char *result;
+   size_t i, len;
+
+   for (i = 0; i < remap_pairs; ++i) {
+       len = strlen (remap_src[i]);
+       if (strncmp (remap_src[i], arg, len))
+           continue;
+       if (arg[len] == '\0')
+           return xstrdup (remap_dst[i]);
+       if (arg[len] != '/')
+           continue;
+       arg += len;
+       len = strlen (remap_dst[i]);
+       result = xmalloc (len + strlen (arg) + 1);
+       memcpy(result, remap_dst[i], len);
+       strcpy(result + len, arg);
+       *tmp_name = result;
+
+       return result;
+   }
+
+   return arg;
+}
+
 /* Helper function for builtin_macro.  Returns the text generated by
    a builtin macro. */
 const uchar *
@@ -166,6 +221,7 @@
       {
        unsigned int len;
        const char *name;
+       char *tmp_name;
        uchar *buf;
        map = linemap_lookup (pfile->line_table, pfile->line_table->highest_line);
 
@@ -173,12 +229,14 @@
          while (! MAIN_FILE_P (map))
            map = INCLUDED_FROM (pfile->line_table, map);
 
-       name = map->to_file;
+       tmp_name = NULL;
+       name = cpp_remap_file (map->to_file, &tmp_name);
        len = strlen (name);
        buf = _cpp_unaligned_alloc (pfile, len * 2 + 3);
        result = buf;
        *buf = '"';
        buf = cpp_quote_string (buf + 1, (const unsigned char *) name, len);
+       free (tmp_name);
        *buf++ = '"';
        *buf = '\0';
       }
Index: contrib/gcclibs/libcpp/include/cpplib.h
===================================================================
--- contrib/gcclibs/libcpp/include/cpplib.h     (revision 235514)
+++ contrib/gcclibs/libcpp/include/cpplib.h     (working copy)
@@ -627,6 +627,9 @@
 /* Set the include paths.  */
 extern void cpp_set_include_chains (cpp_reader *, cpp_dir *, cpp_dir *, int);
 
+/* Provide src:dst pair for __FILE__ remapping.  */
+extern void add_cpp_remap_path (const char *);
+
 /* Call these to get pointers to the options, callback, and deps
    structures for a given reader.  These pointers are good until you
    call cpp_finish on that reader.  You can either edit the callbacks
--Apple-Mail=_66DC85C9-BA2A-4669-9DD5-E0AB01E81FCA--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?21DCECE3-64F2-4D50-879A-5F9EA6E442BD>