diff options
author | Costas Argyris <costas.argyris@gmail.com> | 2023-07-31 10:56:20 -0600 |
---|---|---|
committer | Jeff Law <jlaw@ventanamicro.com> | 2023-07-31 10:57:50 -0600 |
commit | c6523ae786e36dccd64589682140e9221628bb5b (patch) | |
tree | 80f6e8078bfa4e5e402e879b665ff54bc83daf0a /gcc | |
parent | b769811e7c1b3dff2fa0ec2c37b52859d7bceed4 (diff) | |
download | gcc-c6523ae786e36dccd64589682140e9221628bb5b.zip gcc-c6523ae786e36dccd64589682140e9221628bb5b.tar.gz gcc-c6523ae786e36dccd64589682140e9221628bb5b.tar.bz2 |
Re: [PATCH] gcc-ar: Handle response files properly [PR77576]
Problem: gcc-ar fails when a @file is passed to it:
$ cat rsp
--version
$ gcc-ar @rsp
/usr/bin/ar: invalid option -- '@'
This is because a dash '-' is prepended to the first
argument if it doesn't start with one, resulting in
the wrong call 'ar -@rsp'.
Fix: Expand argv to get rid of any @files and if any
expansions were made, pass everything through a
temporary response file.
$ gcc-ar @rsp
GNU ar (GNU Binutils for Debian) 2.35.2
...
gcc/
PR driver/77576
* gcc-ar.cc (main): Expand argv and use
temporary response file to call ar if any
expansions were made.
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/gcc-ar.cc | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/gcc/gcc-ar.cc b/gcc/gcc-ar.cc index 4e4c525..4d9da81 100644 --- a/gcc/gcc-ar.cc +++ b/gcc/gcc-ar.cc @@ -135,6 +135,10 @@ main (int ac, char **av) int k, status, err; const char *err_msg; const char **nargv; + char **old_argv; + const char *rsp_file = NULL; + const char *rsp_arg = NULL; + const char *rsp_argv[3]; bool is_ar = !strcmp (PERSONALITY, "ar"); int exit_code = FATAL_EXIT_CODE; int i; @@ -209,6 +213,13 @@ main (int ac, char **av) } } + /* Expand any @files before modifying the command line + and use a temporary response file if there were any. */ + old_argv = av; + expandargv (&ac, &av); + if (av != old_argv) + rsp_file = make_temp_file (""); + /* Prepend - if necessary. */ if (is_ar && av[1] && av[1][0] != '-') av[1] = concat ("-", av[1], NULL); @@ -225,6 +236,39 @@ main (int ac, char **av) nargv[j + k] = av[k]; nargv[j + k] = NULL; + /* If @file was passed, put nargv into the temporary response + file and then change it to a single @FILE argument, where + FILE is the temporary filename. */ + if (rsp_file) + { + FILE *f; + int status; + f = fopen (rsp_file, "w"); + if (f == NULL) + { + fprintf (stderr, "Cannot open temporary file %s\n", rsp_file); + exit (1); + } + status = writeargv ( + CONST_CAST2 (char * const *, const char **, nargv) + 1, f); + if (status) + { + fprintf (stderr, "Cannot write to temporary file %s\n", rsp_file); + exit (1); + } + status = fclose (f); + if (EOF == status) + { + fprintf (stderr, "Cannot close temporary file %s\n", rsp_file); + exit (1); + } + rsp_arg = concat ("@", rsp_file, NULL); + rsp_argv[0] = nargv[0]; + rsp_argv[1] = rsp_arg; + rsp_argv[2] = NULL; + nargv = rsp_argv; + } + /* Run utility */ /* ??? the const is misplaced in pex_one's argv? */ err_msg = pex_one (PEX_LAST|PEX_SEARCH, @@ -249,5 +293,8 @@ main (int ac, char **av) else exit_code = SUCCESS_EXIT_CODE; + if (rsp_file) + unlink (rsp_file); + return exit_code; } |