1*759b177aSSimon J. Gerraty# $NetBSD: directive-include.mk,v 1.19 2025/03/30 09:51:50 rillig Exp $ 2956e45f6SSimon J. Gerraty# 3956e45f6SSimon J. Gerraty# Tests for the .include directive, which includes another file. 4956e45f6SSimon J. Gerraty 5956e45f6SSimon J. Gerraty# TODO: Implementation 6956e45f6SSimon J. Gerraty 7956e45f6SSimon J. Gerraty.MAKEFLAGS: -dc 8956e45f6SSimon J. Gerraty 9956e45f6SSimon J. Gerraty# All included files are recorded in the variable .MAKE.MAKEFILES. 10956e45f6SSimon J. Gerraty# In this test, only the basenames of the files are compared since 11956e45f6SSimon J. Gerraty# the directories can differ. 12956e45f6SSimon J. Gerraty.include "/dev/null" 13956e45f6SSimon J. Gerraty.if ${.MAKE.MAKEFILES:T} != "${.PARSEFILE} null" 14956e45f6SSimon J. Gerraty. error 15956e45f6SSimon J. Gerraty.endif 16956e45f6SSimon J. Gerraty 17956e45f6SSimon J. Gerraty# Each file is recorded only once in the variable .MAKE.MAKEFILES. 18956e45f6SSimon J. Gerraty# Between 2015-11-26 and 2020-10-31, the very last file could be repeated, 19956e45f6SSimon J. Gerraty# due to an off-by-one bug in ParseTrackInput. 20956e45f6SSimon J. Gerraty.include "/dev/null" 21956e45f6SSimon J. Gerraty.if ${.MAKE.MAKEFILES:T} != "${.PARSEFILE} null" 22956e45f6SSimon J. Gerraty. error 23956e45f6SSimon J. Gerraty.endif 24956e45f6SSimon J. Gerraty 25148ee845SSimon J. Gerraty# expect+1: Could not find nonexistent.mk 26e2eeea75SSimon J. Gerraty.include "nonexistent.mk" 27e2eeea75SSimon J. Gerraty.include "/dev/null" # size 0 28e2eeea75SSimon J. Gerraty# including a directory technically succeeds, but shouldn't. 29e2eeea75SSimon J. Gerraty#.include "." # directory 30e2eeea75SSimon J. Gerraty 3106b9b3e0SSimon J. Gerraty# As of 2020-11-21, anything after the delimiter '"' is ignored. 3206b9b3e0SSimon J. Gerraty.include "/dev/null" and ignore anything in the rest of the line. 3306b9b3e0SSimon J. Gerraty 3412904384SSimon J. Gerraty# The filename to be included can contain expressions. 3512904384SSimon J. GerratyDEV= null 3612904384SSimon J. Gerraty.include "/dev/${DEV}" 3712904384SSimon J. Gerraty 3812904384SSimon J. Gerraty# Expressions in double quotes or angle quotes are first parsed naively, to 3912904384SSimon J. Gerraty# find the closing '"'. In a second step, the expressions are expanded. This 4012904384SSimon J. Gerraty# means that the expressions cannot include the characters '"' or '>'. This 4112904384SSimon J. Gerraty# restriction is not practically relevant since the expressions inside 4212904384SSimon J. Gerraty# '.include' directives are typically kept as simple as possible. 4312904384SSimon J. Gerraty# 4412904384SSimon J. Gerraty# If the whole line were expanded before parsing, the filename to be included 4512904384SSimon J. Gerraty# would be empty, and the closing '"' would be in the trailing part of the 4612904384SSimon J. Gerraty# line, which is ignored as of 2021-12-03. 4712904384SSimon J. GerratyDQUOT= " 48148ee845SSimon J. Gerraty# expect+1: Could not find " 4912904384SSimon J. Gerraty.include "${DQUOT}" 5012904384SSimon J. Gerraty 5112904384SSimon J. Gerraty# When the expression in a filename cannot be evaluated, the failing 5212904384SSimon J. Gerraty# expression is skipped and the file is included nevertheless. 5312904384SSimon J. Gerraty# FIXME: Add proper error handling, no file must be included here. 54*759b177aSSimon J. Gerraty# expect+2: Unknown modifier ":Z" 556a7405f5SSimon J. Gerraty# expect+1: Could not find nonexistent.mk 5612904384SSimon J. Gerraty.include "nonexistent${:U123:Z}.mk" 5712904384SSimon J. Gerraty 589f45a3c8SSimon J. Gerraty# The traditional include directive is seldom used. 599f45a3c8SSimon J. Gerratyinclude /dev/null # comment 609f45a3c8SSimon J. Gerraty# expect+1: Cannot open /nonexistent 619f45a3c8SSimon J. Gerratyinclude /nonexistent # comment 629f45a3c8SSimon J. Gerratysinclude /nonexistent # comment 639f45a3c8SSimon J. Gerratyinclude ${:U/dev/null} # comment 649f45a3c8SSimon J. Gerratyinclude /dev/null /dev/null 6598875883SSimon J. Gerraty# expect+1: Invalid line 'include' 669f45a3c8SSimon J. Gerratyinclude 679f45a3c8SSimon J. Gerraty 689f45a3c8SSimon J. Gerraty# XXX: trailing whitespace in diagnostic, missing quotes around filename 699f45a3c8SSimon J. Gerraty### TODO: expect+1: Could not find 709f45a3c8SSimon J. Gerraty# The following include directive behaves differently, depending on whether 719f45a3c8SSimon J. Gerraty# the current file has a slash or is a relative filename. In the first case, 729f45a3c8SSimon J. Gerraty# make opens the directory of the current file and tries to read from it, 73*759b177aSSimon J. Gerraty# resulting in the error message ":1: Zero byte read from file". 749f45a3c8SSimon J. Gerraty# In the second case, the error message is "Could not find ", without quotes 759f45a3c8SSimon J. Gerraty# or any other indicator for the empty filename at the end of the line. 769f45a3c8SSimon J. Gerraty#include ${:U} 779f45a3c8SSimon J. Gerraty 789f45a3c8SSimon J. Gerraty 799f45a3c8SSimon J. Gerraty# Since parse.c 1.612 from 2022-01-01 and before parse.c 1.620 from 809f45a3c8SSimon J. Gerraty# 2022-01-07, including an empty regular file called bmake_malloc(0), which 819f45a3c8SSimon J. Gerraty# may return a null pointer. On OpenBSD, this led to a segmentation fault in 829f45a3c8SSimon J. Gerraty# Buf_InitSize, which assumes that bmake_malloc never returns NULL, just like 839f45a3c8SSimon J. Gerraty# all other places in the code. 849f45a3c8SSimon J. Gerraty_!= > directive-include-empty 859f45a3c8SSimon J. Gerraty.include "${.CURDIR}/directive-include-empty" 869f45a3c8SSimon J. Gerraty_!= rm directive-include-empty 879f45a3c8SSimon J. Gerraty 889f45a3c8SSimon J. Gerraty 89956e45f6SSimon J. Gerratyall: 90