make/tests/scripts/functions/shell
Paul Smith 4da2055a10 Rework output sync to lock a temp file on POSIX
Some POSIX systems do not allow locks to be taken on non-files, such
as pipes.  This is a problem since very often make is invoked with
its stdout redirected to a pipe.  Also, if stdout is redirected to a
file that already has a lock on it for some other reason (perhaps a
shared file such as /dev/null) it can cause a hang.

This means our previous method of locking stdout, although it had some
nice advantages, is not portable enough.  Instead, use a temporary
file and take the lock on that.  We pass the name of the file to child
make processes.  On Windows we continue to use a shared mutex for
output sync.

Remove POSIX emulation functions like fcntl from Windows; instead
follow the lead of the jobserver and create an interface in os.h for
output sync, and move the OS-specific content to posixos.c and
w32os.c.

* NEWS: Add a note.
* src/makeint.h (ALL_SET): Check that all bits are set.
* src/os.h: Add bits for checking the state of stdin/stdout/stderr.
Add prototypes for OS-specific output sync methods.
* src/posixos.c (check_io_state): Determine the status of stdin,
stdout, stderr an return a suite of bits describing them.
(osync_enabled): If the global variable holding the FD of the lock
file (osync_handle) is valid return true.
(osync_setup): Create a temporary file and remember its name in a
global variable (osync_tmpfile), and set osync_handle.
(osync_get_mutex): If output sync is enabled, return the filename
of the lock file prefixed with "fnm:" to denote a filename.
(osync_parse_mutex): If the provided filename has the wrong format
disable output sync.  Else open the lock file and set osync_handle.
(osync_clear): Close osync_handle.  If we're the parent make, then
also unlink the temporary file.
(osync_acquire): Take a lock on the osync_handle descriptor.
(osync_release): Release the lock on the osync_handle descriptor.
(fd_set_append): Add APPEND mode to a file descriptor.
* src/w32/w32os.c: Perform the same actions as posixos.c, copying
the details from src/w32/compat/posixfcn.c.  Use a mutex rather
than locking a temporary file.
* src/output.h: Remove all the OS-specific content.
* src/output.c: Remove all the OS-specific content.
(set_append_mode): Remove and replace with fd_set_append().
(sync_init): Remove and replace with check_io_state().
(acquire_semaphore): Remove and replace with osync_acquire().
(release_semaphore): Remove and replace with osync_release().
(setup_tmpfile): If the IO state is not obtained, get it.  If stdout
and/or stderr are valid, set up a tempfile to capture them.
(output_init): Set io_state if not set already, and check it when
deciding whether to close stdout on exit.
* src/main.c (main): If we're syncing, set up the mutex using the
new osync_setup() / osync_parse_mutex() methods.
(prepare_mutex_handl_string): Replace with osync_parse_mutex().
(die): Call osync_clear().
* src/w32/compat/posixfcn.c: Remove implementations of fcntl(),
record_sync_mutex(), create_mutex(), and same_stream().
2022-08-30 15:44:43 -04:00

179 lines
4.2 KiB
Perl

