[1/2] Driver: Add new -truncate option

Message ID 20240418005602.24118-1-peter0x44@disroot.org
State New
Headers
Series [1/2] Driver: Add new -truncate option |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gcc_build--master-arm fail Testing failed
linaro-tcwg-bot/tcwg_gcc_build--master-aarch64 fail Testing failed

Commit Message

Peter0x44 April 18, 2024, 12:56 a.m. UTC
  This commit adds a new option to the driver that truncates one file after
linking.

Tested likeso:

$ gcc hello.c -c
$ du -h hello.o
4.0K  hello.o
$ gcc hello.o -truncate hello
$ ./a.out
Hello world
$ du -h hello.o
$ 0   hello.o

$ gcc hello.o -truncate
gcc: error: missing filename after '-truncate'

The motivation for adding this is PR110710. It is used by lto-wrapper to
truncate files in a shell-independent manner.

Signed-off-by: Peter Damianov <peter0x44@disroot.org>
---
 gcc/common.opt |  5 +++++
 gcc/gcc.cc     | 13 +++++++++++++
 2 files changed, 18 insertions(+)
  

Comments

Andrew Pinski April 18, 2024, 1:08 a.m. UTC | #1
On Wed, Apr 17, 2024 at 5:57 PM Peter Damianov <peter0x44@disroot.org> wrote:
>
> This commit adds a new option to the driver that truncates one file after
> linking.
>
> Tested likeso:
>
> $ gcc hello.c -c
> $ du -h hello.o
> 4.0K  hello.o
> $ gcc hello.o -truncate hello
> $ ./a.out
> Hello world
> $ du -h hello.o
> $ 0   hello.o
>
> $ gcc hello.o -truncate
> gcc: error: missing filename after '-truncate'
>
> The motivation for adding this is PR110710. It is used by lto-wrapper to
> truncate files in a shell-independent manner.

I wonder if we should document this option or not. On one hand it is
only supposed to be used by lto but on the other hand, someone could
use it on accident from the command line and we would get a bug report
saying the file passed to it is now 0.

Thanks,
Andrew Pinski

>
> Signed-off-by: Peter Damianov <peter0x44@disroot.org>
> ---
>  gcc/common.opt |  5 +++++
>  gcc/gcc.cc     | 13 +++++++++++++
>  2 files changed, 18 insertions(+)
>
> diff --git a/gcc/common.opt b/gcc/common.opt
> index ad348844775..3ede2fa8552 100644
> --- a/gcc/common.opt
> +++ b/gcc/common.opt
> @@ -422,6 +422,11 @@ Display target specific command line options (including assembler and linker opt
>  -time
>  Driver Alias(time)
>
> +;; Truncate the file specified after linking.
> +;; This option is used by lto-wrapper to reduce the peak disk when linking with
> +;; many .LTRANS units.
> +Driver Separate Undocumented MissingArgError(missing filename after %qs)
> +
>  -verbose
>  Driver Alias(v)
>
> diff --git a/gcc/gcc.cc b/gcc/gcc.cc
> index 728332b8153..00017964295 100644
> --- a/gcc/gcc.cc
> +++ b/gcc/gcc.cc
> @@ -2138,6 +2138,10 @@ static int have_E = 0;
>  /* Pointer to output file name passed in with -o. */
>  static const char *output_file = 0;
>
> +/* Pointer to input file name passed in with -truncate.
> +   This file should be truncated after linking. */
> +static const char *totruncate_file = 0;
> +
>  /* This is the list of suffixes and codes (%g/%u/%U/%j) and the associated
>     temp file.  If the HOST_BIT_BUCKET is used for %j, no entry is made for
>     it here.  */
> @@ -4607,6 +4611,10 @@ driver_handle_option (struct gcc_options *opts,
>        save_switch ("-o", 1, &arg, validated, true);
>        return true;
>
> +    case OPT_truncate:
> +      totruncate_file = arg;
> +      break;
> +
>      case OPT_pie:
>  #ifdef ENABLE_DEFAULT_PIE
>        /* -pie is turned on by default.  */
> @@ -9273,6 +9281,11 @@ driver::maybe_run_linker (const char *argv0) const
>                option).  */
>             error ("%s: linker input file not found: %m", outfiles[i]);
>         }
> +
> +  if (totruncate_file != NULL && linker_was_run && !seen_error ())
> +    /* Truncate file specified by -truncate.
> +       Used by lto-wrapper to reduce temporary disk-space usage. */
> +    truncate(totruncate_file, 0);
>  }
>
>  /* The end of "main".  */
> --
> 2.39.2
>
  
