* Re: Optional machine prefix for programs in for -B dirs
[not found] <9b1220b2-5407-4b6e-b6d1-5c33c52b4110@www.fastmail.com>
@ 2021-08-18 6:12 ` John Ericson
2021-08-18 6:12 ` [PATCH 1/3] find_a_program: First search with machine prefix John Ericson
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: John Ericson @ 2021-08-18 6:12 UTC (permalink / raw)
To: gcc-patches; +Cc: Michael Matz, Jonathan Wakely, Paul Koning
OK I have polished off my code in light of previous discussion and will
submit it in follow-up emails.
As mentioned before, this patch series is on top of the
non-behavior-changing cleanup I previously submitted in
https://gcc.gnu.org/pipermail/gcc-patches/2021-August/576725.html
The first patch implements my original approach of always searching for
`$machine-prog`. The next two patches refine that approach by only
searching for `$machine-prog` in directories that are not already
machine-disambiguated, as discussed. I wanted to include this fuller
history to allow both approaches to be compared, but if desired I am
happy to submit a v2 patch set with a more condensed history for
whichever option is chosen.
Thanks,
John
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH 1/3] find_a_program: First search with machine prefix
2021-08-18 6:12 ` Optional machine prefix for programs in for -B dirs John Ericson
@ 2021-08-18 6:12 ` John Ericson
2021-08-18 6:12 ` [PATCH 2/3] driver: for_each_pass: Pass to callback whether dir is machine-disambiguated John Ericson
2021-08-18 6:12 ` [PATCH 3/3] find_a_program: Only search for prefixed paths in undisambiguated dirs John Ericson
2 siblings, 0 replies; 4+ messages in thread
From: John Ericson @ 2021-08-18 6:12 UTC (permalink / raw)
To: gcc-patches; +Cc: Michael Matz, Jonathan Wakely, Paul Koning
This matches the behavior of Clang, and makes it easier to work with
cross compilers without heeding to hard-code paths at build time.
---
gcc/gcc.c | 78 ++++++++++++++++++++++++++++++++++++++++++++++++-------
1 file changed, 68 insertions(+), 10 deletions(-)
diff --git a/gcc/gcc.c b/gcc/gcc.c
index 1a74bf92f7a..710cbfe9a66 100644
--- a/gcc/gcc.c
+++ b/gcc/gcc.c
@@ -1582,6 +1582,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;
@@ -3026,15 +3031,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;
@@ -3074,12 +3070,52 @@ 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. */
static char*
find_a_program (const char *name)
{
+ const int mode = X_OK;
+
/* Do not search if default matches query. */
#ifdef DEFAULT_ASSEMBLER
@@ -3097,7 +3133,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 = mode;
+
+ 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
@@ -8328,6 +8385,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.31.1
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH 2/3] driver: for_each_pass: Pass to callback whether dir is machine-disambiguated
2021-08-18 6:12 ` Optional machine prefix for programs in for -B dirs John Ericson
2021-08-18 6:12 ` [PATCH 1/3] find_a_program: First search with machine prefix John Ericson
@ 2021-08-18 6:12 ` John Ericson
2021-08-18 6:12 ` [PATCH 3/3] find_a_program: Only search for prefixed paths in undisambiguated dirs John Ericson
2 siblings, 0 replies; 4+ messages in thread
From: John Ericson @ 2021-08-18 6:12 UTC (permalink / raw)
To: gcc-patches; +Cc: Michael Matz, Jonathan Wakely, Paul Koning
We will use this in the subsequent diff to control what basenames we
search for. In machine-specific subdirectories, we should just look for
the original basename, but in machine-agnostic subdirectories, we might
additionally look for prefixed disambiguated names, as an alternate
method of keeping targets apart.
---
gcc/gcc.c | 18 +++++++++---------
1 file changed, 9 insertions(+), 9 deletions(-)
diff --git a/gcc/gcc.c b/gcc/gcc.c
index 710cbfe9a66..f32c7a8de46 100644
--- a/gcc/gcc.c
+++ b/gcc/gcc.c
@@ -2766,7 +2766,7 @@ static void *
for_each_path (const struct path_prefix *paths,
bool do_multi,
size_t extra_space,
- void *(*callback) (char *, void *),
+ void *(*callback) (char *, bool, void *),
void *callback_info)
{
struct prefix_list *pl;
@@ -2827,7 +2827,7 @@ for_each_path (const struct path_prefix *paths,
if (!skip_multi_dir)
{
memcpy (path + len, multi_suffix, suffix_len + 1);
- ret = callback (path, callback_info);
+ ret = callback (path, true, callback_info);
if (ret)
break;
}
@@ -2838,7 +2838,7 @@ for_each_path (const struct path_prefix *paths,
&& pl->require_machine_suffix == 2)
{
memcpy (path + len, just_multi_suffix, just_suffix_len + 1);
- ret = callback (path, callback_info);
+ ret = callback (path, true, callback_info);
if (ret)
break;
}
@@ -2848,7 +2848,7 @@ for_each_path (const struct path_prefix *paths,
&& !pl->require_machine_suffix && multiarch_dir)
{
memcpy (path + len, multiarch_suffix, multiarch_len + 1);
- ret = callback (path, callback_info);
+ ret = callback (path, true, callback_info);
if (ret)
break;
}
@@ -2876,7 +2876,7 @@ for_each_path (const struct path_prefix *paths,
else
path[len] = '\0';
- ret = callback (path, callback_info);
+ ret = callback (path, false, callback_info);
if (ret)
break;
}
@@ -2931,7 +2931,7 @@ struct add_to_obstack_info {
};
static void *
-add_to_obstack (char *path, void *data)
+add_to_obstack (char *path, bool, void *data)
{
struct add_to_obstack_info *info = (struct add_to_obstack_info *) data;
@@ -3023,7 +3023,7 @@ struct file_at_path_info {
};
static void *
-file_at_path (char *path, void *data)
+file_at_path (char *path, bool, void *data)
{
struct file_at_path_info *info = (struct file_at_path_info *) data;
size_t len = strlen (path);
@@ -3074,7 +3074,7 @@ find_a_file (const struct path_prefix *pprefix, const char *name, int mode,
path. Like file_at_path but tries machine prefix and exe suffix too. */
static void *
-program_at_path (char *path, void *data)
+program_at_path (char *path, bool machine_specific, void *data)
{
/* try first with machine-prefixed name */
struct file_at_path_info *info = (struct file_at_path_info *) data;
@@ -5945,7 +5945,7 @@ struct spec_path_info {
};
static void *
-spec_path (char *path, void *data)
+spec_path (char *path, bool, void *data)
{
struct spec_path_info *info = (struct spec_path_info *) data;
size_t len = 0;
--
2.31.1
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH 3/3] find_a_program: Only search for prefixed paths in undisambiguated dirs
2021-08-18 6:12 ` Optional machine prefix for programs in for -B dirs John Ericson
2021-08-18 6:12 ` [PATCH 1/3] find_a_program: First search with machine prefix John Ericson
2021-08-18 6:12 ` [PATCH 2/3] driver: for_each_pass: Pass to callback whether dir is machine-disambiguated John Ericson
@ 2021-08-18 6:12 ` John Ericson
2 siblings, 0 replies; 4+ messages in thread
From: John Ericson @ 2021-08-18 6:12 UTC (permalink / raw)
To: gcc-patches; +Cc: Michael Matz, Jonathan Wakely, Paul Koning
This means, we might search for:
- path/$machine/$version/prog
- path/$machine/prog
- path/$machine-prog
But not
- path/$machine/$version/$machine-prog
because disambiguating $machine twice is unnecessary.
This does mean we less liberal in what we accept than LLVM, but that's
OK. The down side of always Postel's law is everyone converges on
accepting all sorts of garbage, which makes debugging end-to-end hard
when mistakes are not caught early.
---
gcc/gcc.c | 25 ++++++++++++++++---------
1 file changed, 16 insertions(+), 9 deletions(-)
diff --git a/gcc/gcc.c b/gcc/gcc.c
index f32c7a8de46..7b6b89ac6e9 100644
--- a/gcc/gcc.c
+++ b/gcc/gcc.c
@@ -3080,15 +3080,9 @@ program_at_path (char *path, bool machine_specific, void *data)
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 search = [=](size_t len) -> void *
{
- 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);
+ memcpy (path + len, info->name, info->name_len + 1);
len += info->name_len;
/* Some systems have a suffix for executable files.
@@ -3103,9 +3097,22 @@ program_at_path (char *path, bool machine_specific, void *data)
path[len] = '\0';
if (access_check (path, info->mode) == 0)
return path;
+
+ return NULL;
+ };
+
+ /* Additionally search for $target-prog in machine-agnostic dirs, as an
+ additional way to disambiguate targets. Do not do this in machine-specific
+ dirs because so further disambiguation is needed. */
+ if (!machine_specific)
+ {
+ auto prefix_len = strlen(just_machine_prefix);
+ memcpy (path + path_len, just_machine_prefix, prefix_len);
+ auto res = search(path_len + prefix_len);
+ if (res) return res;
}
- return NULL;
+ return search(path_len);
}
/* Specialization of find_a_file for programs that also takes into account
--
2.31.1
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2021-08-18 6:12 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
[not found] <9b1220b2-5407-4b6e-b6d1-5c33c52b4110@www.fastmail.com>
2021-08-18 6:12 ` Optional machine prefix for programs in for -B dirs John Ericson
2021-08-18 6:12 ` [PATCH 1/3] find_a_program: First search with machine prefix John Ericson
2021-08-18 6:12 ` [PATCH 2/3] driver: for_each_pass: Pass to callback whether dir is machine-disambiguated John Ericson
2021-08-18 6:12 ` [PATCH 3/3] find_a_program: Only search for prefixed paths in undisambiguated dirs John Ericson
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).