make/tests/scripts/targets/POSIX
Paul Smith 3176b60566 [SV 64085] Handle .POSIX plus .IGNORE correctly
POSIX requires that a conforming makefile should not use -e if
 1) make is invoked with -i
 2) A .IGNORE target exists with no prerequisites
 3) The current target is a prerequisite of .IGNORE

* src/job.c (start_job_command): Add the target's flags when
constructing argv so it can check (3) above.
(construct_command_argv_internal): Don't set shellflags if it's not
set: this only happens if we're parsing for the slow path and we
don't need them.
(construct_command_argv): Don't allocate buffers if not needed.
When detecting "-ec", check the global ignore_errors_flag and the
current command line flags.
* tests/scripts/targets/IGNORE: Add tests for .IGNORE.
* tests/scripts/targets/POSIX: Add tests for the three cases above.
2024-02-04 19:41:50 -05:00

118 lines
2.8 KiB
Perl

# -*-perl-*-
$description = "Test the behaviour of the .POSIX target.";
$details = "";
# Ensure turning on .POSIX enables the -e flag for the shell
run_make_test(q!
.POSIX:
all: ; @#HELPER# -q fail 1; #HELPER# out hello
!,
'', "#MAKE#: *** [#MAKEFILE#:3: all] Error 1\n", 512);
# But explicit settings must still take precedence
run_make_test(q!
.POSIX:
all: ; @-#HELPER# -q fail 1; #HELPER# out hello
.SHELLFLAGS = -c
!,
'', "hello");
run_make_test(q!
.POSIX:
all: ; @-#HELPER# -q fail 1; #HELPER# out hello
all: .SHELLFLAGS = -c
!,
'', "hello");
# SV 63667: We shouldn't add -e to sh if errors are ignored
run_make_test(q!
.POSIX:
all: ; @-#HELPER# -q fail 1; #HELPER# out hello
!,
'', "hello\n");
# SV 64085: Also don't add -e if -i or .IGNORE is used
run_make_test(q!
.POSIX:
all: ; @#HELPER# -q fail 1; #HELPER# out hello
!,
'-i', "hello\n");
run_make_test(q!
.POSIX:
.IGNORE:
all: ; @#HELPER# -q fail 1; #HELPER# out hello
!,
'', "hello\n");
run_make_test(q!
.POSIX:
.IGNORE: all
all: ; @#HELPER# -q fail 1; #HELPER# out hello
!,
'', "hello\n");
# But explicit settings must still take precedence
run_make_test(q!
.POSIX:
all: ; @-#HELPER# -q fail 1; #HELPER# out hello
.SHELLFLAGS = -ec
!,
'', "#MAKE#: [#MAKEFILE#:3: all] Error 1 (ignored)\n");
run_make_test(q!
.POSIX:
all: ; @-#HELPER# -q fail 1; #HELPER# out hello
all: .SHELLFLAGS = -ec
!,
'', "#MAKE#: [#MAKEFILE#:3: all] Error 1 (ignored)\n");
# User settings must override .POSIX
# In the standard .POSIX must be the first thing in the makefile
# but we relax that rule in GNU Make.
# Different versions of sh generate different output for -x so check it
my $script = subst_make_string('#HELPER# -q fail 1; true');
my $flags = '-xc';
my $out = `$sh_name $flags '$script' 2>&1`;
run_make_test(qq!
.SHELLFLAGS = $flags
.POSIX:
all: ; \@$script
!,
'', $out);
# Test the default value of various POSIX-specific variables
my %POSIX = (AR => 'ar', ARFLAGS => '-rv',
YACC => 'yacc', YFLAGS => '',
LEX => 'lex', LFLAGS => '',
LDFLAGS => '',
CC => 'c99', CFLAGS => '-O1',
FC => 'fort77', FFLAGS => '-O1',
SCCSFLAGS => '', SCCSGETFLAGS => '-s');
my $make = join('', map { "\t\@echo '$_=\$($_)'\n" } sort keys %POSIX);
my $r = join('', map { "$_=$POSIX{$_}\n"} sort keys %POSIX);
run_make_test(qq!
.POSIX:
all:
$make
!,
'', $r);
# Make sure that local settings take precedence
%ENV = (%ENV, map { $_ => "xx-$_" } keys %POSIX);
$r = join('', map { "$_=xx-$_\n"} sort keys %POSIX);
run_make_test(undef, '', $r);
# This tells the test driver that the perl test script executed properly.
1;