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.48 2005/02/10 14:39:05 harti Exp $
40 * $DragonFly: src/usr.bin/make/arch.c,v 1.35 2005/02/23 00:26:02 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.
89 #include <sys/param.h>
90 #include <sys/types.h>
111 /* Lst of archives we've already examined */
112 static Lst archives = Lst_Initializer(archives);
114 typedef struct Arch {
115 char *name; /* Name of archive */
116 Hash_Table members; /* All the members of the archive described
117 * by <name, struct ar_hdr *> key/value pairs */
118 char *fnametab; /* Extended name table strings */
119 size_t fnamesize; /* Size of the string table */
122 static struct ar_hdr *ArchStatMember(const char *, const char *, Boolean);
123 static FILE *ArchFindMember(const char *, const char *, struct ar_hdr *,
125 #if defined(__svr4__) || defined(__SVR4) || defined(__ELF__)
127 static int ArchSVR4Entry(Arch *, char *, size_t, FILE *);
131 *-----------------------------------------------------------------------
132 * Arch_ParseArchive --
133 * Parse the archive specification in the given line and find/create
134 * the nodes for the specified archive members, placing their nodes
135 * on the given list, given the pointer to the start of the
136 * specification, a Lst on which to place the nodes, and a context
137 * in which to expand variables.
140 * SUCCESS if it was a valid specification. The linePtr is updated
141 * to point to the first non-space after the archive spec. The
142 * nodes for the members are placed on the given list.
145 * Some nodes may be created. The given list is extended.
147 *-----------------------------------------------------------------------
150 Arch_ParseArchive(char **linePtr, Lst *nodeLst, GNode *ctxt)
152 char *cp; /* Pointer into line */
153 GNode *gn; /* New node */
154 char *libName; /* Library-part of specification */
155 char *memName; /* Member-part of specification */
156 char *nameBuf; /* temporary place for node name */
157 char saveChar; /* Ending delimiter of member-name */
158 Boolean subLibName; /* TRUE if libName should have/had
159 * variable substitution performed on it */
165 for (cp = libName; *cp != '(' && *cp != '\0'; cp++) {
168 * Variable spec, so call the Var module to parse the puppy
169 * so we can safely advance beyond it...
175 result = Var_Parse(cp, ctxt, TRUE, &length, &freeIt);
176 if (result == var_Error) {
193 buf = Var_Subst(NULL, libName, ctxt, TRUE);
194 libName = Buf_GetAll(buf, NULL);
195 Buf_Destroy(buf, FALSE);
201 * First skip to the start of the member's name, mark that
202 * place and skip to the end of it (either white-space or
205 Boolean doSubst = FALSE; /* TRUE if need to substitute in memName */
207 while (*cp != '\0' && *cp != ')' && isspace((unsigned char)*cp)) {
211 while (*cp != '\0' && *cp != ')' && !isspace((unsigned char)*cp)) {
214 * Variable spec, so call the Var module to parse the puppy
215 * so we can safely advance beyond it...
221 result = Var_Parse(cp, ctxt, TRUE, &length, &freeIt);
222 if (result == var_Error) {
238 * If the specification ends without a closing parenthesis,
239 * chances are there's something wrong (like a missing backslash),
240 * so it's better to return failure than allow such things to happen
243 printf("No closing parenthesis in archive specification\n");
248 * If we didn't move anywhere, we must be done
258 * XXX: This should be taken care of intelligently by
259 * SuffExpandChildren, both for the archive and the member portions.
262 * If member contains variables, try and substitute for them.
263 * This will slow down archive specs with dynamic sources, of course,
264 * since we'll be (non-)substituting them three times, but them's
265 * the breaks -- we need to do this since SuffExpandChildren calls
266 * us, otherwise we could assume the thing would be taken care of
272 char *oldMemName = memName;
277 * Now form an archive spec and recurse to deal with nested
278 * variables and multi-word variable values.... The results
279 * are just placed at the end of the nodeLst we're returning.
281 buf1 = Var_Subst(NULL, memName, ctxt, TRUE);
282 memName = Buf_GetAll(buf1, NULL);
284 sz = strlen(memName) + strlen(libName) + 3;
286 snprintf(buf, sz, "%s(%s)", libName, memName);
290 if (strchr(memName, '$') && strcmp(memName, oldMemName) == 0) {
292 * Must contain dynamic sources, so we can't deal with it now.
293 * Just create an ARCHV node for the thing and let
294 * SuffExpandChildren handle it...
296 gn = Targ_FindNode(buf, TARG_CREATE);
300 Buf_Destroy(buf1, FALSE);
303 gn->type |= OP_ARCHV;
304 Lst_AtEnd(nodeLst, (void *)gn);
306 } else if (Arch_ParseArchive(&sacrifice, nodeLst, ctxt) != SUCCESS) {
308 * Error in nested call -- free buffer and return FAILURE
312 Buf_Destroy(buf1, FALSE);
316 /* Free buffer and continue with our work. */
318 Buf_Destroy(buf1, FALSE);
320 } else if (Dir_HasWildcards(memName)) {
321 Lst members = Lst_Initializer(members);
323 size_t sz = MAXPATHLEN;
326 nameBuf = emalloc(sz);
328 Dir_Expand(memName, &dirSearchPath, &members);
329 while (!Lst_IsEmpty(&members)) {
330 member = Lst_DeQueue(&members);
331 nsz = strlen(libName) + strlen(member) + 3;
334 nameBuf = erealloc(nameBuf, sz);
337 snprintf(nameBuf, sz, "%s(%s)", libName, member);
339 gn = Targ_FindNode(nameBuf, TARG_CREATE);
342 /* XXXHB Lst_Destroy(&members) */
346 * We've found the node, but have to make sure the rest of
347 * the world knows it's an archive member, without having
348 * to constantly check for parentheses, so we type the
349 * thing with the OP_ARCHV bit before we place it on the
350 * end of the provided list.
352 gn->type |= OP_ARCHV;
353 Lst_AtEnd(nodeLst, (void *)gn);
358 size_t sz = strlen(libName) + strlen(memName) + 3;
359 nameBuf = emalloc(sz);
360 snprintf(nameBuf, sz, "%s(%s)", libName, memName);
361 gn = Targ_FindNode(nameBuf, TARG_CREATE);
367 * We've found the node, but have to make sure the rest of the
368 * world knows it's an archive member, without having to
369 * constantly check for parentheses, so we type the thing with
370 * the OP_ARCHV bit before we place it on the end of the
373 gn->type |= OP_ARCHV;
374 Lst_AtEnd(nodeLst, gn);
385 * If substituted libName, free it now, since we need it no longer.
392 * We promised the pointer would be set up at the next non-space, so
393 * we must advance cp there before setting *linePtr... (note that on
394 * entrance to the loop, cp is guaranteed to point at a ')')
398 } while (*cp != '\0' && isspace((unsigned char)*cp));
405 *-----------------------------------------------------------------------
407 * See if the given archive is the one we are looking for. Called
408 * From ArchStatMember and ArchFindMember via Lst_Find with the
409 * current list element and the name we want.
412 * 0 if it is, non-zero if it isn't.
417 *-----------------------------------------------------------------------
420 ArchFindArchive(const void *ar, const void *archName)
423 return (strcmp(archName, ((const Arch *)ar)->name));
427 *-----------------------------------------------------------------------
429 * Locate a member of an archive, given the path of the archive and
430 * the path of the desired member, and a boolean representing whether
431 * or not the archive should be hashed (if not already hashed).
434 * A pointer to the current struct ar_hdr structure for the member. Note
435 * That no position is returned, so this is not useful for touching
436 * archive members. This is mostly because we have no assurances that
437 * The archive will remain constant after we read all the headers, so
438 * there's not much point in remembering the position...
442 *-----------------------------------------------------------------------
444 static struct ar_hdr *
445 ArchStatMember(const char *archive, const char *member, Boolean hash)
447 #define AR_MAX_NAME_LEN (sizeof(arh.ar_name) - 1)
448 FILE *arch; /* Stream to archive */
449 int size; /* Size of archive member */
450 char *cp; /* Useful character pointer */
452 LstNode *ln; /* Lst member containing archive descriptor */
453 Arch *ar; /* Archive descriptor */
454 Hash_Entry *he; /* Entry containing member's description */
455 struct ar_hdr arh; /* archive-member header for reading archive */
456 char memName[MAXPATHLEN]; /* Current member name while hashing. */
459 * Because of space constraints and similar things, files are archived
460 * using their final path components, not the entire thing, so we need
461 * to point 'member' to the final component, if there is one, to make
462 * the comparisons easier...
464 cp = strrchr(member, '/');
465 if ((cp != NULL) && (strcmp(member, RANLIBMAG) != 0))
468 ln = Lst_Find(&archives, archive, ArchFindArchive);
470 char copy[AR_MAX_NAME_LEN + 1];
474 he = Hash_FindEntry(&ar->members, member);
476 return (Hash_GetValue(he));
479 /* Try truncated name */
480 strncpy(copy, member, AR_MAX_NAME_LEN);
481 copy[AR_MAX_NAME_LEN] = '\0';
483 if ((he = Hash_FindEntry(&ar->members, copy)) != NULL)
484 return (Hash_GetValue(he));
490 * Caller doesn't want the thing hashed, just use ArchFindMember
491 * to read the header for the member out and close down the stream
492 * again. Since the archive is not to be hashed, we assume there's
493 * no need to allocate extra room for the header we're returning,
494 * so just declare it static.
496 static struct ar_hdr sarh;
498 arch = ArchFindMember(archive, member, &sarh, "r");
509 * We don't have this archive on the list yet, so we want to find out
510 * everything that's in it and cache it so we can get at it quickly.
512 arch = fopen(archive, "r");
518 * We use the ARMAG string to make sure this is an archive we
521 if ((fread(magic, SARMAG, 1, arch) != 1) ||
522 (strncmp(magic, ARMAG, SARMAG) != 0)) {
527 ar = emalloc(sizeof(Arch));
528 ar->name = estrdup(archive);
531 Hash_InitTable(&ar->members, -1);
532 memName[AR_MAX_NAME_LEN] = '\0';
534 while (fread(&arh, sizeof(struct ar_hdr), 1, arch) == 1) {
535 if (strncmp(arh.ar_fmag, ARFMAG, sizeof(arh.ar_fmag)) != 0) {
537 * The header is bogus, so the archive is bad
538 * and there's no way we can recover...
543 * We need to advance the stream's pointer to the start of the
544 * next header. Files are padded with newlines to an even-byte
545 * boundary, so we need to extract the size of the file from the
546 * 'size' field of the header and round it up during the seek.
548 arh.ar_size[sizeof(arh.ar_size) - 1] = '\0';
549 size = (int)strtol(arh.ar_size, NULL, 10);
551 strncpy(memName, arh.ar_name, sizeof(arh.ar_name));
552 for (cp = &memName[AR_MAX_NAME_LEN]; *cp == ' '; cp--) {
559 * svr4 names are slash terminated. Also svr4 extended AR format.
561 if (memName[0] == '/') {
563 * svr4 magic mode; handle it
565 switch (ArchSVR4Entry(ar, memName, size, arch)) {
566 case -1: /* Invalid data */
568 case 0: /* List of files entry */
570 default: /* Got the entry */
582 * BSD 4.4 extended AR format: #1/<namelen>, with name as the
583 * first <namelen> bytes of the file
585 if (strncmp(memName, AR_EFMT1, sizeof(AR_EFMT1) - 1) == 0 &&
586 isdigit(memName[sizeof(AR_EFMT1) - 1])) {
588 unsigned int elen = atoi(&memName[sizeof(AR_EFMT1)-1]);
590 if (elen > MAXPATHLEN)
592 if (fread(memName, elen, 1, arch) != 1)
594 memName[elen] = '\0';
595 fseek(arch, -elen, SEEK_CUR);
596 /* XXX Multiple levels may be asked for, make this conditional
597 * on one, and use DEBUGF.
599 if (DEBUG(ARCH) || DEBUG(MAKE)) {
600 fprintf(stderr, "ArchStat: Extended format entry for %s\n", memName);
605 he = Hash_CreateEntry(&ar->members, memName, NULL);
606 Hash_SetValue(he, emalloc(sizeof(struct ar_hdr)));
607 memcpy(Hash_GetValue(he), &arh, sizeof(struct ar_hdr));
609 fseek(arch, (size + 1) & ~1, SEEK_CUR);
614 Lst_AtEnd(&archives, ar);
617 * Now that the archive has been read and cached, we can look into
618 * the hash table to find the desired member's header.
620 he = Hash_FindEntry(&ar->members, member);
623 return (Hash_GetValue (he));
630 Hash_DeleteTable(&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.
646 * -1: Bad data in archive
647 * 0: A table was loaded from the file
648 * 1: Name was successfully substituted from table
649 * 2: Name was not successfully substituted from table
652 * If a table is read, the file pointer is moved to the next archive
655 *-----------------------------------------------------------------------
658 ArchSVR4Entry(Arch *ar, char *name, size_t size, FILE *arch)
660 #define ARLONGNAMES1 "//"
661 #define ARLONGNAMES2 "/ARFILENAMES"
665 if (strncmp(name, ARLONGNAMES1, sizeof(ARLONGNAMES1) - 1) == 0 ||
666 strncmp(name, ARLONGNAMES2, sizeof(ARLONGNAMES2) - 1) == 0) {
668 if (ar->fnametab != NULL) {
669 DEBUGF(ARCH, ("Attempted to redefine an SVR4 name table\n"));
674 * This is a table of archive names, so we build one for
677 ar->fnametab = emalloc(size);
678 ar->fnamesize = size;
680 if (fread(ar->fnametab, size, 1, arch) != 1) {
681 DEBUGF(ARCH, ("Reading an SVR4 name table failed\n"));
684 eptr = ar->fnametab + size;
685 for (entry = 0, ptr = ar->fnametab; ptr < eptr; ptr++)
698 DEBUGF(ARCH, ("Found svr4 archive name table with %zu entries\n", entry));
702 if (name[1] == ' ' || name[1] == '\0')
705 entry = (size_t)strtol(&name[1], &eptr, 0);
706 if ((*eptr != ' ' && *eptr != '\0') || eptr == &name[1]) {
707 DEBUGF(ARCH, ("Could not parse SVR4 name %s\n", name));
710 if (entry >= ar->fnamesize) {
711 DEBUGF(ARCH, ("SVR4 entry offset %s is greater than %zu\n",
712 name, ar->fnamesize));
716 DEBUGF(ARCH, ("Replaced %s with %s\n", name, &ar->fnametab[entry]));
718 strncpy(name, &ar->fnametab[entry], MAXPATHLEN);
719 name[MAXPATHLEN - 1] = '\0';
726 *-----------------------------------------------------------------------
728 * Locate a member of an archive, given the path of the archive and
729 * the path of the desired member. If the archive is to be modified,
730 * the mode should be "r+", if not, it should be "r". arhPtr is a
731 * poitner to the header structure to fill in.
734 * An FILE *, opened for reading and writing, positioned at the
735 * start of the member's struct ar_hdr, or NULL if the member was
736 * nonexistent. The current struct ar_hdr for member.
739 * The passed struct ar_hdr structure is filled in.
741 *-----------------------------------------------------------------------
744 ArchFindMember(const char *archive, const char *member, struct ar_hdr *arhPtr,
747 FILE *arch; /* Stream to archive */
748 int size; /* Size of archive member */
749 const char *cp; /* Useful character pointer */
753 arch = fopen(archive, mode);
759 * We use the ARMAG string to make sure this is an archive we
762 if ((fread(magic, SARMAG, 1, arch) != 1) ||
763 (strncmp(magic, ARMAG, SARMAG) != 0)) {
769 * Because of space constraints and similar things, files are archived
770 * using their final path components, not the entire thing, so we need
771 * to point 'member' to the final component, if there is one, to make
772 * the comparisons easier...
774 cp = strrchr(member, '/');
775 if ((cp != NULL) && (strcmp(member, RANLIBMAG) != 0)) {
778 len = tlen = strlen(member);
779 if (len > sizeof(arhPtr->ar_name)) {
780 tlen = sizeof(arhPtr->ar_name);
783 while (fread(arhPtr, sizeof(struct ar_hdr), 1, arch) == 1) {
784 if (strncmp(arhPtr->ar_fmag, ARFMAG, sizeof(arhPtr->ar_fmag) ) != 0) {
786 * The header is bogus, so the archive is bad
787 * and there's no way we can recover...
791 } else if (strncmp(member, arhPtr->ar_name, tlen) == 0) {
793 * If the member's name doesn't take up the entire 'name' field,
794 * we have to be careful of matching prefixes. Names are space-
795 * padded to the right, so if the character in 'name' at the end
796 * of the matched string is anything but a space, this isn't the
799 if (tlen != sizeof(arhPtr->ar_name) && arhPtr->ar_name[tlen] != ' '){
803 * To make life easier, we reposition the file at the start
804 * of the header we just read before we return the stream.
805 * In a more general situation, it might be better to leave
806 * the file at the actual member, rather than its header, but
809 fseek(arch, -sizeof(struct ar_hdr), SEEK_CUR);
815 * BSD 4.4 extended AR format: #1/<namelen>, with name as the
816 * first <namelen> bytes of the file
818 if (strncmp(arhPtr->ar_name, AR_EFMT1,
819 sizeof(AR_EFMT1) - 1) == 0 &&
820 isdigit(arhPtr->ar_name[sizeof(AR_EFMT1) - 1])) {
822 unsigned int elen = atoi(&arhPtr->ar_name[sizeof(AR_EFMT1)-1]);
823 char ename[MAXPATHLEN];
825 if (elen > MAXPATHLEN) {
829 if (fread(ename, elen, 1, arch) != 1) {
837 if (DEBUG(ARCH) || DEBUG(MAKE)) {
838 printf("ArchFind: Extended format entry for %s\n", ename);
840 if (strncmp(ename, member, len) == 0) {
841 /* Found as extended name */
842 fseek(arch, -sizeof(struct ar_hdr) - elen, SEEK_CUR);
845 fseek(arch, -elen, SEEK_CUR);
852 * This isn't the member we're after, so we need to advance the
853 * stream's pointer to the start of the next header. Files are
854 * padded with newlines to an even-byte boundary, so we need to
855 * extract the size of the file from the 'size' field of the
856 * header and round it up during the seek.
858 arhPtr->ar_size[sizeof(arhPtr->ar_size) - 1] = '\0';
859 size = (int)strtol(arhPtr->ar_size, NULL, 10);
860 fseek(arch, (size + 1) & ~1, SEEK_CUR);
865 * We've looked everywhere, but the member is not to be found. Close the
866 * archive and return NULL -- an error.
873 *-----------------------------------------------------------------------
875 * Touch a member of an archive.
878 * The 'time' field of the member's header is updated.
881 * The modification time of the entire archive is also changed.
882 * For a library, this could necessitate the re-ranlib'ing of the
885 *-----------------------------------------------------------------------
888 Arch_Touch(GNode *gn)
890 FILE * arch; /* Stream open to archive, positioned properly */
891 struct ar_hdr arh; /* Current header describing member */
894 arch = ArchFindMember(Var_Value(ARCHIVE, gn, &p1),
895 Var_Value(TARGET, gn, &p2),
899 snprintf(arh.ar_date, sizeof(arh.ar_date), "%-12ld", (long)now);
902 fwrite(&arh, sizeof(struct ar_hdr), 1, arch);
908 *-----------------------------------------------------------------------
910 * Given a node which represents a library, touch the thing, making
911 * sure that the table of contents also is touched.
917 * Both the modification time of the library and of the RANLIBMAG
918 * member are set to 'now'.
920 *-----------------------------------------------------------------------
923 Arch_TouchLib(GNode *gn)
926 FILE *arch; /* Stream open to archive */
927 struct ar_hdr arh; /* Header describing table of contents */
928 struct utimbuf times; /* Times for utime() call */
930 arch = ArchFindMember(gn->path, RANLIBMAG, &arh, "r+");
931 snprintf(arh.ar_date, sizeof(arh.ar_date), "%-12ld", (long) now);
934 fwrite(&arh, sizeof(struct ar_hdr), 1, arch);
937 times.actime = times.modtime = now;
938 utime(gn->path, ×);
944 *-----------------------------------------------------------------------
946 * Return the modification time of a member of an archive, given its
950 * The modification time(seconds).
953 * The mtime field of the given node is filled in with the value
954 * returned by the function.
956 *-----------------------------------------------------------------------
959 Arch_MTime(GNode *gn)
961 struct ar_hdr *arhPtr; /* Header of desired member */
962 int modTime; /* Modification time as an integer */
965 arhPtr = ArchStatMember(Var_Value(ARCHIVE, gn, &p1),
966 Var_Value(TARGET, gn, &p2),
971 if (arhPtr != NULL) {
972 modTime = (int)strtol(arhPtr->ar_date, NULL, 10);
982 *-----------------------------------------------------------------------
984 * Given a non-existent archive member's node, get its modification
985 * time from its archived form, if it exists.
988 * The modification time.
991 * The mtime field is filled in.
993 *-----------------------------------------------------------------------
996 Arch_MemMTime(GNode *gn)
1003 for (ln = Lst_First(&gn->parents); ln != NULL; ln = Lst_Succ(ln)) {
1004 pgn = Lst_Datum(ln);
1006 if (pgn->type & OP_ARCHV) {
1008 * If the parent is an archive specification and is being made
1009 * and its member's name matches the name of the node we were
1010 * given, record the modification time of the parent in the
1011 * child. We keep searching its parents in case some other
1012 * parent requires this child to exist...
1014 nameStart = strchr(pgn->name, '(') + 1;
1015 nameEnd = strchr(nameStart, ')');
1018 strncmp(nameStart, gn->name, nameEnd - nameStart) == 0) {
1019 gn->mtime = Arch_MTime(pgn);
1021 } else if (pgn->make) {
1023 * Something which isn't a library depends on the existence of
1024 * this target, so it needs to exist.
1034 *-----------------------------------------------------------------------
1036 * Search for a named library along the given search path.
1042 * The node's 'path' field is set to the found path (including the
1043 * actual file name, not -l...). If the system can handle the -L
1044 * flag when linking (or we cannot find the library), we assume that
1045 * the user has placed the .LIBRARIES variable in the final linking
1046 * command (or the linker will know where to find it) and set the
1047 * TARGET variable for this node to be the node's name. Otherwise,
1048 * we set the TARGET variable to be the full path of the library,
1049 * as returned by Dir_FindFile.
1051 *-----------------------------------------------------------------------
1054 Arch_FindLib(GNode *gn, Lst *path)
1056 char *libName; /* file name for archive */
1059 sz = strlen(gn->name) + 4;
1060 libName = emalloc(sz);
1061 snprintf(libName, sz, "lib%s.a", &gn->name[2]);
1063 gn->path = Dir_FindFile(libName, path);
1068 Var_Set(TARGET, gn->name, gn);
1070 Var_Set(TARGET, gn->path == NULL ? gn->name : gn->path, gn);
1071 #endif /* LIBRARIES */
1075 *-----------------------------------------------------------------------
1077 * Decide if a node with the OP_LIB attribute is out-of-date. Called
1078 * from Make_OODate to make its life easier, with the library's
1081 * There are several ways for a library to be out-of-date that are
1082 * not available to ordinary files. In addition, there are ways
1083 * that are open to regular files that are not available to
1084 * libraries. A library that is only used as a source is never
1085 * considered out-of-date by itself. This does not preclude the
1086 * library's modification time from making its parent be out-of-date.
1087 * A library will be considered out-of-date for any of these reasons,
1088 * given that it is a target on a dependency line somewhere:
1089 * Its modification time is less than that of one of its
1090 * sources (gn->mtime < gn->cmtime).
1091 * Its modification time is greater than the time at which the
1092 * make began (i.e. it's been modified in the course
1093 * of the make, probably by archiving).
1094 * The modification time of one of its sources is greater than
1095 * the one of its RANLIBMAG member (i.e. its table of contents
1096 * is out-of-date). We don't compare of the archive time
1097 * vs. TOC time because they can be too close. In my
1098 * opinion we should not bother with the TOC at all since
1099 * this is used by 'ar' rules that affect the data contents
1100 * of the archive, not by ranlib rules, which affect the
1104 * TRUE if the library is out-of-date. FALSE otherwise.
1107 * The library will be hashed if it hasn't been already.
1109 *-----------------------------------------------------------------------
1112 Arch_LibOODate(GNode *gn)
1116 if (OP_NOP(gn->type) && Lst_IsEmpty(&gn->children)) {
1118 } else if ((gn->mtime > now) || (gn->mtime < gn->cmtime)) {
1122 struct ar_hdr *arhPtr; /* Header for __.SYMDEF */
1123 int modTimeTOC; /* The table-of-contents's mod time */
1125 arhPtr = ArchStatMember(gn->path, RANLIBMAG, FALSE);
1127 if (arhPtr != NULL) {
1128 modTimeTOC = (int)strtol(arhPtr->ar_date, NULL, 10);
1130 /* XXX choose one. */
1131 if (DEBUG(ARCH) || DEBUG(MAKE)) {
1132 printf("%s modified %s...", RANLIBMAG, Targ_FmtTime(modTimeTOC));
1134 oodate = (gn->cmtime > modTimeTOC);
1137 * A library w/o a table of contents is out-of-date
1139 if (DEBUG(ARCH) || DEBUG(MAKE)) {
1140 printf("No t.o.c....");
1145 oodate = (gn->mtime == 0); /* out-of-date if not present */
1152 *-----------------------------------------------------------------------
1154 * Initialize things for this module.
1159 *-----------------------------------------------------------------------