Peter0x44 April 18, 2024, 2:43 a.m. UTC | #2
On 2024-04-17 17:56, Peter Damianov wrote:
> This commit adds a new option to the driver that truncates one file 
> after
> linking.
> 
> Tested likeso:
> 
> $ gcc hello.c -c
> $ du -h hello.o
> 4.0K  hello.o
> $ gcc hello.o -truncate hello
> $ ./a.out
> Hello world
> $ du -h hello.o
> $ 0   hello.o
> 
> $ gcc hello.o -truncate
> gcc: error: missing filename after '-truncate'
> 
> The motivation for adding this is PR110710. It is used by lto-wrapper 
> to
> truncate files in a shell-independent manner.
> 
> Signed-off-by: Peter Damianov <peter0x44@disroot.org>
> ---
>  gcc/common.opt |  5 +++++
>  gcc/gcc.cc     | 13 +++++++++++++
>  2 files changed, 18 insertions(+)
> 
> diff --git a/gcc/common.opt b/gcc/common.opt
> index ad348844775..3ede2fa8552 100644
> --- a/gcc/common.opt
> +++ b/gcc/common.opt
> @@ -422,6 +422,11 @@ Display target specific command line options 
> (including assembler and linker opt
>  -time
>  Driver Alias(time)
> 
> +;; Truncate the file specified after linking.
> +;; This option is used by lto-wrapper to reduce the peak disk when 
> linking with
> +;; many .LTRANS units.
> +Driver Separate Undocumented MissingArgError(missing filename after 
> %qs)
> +
>  -verbose
>  Driver Alias(v)
> 
> diff --git a/gcc/gcc.cc b/gcc/gcc.cc
> index 728332b8153..00017964295 100644
> --- a/gcc/gcc.cc
> +++ b/gcc/gcc.cc
> @@ -2138,6 +2138,10 @@ static int have_E = 0;
>  /* Pointer to output file name passed in with -o. */
>  static const char *output_file = 0;
> 
> +/* Pointer to input file name passed in with -truncate.
> +   This file should be truncated after linking. */
> +static const char *totruncate_file = 0;
> +
>  /* This is the list of suffixes and codes (%g/%u/%U/%j) and the 
> associated
>     temp file.  If the HOST_BIT_BUCKET is used for %j, no entry is made 
> for
>     it here.  */
> @@ -4607,6 +4611,10 @@ driver_handle_option (struct gcc_options *opts,
>        save_switch ("-o", 1, &arg, validated, true);
>        return true;
> 
> +    case OPT_truncate:
> +      totruncate_file = arg;
> +      break;
> +
>      case OPT_pie:
>  #ifdef ENABLE_DEFAULT_PIE
>        /* -pie is turned on by default.  */
> @@ -9273,6 +9281,11 @@ driver::maybe_run_linker (const char *argv0) 
> const
>  	       option).  */
>  	    error ("%s: linker input file not found: %m", outfiles[i]);
>  	}
> +
> +  if (totruncate_file != NULL && linker_was_run && !seen_error ())
> +    /* Truncate file specified by -truncate.
> +       Used by lto-wrapper to reduce temporary disk-space usage. */
> +    truncate(totruncate_file, 0);
On second thought, doing the truncation in driver::maybe_run_linker() 
seems wrong.
driver::final_actions seems like the better place to put this code.
Will resubmit.
>  }
> 
>  /* The end of "main".  */
  

Patch

diff --git a/gcc/common.opt b/gcc/common.opt
index ad348844775..3ede2fa8552 100644
--- a/gcc/common.opt
+++ b/gcc/common.opt
@@ -422,6 +422,11 @@  Display target specific command line options (including assembler and linker opt
 -time
 Driver Alias(time)
 
+;; Truncate the file specified after linking.
+;; This option is used by lto-wrapper to reduce the peak disk when linking with
+;; many .LTRANS units.
+Driver Separate Undocumented MissingArgError(missing filename after %qs)
+
 -verbose
 Driver Alias(v)
 
diff --git a/gcc/gcc.cc b/gcc/gcc.cc
index 728332b8153..00017964295 100644
--- a/gcc/gcc.cc
+++ b/gcc/gcc.cc
@@ -2138,6 +2138,10 @@  static int have_E = 0;
 /* Pointer to output file name passed in with -o. */
 static const char *output_file = 0;
 
+/* Pointer to input file name passed in with -truncate.
+   This file should be truncated after linking. */
+static const char *totruncate_file = 0;
+
 /* This is the list of suffixes and codes (%g/%u/%U/%j) and the associated
    temp file.  If the HOST_BIT_BUCKET is used for %j, no entry is made for
    it here.  */
@@ -4607,6 +4611,10 @@  driver_handle_option (struct gcc_options *opts,
       save_switch ("-o", 1, &arg, validated, true);
       return true;
 
+    case OPT_truncate:
+      totruncate_file = arg;
+      break;
+
     case OPT_pie:
 #ifdef ENABLE_DEFAULT_PIE
       /* -pie is turned on by default.  */
@@ -9273,6 +9281,11 @@  driver::maybe_run_linker (const char *argv0) const
 	       option).  */
 	    error ("%s: linker input file not found: %m", outfiles[i]);
 	}
+
+  if (totruncate_file != NULL && linker_was_run && !seen_error ())
+    /* Truncate file specified by -truncate.
+       Used by lto-wrapper to reduce temporary disk-space usage. */
+    truncate(totruncate_file, 0);
 }
 
 /* The end of "main".  */