#                                                                    -*-perl-*-
# $Id: automatic,v 1.7 2006/01/05 11:24:54 rockyb Exp $
$description = "Test automatic variable setting.";

$details = "
 TEST #1 -- basic test of automatic variables: 
            \$\@, \$(\@D), \$(\@F), \$*, \$(*D), \$(*F)
            \$<, \$(<D), \$(<F), \$^, \$(^D) \$(^F), \$+ \$(+D), \$(+F)
            \$?, \$(?D), and \$(?F)

 TEST #2 & 3 -- test the SysV emulation of \$\$\@ etc.
";

use Cwd;

$dir = cwd;
$dir =~ s,.*/([^/]+)$,../$1,;

open(MAKEFILE, "> $makefile");
print MAKEFILE "dir = $dir\n";
print MAKEFILE <<'EOF';
.SUFFIXES:
.SUFFIXES: .x .y .z
$(dir)/foo.x : baz.z $(dir)/bar.y baz.z
	@echo '$$@ = $@, $$(@D) = $(@D), $$(@F) = $(@F)'
	@echo '$$* = $*, $$(*D) = $(*D), $$(*F) = $(*F)'
	@echo '$$< = $<, $$(<D) = $(<D), $$(<F) = $(<F)'
	@echo '$$^ = $^, $$(^D) = $(^D), $$(^F) = $(^F)'
	@echo '$$? = $?, $$(?D) = $(?D), $$(?F) = $(?F)'
	touch $@

$(dir)/bar.y baz.z : ; touch $@
EOF

## This is what we should have tested...
##print MAKEFILE <<'EOF';
##.SUFFIXES:
##.SUFFIXES: .x .y .z
##$(dir)/foo.x : baz.z $(dir)/bar.y baz.z
##	@echo '$$@ = $@, $$(@D) = $(@D), $$(@F) = $(@F)'
##	@echo '$$* = $*, $$(*D) = $(*D), $$(*F) = $(*F)'
##	@echo '$$< = $<, $$(<D) = $(<D), $$(<F) = $(<F)'
##	@echo '$$^ = $^, $$(^D) = $(^D), $$(^F) = $(^F)'
##--->	@echo '$$+ = $+, $$(+D) = $(+D), $$(+F) = $(+F)'
##	@echo '$$? = $?, $$(?D) = $(?D), $$(?F) = $(?F)'
##	touch $@
##
##$(dir)/bar.y baz.z : ; touch $@
##EOF
close(MAKEFILE);

# TEST #1 -- simple test
# -------

# Touch these into the past
&utouch(-10, qw(foo.x baz.z));

&run_make_with_options($makefile, "", &get_logfile);
$answer = "touch $dir/bar.y
\$\@ = $dir/foo.x, \$(\@D) = $dir, \$(\@F) = foo.x
\$* = $dir/foo, \$(*D) = $dir, \$(*F) = foo
\$< = baz.z, \$(<D) = ., \$(<F) = baz.z
\$^ = baz.z $dir/bar.y, \$(^D) = . $dir, \$(^F) = baz.z bar.y
\$? = $dir/bar.y, \$(?D) = $dir, \$(?F) = bar.y
touch $dir/foo.x\n";

##$answer = "touch $dir/bar.y
##\$\@ = $dir/foo.x, \$(\@D) = $dir, \$(\@F) = foo.x
##\$* = $dir/foo, \$(*D) = $dir, \$(*F) = foo
##\$< = baz.z, \$(<D) = ., \$(<F) = baz.z
##\$^ = baz.z $dir/bar.y, \$(^D) = . $dir, \$(^F) = baz.z bar.y
##--> \$+ = baz.z $dir/bar.y baz.z, \$(+D) = . $dir ., \$(+F) = baz.z bar.y baz.z
##\$? = $dir/bar.y, \$(?D) = $dir, \$(?F) = bar.y
##touch $dir/foo.x\n";

&compare_output($answer, &get_logfile(1));

unlink(qw(foo.x bar.y baz.z));

# TEST #2 -- test the SysV emulation of $$@ etc.
# -------

$makefile2 = &get_tmpfile;

open(MAKEFILE, "> $makefile2");
print MAKEFILE "dir = $dir\n";
print MAKEFILE <<'EOF';
.SUFFIXES:
.DEFAULT: ; @echo '$@'

$(dir)/foo $(dir)/bar: $@.x $$@.x $$$@.x $$$$@.x $$(@D).x $$(@F).x

$(dir)/x.z $(dir)/y.z: $(dir)/%.z : $@.% $$@.% $$$@.% $$$$@.% $$(@D).% $$(@F).%
EOF

close(MAKEFILE);

&run_make_with_options($makefile2, "$dir/foo $dir/bar", &get_logfile);
$answer = ".x\n$dir/foo.x\n\$.x\n\$@.x\n$dir.x\nfoo.x\n$dir/bar.x\nbar.x\n";
&compare_output($answer, &get_logfile(1));

&run_make_with_options($makefile2, "$dir/x.z $dir/y.z", &get_logfile);
$answer = ".x\n$dir/x.z.x\n\$.x\n\$@.x\n$dir.x\nx.z.x\n.y\n$dir/y.z.y\n\$.y\n\$@.y\n$dir.y\ny.z.y\n";
&compare_output($answer, &get_logfile(1));

# TEST #3 -- test for Savannah bug #12320.
#
run_make_test('
.SUFFIXES: .b .src

mbr.b: mbr.src
	@echo $*

mbr.src: ; @:',
              '',
              'mbr');

# TEST #4 -- test for Savannah bug #8154
# Make sure that nonexistent prerequisites are listed in $?, since they are
# considered reasons for the target to be rebuilt.
#
touch('foo');

run_make_test('
foo: bar ; @echo "\$$? = $?"
bar: ;',
              '',
              '$? = bar');

unlink('foo');
notest:
1;
