1# $NetBSD: archive.mk,v 1.14 2025/01/10 23:00:38 rillig Exp $
2#
3# Very basic demonstration of handling archives, based on the description
4# in PSD.doc/tutorial.ms.
5#
6# This test aims at covering the code, not at being an introduction to
7# archive handling. That's why it deviates from the tutorial style of
8# several other tests.
9
10ARCHIVE=  libprog.a
11FILES=              archive.mk archive-suffix.mk modmisc.mk ternary.mk varmisc.mk
12
13all:
14.if ${.PARSEDIR:tA} != ${.CURDIR:tA}
15          @cd ${MAKEFILE:H} && cp ${FILES} ${.CURDIR}
16.endif
17# The following targets create and remove files.  The filesystem cache in
18# dir.c would probably not handle this correctly, therefore each of the
19# targets is run in its separate sub-make.
20          @${MAKE} -f ${MAKEFILE} remove-archive
21          @${MAKE} -f ${MAKEFILE} create-archive
22          @${MAKE} -f ${MAKEFILE} list-archive
23          @${MAKE} -f ${MAKEFILE} list-archive-wildcard
24          @${MAKE} -f ${MAKEFILE} list-archive-undef-archive || echo "exit $$?"
25          @echo
26          @${MAKE} -f ${MAKEFILE} list-archive-undef-member || echo "exit $$?"
27          @echo
28          @${MAKE} -f ${MAKEFILE} depend-on-existing-member
29          @${MAKE} -f ${MAKEFILE} depend-on-nonexistent-member
30          @${MAKE} -f ${MAKEFILE} remove-archive
31          @${MAKE} -f ${MAKEFILE} set-up-library
32          @${MAKE} -f ${MAKEFILE} -dm library 2>&1 \
33          | sed -n '/^Examining/p' \
34          | sed 's,\.\.\.modified[^.]*,,'
35          @${MAKE} -f ${MAKEFILE} tear-down-library
36
37
38create-archive: ${ARCHIVE} pre post
39
40# The indirect references with the $$ cover the code in Arch_ParseArchive
41# that calls Var_Parse.  It's an esoteric scenario since at the point where
42# Arch_ParseArchive is called, the dependency line is already fully expanded.
43#
44${ARCHIVE}: $${:Ulibprog.a}(archive.mk modmisc.mk $${:Uvarmisc.mk}) pre post
45          ar cru ${.TARGET} ${.OODATE:O}
46          ranlib ${.TARGET}
47
48list-archive: ${ARCHIVE} pre post
49          ar t ${.ALLSRC}
50
51# XXX: I had expected that this dependency would select all *.mk files from
52# the archive.  Instead, the globbing is done in the current directory.
53#
54# To prevent an overly long file list, the pattern is restricted to [at]*.mk.
55list-archive-wildcard: ${ARCHIVE}([at]*.mk) pre post
56          @printf '%s\n' ${.ALLSRC:O:@member@${.TARGET:Q}': '${member:Q}@}
57
58.if make(list-archive-undef-archive)
59# TODO: Be more specific: mention that the variable "UNDEF" is not defined.
60# expect+1: Error in source archive spec "libprog.a${UNDEF}(archive.mk) pre post"
61list-archive-undef-archive: ${ARCHIVE}$${UNDEF}(archive.mk) pre post
62          @printf '%s\n' ${.ALLSRC:O:@member@${.TARGET:Q}': '${member:Q}@}
63.endif
64
65.if make(list-archive-undef-member)
66# TODO: Be more specific: mention that the variable "UNDEF" is not defined.
67# expect+1: Error in source archive spec "libprog.a"
68list-archive-undef-member: ${ARCHIVE}(archive$${UNDEF}.mk) pre post
69          @printf '%s\n' ${.ALLSRC:O:@member@${.TARGET:Q}': '${member:Q}@}
70.endif
71
72depend-on-existing-member: ${ARCHIVE}(archive.mk) pre post
73          @echo $@
74
75depend-on-nonexistent-member: ${ARCHIVE}(nonexistent.mk) pre post
76          @echo $@
77
78remove-archive: pre post
79          rm -f ${ARCHIVE}
80
81pre: .USEBEFORE
82          @echo Making ${.TARGET} ${.OODATE:C,.+,out-of-date,W} ${.OODATE:O}
83post: .USE
84          @echo
85
86
87set-up-library: .PHONY
88          @echo "member" > member.txt
89          @echo "not a library" > libbad.a
90          @ar cr libgood.a member.txt
91          @echo "begin library"
92
93.if make(library)
94.SUFFIXES: .a
95.LIBS: .a
96.endif
97# The two lines for libgood contain the word "library", the two lines for
98# libbad don't.
99#
100# expect: Examining libbad.a...up-to-date.
101# expect: Examining -lbad...up-to-date.
102# expect: Examining libgood.a...library...up-to-date.
103# expect: Examining -lgood...library...up-to-date.
104library: .PHONY libbad.a -lbad libgood.a -lgood
105          : Making ${.TARGET} from ${.ALLSRC}
106
107tear-down-library: .PHONY
108          @echo "end library"
109          @rm member.txt libbad.a libgood.a
110