#!/bin/sh

# 25.08.2007 Volker Quetschke
# Check that dmake handles dependencies correctly.
# (issue 64572)

: ${DMAKEPROG:=dmake}
file1="mfile1.mk"
file2="aa.x"
file3="aa.y"
file4="aa.z"
tmpfiles="$file1 $file2 $file3 $file4"

trap '{ echo "trapped signal - removing temporary files" ; rm -rf $tmpfiles ; }' 1 2 3 15

# Remove files from prior failed run
rm -rf $tmpfiles

# Remember to quote variables in generated makefiles( $ -> \$ ).
# Test 1
cat > $file1 <<EOT
SHELL*:=/bin/sh
SHELLFLAGS*:=-ce

aa.x : aa.y
	@echo nothing

aa.y :
	@echo \$@

EOT

# Create test environment
touch aa.x
# Avoid that aa.x has the same time stamp as aa.y after
# that has been rebuild.
sleep 1

output1=`eval ${DMAKEPROG} -rf $file1 2>&1`
result1=$?

if test $result1 = 0 && echo $output1 | grep 'Warning: -- Target \[aa.x\] was made but the time stamp has not been updated.' > /dev/null 2>&1 ; then
  echo "Subtest 1: OK"
  result1=0
else
  echo "Subtest 1: Wrong result: $output1"
  echo
  result1=1
fi


# Remember to quote variables in generated makefiles( $ -> \$ ).
# Test 2 - Warn if virtual targets have a corresponding file.
cat > $file1 <<EOT
SHELL*:=/bin/sh
SHELLFLAGS*:=-ce

aa.x : aa.y
	@echo X\$@X
	@touch \$@

# Should warn - aa.y exists.
aa.y : aa.z

aa.z :
	@printf Z\$@Z

EOT

# Create test environment
rm -f aa.x
touch aa.y
# Avoid the same time after build.
sleep 1

output2=`eval ${DMAKEPROG} -rf $file1 2>&1`
result2=$?

if test $result2 = 0 && echo $output2 | grep 'Warning: -- Found file corresponding to virtual target \[aa.y\].' > /dev/null 2>&1 ; then
  echo "Subtest 2: OK"
  result2=0
else
  echo "Subtest 2: Wrong result: $output2"
  echo
  result2=1
fi


# Remember to quote variables in generated makefiles( $ -> \$ ).
# Test 3
cat > $file1 <<EOT
SHELL*:=/bin/sh
SHELLFLAGS*:=-ce

aa.x : aa.y
	@echo X\$@X
	@touch \$@

aa.y : aa.z

aa.z :
	@printf Z\$@Z
	@touch \$@

EOT

# Create test environment
rm -f aa.y
touch aa.z ; sleep 1 ; touch aa.x
# Avoid the same time after build.
sleep 1

# This tests that aa.x is not build as the dependency chain is intact with
# the virtual target aa.y having the same time stamp as aa.z.
output3=`eval ${DMAKEPROG} -rf $file1 2>&1`
result3=$?

if test $result3 = 0 -a "$output3" = "\`aa.x' is up to date" ; then
  echo "Subtest 3: OK"
  result3=0
else
  echo "Subtest 3: Wrong result: :$output3:"
  echo
  result3=1
fi


# Remember to quote variables in generated makefiles( $ -> \$ ).
# Test 4
cat > $file1 <<EOT
SHELL*:=/bin/sh
SHELLFLAGS*:=-ce

aa.x : aa.y
	@echo Build \$@
	@touch \$@

aa.y : aa.z

aa.z :
	@printf Z\$@Z
	@touch \$@

EOT

# Create test environment
touch aa.z ; sleep 1 ; touch aa.x
# Create a file for the virtual target that is newer than aa.x
sleep 1 ; touch aa.y
# Avoid the same time after build.
sleep 1

# This tests that aa.x is build.
output4=`eval ${DMAKEPROG} -rf $file1 2>&1`
result4=$?

if test $result4 = 0 -a "$output4" = "Build aa.x" ; then
  echo "Subtest 4: OK"
  result4=0
else
  echo "Subtest 4: Wrong result: :$output4:"
  echo
  result4=1
fi


if test $result1 -eq 0 -a $result2 -eq 0 \
    -a $result3 -eq 0 -a $result4 -eq 0  ; then
  echo "Success - Cleaning up"
  rm -rf $tmpfiles
  exit
else
  echo "Failure!"
  exit 1
fi


syntax highlighted by Code2HTML, v. 0.9.1