# -*-perl-*-
$description = 'Test the $(shell ...) function.';
$details = '';
# Test standard shell
run_make_test('.PHONY: all
OUT := $(shell echo hi)
all: ; @echo $(OUT)
','','hi');
# Test shells inside rules.
run_make_test('.PHONY: all
all: ; @echo $(shell echo hi)
','','hi');
# Verify .SHELLSTATUS
run_make_test('.PHONY: all
PRE := $(.SHELLSTATUS)
$(shell exit 0)
OK := $(.SHELLSTATUS)
$(shell exit 1)
BAD := $(.SHELLSTATUS)
all: ; @echo PRE=$(PRE) OK=$(OK) BAD=$(BAD)
','','PRE= OK=0 BAD=1');
# Test unescaped comment characters in shells. Savannah bug #20513
run_make_test(q!
FOO := $(shell echo '#')
foo: ; echo '$(FOO)'
!,
'', "echo '#'\n#\n");
# Test that exported variables are passed to $(shell ...)
$ENV{FOO} = 'baz';
run_make_test(q!
OUT = $(shell echo $$FOO)
foo: ; @echo '$(OUT)'
!,
'', 'baz');
$ENV{FOO} = 'baz';
run_make_test(q!
FOO = bar
OUT = $(shell echo $$FOO)
foo: ; @echo '$(OUT)'
!,
'', 'bar');
run_make_test(q!
export FOO = bar
OUT = $(shell echo $$FOO)
foo: ; @echo '$(OUT)'
!,
'', 'bar');
# Test shells inside exported environment variables, simply expanded.
run_make_test('
export HI := $(shell echo hi)
.PHONY: all
all: ; @echo $$HI
',
'','hi');
# Test shells inside exported environment variables. See SV 10593
run_make_test('
export HI = $(shell echo hi)
.PHONY: all
all: ; @echo $$HI
',
'',"hi");
$ENV{HI} = 'foo';
run_make_test('
HI = $(shell echo hi)
.PHONY: all
all: ; @echo $$HI
',
'',"hi");
$ENV{HI} = 'foo';
run_make_test('
HI = $(shell echo hi)
bad := $(HI)
.PHONY: all
all: ; @echo $$HI $(bad)
',
'',"hi hi");
if ($port_type ne 'W32') {
# Test shell errors in recipes including offset
# This needs to be ported to Windows, or else Windows error messages
# need to converted to look like more normal make errors.
run_make_test('
.RECIPEPREFIX = >
all:
>@echo hi
>$(shell ./basdfdfsed there)
>@echo $(.SHELLSTATUS)
',
'', "#MAKE#: ./basdfdfsed: $ERR_no_such_file\nhi\n127\n");
run_make_test('
$(shell ./basdfdfsed where)
all: ; @echo $(.SHELLSTATUS)
',
'', "#MAKE#: ./basdfdfsed: $ERR_no_such_file\n127\n");
# Test SHELLSTATUS for kill.
# This test could be ported to Windows, using taskkill ... ?
# Figure out the exit code for SIGINT
my $pid = fork();
if (! $pid) {
exec('kill -2 $$') or die "exec: Cannot execute sleep\n";
}
waitpid($pid, 0);
# .SHELLSTATUS for a signal gives 128 + the signal number
my $ret = $?;
if ($ret > 255) {
# Solaris 10 perl 5.8.4 puts signal number + 128 into the high 8 bits.
$ret >>= 8;
}
$ret |= 128;
run_make_test('.PHONY: all
$(shell kill -2 $$$$)
STAT := $(.SHELLSTATUS)
all: ; @echo STAT=$(STAT)
','',"STAT=$ret\n");
# Test that not-found errors can be redirected
if ($ERR_command_not_found) {
$_ = $ERR_command_not_found;
s/#CMDNAME#/bad-command/g;
run_make_test(q!
out := $(shell bad-command 2>&1)
all: ; @echo '$(.SHELLSTATUS): $(out)'
!,
'', "127: $_\n");
}
# If we're using pipes for jobserver, then we will close them and not
# allow them to be available to sub-makes invoked via $(shell ...)
if (exists $FEATURES{'jobserver'}) {
run_make_test(q!
ifeq ($(ELT),)
default:; @$(MAKE) -f #MAKEFILE# ELT=1
else ifeq ($(ELT),1)
OUTPUT := $(shell $(MAKE) -f #MAKEFILE# ELT=2)
$(info $(OUTPUT))
default:;: $(ELT)
else
default:;: $(ELT)
endif
!,
'--no-print-directory -j2 --jobserver-style=pipe', "#MAKE#[2]: warning: jobserver unavailable: using -j1. Add '+' to parent make rule.\n: 2\n: 1");
}
}
# If we're not using pipes for jobserver, then they are available in sub-makes
# invoked by $(shell ...)
if ($port_type eq 'W32' || exists $FEATURES{'jobserver-fifo'}) {
run_make_test(q!
ifeq ($(ELT),)
default:; @$(MAKE) -f #MAKEFILE# ELT=1
else ifeq ($(ELT),1)
OUTPUT := $(shell $(MAKE) -f #MAKEFILE# ELT=2)
$(info $(OUTPUT))
default:;: $(ELT)
else
default:;: $(ELT)
endif
!,
'--no-print-directory -j2', ": 2\n: 1");
}
1;