1 1.10 rillig # $NetBSD: directive-export-gmake.mk,v 1.10 2025/06/28 22:39:28 rillig Exp $ 2 1.1 rillig # 3 1.1 rillig # Tests for the export directive (without leading dot), as in GNU make. 4 1.1 rillig 5 1.1 rillig # The "export" directive only affects the environment of the make process 6 1.1 rillig # and its child processes. It does not affect the global variables or any 7 1.1 rillig # other variables. 8 1.1 rillig VAR= before 9 1.1 rillig export VAR=exported 10 1.1 rillig .if ${VAR} != "before" 11 1.1 rillig . error 12 1.1 rillig .endif 13 1.1 rillig 14 1.1 rillig # Ensure that the name-value pair is actually exported. 15 1.1 rillig .if ${:!echo "\$VAR"!} != "exported" 16 1.1 rillig . error 17 1.1 rillig .endif 18 1.1 rillig 19 1.1 rillig # This line looks like it would export 2 variables, but it doesn't. 20 1.1 rillig # It only exports VAR and appends everything else as the variable value. 21 1.1 rillig export VAR=exported VAR2=exported-as-well 22 1.1 rillig .if ${:!echo "\$VAR"!} != "exported VAR2=exported-as-well" 23 1.1 rillig . error ${:!echo "\$VAR"!} 24 1.1 rillig .endif 25 1.1 rillig 26 1.1 rillig # Contrary to the usual variable assignments, spaces are significant 27 1.1 rillig # after the '=' sign and are prepended to the value of the environment 28 1.1 rillig # variable. 29 1.1 rillig export VAR= leading spaces 30 1.1 rillig .if ${:!echo "\$VAR"!} != " leading spaces" 31 1.1 rillig . error 32 1.1 rillig .endif 33 1.1 rillig 34 1.1 rillig # Contrary to the usual variable assignments, spaces are significant 35 1.1 rillig # before the '=' sign and are appended to the name of the environment 36 1.1 rillig # variable. 37 1.2 rillig # 38 1.2 rillig # Depending on the shell, environment variables with such exotic names 39 1.2 rillig # may be silently discarded. One such shell is dash, which is the default 40 1.2 rillig # shell on Ubuntu and Debian. 41 1.1 rillig export VAR =trailing space in varname 42 1.3 rillig .if ${:!env | grep trailing || true!} != "VAR =trailing space in varname" 43 1.3 rillig . if ${:!env | grep trailing || true!} != "" # for dash 44 1.2 rillig . error 45 1.2 rillig . endif 46 1.1 rillig .endif 47 1.1 rillig 48 1.1 rillig # The right-hand side of the exported variable is expanded exactly once. 49 1.1 rillig TWICE= expanded twice 50 1.1 rillig ONCE= expanded once, leaving $${TWICE} as-is 51 1.1 rillig export VAR=${ONCE} 52 1.1 rillig .if ${:!echo "\$VAR"!} != "expanded once, leaving \${TWICE} as-is" 53 1.1 rillig . error 54 1.1 rillig .endif 55 1.1 rillig 56 1.1 rillig # Undefined variables are allowed on the right-hand side, they expand 57 1.1 rillig # to an empty string, as usual. 58 1.1 rillig export VAR=an ${UNDEF} variable 59 1.1 rillig .if ${:!echo "\$VAR"!} != "an variable" 60 1.1 rillig . error 61 1.1 rillig .endif 62 1.1 rillig 63 1.4 rillig 64 1.4 rillig # The body of the .for loop expands to 'export VAR=${:U1}', and the 'export' 65 1.4 rillig # directive is only recognized if the line does not contain a ':', to allow 66 1.4 rillig # 'export' to be a regular target. 67 1.4 rillig .for value in 1 68 1.6 rillig # XXX: The ':' in this line is inside an expression and should thus not be 69 1.6 rillig # interpreted as a dependency operator. 70 1.10 rillig # expect+1: Invalid line "export VAR=${:U1}", expanded to "export VAR=1" 71 1.4 rillig export VAR=${value} 72 1.4 rillig .endfor 73 1.7 rillig 74 1.7 rillig 75 1.7 rillig # The 'export' directive expands expressions, but the expressions must not 76 1.7 rillig # contain a ':', due to the overly strict parser. The indirect expressions 77 1.7 rillig # may contain a ':', though. 78 1.7 rillig # 79 1.7 rillig # As a side effect, this test demonstrates that the 'export' directive exports 80 1.7 rillig # the environment variable immediately, other than the '.export' directive, 81 1.7 rillig # which defers that action if the variable value contains a '$'. 82 1.7 rillig INDIRECT_TZ= ${:UAmerica/Los_Angeles} 83 1.7 rillig export TZ=${INDIRECT_TZ} 84 1.7 rillig # expect+1: 16:00:00 85 1.7 rillig .info ${%T:L:localtime=86400} 86 1.8 rillig 87 1.8 rillig 88 1.8 rillig # The '=' must be present in the unexpanded line, it cannot be generated by 89 1.8 rillig # an expression. 90 1.8 rillig EQ= = 91 1.8 rillig # expect+1: Variable/Value missing from "export" 92 1.8 rillig export EQ_VAR${EQ}eq-value 93 1.8 rillig .if ${:!env!:MEQ_VAR=*} 94 1.8 rillig . error 95 1.8 rillig .endif 96 1.8 rillig 97 1.8 rillig 98 1.8 rillig # The variable name must be given directly, it is not expanded. The name of 99 1.8 rillig # the exported variable thus starts with a '$', and that name may be filtered 100 1.8 rillig # out by the platform. 101 1.8 rillig INDIRECT_NAME= I_NAME 102 1.8 rillig INDIRECT_VALUE= indirect value 103 1.8 rillig export ${INDIRECT_NAME}=${INDIRECT_VALUE} 104 1.8 rillig .if ${:!env!:MI_NAME=*} 105 1.8 rillig . error 106 1.8 rillig .endif 107