1 /* $NetBSD: arch.c,v 1.213 2023/02/14 21:08:00 rillig Exp $ */
4 * Copyright (c) 1988, 1989, 1990, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * Copyright (c) 1989 by Berkeley Softworks
37 * All rights reserved.
39 * This code is derived from software contributed to Berkeley by
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 3. All advertising materials mentioning features or use of this software
51 * must display the following acknowledgement:
52 * This product includes software developed by the University of
53 * California, Berkeley and its contributors.
54 * 4. Neither the name of the University nor the names of its contributors
55 * may be used to endorse or promote products derived from this software
56 * without specific prior written permission.
58 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
72 * Manipulate libraries, archives and their members.
74 * The first time an archive is referenced, all of its members' headers are
75 * read and cached and the archive closed again. All cached archives are kept
76 * on a list which is searched each time an archive member is referenced.
78 * The interface to this module is:
80 * Arch_Init Initialize this module.
82 * Arch_End Clean up this module.
85 * Parse an archive specification such as
86 * "archive.a(member1 member2)".
88 * Arch_Touch Alter the modification time of the archive
89 * member described by the given node to be
90 * the time when make was started.
92 * Arch_TouchLib Update the modification time of the library
93 * described by the given node. This is special
94 * because it also updates the modification time
95 * of the library's table of contents.
98 * Find the modification time of a member of
99 * an archive *in the archive* and place it in the
102 * Arch_UpdateMemberMTime
103 * Find the modification time of a member of
104 * an archive. Called when the member doesn't
105 * already exist. Looks in the archive for the
106 * modification time. Returns the modification
109 * Arch_FindLib Search for a library along a path. The
110 * library name in the GNode should be in
113 * Arch_LibOODate Decide if a library node is out-of-date.
119 #include <sys/types.h>
120 #include <sys/stat.h>
121 #include <sys/time.h>
122 #include <sys/param.h>
127 char ar_name[16]; /* name */
128 char ar_date[12]; /* modification time */
129 char ar_uid[6]; /* user id */
130 char ar_gid[6]; /* group id */
131 char ar_mode[8]; /* octal file permissions */
132 char ar_size[10]; /* size in bytes */
136 char ar_fmag[2]; /* consistency check */
139 #if defined(HAVE_RANLIB_H) && !(defined(__ELF__) || defined(NO_RANLIB))
149 /* "@(#)arch.c 8.2 (Berkeley) 1/2/94" */
150 MAKE_RCSID("$NetBSD: arch.c,v 1.213 2023/02/14 21:08:00 rillig Exp $");
152 typedef struct List ArchList;
153 typedef struct ListNode ArchListNode;
155 static ArchList archives; /* The archives we've already examined */
157 typedef struct Arch {
158 char *name; /* Name of archive */
159 HashTable members; /* All the members of the archive described
160 * by <name, struct ar_hdr *> key/value pairs */
161 char *fnametab; /* Extended name table strings */
162 size_t fnamesize; /* Size of the string table */
165 static FILE *ArchFindMember(const char *, const char *,
166 struct ar_hdr *, const char *);
167 #if defined(__svr4__) || defined(__SVR4) || defined(__ELF__)
169 static int ArchSVR4Entry(Arch *, char *, size_t, FILE *);
174 # define AR_NAME _ar_name.ar_name
175 # define AR_FMAG _ar_name.ar_fmag
176 # define SARMAG SAIAMAG
177 # define ARMAG AIAMAG
178 # define ARFMAG AIAFMAG
181 # define AR_NAME ar_name
184 # define AR_DATE ar_date
187 # define AR_SIZE ar_size
190 # define AR_FMAG ar_fmag
193 # define ARMAG "!<arch>\n"
207 /* Free memory from hash entries */
208 HashIter_Init(&hi, &a->members);
209 while (HashIter_Next(&hi) != NULL)
210 free(hi.entry->value);
214 HashTable_Done(&a->members);
219 /* Return "archive(member)". */
221 FullName(const char *archive, const char *member)
223 size_t len1 = strlen(archive);
224 size_t len3 = strlen(member);
225 char *result = bmake_malloc(len1 + 1 + len3 + 1 + 1);
226 memcpy(result, archive, len1);
227 memcpy(result + len1, "(", 1);
228 memcpy(result + len1 + 1, member, len3);
229 memcpy(result + len1 + 1 + len3, ")", 1 + 1);
234 * Parse an archive specification such as "archive.a(member1 member2.${EXT})",
235 * adding nodes for the expanded members to gns. Nodes are created as
239 * pp The start of the specification.
240 * gns The list on which to place the nodes.
241 * scope The scope in which to expand variables.
244 * return True if it was a valid specification.
245 * *pp Points to the first non-space after the archive spec.
248 Arch_ParseArchive(char **pp, GNodeList *gns, GNode *scope)
250 char *spec; /* For modifying some bytes of *pp */
251 const char *cp; /* Pointer into line */
252 GNode *gn; /* New node */
253 FStr lib; /* Library-part of specification */
254 FStr mem; /* Member-part of specification */
255 char saveChar; /* Ending delimiter of member-name */
256 bool expandLib; /* Whether the parsed lib contains variable
257 * expressions that need to be expanded */
260 lib = FStr_InitRefer(spec);
263 for (cp = lib.str; *cp != '(' && *cp != '\0';) {
265 /* Expand nested variable expressions. */
266 /* XXX: This code can probably be shortened. */
267 const char *nested_p = cp;
271 /* XXX: is expanded twice: once here and once below */
272 result = Var_Parse(&nested_p, scope, VARE_UNDEFERR);
273 /* TODO: handle errors */
274 isError = result.str == var_Error;
285 spec[cp++ - spec] = '\0';
287 Var_Expand(&lib, scope, VARE_UNDEFERR);
291 * First skip to the start of the member's name, mark that
292 * place and skip to the end of it (either white-space or
295 bool doSubst = false;
297 cpp_skip_whitespace(&cp);
299 mem = FStr_InitRefer(cp);
300 while (*cp != '\0' && *cp != ')' && !ch_isspace(*cp)) {
302 /* Expand nested variable expressions. */
304 * XXX: This code can probably be shortened.
308 const char *nested_p = cp;
310 result = Var_Parse(&nested_p, scope,
312 /* TODO: handle errors */
313 isError = result.str == var_Error;
327 * If the specification ends without a closing parenthesis,
328 * chances are there's something wrong (like a missing
329 * backslash), so it's better to return failure than allow
330 * such things to happen
333 Parse_Error(PARSE_FATAL,
334 "No closing parenthesis "
335 "in archive specification");
340 * If we didn't move anywhere, we must be done
346 spec[cp - spec] = '\0';
349 * XXX: This should be taken care of intelligently by
350 * SuffExpandChildren, both for the archive and the member
354 * If member contains variables, try and substitute for them.
355 * This slows down archive specs with dynamic sources, since
356 * they are (non-)substituted three times, but we need to do
357 * this since SuffExpandChildren calls us, otherwise we could
358 * assume the substitutions would be taken care of later.
363 const char *unexpandedMem = mem.str;
365 Var_Expand(&mem, scope, VARE_UNDEFERR);
368 * Now form an archive spec and recurse to deal with
369 * nested variables and multi-word variable values.
371 fullName = FullName(lib.str, mem.str);
374 if (strcmp(mem.str, unexpandedMem) == 0) {
376 * Must contain dynamic sources, so we can't
377 * deal with it now. Just create an ARCHV node
378 * for the thing and let SuffExpandChildren
381 gn = Targ_GetNode(fullName);
382 gn->type |= OP_ARCHV;
385 } else if (!Arch_ParseArchive(&p, gns, scope)) {
386 /* Error in nested call. */
388 /* XXX: does unexpandedMemName leak? */
392 /* XXX: does unexpandedMemName leak? */
394 } else if (Dir_HasWildcards(mem.str)) {
395 StringList members = LST_INIT;
396 SearchPath_Expand(&dirSearchPath, mem.str, &members);
398 while (!Lst_IsEmpty(&members)) {
399 char *member = Lst_Dequeue(&members);
400 char *fullname = FullName(lib.str, member);
403 gn = Targ_GetNode(fullname);
406 gn->type |= OP_ARCHV;
412 char *fullname = FullName(lib.str, mem.str);
413 gn = Targ_GetNode(fullname);
417 * We've found the node, but have to make sure the
418 * rest of the world knows it's an archive member,
419 * without having to constantly check for parentheses,
420 * so we type the thing with the OP_ARCHV bit before
421 * we place it on the end of the provided list.
423 gn->type |= OP_ARCHV;
428 spec[cp - spec] = saveChar;
433 cp++; /* skip the ')' */
434 /* We promised that pp would be set up at the next non-space. */
435 cpp_skip_whitespace(&cp);
441 * Locate a member of an archive, given the path of the archive and the path
442 * of the desired member.
445 * archive Path to the archive
446 * member Name of member; only its basename is used.
447 * addToCache True if archive should be cached if not already so.
450 * The ar_hdr for the member, or NULL.
452 * See ArchFindMember for an almost identical copy of this code.
454 static struct ar_hdr *
455 ArchStatMember(const char *archive, const char *member, bool addToCache)
457 #define AR_MAX_NAME_LEN (sizeof arh.ar_name - 1)
459 size_t size; /* Size of archive member */
462 Arch *ar; /* Archive descriptor */
463 struct ar_hdr arh; /* archive-member header for reading archive */
464 char memName[MAXPATHLEN + 1];
465 /* Current member name while hashing. */
468 * Because of space constraints and similar things, files are archived
469 * using their basename, not the entire path.
471 member = str_basename(member);
473 for (ln = archives.first; ln != NULL; ln = ln->next) {
474 const Arch *a = ln->datum;
475 if (strcmp(a->name, archive) == 0)
483 hdr = HashTable_FindValue(&ar->members, member);
488 /* Try truncated name */
489 char copy[AR_MAX_NAME_LEN + 1];
490 size_t len = strlen(member);
492 if (len > AR_MAX_NAME_LEN) {
493 snprintf(copy, sizeof copy, "%s", member);
494 hdr = HashTable_FindValue(&ar->members, copy);
502 * Caller doesn't want the thing cached, just use
503 * ArchFindMember to read the header for the member out and
504 * close down the stream again. Since the archive is not to be
505 * cached, we assume there's no need to allocate extra room
506 * for the header we're returning, so just declare it static.
508 static struct ar_hdr sarh;
510 arch = ArchFindMember(archive, member, &sarh, "r");
519 * We don't have this archive on the list yet, so we want to find out
520 * everything that's in it and cache it so we can get at it quickly.
522 arch = fopen(archive, "r");
527 * We use the ARMAG string to make sure this is an archive we
530 if (fread(magic, SARMAG, 1, arch) != 1 ||
531 strncmp(magic, ARMAG, SARMAG) != 0) {
536 ar = bmake_malloc(sizeof *ar);
537 ar->name = bmake_strdup(archive);
540 HashTable_Init(&ar->members);
541 memName[AR_MAX_NAME_LEN] = '\0';
543 while (fread(&arh, sizeof arh, 1, arch) == 1) {
546 /* If the header is bogus, there's no way we can recover. */
547 if (strncmp(arh.AR_FMAG, ARFMAG, sizeof arh.AR_FMAG) != 0)
551 * We need to advance the stream's pointer to the start of the
552 * next header. Files are padded with newlines to an even-byte
553 * boundary, so we need to extract the size of the file from
554 * the 'size' field of the header and round it up during the
557 arh.AR_SIZE[sizeof arh.AR_SIZE - 1] = '\0';
558 size = (size_t)strtol(arh.AR_SIZE, NULL, 10);
560 memcpy(memName, arh.AR_NAME, sizeof arh.AR_NAME);
561 nameend = memName + AR_MAX_NAME_LEN;
562 while (nameend > memName && *nameend == ' ')
568 * svr4 names are slash-terminated.
569 * Also svr4 extended the AR format.
571 if (memName[0] == '/') {
572 /* svr4 magic mode; handle it */
573 switch (ArchSVR4Entry(ar, memName, size, arch)) {
574 case -1: /* Invalid data */
576 case 0: /* List of files entry */
578 default: /* Got the entry */
582 if (nameend[0] == '/')
589 * BSD 4.4 extended AR format: #1/<namelen>, with name as the
590 * first <namelen> bytes of the file
592 if (strncmp(memName, AR_EFMT1, sizeof AR_EFMT1 - 1) == 0 &&
593 ch_isdigit(memName[sizeof AR_EFMT1 - 1])) {
595 size_t elen = (size_t)atoi(
596 memName + sizeof AR_EFMT1 - 1);
598 if (elen > MAXPATHLEN)
600 if (fread(memName, elen, 1, arch) != 1)
602 memName[elen] = '\0';
603 if (fseek(arch, -(long)elen, SEEK_CUR) != 0)
605 if (DEBUG(ARCH) || DEBUG(MAKE))
608 "Extended format entry for %s\n",
614 struct ar_hdr *cached_hdr = bmake_malloc(
616 memcpy(cached_hdr, &arh, sizeof arh);
617 HashTable_Set(&ar->members, memName, cached_hdr);
620 if (fseek(arch, ((long)size + 1) & ~1, SEEK_CUR) != 0)
626 Lst_Append(&archives, ar);
629 * Now that the archive has been read and cached, we can look into
630 * the addToCache table to find the desired member's header.
632 return HashTable_FindValue(&ar->members, member);
636 HashTable_Done(&ar->members);
644 * Parse an SVR4 style entry that begins with a slash.
645 * If it is "//", then load the table of filenames.
646 * If it is "/<offset>", then try to substitute the long file name
647 * from offset of a table previously read.
648 * If a table is read, the file pointer is moved to the next archive member.
651 * -1: Bad data in archive
652 * 0: A table was loaded from the file
653 * 1: Name was successfully substituted from table
654 * 2: Name was not successfully substituted from table
657 ArchSVR4Entry(Arch *ar, char *inout_name, size_t size, FILE *arch)
659 #define ARLONGNAMES1 "//"
660 #define ARLONGNAMES2 "/ARFILENAMES"
664 if (strncmp(inout_name, ARLONGNAMES1, sizeof ARLONGNAMES1 - 1) == 0 ||
665 strncmp(inout_name, ARLONGNAMES2, sizeof ARLONGNAMES2 - 1) == 0) {
667 if (ar->fnametab != NULL) {
669 "Attempted to redefine an SVR4 name table\n");
674 * This is a table of archive names, so we build one for
677 ar->fnametab = bmake_malloc(size);
678 ar->fnamesize = size;
680 if (fread(ar->fnametab, size, 1, arch) != 1) {
681 DEBUG0(ARCH, "Reading an SVR4 name table failed\n");
684 eptr = ar->fnametab + size;
685 for (entry = 0, ptr = ar->fnametab; ptr < eptr; ptr++)
691 "Found svr4 archive name table with %lu entries\n",
692 (unsigned long)entry);
696 if (inout_name[1] == ' ' || inout_name[1] == '\0')
699 entry = (size_t)strtol(&inout_name[1], &eptr, 0);
700 if ((*eptr != ' ' && *eptr != '\0') || eptr == &inout_name[1]) {
701 DEBUG1(ARCH, "Could not parse SVR4 name %s\n", inout_name);
704 if (entry >= ar->fnamesize) {
705 DEBUG2(ARCH, "SVR4 entry offset %s is greater than %lu\n",
706 inout_name, (unsigned long)ar->fnamesize);
710 DEBUG2(ARCH, "Replaced %s with %s\n", inout_name, &ar->fnametab[entry]);
712 snprintf(inout_name, MAXPATHLEN + 1, "%s", &ar->fnametab[entry]);
719 ArchiveMember_HasName(const struct ar_hdr *hdr,
720 const char *name, size_t namelen)
722 const size_t ar_name_len = sizeof hdr->AR_NAME;
723 const char *ar_name = hdr->AR_NAME;
725 if (strncmp(ar_name, name, namelen) != 0)
728 if (namelen >= ar_name_len)
729 return namelen == ar_name_len;
731 /* hdr->AR_NAME is space-padded to the right. */
732 if (ar_name[namelen] == ' ')
736 * In archives created by GNU binutils 2.27, the member names end
739 if (ar_name[namelen] == '/' &&
740 (namelen == ar_name_len || ar_name[namelen + 1] == ' '))
747 * Locate a member of an archive, given the path of the archive and the path
748 * of the desired member.
751 * archive Path to the archive
752 * member Name of member. If it is a path, only the last
754 * out_arh Archive header to be filled in
755 * mode "r" for read-only access, "r+" for read-write access
758 * return The archive file, positioned at the start of the
759 * member's struct ar_hdr, or NULL if the member doesn't
761 * *out_arh The current struct ar_hdr for member.
763 * See ArchStatMember for an almost identical copy of this code.
766 ArchFindMember(const char *archive, const char *member, struct ar_hdr *out_arh,
769 FILE *arch; /* Stream to archive */
770 int size; /* Size of archive member */
774 arch = fopen(archive, mode);
779 * We use the ARMAG string to make sure this is an archive we
782 if (fread(magic, SARMAG, 1, arch) != 1 ||
783 strncmp(magic, ARMAG, SARMAG) != 0) {
789 * Because of space constraints and similar things, files are archived
790 * using their basename, not the entire path.
792 member = str_basename(member);
794 len = strlen(member);
796 while (fread(out_arh, sizeof *out_arh, 1, arch) == 1) {
798 if (strncmp(out_arh->AR_FMAG, ARFMAG,
799 sizeof out_arh->AR_FMAG) != 0) {
801 * The header is bogus, so the archive is bad
802 * and there's no way we can recover...
808 DEBUG5(ARCH, "Reading archive %s member %.*s mtime %.*s\n",
810 (int)sizeof out_arh->AR_NAME, out_arh->AR_NAME,
811 (int)sizeof out_arh->ar_date, out_arh->ar_date);
813 if (ArchiveMember_HasName(out_arh, member, len)) {
815 * To make life easier for callers that want to update
816 * the archive, we reposition the file at the start of
817 * the header we just read before we return the
818 * stream. In a more general situation, it might be
819 * better to leave the file at the actual member,
820 * rather than its header, but not here.
822 if (fseek(arch, -(long)sizeof *out_arh, SEEK_CUR) !=
832 * BSD 4.4 extended AR format: #1/<namelen>, with name as the
833 * first <namelen> bytes of the file
835 if (strncmp(out_arh->AR_NAME, AR_EFMT1, sizeof AR_EFMT1 - 1) ==
837 (ch_isdigit(out_arh->AR_NAME[sizeof AR_EFMT1 - 1]))) {
838 size_t elen = (size_t)atoi(
839 &out_arh->AR_NAME[sizeof AR_EFMT1 - 1]);
840 char ename[MAXPATHLEN + 1];
842 if (elen > MAXPATHLEN) {
846 if (fread(ename, elen, 1, arch) != 1) {
851 if (DEBUG(ARCH) || DEBUG(MAKE))
854 "Extended format entry for %s\n",
856 if (strncmp(ename, member, len) == 0) {
857 /* Found as extended name */
859 -(long)(sizeof(struct ar_hdr) - elen),
866 if (fseek(arch, -(long)elen, SEEK_CUR) != 0) {
874 * This isn't the member we're after, so we need to advance the
875 * stream's pointer to the start of the next header. Files are
876 * padded with newlines to an even-byte boundary, so we need to
877 * extract the size of the file from the 'size' field of the
878 * header and round it up during the seek.
880 out_arh->AR_SIZE[sizeof out_arh->AR_SIZE - 1] = '\0';
881 size = (int)strtol(out_arh->AR_SIZE, NULL, 10);
882 if (fseek(arch, (size + 1) & ~1L, SEEK_CUR) != 0) {
893 * Touch a member of an archive, on disk.
894 * The GNode's modification time is left as-is.
896 * The st_mtime of the entire archive is also changed.
897 * For a library, it may be required to run ranlib after this.
900 * gn Node of member to touch
903 * The 'time' field of the member's header is updated.
906 Arch_Touch(GNode *gn)
911 f = ArchFindMember(GNode_VarArchive(gn), GNode_VarMember(gn), &arh,
916 snprintf(arh.ar_date, sizeof arh.ar_date, "%-ld", (unsigned long)now);
917 (void)fwrite(&arh, sizeof arh, 1, f);
918 fclose(f); /* TODO: handle errors */
922 * Given a node which represents a library, touch the thing, making sure that
923 * the table of contents is also touched.
925 * Both the modification time of the library and of the RANLIBMAG member are
930 Arch_TouchLib(GNode *gn MAKE_ATTR_UNUSED)
934 struct ar_hdr arh; /* Header describing table of contents */
935 struct utimbuf times;
937 f = ArchFindMember(gn->path, RANLIBMAG, &arh, "r+");
941 snprintf(arh.ar_date, sizeof arh.ar_date, "%-ld", (unsigned long)now);
942 (void)fwrite(&arh, sizeof arh, 1, f);
943 fclose(f); /* TODO: handle errors */
945 times.actime = times.modtime = now;
946 utime(gn->path, ×); /* TODO: handle errors */
951 * Update the mtime of the GNode with the mtime from the archive member on
952 * disk (or in the cache).
955 Arch_UpdateMTime(GNode *gn)
959 arh = ArchStatMember(GNode_VarArchive(gn), GNode_VarMember(gn), true);
961 gn->mtime = (time_t)strtol(arh->ar_date, NULL, 10);
967 * Given a nonexistent archive member's node, update gn->mtime from its
968 * archived form, if it exists.
971 Arch_UpdateMemberMTime(GNode *gn)
975 for (ln = gn->parents.first; ln != NULL; ln = ln->next) {
976 GNode *pgn = ln->datum;
978 if (pgn->type & OP_ARCHV) {
980 * If the parent is an archive specification and is
981 * being made and its member's name matches the name
982 * of the node we were given, record the modification
983 * time of the parent in the child. We keep searching
984 * its parents in case some other parent requires this
987 const char *nameStart = strchr(pgn->name, '(') + 1;
988 const char *nameEnd = strchr(nameStart, ')');
989 size_t nameLen = (size_t)(nameEnd - nameStart);
991 if (pgn->flags.remake &&
992 strncmp(nameStart, gn->name, nameLen) == 0) {
993 Arch_UpdateMTime(pgn);
994 gn->mtime = pgn->mtime;
996 } else if (pgn->flags.remake) {
998 * Something which isn't a library depends on the
999 * existence of this target, so it needs to exist.
1008 * Search for a library along the given search path.
1010 * The node's 'path' field is set to the found path (including the
1011 * actual file name, not -l...). If the system can handle the -L
1012 * flag when linking (or we cannot find the library), we assume that
1013 * the user has placed the .LIBS variable in the final linking
1014 * command (or the linker will know where to find it) and set the
1015 * TARGET variable for this node to be the node's name. Otherwise,
1016 * we set the TARGET variable to be the full path of the library,
1017 * as returned by Dir_FindFile.
1020 * gn Node of library to find
1023 Arch_FindLib(GNode *gn, SearchPath *path)
1025 char *libName = str_concat3("lib", gn->name + 2, ".a");
1026 gn->path = Dir_FindFile(libName, path);
1030 Var_Set(gn, TARGET, gn->name);
1032 Var_Set(gn, TARGET, GNode_Path(gn));
1038 RanlibOODate(const GNode *gn MAKE_ATTR_UNUSED)
1041 struct ar_hdr *arh; /* Header for __.SYMDEF */
1042 int tocModTime; /* The table-of-contents' mod time */
1044 arh = ArchStatMember(gn->path, RANLIBMAG, false);
1047 /* A library without a table of contents is out-of-date. */
1048 if (DEBUG(ARCH) || DEBUG(MAKE))
1049 debug_printf("no toc...");
1053 tocModTime = (int)strtol(arh->ar_date, NULL, 10);
1055 if (DEBUG(ARCH) || DEBUG(MAKE))
1056 debug_printf("%s modified %s...",
1057 RANLIBMAG, Targ_FmtTime(tocModTime));
1058 return gn->youngestChild == NULL ||
1059 gn->youngestChild->mtime > tocModTime;
1066 * Decide if a node with the OP_LIB attribute is out-of-date. Called from
1067 * GNode_IsOODate to make its life easier.
1068 * The library is cached if it hasn't been already.
1070 * There are several ways for a library to be out-of-date that are
1071 * not available to ordinary files. In addition, there are ways
1072 * that are open to regular files that are not available to
1075 * A library that is only used as a source is never
1076 * considered out-of-date by itself. This does not preclude the
1077 * library's modification time from making its parent be out-of-date.
1078 * A library will be considered out-of-date for any of these reasons,
1079 * given that it is a target on a dependency line somewhere:
1081 * Its modification time is less than that of one of its sources
1082 * (gn->mtime < gn->youngestChild->mtime).
1084 * Its modification time is greater than the time at which the make
1085 * began (i.e. it's been modified in the course of the make, probably
1088 * The modification time of one of its sources is greater than the one
1089 * of its RANLIBMAG member (i.e. its table of contents is out-of-date).
1090 * We don't compare the archive time vs. TOC time because they can be
1091 * too close. In my opinion we should not bother with the TOC at all
1092 * since this is used by 'ar' rules that affect the data contents of the
1093 * archive, not by ranlib rules, which affect the TOC.
1096 Arch_LibOODate(GNode *gn)
1099 if (gn->type & OP_PHONY) {
1101 } else if (!GNode_IsTarget(gn) && Lst_IsEmpty(&gn->children)) {
1103 } else if ((!Lst_IsEmpty(&gn->children) && gn->youngestChild == NULL) ||
1104 (gn->mtime > now) ||
1105 (gn->youngestChild != NULL &&
1106 gn->mtime < gn->youngestChild->mtime)) {
1109 return RanlibOODate(gn);
1113 /* Initialize the archives module. */
1117 Lst_Init(&archives);
1120 /* Clean up the archives module. */
1125 Lst_DoneCall(&archives, ArchFree);
1130 Arch_IsLib(GNode *gn)
1132 static const char armag[] = "!<arch>\n";
1133 char buf[sizeof armag - 1];
1136 if ((fd = open(gn->path, O_RDONLY)) == -1)
1139 if (read(fd, buf, sizeof buf) != sizeof buf) {
1146 return memcmp(buf, armag, sizeof buf) == 0;