var-op-shell.mk revision 1.6 1 1.6 rillig # $NetBSD: var-op-shell.mk,v 1.6 2022/01/10 20:32:29 rillig Exp $
2 1.1 rillig #
3 1.2 rillig # Tests for the != variable assignment operator, which runs its right-hand
4 1.2 rillig # side through the shell.
5 1.1 rillig
6 1.3 rillig # The variable OUTPUT gets the output from running the shell command.
7 1.3 rillig OUTPUT!= echo "success"'ful'
8 1.3 rillig .if ${OUTPUT} != "successful"
9 1.3 rillig . error
10 1.3 rillig .endif
11 1.3 rillig
12 1.3 rillig # Since 2014-08-20, the output of the shell command may be empty.
13 1.3 rillig #
14 1.3 rillig # On 1996-05-29, when the '!=' assignment operator and Cmd_Exec were added,
15 1.3 rillig # an empty output produced the error message "Couldn't read shell's output
16 1.3 rillig # for \"%s\"".
17 1.3 rillig #
18 1.5 rillig # The error message is still in Cmd_Exec but reserved for technical errors.
19 1.3 rillig # It may be possible to trigger the error message by killing the shell after
20 1.3 rillig # reading part of its output.
21 1.3 rillig OUTPUT!= true
22 1.3 rillig .if ${OUTPUT} != ""
23 1.3 rillig . error
24 1.3 rillig .endif
25 1.3 rillig
26 1.3 rillig # The output of a shell command that failed is processed nevertheless.
27 1.5 rillig # Unlike the other places that run external commands (expression modifier
28 1.5 rillig # '::!=', expression modifier ':!...!'), a failed command generates only a
29 1.5 rillig # warning, not an "error". These "errors" are ignored in default mode, for
30 1.5 rillig # compatibility, but not in lint mode (-dL).
31 1.3 rillig OUTPUT!= echo "failed"; false
32 1.3 rillig .if ${OUTPUT} != "failed"
33 1.3 rillig . error
34 1.3 rillig .endif
35 1.3 rillig
36 1.3 rillig # A command with empty output may fail as well.
37 1.3 rillig OUTPUT!= false
38 1.3 rillig .if ${OUTPUT} != ""
39 1.3 rillig . error
40 1.3 rillig .endif
41 1.3 rillig
42 1.3 rillig # In the output of the command, each newline is replaced with a space.
43 1.3 rillig # Except for the very last one, which is discarded.
44 1.3 rillig OUTPUT!= echo "line 1"; echo "line 2"
45 1.3 rillig .if ${OUTPUT} != "line 1 line 2"
46 1.3 rillig . error
47 1.3 rillig .endif
48 1.3 rillig
49 1.3 rillig # A failing command in the middle results in the exit status 0, which in the
50 1.3 rillig # end means that the whole sequence of commands succeeded.
51 1.3 rillig OUTPUT!= echo "before"; false; echo "after"
52 1.3 rillig .if ${OUTPUT} != "before after"
53 1.3 rillig . error
54 1.3 rillig .endif
55 1.3 rillig
56 1.4 sjg # This should result in a warning about "exited on a signal".
57 1.4 sjg # This used to be kill -14 (SIGALRM), but that stopped working on
58 1.4 sjg # Darwin18 after recent update.
59 1.4 sjg OUTPUT!= kill $$$$
60 1.3 rillig .if ${OUTPUT} != ""
61 1.3 rillig . error
62 1.3 rillig .endif
63 1.3 rillig
64 1.3 rillig # A nonexistent command produces a non-zero exit status.
65 1.3 rillig OUTPUT!= /bin/no/such/command
66 1.3 rillig .if ${OUTPUT} != ""
67 1.3 rillig . error
68 1.3 rillig .endif
69 1.3 rillig
70 1.3 rillig # The output from the shell's stderr is not captured, it just passes through.
71 1.3 rillig OUTPUT!= echo "stdout"; echo "stderr" 1>&2
72 1.3 rillig .if ${OUTPUT} != "stdout"
73 1.3 rillig . error
74 1.3 rillig .endif
75 1.3 rillig
76 1.3 rillig # The 8 dollar signs end up as 4 dollar signs when expanded. The shell sees
77 1.3 rillig # the command "echo '$$$$'". The 4 dollar signs are stored in OUTPUT, and
78 1.3 rillig # when that variable is expanded, they expand to 2 dollar signs.
79 1.3 rillig OUTPUT!= echo '$$$$$$$$'
80 1.3 rillig .if ${OUTPUT} != "\$\$"
81 1.3 rillig . error
82 1.3 rillig .endif
83 1.1 rillig
84 1.6 rillig
85 1.6 rillig # As a debugging aid, log the exact command that is run via the shell.
86 1.6 rillig .MAKEFLAGS: -dv
87 1.6 rillig OUTPUT!= echo '$$$$$$$$'
88 1.6 rillig .MAKEFLAGS: -d0
89 1.6 rillig
90 1.1 rillig all:
91