1 /* $NetBSD: arch.c,v 1.177 2020/11/14 21:29:44 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
71 /* Manipulate libraries, archives and their members.
73 * The first time an archive is referenced, all of its members' headers are
74 * read and cached and the archive closed again. All cached archives are kept
75 * on a list which is searched each time an archive member is referenced.
77 * The interface to this module is:
79 * Arch_Init Initialize this module.
81 * Arch_End Clean up this module.
84 * Parse an archive specification such as
85 * "archive.a(member1 member2)".
87 * Arch_Touch Alter the modification time of the archive
88 * member described by the given node to be
89 * the time when make was started.
91 * Arch_TouchLib Update the modification time of the library
92 * described by the given node. This is special
93 * because it also updates the modification time
94 * of the library's table of contents.
97 * Find the modification time of a member of
98 * an archive *in the archive* and place it in the
101 * Arch_UpdateMemberMTime
102 * Find the modification time of a member of
103 * an archive. Called when the member doesn't
104 * already exist. Looks in the archive for the
105 * modification time. Returns the modification
108 * Arch_FindLib Search for a library along a path. The
109 * library name in the GNode should be in
112 * Arch_LibOODate Decide if a library node is out-of-date.
118 #include <sys/types.h>
119 #include <sys/stat.h>
120 #include <sys/time.h>
121 #include <sys/param.h>
126 char ar_name[16]; /* name */
127 char ar_date[12]; /* modification time */
128 char ar_uid[6]; /* user id */
129 char ar_gid[6]; /* group id */
130 char ar_mode[8]; /* octal file permissions */
131 char ar_size[10]; /* size in bytes */
135 char ar_fmag[2]; /* consistency check */
138 #if defined(HAVE_RANLIB_H) && !(defined(__ELF__) || defined(NO_RANLIB))
148 /* "@(#)arch.c 8.2 (Berkeley) 1/2/94" */
149 MAKE_RCSID("$NetBSD: arch.c,v 1.177 2020/11/14 21:29:44 rillig Exp $");
151 typedef struct List ArchList;
152 typedef struct ListNode ArchListNode;
154 static ArchList *archives; /* The archives we've already examined */
156 typedef struct Arch {
157 char *name; /* Name of archive */
158 HashTable members; /* All the members of the archive described
159 * by <name, struct ar_hdr *> key/value pairs */
160 char *fnametab; /* Extended name table strings */
161 size_t fnamesize; /* Size of the string table */
164 static FILE *ArchFindMember(const char *, const char *,
165 struct ar_hdr *, const char *);
166 #if defined(__svr4__) || defined(__SVR4) || defined(__ELF__)
168 static int ArchSVR4Entry(Arch *, char *, size_t, FILE *);
173 # define AR_NAME _ar_name.ar_name
174 # define AR_FMAG _ar_name.ar_fmag
175 # define SARMAG SAIAMAG
176 # define ARMAG AIAMAG
177 # define ARFMAG AIAFMAG
180 # define AR_NAME ar_name
183 # define AR_DATE ar_date
186 # define AR_SIZE ar_size
189 # define AR_FMAG ar_fmag
192 # define ARMAG "!<arch>\n"
206 /* Free memory from hash entries */
207 HashIter_Init(&hi, &a->members);
208 while (HashIter_Next(&hi) != NULL)
209 free(hi.entry->value);
213 HashTable_Done(&a->members);
220 * Parse an archive specification such as "archive.a(member1 member2.${EXT})",
221 * adding nodes for the expanded members to nodeLst. Nodes are created as
225 * pp The start of the specification.
226 * nodeLst The list on which to place the nodes.
227 * ctxt The context in which to expand variables.
230 * return TRUE if it was a valid specification.
231 * *pp Points to the first non-space after the archive spec.
232 * *nodeLst Nodes for the members have been added.
235 Arch_ParseArchive(char **pp, GNodeList *nodeLst, GNode *ctxt)
237 char *cp; /* Pointer into line */
238 GNode *gn; /* New node */
239 char *libName; /* Library-part of specification */
240 char *libName_freeIt = NULL;
241 char *memName; /* Member-part of specification */
242 char saveChar; /* Ending delimiter of member-name */
243 Boolean expandLibName; /* Whether the parsed libName contains
244 * variable expressions that need to be
248 expandLibName = FALSE;
250 for (cp = libName; *cp != '(' && *cp != '\0';) {
253 * Variable spec, so call the Var module to parse the puppy
254 * so we can safely advance beyond it...
256 const char *nested_p = cp;
261 /* XXX: is expanded twice: once here and once below */
262 (void)Var_Parse(&nested_p, ctxt, VARE_WANTRES | VARE_UNDEFERR,
263 &result, &result_freeIt);
264 /* TODO: handle errors */
265 isError = result == var_Error;
270 expandLibName = TRUE;
278 (void)Var_Subst(libName, ctxt, VARE_WANTRES | VARE_UNDEFERR, &libName);
279 /* TODO: handle errors */
280 libName_freeIt = libName;
286 * First skip to the start of the member's name, mark that
287 * place and skip to the end of it (either white-space or
290 Boolean doSubst = FALSE; /* TRUE if need to substitute in memName */
292 pp_skip_whitespace(&cp);
295 while (*cp != '\0' && *cp != ')' && !ch_isspace(*cp)) {
298 * Variable spec, so call the Var module to parse the puppy
299 * so we can safely advance beyond it...
304 const char *nested_p = cp;
306 (void)Var_Parse(&nested_p, ctxt, VARE_WANTRES | VARE_UNDEFERR,
308 /* TODO: handle errors */
309 isError = result == var_Error;
323 * If the specification ends without a closing parenthesis,
324 * chances are there's something wrong (like a missing backslash),
325 * so it's better to return failure than allow such things to happen
328 Parse_Error(PARSE_FATAL, "No closing parenthesis in archive specification");
333 * If we didn't move anywhere, we must be done
343 * XXX: This should be taken care of intelligently by
344 * SuffExpandChildren, both for the archive and the member portions.
347 * If member contains variables, try and substitute for them.
348 * This will slow down archive specs with dynamic sources, of course,
349 * since we'll be (non-)substituting them three times, but them's
350 * the breaks -- we need to do this since SuffExpandChildren calls
351 * us, otherwise we could assume the thing would be taken care of
357 char *oldMemName = memName;
359 (void)Var_Subst(memName, ctxt, VARE_WANTRES | VARE_UNDEFERR,
361 /* TODO: handle errors */
364 * Now form an archive spec and recurse to deal with nested
365 * variables and multi-word variable values.... The results
366 * are just placed at the end of the nodeLst we're returning.
368 buf = sacrifice = str_concat4(libName, "(", memName, ")");
370 if (strchr(memName, '$') != NULL &&
371 strcmp(memName, oldMemName) == 0) {
373 * Must contain dynamic sources, so we can't deal with it now.
374 * Just create an ARCHV node for the thing and let
375 * SuffExpandChildren handle it...
377 gn = Targ_GetNode(buf);
378 gn->type |= OP_ARCHV;
379 Lst_Append(nodeLst, gn);
381 } else if (!Arch_ParseArchive(&sacrifice, nodeLst, ctxt)) {
382 /* Error in nested call. */
388 } else if (Dir_HasWildcards(memName)) {
389 StringList *members = Lst_New();
390 Dir_Expand(memName, dirSearchPath, members);
392 while (!Lst_IsEmpty(members)) {
393 char *member = Lst_Dequeue(members);
394 char *fullname = str_concat4(libName, "(", member, ")");
397 gn = Targ_GetNode(fullname);
400 gn->type |= OP_ARCHV;
401 Lst_Append(nodeLst, gn);
406 char *fullname = str_concat4(libName, "(", memName, ")");
407 gn = Targ_GetNode(fullname);
411 * We've found the node, but have to make sure the rest of the
412 * world knows it's an archive member, without having to
413 * constantly check for parentheses, so we type the thing with
414 * the OP_ARCHV bit before we place it on the end of the
417 gn->type |= OP_ARCHV;
418 Lst_Append(nodeLst, gn);
427 free(libName_freeIt);
429 cp++; /* skip the ')' */
430 /* We promised that pp would be set up at the next non-space. */
431 pp_skip_whitespace(&cp);
436 /* Locate a member of an archive, given the path of the archive and the path
437 * of the desired member.
440 * archive Path to the archive
441 * member Name of member; only its basename is used.
442 * addToCache TRUE if archive should be cached if not already so.
445 * The ar_hdr for the member, or NULL.
447 * See ArchFindMember for an almost identical copy of this code.
449 static struct ar_hdr *
450 ArchStatMember(const char *archive, const char *member, Boolean addToCache)
452 #define AR_MAX_NAME_LEN (sizeof arh.AR_NAME - 1)
453 FILE *arch; /* Stream to archive */
454 size_t size; /* Size of archive member */
457 Arch *ar; /* Archive descriptor */
458 struct ar_hdr arh; /* archive-member header for reading archive */
459 char memName[MAXPATHLEN + 1];
460 /* Current member name while hashing. */
463 * Because of space constraints and similar things, files are archived
464 * using their basename, not the entire path.
466 const char *lastSlash = strrchr(member, '/');
467 if (lastSlash != NULL)
468 member = lastSlash + 1;
470 for (ln = archives->first; ln != NULL; ln = ln->next) {
471 const Arch *a = ln->datum;
472 if (strcmp(a->name, archive) == 0)
480 hdr = HashTable_FindValue(&ar->members, member);
485 /* Try truncated name */
486 char copy[AR_MAX_NAME_LEN + 1];
487 size_t len = strlen(member);
489 if (len > AR_MAX_NAME_LEN) {
490 len = AR_MAX_NAME_LEN;
491 snprintf(copy, sizeof copy, "%s", member);
492 hdr = HashTable_FindValue(&ar->members, copy);
500 * Caller doesn't want the thing cached, just use ArchFindMember
501 * to read the header for the member out and close down the stream
502 * again. Since the archive is not to be cached, we assume there's
503 * no need to allocate extra room for the header we're returning,
504 * so just declare it static.
506 static struct ar_hdr sarh;
508 arch = ArchFindMember(archive, member, &sarh, "r");
517 * We don't have this archive on the list yet, so we want to find out
518 * everything that's in it and cache it so we can get at it quickly.
520 arch = fopen(archive, "r");
525 * We use the ARMAG string to make sure this is an archive we
528 if (fread(magic, SARMAG, 1, arch) != 1 ||
529 strncmp(magic, ARMAG, SARMAG) != 0) {
534 ar = bmake_malloc(sizeof *ar);
535 ar->name = bmake_strdup(archive);
538 HashTable_Init(&ar->members);
539 memName[AR_MAX_NAME_LEN] = '\0';
541 while (fread(&arh, sizeof arh, 1, arch) == 1) {
544 /* If the header is bogus, there's no way we can recover. */
545 if (strncmp(arh.AR_FMAG, ARFMAG, sizeof arh.AR_FMAG) != 0)
549 * We need to advance the stream's pointer to the start of the
550 * next header. Files are padded with newlines to an even-byte
551 * boundary, so we need to extract the size of the file from the
552 * 'size' field of the header and round it up during the seek.
554 arh.AR_SIZE[sizeof arh.AR_SIZE - 1] = '\0';
555 size = (size_t)strtol(arh.AR_SIZE, NULL, 10);
557 memcpy(memName, arh.AR_NAME, sizeof arh.AR_NAME);
558 nameend = memName + AR_MAX_NAME_LEN;
559 while (nameend > memName && *nameend == ' ')
565 * svr4 names are slash terminated. Also svr4 extended AR format.
567 if (memName[0] == '/') {
569 * svr4 magic mode; handle it
571 switch (ArchSVR4Entry(ar, memName, size, arch)) {
572 case -1: /* Invalid data */
574 case 0: /* List of files entry */
576 default: /* Got the entry */
580 if (nameend[0] == '/')
587 * BSD 4.4 extended AR format: #1/<namelen>, with name as the
588 * first <namelen> bytes of the file
590 if (strncmp(memName, AR_EFMT1, sizeof AR_EFMT1 - 1) == 0 &&
591 ch_isdigit(memName[sizeof AR_EFMT1 - 1])) {
593 int elen = atoi(memName + sizeof AR_EFMT1 - 1);
595 if ((unsigned int)elen > MAXPATHLEN)
597 if (fread(memName, (size_t)elen, 1, arch) != 1)
599 memName[elen] = '\0';
600 if (fseek(arch, -elen, SEEK_CUR) != 0)
602 if (DEBUG(ARCH) || DEBUG(MAKE))
603 debug_printf("ArchStatMember: Extended format entry for %s\n",
609 struct ar_hdr *cached_hdr = bmake_malloc(sizeof *cached_hdr);
610 memcpy(cached_hdr, &arh, sizeof arh);
611 HashTable_Set(&ar->members, memName, cached_hdr);
614 if (fseek(arch, ((long)size + 1) & ~1, SEEK_CUR) != 0)
620 Lst_Append(archives, ar);
623 * Now that the archive has been read and cached, we can look into
624 * the addToCache table to find the desired member's header.
626 return HashTable_FindValue(&ar->members, member);
630 HashTable_Done(&ar->members);
638 *-----------------------------------------------------------------------
640 * Parse an SVR4 style entry that begins with a slash.
641 * If it is "//", then load the table of filenames
642 * If it is "/<offset>", then try to substitute the long file name
643 * from offset of a table previously read.
644 * If a table is read, the file pointer is moved to the next archive
648 * -1: Bad data in archive
649 * 0: A table was loaded from the file
650 * 1: Name was successfully substituted from table
651 * 2: Name was not successfully substituted from table
652 *-----------------------------------------------------------------------
655 ArchSVR4Entry(Arch *ar, char *inout_name, size_t size, FILE *arch)
657 #define ARLONGNAMES1 "//"
658 #define ARLONGNAMES2 "/ARFILENAMES"
662 if (strncmp(inout_name, ARLONGNAMES1, sizeof ARLONGNAMES1 - 1) == 0 ||
663 strncmp(inout_name, ARLONGNAMES2, sizeof ARLONGNAMES2 - 1) == 0) {
665 if (ar->fnametab != NULL) {
666 DEBUG0(ARCH, "Attempted to redefine an SVR4 name table\n");
671 * This is a table of archive names, so we build one for
674 ar->fnametab = bmake_malloc(size);
675 ar->fnamesize = size;
677 if (fread(ar->fnametab, size, 1, arch) != 1) {
678 DEBUG0(ARCH, "Reading an SVR4 name table failed\n");
681 eptr = ar->fnametab + size;
682 for (entry = 0, ptr = ar->fnametab; ptr < eptr; ptr++)
687 DEBUG1(ARCH, "Found svr4 archive name table with %lu entries\n",
688 (unsigned long)entry);
692 if (inout_name[1] == ' ' || inout_name[1] == '\0')
695 entry = (size_t)strtol(&inout_name[1], &eptr, 0);
696 if ((*eptr != ' ' && *eptr != '\0') || eptr == &inout_name[1]) {
697 DEBUG1(ARCH, "Could not parse SVR4 name %s\n", inout_name);
700 if (entry >= ar->fnamesize) {
701 DEBUG2(ARCH, "SVR4 entry offset %s is greater than %lu\n",
702 inout_name, (unsigned long)ar->fnamesize);
706 DEBUG2(ARCH, "Replaced %s with %s\n", inout_name, &ar->fnametab[entry]);
708 snprintf(inout_name, MAXPATHLEN + 1, "%s", &ar->fnametab[entry]);
715 ArchiveMember_HasName(const struct ar_hdr *hdr,
716 const char *name, size_t namelen)
718 const size_t ar_name_len = sizeof hdr->AR_NAME;
719 const char *ar_name = hdr->AR_NAME;
721 if (strncmp(ar_name, name, namelen) != 0)
724 if (namelen >= ar_name_len)
725 return namelen == ar_name_len;
727 /* hdr->AR_NAME is space-padded to the right. */
728 if (ar_name[namelen] == ' ')
731 /* In archives created by GNU binutils 2.27, the member names end with
733 if (ar_name[namelen] == '/' &&
734 (namelen == ar_name_len || ar_name[namelen + 1] == ' '))
740 /* Locate a member of an archive, given the path of the archive and the path
741 * of the desired member.
744 * archive Path to the archive
745 * member Name of member. If it is a path, only the last
747 * out_arh Archive header to be filled in
748 * mode "r" for read-only access, "r+" for read-write access
751 * return The archive file, positioned at the start of the
752 * member's struct ar_hdr, or NULL if the member doesn't
754 * *out_arh The current struct ar_hdr for member.
756 * See ArchStatMember for an almost identical copy of this code.
759 ArchFindMember(const char *archive, const char *member, struct ar_hdr *out_arh,
762 FILE *arch; /* Stream to archive */
763 int size; /* Size of archive member */
766 const char *lastSlash;
768 arch = fopen(archive, mode);
773 * We use the ARMAG string to make sure this is an archive we
776 if (fread(magic, SARMAG, 1, arch) != 1 ||
777 strncmp(magic, ARMAG, SARMAG) != 0) {
783 * Because of space constraints and similar things, files are archived
784 * using their basename, not the entire path.
786 lastSlash = strrchr(member, '/');
787 if (lastSlash != NULL)
788 member = lastSlash + 1;
790 len = tlen = strlen(member);
791 if (len > sizeof out_arh->AR_NAME) {
792 tlen = sizeof out_arh->AR_NAME;
795 while (fread(out_arh, sizeof *out_arh, 1, arch) == 1) {
797 if (strncmp(out_arh->AR_FMAG, ARFMAG, sizeof out_arh->AR_FMAG) != 0) {
799 * The header is bogus, so the archive is bad
800 * and there's no way we can recover...
806 DEBUG5(ARCH, "Reading archive %s member %.*s mtime %.*s\n",
808 (int)sizeof out_arh->AR_NAME, out_arh->AR_NAME,
809 (int)sizeof out_arh->ar_date, out_arh->ar_date);
811 if (ArchiveMember_HasName(out_arh, member, len)) {
813 * To make life easier for callers that want to update the
814 * archive, we reposition the file at the start
815 * of the header we just read before we return the stream.
816 * In a more general situation, it might be better to leave
817 * the file at the actual member, rather than its header, but
820 if (fseek(arch, -(long)sizeof *out_arh, SEEK_CUR) != 0) {
829 * BSD 4.4 extended AR format: #1/<namelen>, with name as the
830 * first <namelen> bytes of the file
832 if (strncmp(out_arh->AR_NAME, AR_EFMT1, sizeof AR_EFMT1 - 1) == 0 &&
833 ch_isdigit(out_arh->AR_NAME[sizeof AR_EFMT1 - 1]))
835 int elen = atoi(&out_arh->AR_NAME[sizeof AR_EFMT1 - 1]);
836 char ename[MAXPATHLEN + 1];
838 if ((unsigned int)elen > MAXPATHLEN) {
842 if (fread(ename, (size_t)elen, 1, arch) != 1) {
847 if (DEBUG(ARCH) || DEBUG(MAKE))
848 debug_printf("ArchFindMember: Extended format entry for %s\n",
850 if (strncmp(ename, member, len) == 0) {
851 /* Found as extended name */
852 if (fseek(arch, -(long)sizeof(struct ar_hdr) - elen,
859 if (fseek(arch, -elen, SEEK_CUR) != 0) {
867 * This isn't the member we're after, so we need to advance the
868 * stream's pointer to the start of the next header. Files are
869 * padded with newlines to an even-byte boundary, so we need to
870 * extract the size of the file from the 'size' field of the
871 * header and round it up during the seek.
873 out_arh->AR_SIZE[sizeof out_arh->AR_SIZE - 1] = '\0';
874 size = (int)strtol(out_arh->AR_SIZE, NULL, 10);
875 if (fseek(arch, (size + 1) & ~1, SEEK_CUR) != 0) {
885 /* Touch a member of an archive, on disk.
886 * The GNode's modification time is left as-is.
888 * The st_mtime of the entire archive is also changed.
889 * For a library, it may be required to run ranlib after this.
892 * gn Node of member to touch
895 * The 'time' field of the member's header is updated.
898 Arch_Touch(GNode *gn)
903 f = ArchFindMember(GNode_VarArchive(gn), GNode_VarMember(gn), &arh, "r+");
907 snprintf(arh.ar_date, sizeof arh.ar_date, "%-ld", (unsigned long)now);
908 (void)fwrite(&arh, sizeof arh, 1, f);
909 fclose(f); /* TODO: handle errors */
912 /* Given a node which represents a library, touch the thing, making sure that
913 * the table of contents is also touched.
915 * Both the modification time of the library and of the RANLIBMAG member are
918 Arch_TouchLib(GNode *gn MAKE_ATTR_UNUSED)
922 struct ar_hdr arh; /* Header describing table of contents */
923 struct utimbuf times;
925 f = ArchFindMember(gn->path, RANLIBMAG, &arh, "r+");
929 snprintf(arh.ar_date, sizeof arh.ar_date, "%-ld", (unsigned long)now);
930 (void)fwrite(&arh, sizeof arh, 1, f);
931 fclose(f); /* TODO: handle errors */
933 times.actime = times.modtime = now;
934 utime(gn->path, ×); /* TODO: handle errors */
938 /* Update the mtime of the GNode with the mtime from the archive member on
939 * disk (or in the cache). */
941 Arch_UpdateMTime(GNode *gn)
945 arh = ArchStatMember(GNode_VarArchive(gn), GNode_VarMember(gn), TRUE);
947 gn->mtime = (time_t)strtol(arh->ar_date, NULL, 10);
952 /* Given a non-existent archive member's node, update gn->mtime from its
953 * archived form, if it exists. */
955 Arch_UpdateMemberMTime(GNode *gn)
959 for (ln = gn->parents->first; ln != NULL; ln = ln->next) {
960 GNode *pgn = ln->datum;
962 if (pgn->type & OP_ARCHV) {
964 * If the parent is an archive specification and is being made
965 * and its member's name matches the name of the node we were
966 * given, record the modification time of the parent in the
967 * child. We keep searching its parents in case some other
968 * parent requires this child to exist...
970 const char *nameStart = strchr(pgn->name, '(') + 1;
971 const char *nameEnd = strchr(nameStart, ')');
972 size_t nameLen = (size_t)(nameEnd - nameStart);
974 if ((pgn->flags & REMAKE) &&
975 strncmp(nameStart, gn->name, nameLen) == 0) {
976 Arch_UpdateMTime(pgn);
977 gn->mtime = pgn->mtime;
979 } else if (pgn->flags & REMAKE) {
981 * Something which isn't a library depends on the existence of
982 * this target, so it needs to exist.
990 /* Search for a library along the given search path.
992 * The node's 'path' field is set to the found path (including the
993 * actual file name, not -l...). If the system can handle the -L
994 * flag when linking (or we cannot find the library), we assume that
995 * the user has placed the .LIBS variable in the final linking
996 * command (or the linker will know where to find it) and set the
997 * TARGET variable for this node to be the node's name. Otherwise,
998 * we set the TARGET variable to be the full path of the library,
999 * as returned by Dir_FindFile.
1002 * gn Node of library to find
1005 Arch_FindLib(GNode *gn, SearchPath *path)
1007 char *libName = str_concat3("lib", gn->name + 2, ".a");
1008 gn->path = Dir_FindFile(libName, path);
1012 Var_Set(TARGET, gn->name, gn);
1014 Var_Set(TARGET, gn->path == NULL ? gn->name : gn->path, gn);
1018 /* Decide if a node with the OP_LIB attribute is out-of-date. Called from
1019 * GNode_IsOODate to make its life easier.
1020 * The library is cached if it hasn't been already.
1022 * There are several ways for a library to be out-of-date that are
1023 * not available to ordinary files. In addition, there are ways
1024 * that are open to regular files that are not available to
1027 * A library that is only used as a source is never
1028 * considered out-of-date by itself. This does not preclude the
1029 * library's modification time from making its parent be out-of-date.
1030 * A library will be considered out-of-date for any of these reasons,
1031 * given that it is a target on a dependency line somewhere:
1033 * Its modification time is less than that of one of its sources
1034 * (gn->mtime < gn->youngestChild->mtime).
1036 * Its modification time is greater than the time at which the make
1037 * began (i.e. it's been modified in the course of the make, probably
1040 * The modification time of one of its sources is greater than the one
1041 * of its RANLIBMAG member (i.e. its table of contents is out-of-date).
1042 * We don't compare the archive time vs. TOC time because they can be
1043 * too close. In my opinion we should not bother with the TOC at all
1044 * since this is used by 'ar' rules that affect the data contents of the
1045 * archive, not by ranlib rules, which affect the TOC.
1048 Arch_LibOODate(GNode *gn)
1052 if (gn->type & OP_PHONY) {
1054 } else if (!GNode_IsTarget(gn) && Lst_IsEmpty(gn->children)) {
1056 } else if ((!Lst_IsEmpty(gn->children) && gn->youngestChild == NULL) ||
1057 (gn->mtime > now) ||
1058 (gn->youngestChild != NULL &&
1059 gn->mtime < gn->youngestChild->mtime)) {
1063 struct ar_hdr *arh; /* Header for __.SYMDEF */
1064 int modTimeTOC; /* The table-of-contents's mod time */
1066 arh = ArchStatMember(gn->path, RANLIBMAG, FALSE);
1069 modTimeTOC = (int)strtol(arh->ar_date, NULL, 10);
1071 if (DEBUG(ARCH) || DEBUG(MAKE))
1072 debug_printf("%s modified %s...",
1073 RANLIBMAG, Targ_FmtTime(modTimeTOC));
1074 oodate = gn->youngestChild == NULL ||
1075 gn->youngestChild->mtime > modTimeTOC;
1077 /* A library without a table of contents is out-of-date. */
1078 if (DEBUG(ARCH) || DEBUG(MAKE))
1079 debug_printf("no toc...");
1089 /* Initialize the archives module. */
1093 archives = Lst_New();
1096 /* Clean up the archives module. */
1101 Lst_Destroy(archives, ArchFree);
1106 Arch_IsLib(GNode *gn)
1108 static const char armag[] = "!<arch>\n";
1109 char buf[sizeof armag - 1];
1112 if ((fd = open(gn->path, O_RDONLY)) == -1)
1115 if (read(fd, buf, sizeof buf) != sizeof buf) {
1122 return memcmp(buf, armag, sizeof buf) == 0;