2012-03-04 00:24:20 +00:00
|
|
|
|
/* Interface to 'ar' archives for GNU Make.
|
2016-02-28 17:55:20 +00:00
|
|
|
|
Copyright (C) 1988-2016 Free Software Foundation, Inc.
|
2010-07-13 01:20:10 +00:00
|
|
|
|
|
1992-01-17 08:30:31 +00:00
|
|
|
|
This file is part of GNU Make.
|
|
|
|
|
|
2006-02-11 19:02:21 +00:00
|
|
|
|
GNU Make is free software; you can redistribute it and/or modify it under the
|
|
|
|
|
terms of the GNU General Public License as published by the Free Software
|
2007-07-04 19:35:15 +00:00
|
|
|
|
Foundation; either version 3 of the License, or (at your option) any later
|
|
|
|
|
version.
|
1992-01-17 08:30:31 +00:00
|
|
|
|
|
2006-02-11 19:02:21 +00:00
|
|
|
|
GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
|
|
|
|
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
1992-01-17 08:30:31 +00:00
|
|
|
|
|
2006-02-11 19:02:21 +00:00
|
|
|
|
You should have received a copy of the GNU General Public License along with
|
2007-07-04 19:35:15 +00:00
|
|
|
|
this program. If not, see <http://www.gnu.org/licenses/>. */
|
1992-01-17 08:30:31 +00:00
|
|
|
|
|
2013-01-20 16:01:01 +00:00
|
|
|
|
#include "makeint.h"
|
1992-01-17 08:30:31 +00:00
|
|
|
|
|
2013-05-17 06:29:46 +00:00
|
|
|
|
#ifndef NO_ARCHIVES
|
1992-01-17 08:30:31 +00:00
|
|
|
|
|
1996-03-20 14:57:41 +00:00
|
|
|
|
#include "filedef.h"
|
1993-06-10 01:43:06 +00:00
|
|
|
|
#include "dep.h"
|
|
|
|
|
#include <fnmatch.h>
|
|
|
|
|
|
2009-06-13 22:47:40 +00:00
|
|
|
|
/* Return nonzero if NAME is an archive-member reference, zero if not. An
|
2012-03-04 00:24:20 +00:00
|
|
|
|
archive-member reference is a name like 'lib(member)' where member is a
|
2009-06-13 22:47:40 +00:00
|
|
|
|
non-empty string.
|
2012-03-04 00:24:20 +00:00
|
|
|
|
If a name like 'lib((entry))' is used, a fatal error is signaled at
|
1992-01-17 08:30:31 +00:00
|
|
|
|
the attempt to use this unsupported feature. */
|
|
|
|
|
|
|
|
|
|
int
|
2006-11-18 20:53:44 +00:00
|
|
|
|
ar_name (const char *name)
|
1992-01-17 08:30:31 +00:00
|
|
|
|
{
|
2006-11-18 20:53:44 +00:00
|
|
|
|
const char *p = strchr (name, '(');
|
|
|
|
|
const char *end;
|
1997-08-27 20:30:54 +00:00
|
|
|
|
|
2002-07-11 06:38:57 +00:00
|
|
|
|
if (p == 0 || p == name)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
end = p + strlen (p) - 1;
|
2009-06-13 22:47:40 +00:00
|
|
|
|
if (*end != ')' || end == p + 1)
|
1992-01-17 08:30:31 +00:00
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
if (p[1] == '(' && end[-1] == ')')
|
2013-11-24 03:23:52 +00:00
|
|
|
|
OS (fatal, NILF, _("attempt to use unsupported feature: '%s'"), name);
|
1992-01-17 08:30:31 +00:00
|
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Parse the archive-member reference NAME into the archive and member names.
|
2007-03-20 03:02:26 +00:00
|
|
|
|
Creates one allocated string containing both names, pointed to by ARNAME_P.
|
|
|
|
|
MEMNAME_P points to the member. */
|
1992-01-17 08:30:31 +00:00
|
|
|
|
|
|
|
|
|
void
|
2006-11-18 20:53:44 +00:00
|
|
|
|
ar_parse_name (const char *name, char **arname_p, char **memname_p)
|
1992-01-17 08:30:31 +00:00
|
|
|
|
{
|
2007-03-20 03:02:26 +00:00
|
|
|
|
char *p;
|
1992-01-17 08:30:31 +00:00
|
|
|
|
|
2007-03-20 03:02:26 +00:00
|
|
|
|
*arname_p = xstrdup (name);
|
|
|
|
|
p = strchr (*arname_p, '(');
|
|
|
|
|
*(p++) = '\0';
|
2013-05-17 06:29:46 +00:00
|
|
|
|
p[strlen (p) - 1] = '\0';
|
2007-03-20 03:02:26 +00:00
|
|
|
|
*memname_p = p;
|
1996-03-20 14:57:41 +00:00
|
|
|
|
}
|
1992-01-17 08:30:31 +00:00
|
|
|
|
|
2006-11-18 20:53:44 +00:00
|
|
|
|
|
2012-03-04 00:24:20 +00:00
|
|
|
|
/* This function is called by 'ar_scan' to find which member to look at. */
|
2006-11-18 20:53:44 +00:00
|
|
|
|
|
|
|
|
|
/* ARGSUSED */
|
|
|
|
|
static long int
|
|
|
|
|
ar_member_date_1 (int desc UNUSED, const char *mem, int truncated,
|
2013-05-17 06:29:46 +00:00
|
|
|
|
long int hdrpos UNUSED, long int datapos UNUSED,
|
2006-11-18 20:53:44 +00:00
|
|
|
|
long int size UNUSED, long int date,
|
|
|
|
|
int uid UNUSED, int gid UNUSED, int mode UNUSED,
|
2013-05-17 06:29:46 +00:00
|
|
|
|
const void *name)
|
2006-11-18 20:53:44 +00:00
|
|
|
|
{
|
|
|
|
|
return ar_name_equal (name, mem, truncated) ? date : 0;
|
|
|
|
|
}
|
1992-01-17 08:30:31 +00:00
|
|
|
|
|
|
|
|
|
/* Return the modtime of NAME. */
|
|
|
|
|
|
|
|
|
|
time_t
|
2006-11-18 20:53:44 +00:00
|
|
|
|
ar_member_date (const char *name)
|
1992-01-17 08:30:31 +00:00
|
|
|
|
{
|
|
|
|
|
char *arname;
|
|
|
|
|
char *memname;
|
|
|
|
|
long int val;
|
|
|
|
|
|
|
|
|
|
ar_parse_name (name, &arname, &memname);
|
|
|
|
|
|
1993-12-14 21:06:33 +00:00
|
|
|
|
/* Make sure we know the modtime of the archive itself because we are
|
|
|
|
|
likely to be called just before commands to remake a member are run,
|
|
|
|
|
and they will change the archive itself.
|
|
|
|
|
|
|
|
|
|
But we must be careful not to enter_file the archive itself if it does
|
|
|
|
|
not exist, because pattern_search assumes that files found in the data
|
|
|
|
|
base exist or can be made. */
|
1992-01-17 08:30:31 +00:00
|
|
|
|
{
|
|
|
|
|
struct file *arfile;
|
|
|
|
|
arfile = lookup_file (arname);
|
1993-12-14 21:06:33 +00:00
|
|
|
|
if (arfile == 0 && file_exists_p (arname))
|
2007-03-20 03:02:26 +00:00
|
|
|
|
arfile = enter_file (strcache_add (arname));
|
1992-01-17 08:30:31 +00:00
|
|
|
|
|
1993-12-14 21:06:33 +00:00
|
|
|
|
if (arfile != 0)
|
|
|
|
|
(void) f_mtime (arfile, 0);
|
1992-01-17 08:30:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
2006-11-18 20:53:44 +00:00
|
|
|
|
val = ar_scan (arname, ar_member_date_1, memname);
|
1992-01-17 08:30:31 +00:00
|
|
|
|
|
2007-03-20 03:02:26 +00:00
|
|
|
|
free (arname);
|
1992-01-17 08:30:31 +00:00
|
|
|
|
|
|
|
|
|
return (val <= 0 ? (time_t) -1 : (time_t) val);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Set the archive-member NAME's modtime to now. */
|
|
|
|
|
|
1996-03-20 14:57:41 +00:00
|
|
|
|
#ifdef VMS
|
|
|
|
|
int
|
2006-11-18 20:53:44 +00:00
|
|
|
|
ar_touch (const char *name)
|
1996-03-20 14:57:41 +00:00
|
|
|
|
{
|
2013-11-24 03:23:52 +00:00
|
|
|
|
O (error, NILF, _("touch archive member is not available on VMS"));
|
1996-03-20 14:57:41 +00:00
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
#else
|
1992-01-17 08:30:31 +00:00
|
|
|
|
int
|
2006-11-18 20:53:44 +00:00
|
|
|
|
ar_touch (const char *name)
|
1992-01-17 08:30:31 +00:00
|
|
|
|
{
|
|
|
|
|
char *arname, *memname;
|
2007-03-20 03:02:26 +00:00
|
|
|
|
int val;
|
1992-01-17 08:30:31 +00:00
|
|
|
|
|
|
|
|
|
ar_parse_name (name, &arname, &memname);
|
|
|
|
|
|
|
|
|
|
/* Make sure we know the modtime of the archive itself before we
|
2007-03-20 03:02:26 +00:00
|
|
|
|
touch the member, since this will change the archive modtime. */
|
1992-01-17 08:30:31 +00:00
|
|
|
|
{
|
|
|
|
|
struct file *arfile;
|
2007-03-20 03:02:26 +00:00
|
|
|
|
arfile = enter_file (strcache_add (arname));
|
|
|
|
|
f_mtime (arfile, 0);
|
1992-01-17 08:30:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
val = 1;
|
|
|
|
|
switch (ar_member_touch (arname, memname))
|
|
|
|
|
{
|
|
|
|
|
case -1:
|
2013-11-24 03:23:52 +00:00
|
|
|
|
OS (error, NILF, _("touch: Archive '%s' does not exist"), arname);
|
1992-01-17 08:30:31 +00:00
|
|
|
|
break;
|
|
|
|
|
case -2:
|
2013-11-24 03:23:52 +00:00
|
|
|
|
OS (error, NILF, _("touch: '%s' is not a valid archive"), arname);
|
1992-01-17 08:30:31 +00:00
|
|
|
|
break;
|
|
|
|
|
case -3:
|
|
|
|
|
perror_with_name ("touch: ", arname);
|
|
|
|
|
break;
|
|
|
|
|
case 1:
|
2013-11-24 03:23:52 +00:00
|
|
|
|
OSS (error, NILF,
|
|
|
|
|
_("touch: Member '%s' does not exist in '%s'"), memname, arname);
|
1992-01-17 08:30:31 +00:00
|
|
|
|
break;
|
|
|
|
|
case 0:
|
|
|
|
|
val = 0;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
2013-11-24 03:23:52 +00:00
|
|
|
|
OS (error, NILF,
|
|
|
|
|
_("touch: Bad return code from ar_member_touch on '%s'"), name);
|
1992-01-17 08:30:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
2007-03-20 03:02:26 +00:00
|
|
|
|
free (arname);
|
1992-01-17 08:30:31 +00:00
|
|
|
|
|
|
|
|
|
return val;
|
|
|
|
|
}
|
1996-03-20 14:57:41 +00:00
|
|
|
|
#endif /* !VMS */
|
1993-06-10 01:43:06 +00:00
|
|
|
|
|
2012-03-04 00:24:20 +00:00
|
|
|
|
/* State of an 'ar_glob' run, passed to 'ar_glob_match'. */
|
1993-06-10 01:43:06 +00:00
|
|
|
|
|
2014-09-04 19:04:52 +00:00
|
|
|
|
/* On VMS, (object) modules in libraries do not have suffixes. That is, to
|
|
|
|
|
find a match for a pattern, the pattern must not have any suffix. So the
|
|
|
|
|
suffix of the pattern is saved and the pattern is stripped (ar_glob).
|
|
|
|
|
If there is a match and the match, which is a module name, is added to
|
|
|
|
|
the chain, the saved suffix is added back to construct a source filename
|
|
|
|
|
(ar_glob_match). */
|
|
|
|
|
|
1993-06-10 01:43:06 +00:00
|
|
|
|
struct ar_glob_state
|
|
|
|
|
{
|
2007-03-20 03:02:26 +00:00
|
|
|
|
const char *arname;
|
2006-11-18 20:53:44 +00:00
|
|
|
|
const char *pattern;
|
2014-09-04 19:04:52 +00:00
|
|
|
|
#ifdef VMS
|
|
|
|
|
char *suffix;
|
|
|
|
|
#endif
|
1993-06-10 01:43:06 +00:00
|
|
|
|
unsigned int size;
|
|
|
|
|
struct nameseq *chain;
|
|
|
|
|
unsigned int n;
|
|
|
|
|
};
|
|
|
|
|
|
2012-03-04 00:24:20 +00:00
|
|
|
|
/* This function is called by 'ar_scan' to match one archive
|
1993-06-10 01:43:06 +00:00
|
|
|
|
element against the pattern in STATE. */
|
|
|
|
|
|
|
|
|
|
static long int
|
2006-11-18 20:53:44 +00:00
|
|
|
|
ar_glob_match (int desc UNUSED, const char *mem, int truncated UNUSED,
|
2013-05-17 06:29:46 +00:00
|
|
|
|
long int hdrpos UNUSED, long int datapos UNUSED,
|
2004-02-24 13:50:19 +00:00
|
|
|
|
long int size UNUSED, long int date UNUSED, int uid UNUSED,
|
2006-11-18 20:53:44 +00:00
|
|
|
|
int gid UNUSED, int mode UNUSED, const void *arg)
|
1993-06-10 01:43:06 +00:00
|
|
|
|
{
|
2006-11-18 20:53:44 +00:00
|
|
|
|
struct ar_glob_state *state = (struct ar_glob_state *)arg;
|
|
|
|
|
|
1993-06-10 01:43:06 +00:00
|
|
|
|
if (fnmatch (state->pattern, mem, FNM_PATHNAME|FNM_PERIOD) == 0)
|
|
|
|
|
{
|
|
|
|
|
/* We have a match. Add it to the chain. */
|
2009-09-16 17:07:01 +00:00
|
|
|
|
struct nameseq *new = xcalloc (state->size);
|
2014-09-04 19:04:52 +00:00
|
|
|
|
#ifdef VMS
|
|
|
|
|
if (state->suffix)
|
|
|
|
|
new->name = strcache_add(
|
|
|
|
|
concat(5, state->arname, "(", mem, state->suffix, ")"));
|
|
|
|
|
else
|
|
|
|
|
#endif
|
|
|
|
|
new->name = strcache_add(concat(4, state->arname, "(", mem, ")"));
|
1993-06-10 01:43:06 +00:00
|
|
|
|
new->next = state->chain;
|
|
|
|
|
state->chain = new;
|
|
|
|
|
++state->n;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0L;
|
|
|
|
|
}
|
|
|
|
|
|
1993-06-10 22:13:54 +00:00
|
|
|
|
/* Return nonzero if PATTERN contains any metacharacters.
|
|
|
|
|
Metacharacters can be quoted with backslashes if QUOTE is nonzero. */
|
|
|
|
|
static int
|
2002-10-14 21:54:04 +00:00
|
|
|
|
glob_pattern_p (const char *pattern, int quote)
|
1993-06-10 22:13:54 +00:00
|
|
|
|
{
|
2002-10-14 21:54:04 +00:00
|
|
|
|
const char *p;
|
2010-07-05 18:32:03 +00:00
|
|
|
|
int opened = 0;
|
1993-06-10 22:13:54 +00:00
|
|
|
|
|
|
|
|
|
for (p = pattern; *p != '\0'; ++p)
|
|
|
|
|
switch (*p)
|
|
|
|
|
{
|
|
|
|
|
case '?':
|
|
|
|
|
case '*':
|
2013-05-17 06:29:46 +00:00
|
|
|
|
return 1;
|
1993-06-10 22:13:54 +00:00
|
|
|
|
|
|
|
|
|
case '\\':
|
2013-05-17 06:29:46 +00:00
|
|
|
|
if (quote)
|
|
|
|
|
++p;
|
|
|
|
|
break;
|
1993-06-10 22:13:54 +00:00
|
|
|
|
|
|
|
|
|
case '[':
|
2013-05-17 06:29:46 +00:00
|
|
|
|
opened = 1;
|
|
|
|
|
break;
|
1993-06-10 22:13:54 +00:00
|
|
|
|
|
|
|
|
|
case ']':
|
2013-05-17 06:29:46 +00:00
|
|
|
|
if (opened)
|
|
|
|
|
return 1;
|
|
|
|
|
break;
|
1993-06-10 22:13:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
1993-06-10 01:43:06 +00:00
|
|
|
|
|
|
|
|
|
/* Glob for MEMBER_PATTERN in archive ARNAME.
|
|
|
|
|
Return a malloc'd chain of matching elements (or nil if none). */
|
|
|
|
|
|
|
|
|
|
struct nameseq *
|
2006-11-18 20:53:44 +00:00
|
|
|
|
ar_glob (const char *arname, const char *member_pattern, unsigned int size)
|
1993-06-10 01:43:06 +00:00
|
|
|
|
{
|
|
|
|
|
struct ar_glob_state state;
|
|
|
|
|
struct nameseq *n;
|
2007-03-20 03:02:26 +00:00
|
|
|
|
const char **names;
|
1993-06-10 01:43:06 +00:00
|
|
|
|
unsigned int i;
|
2014-09-04 19:04:52 +00:00
|
|
|
|
#ifdef VMS
|
|
|
|
|
char *vms_member_pattern;
|
|
|
|
|
#endif
|
1993-06-10 22:13:54 +00:00
|
|
|
|
if (! glob_pattern_p (member_pattern, 1))
|
|
|
|
|
return 0;
|
|
|
|
|
|
1993-06-10 01:43:06 +00:00
|
|
|
|
/* Scan the archive for matches.
|
|
|
|
|
ar_glob_match will accumulate them in STATE.chain. */
|
2009-09-16 17:07:01 +00:00
|
|
|
|
state.arname = arname;
|
1993-06-10 01:43:06 +00:00
|
|
|
|
state.pattern = member_pattern;
|
2014-09-04 19:04:52 +00:00
|
|
|
|
#ifdef VMS
|
|
|
|
|
{
|
|
|
|
|
/* In a copy of the pattern, find the suffix, save it and remove it from
|
|
|
|
|
the pattern */
|
|
|
|
|
char *lastdot;
|
|
|
|
|
vms_member_pattern = xstrdup(member_pattern);
|
|
|
|
|
lastdot = strrchr(vms_member_pattern, '.');
|
|
|
|
|
state.suffix = lastdot;
|
|
|
|
|
if (lastdot)
|
|
|
|
|
{
|
|
|
|
|
state.suffix = xstrdup(lastdot);
|
|
|
|
|
*lastdot = 0;
|
|
|
|
|
}
|
|
|
|
|
state.pattern = vms_member_pattern;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
1993-06-10 01:43:06 +00:00
|
|
|
|
state.size = size;
|
|
|
|
|
state.chain = 0;
|
|
|
|
|
state.n = 0;
|
2006-11-18 20:53:44 +00:00
|
|
|
|
ar_scan (arname, ar_glob_match, &state);
|
1993-06-10 01:43:06 +00:00
|
|
|
|
|
2014-09-04 19:04:52 +00:00
|
|
|
|
#ifdef VMS
|
|
|
|
|
/* Deallocate any duplicated string */
|
|
|
|
|
free(vms_member_pattern);
|
|
|
|
|
if (state.suffix)
|
|
|
|
|
{
|
|
|
|
|
free(state.suffix);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
1993-06-10 01:43:06 +00:00
|
|
|
|
if (state.chain == 0)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
/* Now put the names into a vector for sorting. */
|
2007-03-20 03:02:26 +00:00
|
|
|
|
names = alloca (state.n * sizeof (const char *));
|
1993-06-10 01:43:06 +00:00
|
|
|
|
i = 0;
|
|
|
|
|
for (n = state.chain; n != 0; n = n->next)
|
|
|
|
|
names[i++] = n->name;
|
|
|
|
|
|
|
|
|
|
/* Sort them alphabetically. */
|
2010-07-19 07:10:53 +00:00
|
|
|
|
/* MSVC erroneously warns without a cast here. */
|
|
|
|
|
qsort ((void *)names, i, sizeof (*names), alpha_compare);
|
1993-06-10 01:43:06 +00:00
|
|
|
|
|
|
|
|
|
/* Put them back into the chain in the sorted order. */
|
|
|
|
|
i = 0;
|
|
|
|
|
for (n = state.chain; n != 0; n = n->next)
|
|
|
|
|
n->name = names[i++];
|
|
|
|
|
|
|
|
|
|
return state.chain;
|
|
|
|
|
}
|
1992-06-11 04:58:08 +00:00
|
|
|
|
|
2013-05-17 06:29:46 +00:00
|
|
|
|
#endif /* Not NO_ARCHIVES. */
|