var-op-shell.mk revision 1.4 1 1.4 sjg # $NetBSD: var-op-shell.mk,v 1.4 2021/02/06 04:55:08 sjg 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.3 rillig # The error message is still there 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.3 rillig # TODO: Make this an error in lint mode.
28 1.3 rillig OUTPUT!= echo "failed"; false
29 1.3 rillig .if ${OUTPUT} != "failed"
30 1.3 rillig . error
31 1.3 rillig .endif
32 1.3 rillig
33 1.3 rillig # A command with empty output may fail as well.
34 1.3 rillig OUTPUT!= false
35 1.3 rillig .if ${OUTPUT} != ""
36 1.3 rillig . error
37 1.3 rillig .endif
38 1.3 rillig
39 1.3 rillig # In the output of the command, each newline is replaced with a space.
40 1.3 rillig # Except for the very last one, which is discarded.
41 1.3 rillig OUTPUT!= echo "line 1"; echo "line 2"
42 1.3 rillig .if ${OUTPUT} != "line 1 line 2"
43 1.3 rillig . error
44 1.3 rillig .endif
45 1.3 rillig
46 1.3 rillig # A failing command in the middle results in the exit status 0, which in the
47 1.3 rillig # end means that the whole sequence of commands succeeded.
48 1.3 rillig OUTPUT!= echo "before"; false; echo "after"
49 1.3 rillig .if ${OUTPUT} != "before after"
50 1.3 rillig . error
51 1.3 rillig .endif
52 1.3 rillig
53 1.4 sjg # This should result in a warning about "exited on a signal".
54 1.4 sjg # This used to be kill -14 (SIGALRM), but that stopped working on
55 1.4 sjg # Darwin18 after recent update.
56 1.4 sjg OUTPUT!= kill $$$$
57 1.3 rillig .if ${OUTPUT} != ""
58 1.3 rillig . error
59 1.3 rillig .endif
60 1.3 rillig
61 1.3 rillig # A nonexistent command produces a non-zero exit status.
62 1.3 rillig OUTPUT!= /bin/no/such/command
63 1.3 rillig .if ${OUTPUT} != ""
64 1.3 rillig . error
65 1.3 rillig .endif
66 1.3 rillig
67 1.3 rillig # The output from the shell's stderr is not captured, it just passes through.
68 1.3 rillig OUTPUT!= echo "stdout"; echo "stderr" 1>&2
69 1.3 rillig .if ${OUTPUT} != "stdout"
70 1.3 rillig . error
71 1.3 rillig .endif
72 1.3 rillig
73 1.3 rillig # The 8 dollar signs end up as 4 dollar signs when expanded. The shell sees
74 1.3 rillig # the command "echo '$$$$'". The 4 dollar signs are stored in OUTPUT, and
75 1.3 rillig # when that variable is expanded, they expand to 2 dollar signs.
76 1.3 rillig OUTPUT!= echo '$$$$$$$$'
77 1.3 rillig .if ${OUTPUT} != "\$\$"
78 1.3 rillig . error
79 1.3 rillig .endif
80 1.1 rillig
81 1.1 rillig all:
82