xref: /freebsd/contrib/bmake/unit-tests/archive.mk (revision 6a7405f5a6b639682cacf01e35d561411ff556aa)
1*6a7405f5SSimon J. Gerraty# $NetBSD: archive.mk,v 1.14 2025/01/10 23:00:38 rillig Exp $
22c3632d1SSimon J. Gerraty#
32c3632d1SSimon J. Gerraty# Very basic demonstration of handling archives, based on the description
42c3632d1SSimon J. Gerraty# in PSD.doc/tutorial.ms.
5956e45f6SSimon J. Gerraty#
6956e45f6SSimon J. Gerraty# This test aims at covering the code, not at being an introduction to
7e2eeea75SSimon J. Gerraty# archive handling. That's why it deviates from the tutorial style of
8e2eeea75SSimon J. Gerraty# several other tests.
92c3632d1SSimon J. Gerraty
10956e45f6SSimon J. GerratyARCHIVE=	libprog.a
11b0c40a00SSimon J. GerratyFILES=		archive.mk archive-suffix.mk modmisc.mk ternary.mk varmisc.mk
122c3632d1SSimon J. Gerraty
132c3632d1SSimon J. Gerratyall:
14956e45f6SSimon J. Gerraty.if ${.PARSEDIR:tA} != ${.CURDIR:tA}
15b0c40a00SSimon J. Gerraty	@cd ${MAKEFILE:H} && cp ${FILES} ${.CURDIR}
16956e45f6SSimon J. Gerraty.endif
17956e45f6SSimon J. Gerraty# The following targets create and remove files.  The filesystem cache in
18956e45f6SSimon J. Gerraty# dir.c would probably not handle this correctly, therefore each of the
19956e45f6SSimon J. Gerraty# targets is run in its separate sub-make.
20e2eeea75SSimon J. Gerraty	@${MAKE} -f ${MAKEFILE} remove-archive
21e2eeea75SSimon J. Gerraty	@${MAKE} -f ${MAKEFILE} create-archive
22e2eeea75SSimon J. Gerraty	@${MAKE} -f ${MAKEFILE} list-archive
23e2eeea75SSimon J. Gerraty	@${MAKE} -f ${MAKEFILE} list-archive-wildcard
24*6a7405f5SSimon J. Gerraty	@${MAKE} -f ${MAKEFILE} list-archive-undef-archive || echo "exit $$?"
25*6a7405f5SSimon J. Gerraty	@echo
26*6a7405f5SSimon J. Gerraty	@${MAKE} -f ${MAKEFILE} list-archive-undef-member || echo "exit $$?"
27*6a7405f5SSimon J. Gerraty	@echo
28e2eeea75SSimon J. Gerraty	@${MAKE} -f ${MAKEFILE} depend-on-existing-member
29e2eeea75SSimon J. Gerraty	@${MAKE} -f ${MAKEFILE} depend-on-nonexistent-member
30e2eeea75SSimon J. Gerraty	@${MAKE} -f ${MAKEFILE} remove-archive
31548bfc56SSimon J. Gerraty	@${MAKE} -f ${MAKEFILE} set-up-library
32548bfc56SSimon J. Gerraty	@${MAKE} -f ${MAKEFILE} -dm library 2>&1 \
33548bfc56SSimon J. Gerraty	| sed -n '/^Examining/p' \
34548bfc56SSimon J. Gerraty	| sed 's,\.\.\.modified[^.]*,,'
35548bfc56SSimon J. Gerraty	@${MAKE} -f ${MAKEFILE} tear-down-library
36548bfc56SSimon J. Gerraty
372c3632d1SSimon J. Gerraty
38956e45f6SSimon J. Gerratycreate-archive: ${ARCHIVE} pre post
39956e45f6SSimon J. Gerraty
40956e45f6SSimon J. Gerraty# The indirect references with the $$ cover the code in Arch_ParseArchive
41956e45f6SSimon J. Gerraty# that calls Var_Parse.  It's an esoteric scenario since at the point where
42956e45f6SSimon J. Gerraty# Arch_ParseArchive is called, the dependency line is already fully expanded.
43956e45f6SSimon J. Gerraty#
44956e45f6SSimon J. Gerraty${ARCHIVE}: $${:Ulibprog.a}(archive.mk modmisc.mk $${:Uvarmisc.mk}) pre post
45956e45f6SSimon J. Gerraty	ar cru ${.TARGET} ${.OODATE:O}
462c3632d1SSimon J. Gerraty	ranlib ${.TARGET}
472c3632d1SSimon J. Gerraty
48956e45f6SSimon J. Gerratylist-archive: ${ARCHIVE} pre post
492c3632d1SSimon J. Gerraty	ar t ${.ALLSRC}
502c3632d1SSimon J. Gerraty
512c3632d1SSimon J. Gerraty# XXX: I had expected that this dependency would select all *.mk files from
522c3632d1SSimon J. Gerraty# the archive.  Instead, the globbing is done in the current directory.
53e2eeea75SSimon J. Gerraty#
542c3632d1SSimon J. Gerraty# To prevent an overly long file list, the pattern is restricted to [at]*.mk.
55956e45f6SSimon J. Gerratylist-archive-wildcard: ${ARCHIVE}([at]*.mk) pre post
56e2eeea75SSimon J. Gerraty	@printf '%s\n' ${.ALLSRC:O:@member@${.TARGET:Q}': '${member:Q}@}
572c3632d1SSimon J. Gerraty
58*6a7405f5SSimon J. Gerraty.if make(list-archive-undef-archive)
59*6a7405f5SSimon J. Gerraty# TODO: Be more specific: mention that the variable "UNDEF" is not defined.
60*6a7405f5SSimon J. Gerraty# expect+1: Error in source archive spec "libprog.a${UNDEF}(archive.mk) pre post"
61*6a7405f5SSimon J. Gerratylist-archive-undef-archive: ${ARCHIVE}$${UNDEF}(archive.mk) pre post
62*6a7405f5SSimon J. Gerraty	@printf '%s\n' ${.ALLSRC:O:@member@${.TARGET:Q}': '${member:Q}@}
63*6a7405f5SSimon J. Gerraty.endif
64*6a7405f5SSimon J. Gerraty
65*6a7405f5SSimon J. Gerraty.if make(list-archive-undef-member)
66*6a7405f5SSimon J. Gerraty# TODO: Be more specific: mention that the variable "UNDEF" is not defined.
67*6a7405f5SSimon J. Gerraty# expect+1: Error in source archive spec "libprog.a"
68*6a7405f5SSimon J. Gerratylist-archive-undef-member: ${ARCHIVE}(archive$${UNDEF}.mk) pre post
69*6a7405f5SSimon J. Gerraty	@printf '%s\n' ${.ALLSRC:O:@member@${.TARGET:Q}': '${member:Q}@}
70*6a7405f5SSimon J. Gerraty.endif
71*6a7405f5SSimon J. Gerraty
72956e45f6SSimon J. Gerratydepend-on-existing-member: ${ARCHIVE}(archive.mk) pre post
73e2eeea75SSimon J. Gerraty	@echo $@
742c3632d1SSimon J. Gerraty
75956e45f6SSimon J. Gerratydepend-on-nonexistent-member: ${ARCHIVE}(nonexistent.mk) pre post
76e2eeea75SSimon J. Gerraty	@echo $@
772c3632d1SSimon J. Gerraty
78956e45f6SSimon J. Gerratyremove-archive: pre post
792c3632d1SSimon J. Gerraty	rm -f ${ARCHIVE}
80956e45f6SSimon J. Gerraty
81956e45f6SSimon J. Gerratypre: .USEBEFORE
82956e45f6SSimon J. Gerraty	@echo Making ${.TARGET} ${.OODATE:C,.+,out-of-date,W} ${.OODATE:O}
83956e45f6SSimon J. Gerratypost: .USE
84956e45f6SSimon J. Gerraty	@echo
85548bfc56SSimon J. Gerraty
86548bfc56SSimon J. Gerraty
87548bfc56SSimon J. Gerratyset-up-library: .PHONY
88548bfc56SSimon J. Gerraty	@echo "member" > member.txt
89548bfc56SSimon J. Gerraty	@echo "not a library" > libbad.a
90548bfc56SSimon J. Gerraty	@ar cr libgood.a member.txt
91548bfc56SSimon J. Gerraty	@echo "begin library"
92548bfc56SSimon J. Gerraty
93548bfc56SSimon J. Gerraty.if make(library)
94548bfc56SSimon J. Gerraty.SUFFIXES: .a
95548bfc56SSimon J. Gerraty.LIBS: .a
96548bfc56SSimon J. Gerraty.endif
97548bfc56SSimon J. Gerraty# The two lines for libgood contain the word "library", the two lines for
98548bfc56SSimon J. Gerraty# libbad don't.
99548bfc56SSimon J. Gerraty#
100548bfc56SSimon J. Gerraty# expect: Examining libbad.a...up-to-date.
101548bfc56SSimon J. Gerraty# expect: Examining -lbad...up-to-date.
102548bfc56SSimon J. Gerraty# expect: Examining libgood.a...library...up-to-date.
103548bfc56SSimon J. Gerraty# expect: Examining -lgood...library...up-to-date.
104548bfc56SSimon J. Gerratylibrary: .PHONY libbad.a -lbad libgood.a -lgood
105548bfc56SSimon J. Gerraty	: Making ${.TARGET} from ${.ALLSRC}
106548bfc56SSimon J. Gerraty
107548bfc56SSimon J. Gerratytear-down-library: .PHONY
108548bfc56SSimon J. Gerraty	@echo "end library"
109548bfc56SSimon J. Gerraty	@rm member.txt libbad.a libgood.a
110