注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

一路

To find the final symmetry & beauty

 
 
 

日志

 
 
 
 

gcc response file feature intro & implementation analysis[origin]  

2012-03-29 01:32:54|  分类: 共享 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
        GCC has one feature that not many even known that. but it is really very convenient when doing simple testing work if you don't want to craft one Makefile or even you don't know how to write Makefile
        GCC can accept one plain text file contain parameters to gcc to compile files. let's make one demo.
suppose you has one source file named hello.cpp you could create one text file called param.txt with the following content

hello.cpp -o hello -O2 -Wall

know you could using GCC to compile with the following command 

gcc @param.txt 

now gcc compiles hello.cpp successfully.

the following is the source code in libiberty that implements this feature.
here are several key points that needs attention, the first technique used here is reading all params from text file and add these paramesters to argv array(argc should also make changes). the second is the usage of memcpy & memmove there is subtle difference between memcpy & memmove both in performance & behavior. memmove works properly when source & destination has overlap but with lower performance, memcpy may make trouble when source & destination overlaps but with a bit high performance. 

void

expandargv (int *argcp, char ***argvp)

{

  /* The argument we are currently processing.  */

  int i = 0;

  /* Non-zero if ***argvp has been dynamically allocated.  */

  int argv_dynamic = 0;

  /* Limit the number of response files that we parse in order

     to prevent infinite recursion.  */

  unsigned int iteration_limit = 2000;

  /* Loop over the arguments, handling response files.  We always skip

     ARGVP[0], as that is the name of the program being run.  */

  while (++i < *argcp)

    {

      /* The name of the response file.  */

      const char *filename;

      /* The response file.  */

      FILE *f;

      /* An upper bound on the number of characters in the response

     file.  */

      long pos;

      /* The number of characters in the response file, when actually

     read.  */

      size_t len;

      /* A dynamically allocated buffer used to hold options read from a

     response file.  */

      char *buffer;

      /* Dynamically allocated storage for the options read from the

     response file.  */

      char **file_argv;

      /* The number of options read from the response file, if any.  */

      size_t file_argc;

      /* We are only interested in options of the form "@file".  */

      filename = (*argvp)[i];

      if (filename[0] != '@')

    continue;

      /* If we have iterated too many times then stop.  */

      if (-- iteration_limit == 0)

    {

      fprintf (stderr, "%s: error: too many @-files encountered\n", (*argvp)[0]);

      xexit (1);

    }

      /* Read the contents of the file.  */

      f = fopen (++filename, "r");

      if (!f)

    continue;

      if (fseek (f, 0L, SEEK_END) == -1)

    goto error;

      pos = ftell (f);

      if (pos == -1)

    goto error;

      if (fseek (f, 0L, SEEK_SET) == -1)

    goto error;

      buffer = (char *) xmalloc (pos * sizeof (char) + 1);

      len = fread (buffer, sizeof (char), pos, f);

      if (len != (size_t) pos

      /* On Windows, fread may return a value smaller than POS,

         due to CR/LF->CR translation when reading text files.

         That does not in-and-of itself indicate failure.  */

      && ferror (f))

    goto error;

      /* Add a NUL terminator.  */

      buffer[len] = '\0';

      /* If the file is empty or contains only whitespace, buildargv would

     return a single empty argument.  In this context we want no arguments,

     instead.  */

      if (only_whitespace (buffer))

    {

      file_argv = (char **) xmalloc (sizeof (char *));

      file_argv[0] = NULL;

    }

      else

    /* Parse the string.  */

    file_argv = buildargv (buffer);

      /* If *ARGVP is not already dynamically allocated, copy it.  */

      if (!argv_dynamic)

    {

      *argvp = dupargv (*argvp);

      if (!*argvp)

        {

          fputs ("\nout of memory\n", stderr);

          xexit (1);

        }

    }

      /* Count the number of arguments.  */

      file_argc = 0;

      while (file_argv[file_argc])

    ++file_argc;

      /* Now, insert FILE_ARGV into ARGV.  The "+1" below handles the

     NULL terminator at the end of ARGV.  */

      *argvp = ((char **)

        xrealloc (*argvp,

              (*argcp + file_argc + 1) * sizeof (char *)));

      memmove (*argvp + i + file_argc, *argvp + i + 1,

           (*argcp - i) * sizeof (char *));

      memcpy (*argvp + i, file_argv, file_argc * sizeof (char *));

      /* The original option has been replaced by all the new

     options.  */

      *argcp += file_argc - 1;

      /* Free up memory allocated to process the response file.  We do

     not use freeargv because the individual options in FILE_ARGV

     are now in the main ARGV.  */

      free (file_argv);

      free (buffer);

      /* Rescan all of the arguments just read to support response

     files that include other response files.  */

      --i;

    error:

      /* We're all done with the file now.  */

      fclose (f);

    }

}

 


  评论这张
 
阅读(869)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017