xref: /freebsd/contrib/bmake/unit-tests/archive.mk (revision 548bfc56)
1# $NetBSD: archive.mk,v 1.13 2024/04/27 20:23:22 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} depend-on-existing-member
25	@${MAKE} -f ${MAKEFILE} depend-on-nonexistent-member
26	@${MAKE} -f ${MAKEFILE} remove-archive
27	@${MAKE} -f ${MAKEFILE} set-up-library
28	@${MAKE} -f ${MAKEFILE} -dm library 2>&1 \
29	| sed -n '/^Examining/p' \
30	| sed 's,\.\.\.modified[^.]*,,'
31	@${MAKE} -f ${MAKEFILE} tear-down-library
32
33
34create-archive: ${ARCHIVE} pre post
35
36# The indirect references with the $$ cover the code in Arch_ParseArchive
37# that calls Var_Parse.  It's an esoteric scenario since at the point where
38# Arch_ParseArchive is called, the dependency line is already fully expanded.
39#
40${ARCHIVE}: $${:Ulibprog.a}(archive.mk modmisc.mk $${:Uvarmisc.mk}) pre post
41	ar cru ${.TARGET} ${.OODATE:O}
42	ranlib ${.TARGET}
43
44list-archive: ${ARCHIVE} pre post
45	ar t ${.ALLSRC}
46
47# XXX: I had expected that this dependency would select all *.mk files from
48# the archive.  Instead, the globbing is done in the current directory.
49#
50# To prevent an overly long file list, the pattern is restricted to [at]*.mk.
51list-archive-wildcard: ${ARCHIVE}([at]*.mk) pre post
52	@printf '%s\n' ${.ALLSRC:O:@member@${.TARGET:Q}': '${member:Q}@}
53
54depend-on-existing-member: ${ARCHIVE}(archive.mk) pre post
55	@echo $@
56
57depend-on-nonexistent-member: ${ARCHIVE}(nonexistent.mk) pre post
58	@echo $@
59
60remove-archive: pre post
61	rm -f ${ARCHIVE}
62
63pre: .USEBEFORE
64	@echo Making ${.TARGET} ${.OODATE:C,.+,out-of-date,W} ${.OODATE:O}
65post: .USE
66	@echo
67
68
69set-up-library: .PHONY
70	@echo "member" > member.txt
71	@echo "not a library" > libbad.a
72	@ar cr libgood.a member.txt
73	@echo "begin library"
74
75.if make(library)
76.SUFFIXES: .a
77.LIBS: .a
78.endif
79# The two lines for libgood contain the word "library", the two lines for
80# libbad don't.
81#
82# expect: Examining libbad.a...up-to-date.
83# expect: Examining -lbad...up-to-date.
84# expect: Examining libgood.a...library...up-to-date.
85# expect: Examining -lgood...library...up-to-date.
86library: .PHONY libbad.a -lbad libgood.a -lgood
87	: Making ${.TARGET} from ${.ALLSRC}
88
89tear-down-library: .PHONY
90	@echo "end library"
91	@rm member.txt libbad.a libgood.a
92