tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: Prefix substitution support for cpp(1)



On Sun, Oct 25, 2009 at 05:24:19PM +0100, Joerg Sonnenberger wrote:
> I have a patch in one of my working trees to allow changing the prefix
> for __FILE__ based on prefix patterns. This allows building packages
> without leaking the full path, so that changes src and obj location
> don't alter build.

Attached is a cleaned up patch. Suggestions for a better option name?

Joerg
Index: dist/gcc4/gcc/c-opts.c
===================================================================
RCS file: /home/joerg/repo/netbsd/src/gnu/dist/gcc4/gcc/c-opts.c,v
retrieving revision 1.4
diff -u -p -r1.4 c-opts.c
--- dist/gcc4/gcc/c-opts.c      21 Jun 2007 04:02:00 -0000      1.4
+++ dist/gcc4/gcc/c-opts.c      30 Oct 2009 19:44:08 -0000
@@ -163,6 +163,7 @@ c_common_missing_argument (const char *o
     case OPT_isysroot:
     case OPT_isystem:
     case OPT_iquote:
+    case OPT_iunwrap:
       error ("missing path after %qs", opt);
       break;
 
@@ -841,6 +842,10 @@ c_common_handle_option (size_t scode, co
       add_path (xstrdup (arg), SYSTEM, 0, true);
       break;
 
+    case OPT_iunwrap:
+      add_cpp_unwrap_path (arg);
+      break;
+
     case OPT_iwithprefix:
       add_prefixed_path (arg, SYSTEM);
       break;
Index: dist/gcc4/gcc/c.opt
===================================================================
RCS file: /home/joerg/repo/netbsd/src/gnu/dist/gcc4/gcc/c.opt,v
retrieving revision 1.3
diff -u -p -r1.3 c.opt
--- dist/gcc4/gcc/c.opt 21 Jun 2007 04:02:00 -0000      1.3
+++ dist/gcc4/gcc/c.opt 30 Oct 2009 19:37:36 -0000
@@ -783,6 +783,10 @@ iquote
 C ObjC C++ ObjC++ Joined Separate
 -iquote <dir>  Add <dir> to the end of the quote include path
 
+iunwrap
+C ObjC C++ ObjC++ Joined Separate
+-iunwrap <src:dst>     Convert <src> to <dst> if it occurs as prefix in 
__FILE__.
+
 iwithprefix
 C ObjC C++ ObjC++ Joined Separate
 -iwithprefix <dir>     Add <dir> to the end of the system include path
Index: dist/gcc4/gcc/gcc.h
===================================================================
RCS file: /home/joerg/repo/netbsd/src/gnu/dist/gcc4/gcc/gcc.h,v
retrieving revision 1.2
diff -u -p -r1.2 gcc.h
--- dist/gcc4/gcc/gcc.h 12 May 2006 00:33:26 -0000      1.2
+++ dist/gcc4/gcc/gcc.h 30 Oct 2009 19:37:59 -0000
@@ -50,7 +50,7 @@ struct spec_function
   || !strcmp (STR, "idirafter") || !strcmp (STR, "iprefix") \
   || !strcmp (STR, "iwithprefix") || !strcmp (STR, "iwithprefixbefore") \
   || !strcmp (STR, "iquote") || !strcmp (STR, "isystem") \
-  || !strcmp (STR, "isysroot") \
+  || !strcmp (STR, "-iunwrap") || !strcmp (STR, "isysroot") \
   || !strcmp (STR, "-param") || !strcmp (STR, "specs") \
   || !strcmp (STR, "MF") || !strcmp (STR, "MT") || !strcmp (STR, "MQ"))
 
Index: dist/gcc4/gcc/doc/cpp.texi
===================================================================
RCS file: /home/joerg/repo/netbsd/src/gnu/dist/gcc4/gcc/doc/cpp.texi,v
retrieving revision 1.4
diff -u -p -r1.4 cpp.texi
--- dist/gcc4/gcc/doc/cpp.texi  21 Jun 2007 04:02:02 -0000      1.4
+++ dist/gcc4/gcc/doc/cpp.texi  30 Oct 2009 21:58:20 -0000
@@ -4196,6 +4196,7 @@ without notice.
 @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{-iunwrap}@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: dist/gcc4/gcc/doc/cppopts.texi
===================================================================
RCS file: /home/joerg/repo/netbsd/src/gnu/dist/gcc4/gcc/doc/cppopts.texi,v
retrieving revision 1.2
diff -u -p -r1.2 cppopts.texi
--- dist/gcc4/gcc/doc/cppopts.texi      12 May 2006 00:33:26 -0000      1.2
+++ dist/gcc4/gcc/doc/cppopts.texi      30 Oct 2009 21:58:32 -0000
@@ -513,6 +513,12 @@ Search @var{dir} only for header files r
 @xref{Search Path}.
 @end ifset
 
+@item -iunwrap @var{src}:@var{dst}
+@opindex iunwrap
+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: dist/gcc4/gcc/doc/invoke.texi
===================================================================
RCS file: /home/joerg/repo/netbsd/src/gnu/dist/gcc4/gcc/doc/invoke.texi,v
retrieving revision 1.8
diff -u -p -r1.8 invoke.texi
--- dist/gcc4/gcc/doc/invoke.texi       29 Apr 2009 23:53:28 -0000      1.8
+++ dist/gcc4/gcc/doc/invoke.texi       30 Oct 2009 21:57:20 -0000
@@ -376,7 +376,8 @@ Objective-C and Objective-C++ Dialects}.
 
 @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}  
