On Tue, Aug 5, 2025 at 9:50 PM John Ericson <g...@johnericson.me> wrote:
>
> This matches the behavior of Clang, and makes it easier to work with
> cross compilers without heeding to hard-code paths at build time.

Does it really make sense to match clang here?
The paths are relocable already too.
Can you expand on why you need/want this change? Since it changes
behavior that has been around for over 25 years now?

Thanks,
Andrew

>
> gcc/ChangeLog:
>
>         * gcc.cc (file_at_path): Remove program suffix logic (it is
>         moving elsewhere). This function no longer cares about programs
>         in particular, matching its name.
>         (program_at_path): New function that does care about programs in
>         particular. It has the preexisting suffixing logic (moved here)
>         and the new prefixing logic.
>         (find_a_program): Instead work by using for_each_path with
>         file_at_path, just as find_a_file uses file_at_path.
>         (driver::set_up_specs): Initial just_machine_prefix, a constant
>         used above.
>
> Signed-off-by: John Ericson <g...@johnericson.me>
> ---
>  gcc/gcc.cc | 76 +++++++++++++++++++++++++++++++++++++++++++++++-------
>  1 file changed, 66 insertions(+), 10 deletions(-)
>
> diff --git a/gcc/gcc.cc b/gcc/gcc.cc
> index bfa588ee5f0..d3ba4d6565b 100644
> --- a/gcc/gcc.cc
> +++ b/gcc/gcc.cc
> @@ -1600,6 +1600,11 @@ static const char *machine_suffix = 0;
>
>  static const char *just_machine_suffix = 0;
>
> +/* Prefix to attach to *basename* of commands being searched.
> +   This is just `MACHINE-'.  */
> +
> +static const char *just_machine_prefix = 0;
> +
>  /* Adjusted value of GCC_EXEC_PREFIX envvar.  */
>
>  static const char *gcc_exec_prefix;
> @@ -3043,15 +3048,6 @@ file_at_path (char *path, void *data)
>    memcpy (path + len, info->name, info->name_len);
>    len += info->name_len;
>
> -  /* Some systems have a suffix for executable files.
> -     So try appending that first.  */
> -  if (info->suffix_len)
> -    {
> -      memcpy (path + len, info->suffix, info->suffix_len + 1);
> -      if (access_check (path, info->mode) == 0)
> -       return path;
> -    }
> -
>    path[len] = '\0';
>    if (access_check (path, info->mode) == 0)
>      return path;
> @@ -3091,6 +3087,44 @@ find_a_file (const struct path_prefix *pprefix, const 
> char *name, int mode,
>                                 file_at_path, &info);
>  }
>
> +/* Callback for find_a_program.  Appends the file name to the directory
> +   path. Like file_at_path but tries machine prefix and exe suffix too. */
> +
> +static void *
> +program_at_path (char *path, void *data)
> +{
> +  /* try first with machine-prefixed name */
> +  struct file_at_path_info *info = (struct file_at_path_info *) data;
> +  size_t path_len = strlen (path);
> +
> +  for (auto prefix : { just_machine_prefix, "" })
> +    {
> +      auto len = path_len;
> +
> +      auto prefix_len = strlen(prefix);
> +      memcpy (path + len, prefix, prefix_len);
> +      len += prefix_len;
> +
> +      memcpy (path + len, info->name, info->name_len);
> +      len += info->name_len;
> +
> +      /* Some systems have a suffix for executable files.
> +        So try appending that first.  */
> +      if (info->suffix_len)
> +       {
> +         memcpy (path + len, info->suffix, info->suffix_len + 1);
> +         if (access_check (path, info->mode) == 0)
> +           return path;
> +       }
> +
> +      path[len] = '\0';
> +      if (access_check (path, info->mode) == 0)
> +       return path;
> +    }
> +
> +  return NULL;
> +}
> +
>  /* Specialization of find_a_file for programs that also takes into account
>     configure-specified default programs. */
>
> @@ -3114,7 +3148,28 @@ find_a_program (const char *name)
>      return xstrdup (DEFAULT_DSYMUTIL);
>  #endif
>
> -  return find_a_file (&exec_prefixes, name, X_OK, false);
> +  /* Find the filename in question (special case for absolute paths).  */
> +
> +  if (IS_ABSOLUTE_PATH (name))
> +    {
> +      if (access (name, mode) == 0)
> +       return xstrdup (name);
> +
> +      return NULL;
> +    }
> +
> +  struct file_at_path_info info;
> +
> +  info.name = name;
> +  info.suffix = HOST_EXECUTABLE_SUFFIX;
> +  info.name_len = strlen (info.name);
> +  info.suffix_len = strlen (info.suffix);
> +  info.mode = X_OK;
> +
> +  return (char*) for_each_path (
> +    &exec_prefixes, false,
> +    info.name_len + info.suffix_len + strlen(just_machine_prefix),
> +    program_at_path, &info);
>  }
>
>  /* Ranking of prefixes in the sort list. -B prefixes are put before
> @@ -8493,6 +8548,7 @@ driver::set_up_specs () const
>    machine_suffix = concat (spec_host_machine, dir_separator_str, 
> spec_version,
>                            accel_dir_suffix, dir_separator_str, NULL);
>    just_machine_suffix = concat (spec_machine, dir_separator_str, NULL);
> +  just_machine_prefix = concat (spec_machine, "-", NULL);
>
>    specs_file = find_a_file (&startfile_prefixes, "specs", R_OK, true);
>    /* Read the specs file unless it is a default one.  */
> --
> 2.49.0
>

Reply via email to