2 * Copyright (c) 1988, 1989, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1989 by Berkeley Softworks
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. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * @(#)arch.c 8.2 (Berkeley) 1/2/94
39 * $FreeBSD: src/usr.bin/make/arch.c,v 1.15.2.1 2001/02/13 03:13:57 will Exp $
40 * $DragonFly: src/usr.bin/make/arch.c,v 1.22 2004/12/17 21:09:04 okumoto Exp $
45 * Functions to manipulate libraries, archives and their members.
47 * Once again, cacheing/hashing comes into play in the manipulation
48 * of archives. The first time an archive is referenced, all of its members'
49 * headers are read and hashed and the archive closed again. All hashed
50 * archives are kept on a list which is searched each time an archive member
53 * The interface to this module is:
54 * Arch_ParseArchive Given an archive specification, return a list
55 * of GNode's, one for each member in the spec.
56 * FAILURE is returned if the specification is
57 * invalid for some reason.
59 * Arch_Touch Alter the modification time of the archive
60 * member described by the given node to be
63 * Arch_TouchLib Update the modification time of the library
64 * described by the given node. This is special
65 * because it also updates the modification time
66 * of the library's table of contents.
68 * Arch_MTime Find the modification time of a member of
69 * an archive *in the archive*. The time is also
70 * placed in the member's GNode. Returns the
73 * Arch_MemTime Find the modification time of a member of
74 * an archive. Called when the member doesn't
75 * already exist. Looks in the archive for the
76 * modification time. Returns the modification
79 * Arch_FindLib Search for a library along a path. The
80 * library name in the GNode should be in
83 * Arch_LibOODate Special function to decide if a library node
86 * Arch_Init Initialize this module.
88 * Arch_End Cleanup this module.
91 #include <sys/types.h>
94 #include <sys/param.h>
105 /* Lst of archives we've already examined */
106 static Lst archives = Lst_Initializer(archives);
108 typedef struct Arch {
109 char *name; /* Name of archive */
110 Hash_Table members; /* All the members of the archive described
111 * by <name, struct ar_hdr *> key/value pairs */
112 char *fnametab; /* Extended name table strings */
113 size_t fnamesize; /* Size of the string table */
116 static void ArchFree(void *);
117 static struct ar_hdr *ArchStatMember(char *, char *, Boolean);
118 static FILE *ArchFindMember(char *, char *, struct ar_hdr *, char *);
119 #if defined(__svr4__) || defined(__SVR4) || defined(__ELF__)
121 static int ArchSVR4Entry(Arch *, char *, size_t, FILE *);
125 *-----------------------------------------------------------------------
127 * Free memory used by an archive
135 *-----------------------------------------------------------------------
144 /* Free memory from hash entries */
145 for (entry = Hash_EnumFirst(&a->members, &search);
147 entry = Hash_EnumNext(&search))
148 free(Hash_GetValue(entry));
152 Hash_DeleteTable(&a->members);
157 *-----------------------------------------------------------------------
158 * Arch_ParseArchive --
159 * Parse the archive specification in the given line and find/create
160 * the nodes for the specified archive members, placing their nodes
161 * on the given list, given the pointer to the start of the
162 * specification, a Lst on which to place the nodes, and a context
163 * in which to expand variables.
166 * SUCCESS if it was a valid specification. The linePtr is updated
167 * to point to the first non-space after the archive spec. The
168 * nodes for the members are placed on the given list.
171 * Some nodes may be created. The given list is extended.
173 *-----------------------------------------------------------------------
176 Arch_ParseArchive(char **linePtr, Lst *nodeLst, GNode *ctxt)
178 char *cp; /* Pointer into line */
179 GNode *gn; /* New node */
180 char *libName; /* Library-part of specification */
181 char *memName; /* Member-part of specification */
182 char *nameBuf; /* temporary place for node name */
183 char saveChar; /* Ending delimiter of member-name */
184 Boolean subLibName; /* TRUE if libName should have/had
185 * variable substitution performed on it */
191 for (cp = libName; *cp != '(' && *cp != '\0'; cp++) {
194 * Variable spec, so call the Var module to parse the puppy
195 * so we can safely advance beyond it...
201 result = Var_Parse(cp, ctxt, TRUE, &length, &freeIt);
202 if (result == var_Error) {
217 libName = Var_Subst(NULL, libName, ctxt, TRUE);
223 * First skip to the start of the member's name, mark that
224 * place and skip to the end of it (either white-space or
227 Boolean doSubst = FALSE; /* TRUE if need to substitute in memName */
229 while (*cp != '\0' && *cp != ')' && isspace((unsigned char)*cp)) {
233 while (*cp != '\0' && *cp != ')' && !isspace((unsigned char)*cp)) {
236 * Variable spec, so call the Var module to parse the puppy
237 * so we can safely advance beyond it...
243 result = Var_Parse(cp, ctxt, TRUE, &length, &freeIt);
244 if (result == var_Error) {
260 * If the specification ends without a closing parenthesis,
261 * chances are there's something wrong (like a missing backslash),
262 * so it's better to return failure than allow such things to happen
265 printf("No closing parenthesis in archive specification\n");
270 * If we didn't move anywhere, we must be done
280 * XXX: This should be taken care of intelligently by
281 * SuffExpandChildren, both for the archive and the member portions.
284 * If member contains variables, try and substitute for them.
285 * This will slow down archive specs with dynamic sources, of course,
286 * since we'll be (non-)substituting them three times, but them's
287 * the breaks -- we need to do this since SuffExpandChildren calls
288 * us, otherwise we could assume the thing would be taken care of
294 char *oldMemName = memName;
297 memName = Var_Subst(NULL, memName, ctxt, TRUE);
300 * Now form an archive spec and recurse to deal with nested
301 * variables and multi-word variable values.... The results
302 * are just placed at the end of the nodeLst we're returning.
305 sz = strlen(memName) + strlen(libName) + 3;
306 buf = sacrifice = emalloc(sz);
308 snprintf(buf, sz, "%s(%s)", libName, memName);
310 if (strchr(memName, '$') && strcmp(memName, oldMemName) == 0) {
312 * Must contain dynamic sources, so we can't deal with it now.
313 * Just create an ARCHV node for the thing and let
314 * SuffExpandChildren handle it...
316 gn = Targ_FindNode(buf, TARG_CREATE);
322 gn->type |= OP_ARCHV;
323 Lst_AtEnd(nodeLst, (void *)gn);
325 } else if (Arch_ParseArchive(&sacrifice, nodeLst, ctxt) != SUCCESS) {
327 * Error in nested call -- free buffer and return FAILURE
334 * Free buffer and continue with our work.
337 } else if (Dir_HasWildcards(memName)) {
338 Lst members = Lst_Initializer(members);
340 size_t sz = MAXPATHLEN;
343 nameBuf = emalloc(sz);
345 Dir_Expand(memName, &dirSearchPath, &members);
346 while (!Lst_IsEmpty(&members)) {
347 member = Lst_DeQueue(&members);
348 nsz = strlen(libName) + strlen(member) + 3; /* 3 = ()+\0 */
351 nameBuf = erealloc(nameBuf, sz);
353 snprintf(nameBuf, sz, "%s(%s)", libName, member);
355 gn = Targ_FindNode(nameBuf, TARG_CREATE);
358 /* XXXHB Lst_Destroy(&members) */
362 * We've found the node, but have to make sure the rest of
363 * the world knows it's an archive member, without having
364 * to constantly check for parentheses, so we type the
365 * thing with the OP_ARCHV bit before we place it on the
366 * end of the provided list.
368 gn->type |= OP_ARCHV;
369 Lst_AtEnd(nodeLst, (void *)gn);
374 size_t sz = strlen(libName) + strlen(memName) + 3;
375 nameBuf = emalloc(sz);
376 snprintf(nameBuf, sz, "%s(%s)", libName, memName);
377 gn = Targ_FindNode(nameBuf, TARG_CREATE);
383 * We've found the node, but have to make sure the rest of the
384 * world knows it's an archive member, without having to
385 * constantly check for parentheses, so we type the thing with
386 * the OP_ARCHV bit before we place it on the end of the
389 gn->type |= OP_ARCHV;
390 Lst_AtEnd(nodeLst, gn);
401 * If substituted libName, free it now, since we need it no longer.
408 * We promised the pointer would be set up at the next non-space, so
409 * we must advance cp there before setting *linePtr... (note that on
410 * entrance to the loop, cp is guaranteed to point at a ')')
414 } while (*cp != '\0' && isspace((unsigned char)*cp));
421 *-----------------------------------------------------------------------
423 * See if the given archive is the one we are looking for. Called
424 * From ArchStatMember and ArchFindMember via Lst_Find with the
425 * current list element and the name we want.
428 * 0 if it is, non-zero if it isn't.
433 *-----------------------------------------------------------------------
436 ArchFindArchive(const void *ar, const void *archName)
439 return (strcmp(archName, ((const Arch *)ar)->name));
443 *-----------------------------------------------------------------------
445 * Locate a member of an archive, given the path of the archive and
446 * the path of the desired member, and a boolean representing whether
447 * or not the archive should be hashed (if not already hashed).
450 * A pointer to the current struct ar_hdr structure for the member. Note
451 * That no position is returned, so this is not useful for touching
452 * archive members. This is mostly because we have no assurances that
453 * The archive will remain constant after we read all the headers, so
454 * there's not much point in remembering the position...
458 *-----------------------------------------------------------------------
460 static struct ar_hdr *
461 ArchStatMember(char *archive, char *member, Boolean hash)
463 #define AR_MAX_NAME_LEN (sizeof(arh.ar_name) - 1)
464 FILE * arch; /* Stream to archive */
465 int size; /* Size of archive member */
466 char *cp; /* Useful character pointer */
468 LstNode *ln; /* Lst member containing archive descriptor */
469 Arch *ar; /* Archive descriptor */
470 Hash_Entry *he; /* Entry containing member's description */
471 struct ar_hdr arh; /* archive-member header for reading archive */
472 char memName[MAXPATHLEN];
473 /* Current member name while hashing. */
476 * Because of space constraints and similar things, files are archived
477 * using their final path components, not the entire thing, so we need
478 * to point 'member' to the final component, if there is one, to make
479 * the comparisons easier...
481 cp = strrchr(member, '/');
482 if ((cp != NULL) && (strcmp(member, RANLIBMAG) != 0))
485 ln = Lst_Find(&archives, archive, ArchFindArchive);
489 he = Hash_FindEntry(&ar->members, member);
492 return ((struct ar_hdr *)Hash_GetValue (he));
494 /* Try truncated name */
495 char copy[AR_MAX_NAME_LEN + 1];
496 size_t len = strlen(member);
498 if (len > AR_MAX_NAME_LEN) {
499 len = AR_MAX_NAME_LEN;
500 strncpy(copy, member, AR_MAX_NAME_LEN);
501 copy[AR_MAX_NAME_LEN] = '\0';
503 if ((he = Hash_FindEntry(&ar->members, copy)) != NULL)
504 return (Hash_GetValue(he));
511 * Caller doesn't want the thing hashed, just use ArchFindMember
512 * to read the header for the member out and close down the stream
513 * again. Since the archive is not to be hashed, we assume there's
514 * no need to allocate extra room for the header we're returning,
515 * so just declare it static.
517 static struct ar_hdr sarh;
519 arch = ArchFindMember(archive, member, &sarh, "r");
530 * We don't have this archive on the list yet, so we want to find out
531 * everything that's in it and cache it so we can get at it quickly.
533 arch = fopen(archive, "r");
539 * We use the ARMAG string to make sure this is an archive we
542 if ((fread(magic, SARMAG, 1, arch) != 1) ||
543 (strncmp(magic, ARMAG, SARMAG) != 0)) {
548 ar = emalloc(sizeof(Arch));
549 ar->name = estrdup(archive);
552 Hash_InitTable(&ar->members, -1);
553 memName[AR_MAX_NAME_LEN] = '\0';
555 while (fread(&arh, sizeof(struct ar_hdr), 1, arch) == 1) {
556 if (strncmp(arh.ar_fmag, ARFMAG, sizeof(arh.ar_fmag)) != 0) {
558 * The header is bogus, so the archive is bad
559 * and there's no way we can recover...
564 * We need to advance the stream's pointer to the start of the
565 * next header. Files are padded with newlines to an even-byte
566 * boundary, so we need to extract the size of the file from the
567 * 'size' field of the header and round it up during the seek.
569 arh.ar_size[sizeof(arh.ar_size) - 1] = '\0';
570 size = (int)strtol(arh.ar_size, NULL, 10);
572 strncpy(memName, arh.ar_name, sizeof(arh.ar_name));
573 for (cp = &memName[AR_MAX_NAME_LEN]; *cp == ' '; cp--) {
580 * svr4 names are slash terminated. Also svr4 extended AR format.
582 if (memName[0] == '/') {
584 * svr4 magic mode; handle it
586 switch (ArchSVR4Entry(ar, memName, size, arch)) {
587 case -1: /* Invalid data */
589 case 0: /* List of files entry */
591 default: /* Got the entry */
603 * BSD 4.4 extended AR format: #1/<namelen>, with name as the
604 * first <namelen> bytes of the file
606 if (strncmp(memName, AR_EFMT1, sizeof(AR_EFMT1) - 1) == 0 &&
607 isdigit(memName[sizeof(AR_EFMT1) - 1])) {
609 unsigned int elen = atoi(&memName[sizeof(AR_EFMT1)-1]);
611 if (elen > MAXPATHLEN)
613 if (fread(memName, elen, 1, arch) != 1)
615 memName[elen] = '\0';
616 fseek(arch, -elen, SEEK_CUR);
617 /* XXX Multiple levels may be asked for, make this conditional
618 * on one, and use DEBUGF.
620 if (DEBUG(ARCH) || DEBUG(MAKE)) {
621 fprintf(stderr, "ArchStat: Extended format entry for %s\n", memName);
626 he = Hash_CreateEntry(&ar->members, memName, NULL);
627 Hash_SetValue(he, emalloc(sizeof(struct ar_hdr)));
628 memcpy(Hash_GetValue(he), &arh, sizeof(struct ar_hdr));
630 fseek(arch, (size + 1) & ~1, SEEK_CUR);
635 Lst_AtEnd(&archives, ar);
638 * Now that the archive has been read and cached, we can look into
639 * the hash table to find the desired member's header.
641 he = Hash_FindEntry(&ar->members, member);
644 return (Hash_GetValue (he));
651 Hash_DeleteTable(&ar->members);
659 *-----------------------------------------------------------------------
661 * Parse an SVR4 style entry that begins with a slash.
662 * If it is "//", then load the table of filenames
663 * If it is "/<offset>", then try to substitute the long file name
664 * from offset of a table previously read.
667 * -1: Bad data in archive
668 * 0: A table was loaded from the file
669 * 1: Name was successfully substituted from table
670 * 2: Name was not successfully substituted from table
673 * If a table is read, the file pointer is moved to the next archive
676 *-----------------------------------------------------------------------
679 ArchSVR4Entry(Arch *ar, char *name, size_t size, FILE *arch)
681 #define ARLONGNAMES1 "//"
682 #define ARLONGNAMES2 "/ARFILENAMES"
686 if (strncmp(name, ARLONGNAMES1, sizeof(ARLONGNAMES1) - 1) == 0 ||
687 strncmp(name, ARLONGNAMES2, sizeof(ARLONGNAMES2) - 1) == 0) {
689 if (ar->fnametab != NULL) {
690 DEBUGF(ARCH, ("Attempted to redefine an SVR4 name table\n"));
695 * This is a table of archive names, so we build one for
698 ar->fnametab = emalloc(size);
699 ar->fnamesize = size;
701 if (fread(ar->fnametab, size, 1, arch) != 1) {
702 DEBUGF(ARCH, ("Reading an SVR4 name table failed\n"));
705 eptr = ar->fnametab + size;
706 for (entry = 0, ptr = ar->fnametab; ptr < eptr; ptr++)
719 DEBUGF(ARCH, ("Found svr4 archive name table with %zu entries\n", entry));
723 if (name[1] == ' ' || name[1] == '\0')
726 entry = (size_t)strtol(&name[1], &eptr, 0);
727 if ((*eptr != ' ' && *eptr != '\0') || eptr == &name[1]) {
728 DEBUGF(ARCH, ("Could not parse SVR4 name %s\n", name));
731 if (entry >= ar->fnamesize) {
732 DEBUGF(ARCH, ("SVR4 entry offset %s is greater than %zu\n",
733 name, ar->fnamesize));
737 DEBUGF(ARCH, ("Replaced %s with %s\n", name, &ar->fnametab[entry]));
739 strncpy(name, &ar->fnametab[entry], MAXPATHLEN);
740 name[MAXPATHLEN - 1] = '\0';
747 *-----------------------------------------------------------------------
749 * Locate a member of an archive, given the path of the archive and
750 * the path of the desired member. If the archive is to be modified,
751 * the mode should be "r+", if not, it should be "r". arhPtr is a
752 * poitner to the header structure to fill in.
755 * An FILE *, opened for reading and writing, positioned at the
756 * start of the member's struct ar_hdr, or NULL if the member was
757 * nonexistent. The current struct ar_hdr for member.
760 * The passed struct ar_hdr structure is filled in.
762 *-----------------------------------------------------------------------
765 ArchFindMember(char *archive, char *member, struct ar_hdr *arhPtr, char *mode)
767 FILE * arch; /* Stream to archive */
768 int size; /* Size of archive member */
769 char *cp; /* Useful character pointer */
773 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 final path components, not the entire thing, so we need
791 * to point 'member' to the final component, if there is one, to make
792 * the comparisons easier...
794 cp = strrchr(member, '/');
795 if ((cp != NULL) && (strcmp(member, RANLIBMAG) != 0)) {
798 len = tlen = strlen(member);
799 if (len > sizeof(arhPtr->ar_name)) {
800 tlen = sizeof(arhPtr->ar_name);
803 while (fread(arhPtr, sizeof(struct ar_hdr), 1, arch) == 1) {
804 if (strncmp(arhPtr->ar_fmag, ARFMAG, sizeof(arhPtr->ar_fmag) ) != 0) {
806 * The header is bogus, so the archive is bad
807 * and there's no way we can recover...
811 } else if (strncmp(member, arhPtr->ar_name, tlen) == 0) {
813 * If the member's name doesn't take up the entire 'name' field,
814 * we have to be careful of matching prefixes. Names are space-
815 * padded to the right, so if the character in 'name' at the end
816 * of the matched string is anything but a space, this isn't the
819 if (tlen != sizeof(arhPtr->ar_name) && arhPtr->ar_name[tlen] != ' '){
823 * To make life easier, we reposition the file at the start
824 * of the header we just read before we return the stream.
825 * In a more general situation, it might be better to leave
826 * the file at the actual member, rather than its header, but
829 fseek(arch, -sizeof(struct ar_hdr), SEEK_CUR);
835 * BSD 4.4 extended AR format: #1/<namelen>, with name as the
836 * first <namelen> bytes of the file
838 if (strncmp(arhPtr->ar_name, AR_EFMT1,
839 sizeof(AR_EFMT1) - 1) == 0 &&
840 isdigit(arhPtr->ar_name[sizeof(AR_EFMT1) - 1])) {
842 unsigned int elen = atoi(&arhPtr->ar_name[sizeof(AR_EFMT1)-1]);
843 char ename[MAXPATHLEN];
845 if (elen > MAXPATHLEN) {
849 if (fread(ename, elen, 1, arch) != 1) {
857 if (DEBUG(ARCH) || DEBUG(MAKE)) {
858 printf("ArchFind: Extended format entry for %s\n", ename);
860 if (strncmp(ename, member, len) == 0) {
861 /* Found as extended name */
862 fseek(arch, -sizeof(struct ar_hdr) - elen, SEEK_CUR);
865 fseek(arch, -elen, SEEK_CUR);
872 * This isn't the member we're after, so we need to advance the
873 * stream's pointer to the start of the next header. Files are
874 * padded with newlines to an even-byte boundary, so we need to
875 * extract the size of the file from the 'size' field of the
876 * header and round it up during the seek.
878 arhPtr->ar_size[sizeof(arhPtr->ar_size) - 1] = '\0';
879 size = (int)strtol(arhPtr->ar_size, NULL, 10);
880 fseek(arch, (size + 1) & ~1, SEEK_CUR);
885 * We've looked everywhere, but the member is not to be found. Close the
886 * archive and return NULL -- an error.
893 *-----------------------------------------------------------------------
895 * Touch a member of an archive.
898 * The 'time' field of the member's header is updated.
901 * The modification time of the entire archive is also changed.
902 * For a library, this could necessitate the re-ranlib'ing of the
905 *-----------------------------------------------------------------------
908 Arch_Touch(GNode *gn)
910 FILE * arch; /* Stream open to archive, positioned properly */
911 struct ar_hdr arh; /* Current header describing member */
914 arch = ArchFindMember(Var_Value(ARCHIVE, gn, &p1),
915 Var_Value(TARGET, gn, &p2),
919 snprintf(arh.ar_date, sizeof(arh.ar_date), "%-12ld", (long)now);
922 fwrite(&arh, sizeof(struct ar_hdr), 1, arch);
928 *-----------------------------------------------------------------------
930 * Given a node which represents a library, touch the thing, making
931 * sure that the table of contents also is touched.
937 * Both the modification time of the library and of the RANLIBMAG
938 * member are set to 'now'.
940 *-----------------------------------------------------------------------
943 Arch_TouchLib(GNode *gn)
946 FILE * arch; /* Stream open to archive */
947 struct ar_hdr arh; /* Header describing table of contents */
948 struct utimbuf times; /* Times for utime() call */
950 arch = ArchFindMember(gn->path, RANLIBMAG, &arh, "r+");
951 snprintf(arh.ar_date, sizeof(arh.ar_date), "%-12ld", (long) now);
954 fwrite(&arh, sizeof(struct ar_hdr), 1, arch);
957 times.actime = times.modtime = now;
958 utime(gn->path, ×);
964 *-----------------------------------------------------------------------
966 * Return the modification time of a member of an archive, given its
970 * The modification time(seconds).
973 * The mtime field of the given node is filled in with the value
974 * returned by the function.
976 *-----------------------------------------------------------------------
979 Arch_MTime(GNode *gn)
981 struct ar_hdr *arhPtr; /* Header of desired member */
982 int modTime; /* Modification time as an integer */
985 arhPtr = ArchStatMember(Var_Value(ARCHIVE, gn, &p1),
986 Var_Value(TARGET, gn, &p2),
991 if (arhPtr != NULL) {
992 modTime = (int)strtol(arhPtr->ar_date, NULL, 10);
1002 *-----------------------------------------------------------------------
1004 * Given a non-existent archive member's node, get its modification
1005 * time from its archived form, if it exists.
1008 * The modification time.
1011 * The mtime field is filled in.
1013 *-----------------------------------------------------------------------
1016 Arch_MemMTime(GNode *gn)
1023 for (ln = Lst_First(&gn->parents); ln != NULL; ln = Lst_Succ(ln)) {
1024 pgn = Lst_Datum(ln);
1026 if (pgn->type & OP_ARCHV) {
1028 * If the parent is an archive specification and is being made
1029 * and its member's name matches the name of the node we were
1030 * given, record the modification time of the parent in the
1031 * child. We keep searching its parents in case some other
1032 * parent requires this child to exist...
1034 nameStart = strchr(pgn->name, '(') + 1;
1035 nameEnd = strchr(nameStart, ')');
1038 strncmp(nameStart, gn->name, nameEnd - nameStart) == 0) {
1039 gn->mtime = Arch_MTime(pgn);
1041 } else if (pgn->make) {
1043 * Something which isn't a library depends on the existence of
1044 * this target, so it needs to exist.
1054 *-----------------------------------------------------------------------
1056 * Search for a named library along the given search path.
1062 * The node's 'path' field is set to the found path (including the
1063 * actual file name, not -l...). If the system can handle the -L
1064 * flag when linking (or we cannot find the library), we assume that
1065 * the user has placed the .LIBRARIES variable in the final linking
1066 * command (or the linker will know where to find it) and set the
1067 * TARGET variable for this node to be the node's name. Otherwise,
1068 * we set the TARGET variable to be the full path of the library,
1069 * as returned by Dir_FindFile.
1071 *-----------------------------------------------------------------------
1074 Arch_FindLib(GNode *gn, Lst *path)
1076 char *libName; /* file name for archive */
1079 sz = strlen(gn->name) + 4;
1080 libName = emalloc(sz);
1081 snprintf(libName, sz, "lib%s.a", &gn->name[2]);
1083 gn->path = Dir_FindFile(libName, path);
1088 Var_Set(TARGET, gn->name, gn);
1090 Var_Set(TARGET, gn->path == NULL ? gn->name : gn->path, gn);
1091 #endif /* LIBRARIES */
1095 *-----------------------------------------------------------------------
1097 * Decide if a node with the OP_LIB attribute is out-of-date. Called
1098 * from Make_OODate to make its life easier, with the library's
1101 * There are several ways for a library to be out-of-date that are
1102 * not available to ordinary files. In addition, there are ways
1103 * that are open to regular files that are not available to
1104 * libraries. A library that is only used as a source is never
1105 * considered out-of-date by itself. This does not preclude the
1106 * library's modification time from making its parent be out-of-date.
1107 * A library will be considered out-of-date for any of these reasons,
1108 * given that it is a target on a dependency line somewhere:
1109 * Its modification time is less than that of one of its
1110 * sources (gn->mtime < gn->cmtime).
1111 * Its modification time is greater than the time at which the
1112 * make began (i.e. it's been modified in the course
1113 * of the make, probably by archiving).
1114 * The modification time of one of its sources is greater than
1115 * the one of its RANLIBMAG member (i.e. its table of contents
1116 * is out-of-date). We don't compare of the archive time
1117 * vs. TOC time because they can be too close. In my
1118 * opinion we should not bother with the TOC at all since
1119 * this is used by 'ar' rules that affect the data contents
1120 * of the archive, not by ranlib rules, which affect the
1124 * TRUE if the library is out-of-date. FALSE otherwise.
1127 * The library will be hashed if it hasn't been already.
1129 *-----------------------------------------------------------------------
1132 Arch_LibOODate(GNode *gn)
1136 if (OP_NOP(gn->type) && Lst_IsEmpty(&gn->children)) {
1138 } else if ((gn->mtime > now) || (gn->mtime < gn->cmtime)) {
1142 struct ar_hdr *arhPtr; /* Header for __.SYMDEF */
1143 int modTimeTOC; /* The table-of-contents's mod time */
1145 arhPtr = ArchStatMember(gn->path, RANLIBMAG, FALSE);
1147 if (arhPtr != NULL) {
1148 modTimeTOC = (int)strtol(arhPtr->ar_date, NULL, 10);
1150 /* XXX choose one. */
1151 if (DEBUG(ARCH) || DEBUG(MAKE)) {
1152 printf("%s modified %s...", RANLIBMAG, Targ_FmtTime(modTimeTOC));
1154 oodate = (gn->cmtime > modTimeTOC);
1157 * A library w/o a table of contents is out-of-date
1159 if (DEBUG(ARCH) || DEBUG(MAKE)) {
1160 printf("No t.o.c....");
1165 oodate = (gn->mtime == 0); /* out-of-date if not present */
1172 *-----------------------------------------------------------------------
1174 * Initialize things for this module.
1179 *-----------------------------------------------------------------------
1187 *-----------------------------------------------------------------------
1189 * Cleanup things for this module.
1195 * The 'archives' list is freed
1197 *-----------------------------------------------------------------------
1203 Lst_Destroy(&archives, ArchFree);