139172Sjkh# $NetBSD: directive-include.mk,v 1.14 2024/04/20 10:18:55 rillig Exp $
239172Sjkh#
339172Sjkh# Tests for the .include directive, which includes another file.
439172Sjkh
539172Sjkh# TODO: Implementation
639172Sjkh
739172Sjkh.MAKEFLAGS: -dc
839172Sjkh
939172Sjkh# All included files are recorded in the variable .MAKE.MAKEFILES.
1039172Sjkh# In this test, only the basenames of the files are compared since
1139172Sjkh# the directories can differ.
1239172Sjkh.include "/dev/null"
1339172Sjkh.if ${.MAKE.MAKEFILES:T} != "${.PARSEFILE} null"
1439172Sjkh.  error
1539172Sjkh.endif
1639172Sjkh
1739172Sjkh# Each file is recorded only once in the variable .MAKE.MAKEFILES.
1839172Sjkh# Between 2015-11-26 and 2020-10-31, the very last file could be repeated,
1939172Sjkh# due to an off-by-one bug in ParseTrackInput.
2039172Sjkh.include "/dev/null"
2139172Sjkh.if ${.MAKE.MAKEFILES:T} != "${.PARSEFILE} null"
2239172Sjkh.  error
2339172Sjkh.endif
2439172Sjkh
2539172Sjkh# expect+1: Could not find nonexistent.mk
2639172Sjkh.include "nonexistent.mk"
2739172Sjkh.include "/dev/null"		# size 0
2839172Sjkh# including a directory technically succeeds, but shouldn't.
2939172Sjkh#.include "."			# directory
3039172Sjkh
3139172Sjkh# As of 2020-11-21, anything after the delimiter '"' is ignored.
3239172Sjkh.include "/dev/null" and ignore anything in the rest of the line.
3376224Sobrien
3439172Sjkh# The filename to be included can contain expressions.
3539172SjkhDEV=	null
3676224Sobrien.include "/dev/${DEV}"
37108440Sobrien
3839172Sjkh# Expressions in double quotes or angle quotes are first parsed naively, to
3939172Sjkh# find the closing '"'.  In a second step, the expressions are expanded.  This
4039172Sjkh# means that the expressions cannot include the characters '"' or '>'.  This
4139172Sjkh# restriction is not practically relevant since the expressions inside
4239172Sjkh# '.include' directives are typically kept as simple as possible.
4339172Sjkh#
44112767Sobrien# If the whole line were expanded before parsing, the filename to be included
4539172Sjkh# would be empty, and the closing '"' would be in the trailing part of the
4639172Sjkh# line, which is ignored as of 2021-12-03.
4739172SjkhDQUOT=	"
4839172Sjkh# expect+1: Could not find "
4939172Sjkh.include "${DQUOT}"
5039172Sjkh
5139172Sjkh# When the expression in a filename cannot be evaluated, the failing
5239172Sjkh# expression is skipped and the file is included nevertheless.
5339172Sjkh# FIXME: Add proper error handling, no file must be included here.
5439172Sjkh# expect+2: Could not find nonexistent.mk
5539172Sjkh# expect+1: while evaluating "${:U123:Z}.mk": Unknown modifier "Z"
5639172Sjkh.include "nonexistent${:U123:Z}.mk"
5739172Sjkh
5895781Sru# The traditional include directive is seldom used.
5995781Sruinclude /dev/null		# comment
6095781Sru# expect+1: Cannot open /nonexistent
6197226Sruinclude /nonexistent		# comment
62112192Srusinclude /nonexistent		# comment
6395781Sruinclude ${:U/dev/null}		# comment
6495781Sruinclude /dev/null /dev/null
6597226Sru# expect+1: Invalid line 'include'
66112192Sruinclude
6795781Sru
6895781Sru# XXX: trailing whitespace in diagnostic, missing quotes around filename
6939172Sjkh### TODO: expect+1: Could not find
7039172Sjkh# The following include directive behaves differently, depending on whether
7139172Sjkh# the current file has a slash or is a relative filename.  In the first case,
7239172Sjkh# make opens the directory of the current file and tries to read from it,
7339172Sjkh# resulting in the error message """ line 1: Zero byte read from file".
7439172Sjkh# In the second case, the error message is "Could not find ", without quotes
7539172Sjkh# or any other indicator for the empty filename at the end of the line.
7697226Sru#include ${:U}
7797226Sru
7897226Sru
7997226Sru# Since parse.c 1.612 from 2022-01-01 and before parse.c 1.620 from
8039172Sjkh# 2022-01-07, including an empty regular file called bmake_malloc(0), which
8139172Sjkh# may return a null pointer.  On OpenBSD, this led to a segmentation fault in
8239172Sjkh# Buf_InitSize, which assumes that bmake_malloc never returns NULL, just like
8339172Sjkh# all other places in the code.
8439172Sjkh_!=		> directive-include-empty
8539172Sjkh.include "${.CURDIR}/directive-include-empty"
8639172Sjkh_!=		rm directive-include-empty
8739172Sjkh
8839172Sjkh
8939172Sjkhall:
9039172Sjkh