+-iunwrap@var{src}:@var{dst}  -L@var{dir}
 -specs=@var{file}  -I- --sysroot=@var{dir}}
 
 @item Target Options
@@ -6506,6 +6507,12 @@ be searched for header files only for th
 "@var{file}"}; they are not searched for @samp{#include <@var{file}>},
 otherwise just like @option{-I}.
 
+@item -iunwrap @var{src}:@var{dst}
+@opindex iunwrap
+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: dist/gcc4/libcpp/macro.c
===================================================================
RCS file: /home/joerg/repo/netbsd/src/gnu/dist/gcc4/libcpp/macro.c,v
retrieving revision 1.2
diff -u -p -r1.2 macro.c
--- dist/gcc4/libcpp/macro.c    30 May 2008 15:12:24 -0000      1.2
+++ dist/gcc4/libcpp/macro.c    30 Oct 2009 21:52:49 -0000
@@ -109,6 +109,61 @@ static const char * const monthnames[] =
   "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
 };
 
+static size_t unwrap_pairs;
+static char **unwrap_src;
+static char **unwrap_dst;
+
+void
+add_cpp_unwrap_path (const char *arg)
+{
+       const char *arg_dst;
+       size_t len;
+
+       arg_dst = strchr(arg, ':');
+       if (arg_dst == NULL) {
+               fprintf(stderr, "Invalid argument for -iunwrap");
+               exit(1);
+       }
+       len = arg_dst - arg;
+       ++arg_dst;
+
+       unwrap_src = xrealloc(unwrap_src, sizeof(char *) * (unwrap_pairs + 1));
+       unwrap_dst = xrealloc(unwrap_dst, sizeof(char *) * (unwrap_pairs + 1));
+
+       unwrap_src[unwrap_pairs] = xmalloc(len + 1);
+       memcpy(unwrap_src[unwrap_pairs], arg, len);
+       unwrap_src[unwrap_pairs][len] = '\0';
+       unwrap_dst[unwrap_pairs] = xstrdup(arg_dst);
+       ++unwrap_pairs;
+}
+
+static const char *
+cpp_unwrap_file (const char *arg, char **tmp_name)
+{
+       char *result;
+       size_t i, len;
+
+       for (i = 0; i < unwrap_pairs; ++i) {
+               len = strlen (unwrap_src[i]);
+               if (strncmp (unwrap_src[i], arg, len))
+                       continue;
+               if (arg[len] == '\0')
+                       return xstrdup (unwrap_dst[i]);
+               if (arg[len] != '/')
+                       continue;
+               arg += len;
+               len = strlen (unwrap_dst[i]);
+               result = xmalloc (len + strlen (arg) + 1);
+               memcpy(result, unwrap_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 *
@@ -130,6 +185,7 @@ _cpp_builtin_macro_text (cpp_reader *pfi
       {
        unsigned int len;
        const char *name;
+       char *tmp_name;
        uchar *buf;
        map = linemap_lookup (pfile->line_table, 
pfile->line_table->highest_line);
 
@@ -137,12 +193,14 @@ _cpp_builtin_macro_text (cpp_reader *pfi
          while (! MAIN_FILE_P (map))
            map = INCLUDED_FROM (pfile->line_table, map);
 
-       name = map->to_file;
+       tmp_name = NULL;
+       name = cpp_unwrap_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: dist/gcc4/libcpp/include/cpplib.h
===================================================================
RCS file: /home/joerg/repo/netbsd/src/gnu/dist/gcc4/libcpp/include/cpplib.h,v
retrieving revision 1.1.1.1
diff -u -p -r1.1.1.1 cpplib.h
--- dist/gcc4/libcpp/include/cpplib.h   20 Apr 2006 09:55:51 -0000      1.1.1.1
+++ dist/gcc4/libcpp/include/cpplib.h   30 Oct 2009 19:43:35 -0000
@@ -623,6 +623,9 @@ extern void cpp_set_lang (cpp_reader *, 
 /* Set the include paths.  */
 extern void cpp_set_include_chains (cpp_reader *, cpp_dir *, cpp_dir *, int);
 
+/* Provide src:dst pair for __FILE__ unwrapping.  */
+extern void add_cpp_unwrap_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
Index: usr.bin/gcc4/cpp/cpp.1
===================================================================
RCS file: /home/joerg/repo/netbsd/src/gnu/usr.bin/gcc4/cpp/cpp.1,v
retrieving revision 1.2
diff -u -p -r1.2 cpp.1
--- usr.bin/gcc4/cpp/cpp.1      30 Apr 2009 00:45:47 -0000      1.2
+++ usr.bin/gcc4/cpp/cpp.1      30 Oct 2009 22:00:26 -0000
@@ -11,6 +11,7 @@
 .Op Fl U Ns Ar macro
 .Op Fl I Ns Ar dir...
 .Op Fl iquote Ns Ar dir...
+.Op Fl iunwrap Ns Ar src Ns : Ns Ar dst
 .Op Fl W Ns Ar warn...
 .Op Fl M | Fl MM
 .Op Fl MG
@@ -691,6 +692,16 @@ before all directories specified by
 .Fl I
 and before the standard system directories.
 .Pp
+.It Fl iunwrap Ns Ar src Ns : Ns Ar dst
+Replace the prefix
+.Ar src
+in
+.Va __FILE__
+with
+.Ar dst
+at expansion time.
+This option can be specified more than once.
+Processing stops at the first match.
 .It Fl fdollars-in-identifiers
 @anchor{fdollars-in-identifiers} Accept
 .Sy $
Index: usr.bin/gcc4/gcc/gcc.1
===================================================================
RCS file: /home/joerg/repo/netbsd/src/gnu/usr.bin/gcc4/gcc/gcc.1,v
retrieving revision 1.3
diff -u -p -r1.3 gcc.1
--- usr.bin/gcc4/gcc/gcc.1      30 Apr 2009 00:30:56 -0000      1.3
+++ usr.bin/gcc4/gcc/gcc.1      30 Oct 2009 22:01:16 -0000
@@ -183,6 +183,7 @@ Explanations are in the following sectio
 .Fl B Ns Ar prefix
 .Fl I Ns Ar dir
 .Fl iquote Ns Ar dir
+.Fl iunwrap Ns Ar src Ns : Ns Ar dst
 .Fl L Ns Ar dir
 .Fl specs= Ns Ar file
 .Fl I- Fl -sysroot= Ns Ar dir
@@ -6633,6 +6634,16 @@ before all directories specified by
 .Fl I
 and before the standard system directories.
 .Pp
+.It Fl iunwrap Ns Ar src Ns : Ns Ar dst
+Replace the prefix
+.Ar src
+in
+.Va __FILE__
+with
+.Ar dst
+at expansion time.
+This option can be specified more than once.
+Processing stops at the first match.
 .It Fl fdollars-in-identifiers
 @anchor{fdollars-in-identifiers} Accept
 .Sy $


Home | Main Index | Thread Index | Old Index