all0:	all
# Have to make a default target before we include common.mk. Target
# `all' is defined later (it requires things defined by common.mk).

top_cmm = foo
top_cmm_prerequisite = cmm
# setting top_cmm here prevents common.mk from setting it. but
# we override it afterwards. $(top_cmm_prerequisite) is used as
# a prerequisite for all cmm rules, so common.mk makes cmm
# before using it.

include common.mk
# This has all the good stuff - compilation/linking rules for different
# build types, header-file dependency tracking etc etc etc.



-include hierprof.mk
# this is not included in released version so cmm. hierprof is my
# profiling system.



top_cmm	=	./cmm.$(top_build2).exe
# Overrides the default `cmm' used by rules common.mk

all:	$(top_cmm)

help::
	# $@
	#
	# This monolithic makefile builds everything in Cmm, including the
	# release/website generation and tests. Doing
	# things this way means that functionality such as automatic
	# dynamic-dependency information generation (the .d: rules)
	# are more easily available.
	# See http://www.pcug.org.au/~millerp/rmch/recu-make-cons-harm.html
	# for a further rationale for doing things this way.
	#

clean::
	find . -name "*.$(top_build2).*"|xargs rm

# Specify the source files required to make the cmm executable, using the
# function `top_exe' in common.mk:
top_cmm_src	:=\
	cmm/cmm.cpp\
	cmm/cmm-thirdpass.cpp\
	cmm/main.cpp\
	cmm/parser-nodemethods.cpp\
	cmm/parser.cpp\
	cmm/parser2.cpp\
	cmm/lexer.cpp\
	cmm/lookup.cpp\
	utils/debug.cpp\
	utils/tempfile.cpp\
        utils/commandbuf.cpp\
        utils/exceptionstream.cpp\
        utils/profil.cpp\

# Specify source files required by various other executables:
overlap_src	:=\
	examples/overlap/main.cmm\
	cmm/dispatch.cpp\
	
overlap_src2	:=\
	examples/overlap/main-s.cmm\
	cmm/dispatch.cpp\
	
overlap2_src	:=\
	examples/overlap2/factories.cmm\
	examples/overlap2/main.cmm\
	examples/overlap2/shapesquare.cmm\
	examples/overlap2/squareshape.cmm\
	examples/overlap2/squaretriangle.cmm\
	examples/overlap2/trianglesquare.cmm\
	cmm/dispatch.cpp\

# Make rules for building the various executables
$(call top_exe,cmm,$(top_cmm_src))
$(call top_exe,examples/overlap/main,$(overlap_src))
$(call top_exe,examples/overlap/main-s,$(overlap_src2))
$(call top_exe,examples/overlap2/main,$(overlap2_src))

# Ensure that any existing dependency files generated from previous builds
# are included.
-include $(call top_exe_include,$(sort $(top_cmm_src) $(overlap_src) $(overlap_src2) $(overlap2_src)))

# How to make a ctags file for the Cmm source code. `ectags' may be called
# `ctags' on some systems.

ctags_$(top_host)		:= ectags
ctags_CYGWIN_NT-5.0-i686 	:= ctags
ctags	:=	$(ctags_$(top_host))

tagsfile	:=	tags
$(tagsfile):	$(wildcard $(CURDIR)/cmm/*.h) $(top_cmm_src)
	@echo Making tags file
	$(ctags) --totals=yes -o $@ $^
	ls -l $@

cmm	:=	$(top_cmm)

# Things for building the example/speed programmes.
# The Visual C++ builds use the command compiler `cl' rather than a
# project file, so that it's easier to specify precise optimisation flags.
# g++ with -fvtable-thunks doesn't work.
# Note that most of these tests use the old -c/-l build process, not the
# nice shiny cmm -s method.

# the main-ss executable uses constant-time multimethod dispatch.
$(call top_exe,examples/speed/main-s,examples/speed/main.cmm cmm/dispatch.cpp)
$(call top_exe,examples/speed/main-ss,examples/speed/main.cmm.ii-$(top_build2).cmm.cmm-ss.cpp cmm/dispatch.cpp)
-include $(call top_exe_include,\
	examples/speed/main.cmm cmm/dispatch.cpp\
	examples/speed/main.cmm.ii-$(top_build2).cmm.cmm-ss.cpp\
	cmm/dispatch.cpp)


examples/speed/main-vmap-array.$(top_host).exe:	examples/speed/main.cmm.ii-$(top_build2).cmm $(cmm)
	$(cmm) -c $<\
		examples/speed/main-vmap-array.$(top_host).cpp\
		examples/speed/main-vmap-array.$(top_host).cmmi
	$(cmm) -map "" "vmap" -array "" "array" -l\
		examples/speed/main-vmap-array.$(top_host).cmmi
	$(top_g++)	-O2 -o $@ examples/speed/main-vmap-array.$(top_host).cpp $(top_libs)
	#

examples/speed/main-array.$(top_host).exe:	examples/speed/main.cmm.ii-$(top_build2).cmm $(cmm)
	$(cmm)	-c $<\
		examples/speed/main-array.$(top_host).cpp\
		examples/speed/main-array.$(top_host).cmmi
	$(cmm)	-array "" "array" -l examples/speed/main-array.$(top_host).cmmi
	$(top_g++)	-O2 -o $@ examples/speed/main-array.$(top_host).cpp $(top_libs)
	#

examples/speed/main-vmap.$(top_host).exe:	examples/speed/main.cmm.ii-$(top_build2).cmm $(cmm)
	$(cmm) -c $<\
		examples/speed/main-vmap.$(top_host).cpp\
		examples/speed/main-vmap.$(top_host).cmmi
	$(cmm)	-map "" "vmap" -l examples/speed/main-vmap.$(top_host).cmmi
	$(top_g++)	-O2 -o $@ examples/speed/main-vmap.$(top_host).cpp $(top_libs)
	#

examples/speed/main-lokimap.$(top_host).exe:	examples/speed/main.cmm.ii-$(top_build2).cmm $(cmm)
	$(cmm) -c $<\
		examples/speed/main-lokimap.$(top_host).cpp\
		examples/speed/main-lokimap.$(top_host).cmmi
	$(cmm)	-map "#include \"../../loki\AssocVector.h\"" "Loki::AssocVector" \
			-l examples/speed/main-lokimap.cmmi
	$(top_g++)	-O2 -o $@ examples/speed/main-lokimap.$(top_host).cpp $(top_libs)
	#

examples/speed/main.$(top_host).exe:	examples/speed/main.cmm.ii-$(top_build2).cmm $(cmm)
	@echo calling cmm -c:
	$(cmm)	-c $< examples/speed/$(top_host).main.cpp examples/speed/main.$(top_host).cmmi
	@echo calling cmm -l:
	$(cmm)	-l examples/speed/main.$(top_host).cmmi
	@echo Linking
	$(top_g++)	-O2 -o $@ examples/speed/$(top_host).main.cpp $(top_libs)
	#

examples/speed/main-vc.$(top_host).exe:	examples/speed/main.cmm.ii-$(top_build2).cmm $(cmm)
	$(cmm)	-c $< examples/speed/main-vc.$(top_host).cpp examples/speed/main-vc.$(top_host).cmmi
	$(cmm)	-l examples/speed/main-vc.$(top_host).cmmi
	cl /ML /W3 /GB /GR /GX /O2 /Ob2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE"\
		/D "_MBCS" /Fe"examples/speed/main-vc.exe"\
		examples/speed/main-vc.$(top_host).cpp
	#

examples/speed/main-vc-vmap-array.$(top_host).exe:	examples/speed/main.cmm $(cmm)
	$(cmm)	-c $<\
		examples/speed/main-vc-vmap-array.$(top_host).cpp\
		examples/speed/main-vc-vmap-array.$(top_host).cmmi
	$(cmm)	-map "" "vmap" -array "" "array" -l\
		examples/speed/main-vc-vmap-array.$(top_host).cmmi
	cl /ML /W3 /GB /GR /GX /O2 /Ob2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE"\
		/D "_MBCS" /Fe"examples/speed/main-vc-vmap-array.exe"\
		examples/speed/main-vc-vmap-array.$(top_host).cpp
	#

speedtest-vmap-array:		examples/speed/main-vmap-array.$(top_host).exe;		./$< $@
speedtest-vmap:			examples/speed/main-vmap.$(top_host).exe;		./$< $@
speedtest-array:		examples/speed/main-array.$(top_host).exe;		./$< $@
speedtest-lokimap:		examples/speed/main-lokimap.$(top_host).exe;		./$< $@
speedtest:			examples/speed/main.$(top_host).exe;			./$< $@
speedtest-vc:			examples/speed/main-vc.$(top_host).exe;			./$< $@
speedtest-vc-vmap-array:	examples/speed/main-vc-vmap-array.$(top_host).exe;	./$< $@
speedtest-cmm-s:		examples/speed/main-s.$(top_build2).exe;	        ./$< $@
speedtest-cmm-ss:		examples/speed/main-ss.$(top_build2).exe;	        ./$< $@

speedtests:			$(cmm)\
                                speedtest-cmm-s\
                                speedtest-cmm-ss\

#                                speedtest\
                                speedtest-vmap\
                                speedtest-array\
                                speedtest-vmap-array
# the cmm -c/-l speed tests don't run properly. should probably remove them.


.PHONEY:	clean
clean::
	-rm	test*.$(top_host).* 2>/dev/null
	#

.PHONEY:	clean2
clean2:	clean
	-rm -r test* tmp.txt tmpdir gmon.* release.* website website.*\
		cmm/*.o cmm/*.d cmm/msvc/Release cmm/msvc/Debug\
		cmm.*.exe cmm-testbuilddir cmm-*\
		2>/dev/null
	#



# Targets/rules for various tests.
#

.PHONEY:	test
test:		test0-
	@echo
	@echo All tests for cmm executable $(cmm) succeeded
	@echo

test0-49:       test0-9 test10-19 test20-29 test30-39 test40-49
test50-99:      test50-59 test60-69 test70-79 test80-89 test90-99
test0-99:       test0-49 test50-99
test100-149:    test100-109 test110-119 test120-129
test100-199:    test100-149

test0-:         test0-99 test100-199
test10-:        test10-19 test20-
test20-:        test20-29 test30-
test30-:        test30-39 test40-
test40-:        test40-49 test50-
test50-:        test50-99 test100-
test60-:        test60-69 test70-
test70-:        test70-79 test80-
test80-:        test80-89 test90-
test90-:        test90-99 test100-
test100-:       test100-149
test110-:       test110-119 test120-
test120-:       test120-129 test130-
test130-:

test0-9:	test0 test1 test2 test3	test4 test5 test6 test7 test8 test9 test10;	@echo Finished $@
test10-19:	test10 test10 test11 test12 test13 test14 test15 test16 test17 test18;	@echo Finished $@
test20-29:	test20 test21 test22 test23 test24 test25 test26 test27 test28 test29;	@echo Finished $@
test30-39:	test30 test30 test31 test32 test33 test34 test35 test36 test37 test38;	@echo Finished $@
test40-49:	test40 test41 test42 test43 test44 test45 test46 test47 test48 test49;	@echo Finished $@
test50-59:	test50 test51 test52 test53 test54 test55 test56 test57 test58 test59;	@echo Finished $@
test60-69:	test60 test61 test62 test63 test64 test65 test66 test67 test68 test69;	@echo Finished $@
test70-79:	test70 test71 test72 test73 test74 test75 test76 test77 test78 test79;	@echo Finished $@
test80-89:	test80 test81 test82 test83 test84 test85 test86 test87 test88 test89;	@echo Finished $@
test90-99:	test90 test91 test92 test93 test94 test95 test96 test97 test98 test99;	@echo Finished $@
test100-109:	test100 test101 test102 test103 test104 test105 test106 test107 test108 test109;    @echo Finished $@
test110-119:	test110 test111 test112 test113 test114 test115 test116 test117 test118 test119;    @echo Finished $@
test120-129:    test120 test121 test122 test123

testdir:
	mkdir $@
	chmod a+w $@


test0:	$(cmm) testdir
	$(cmm) -parse "int main( int, char**) {}"
	@echo $@ succeeded

test1:	$(cmm) testdir
	$(cmm) -parse "template <class _Iter>	inline typename iterator_traits<_Iter>::iterator_category __ITERATOR_CATEGORY(const _Iter&) {}"
	$(cmm) -parse "namespace std { extern const unsigned char foo[]; }"
	@echo $@ succeeded

test2:	$(cmm) testdir
	$(cmm) -parse "basic_streambuf::basic_streambuf() : _M_gbegin(0), _M_gnext(0), _M_gend(0){}"
	@echo $@ succeeded

test3:	$(cmm) testdir
	$(cmm) -parse "template <class _Tp, class _Alloc>inline typename _Alloc_traits<_Tp, _Alloc>::allocator_type __stl_alloc_create(const _Alloc& __a, const _Tp*) {typedef typename _Alloc_traits<_Tp, _Alloc>::allocator_type _Rebound_type; return _Rebound_type(__a);}"
	@echo $@ succeeded

test4:	$(cmm) testdir
	$(cmm) -parse "namespace std { int* foo ();}"
	@echo $@ succeeded

# Next test checks that cmm can parse one of its own (preprocessed) source 
# files, and output identical text. Actually, it will output an extra first
# line `#line ...'.
test5:	testdir/test5.$(top_build2).cmm.ii-$(top_build2).cmm $(cmm) testdir
	$(cmm)	-c $< testdir/$@.$(top_build2).cpp testdir/$@.$(top_build2).cmmi -shownumnodes
	tail	+2 testdir/$@.$(top_build2).cpp > testdir/$@.$(top_build2).cpp-2
	diff	$< testdir/$@.$(top_build2).cpp-2 > testdir/$@.$(top_build2).diff
	rm	testdir/$@.$(top_build2).*
	@echo $@ succeeded
testdir/test5.$(top_build2).cmm: testdir
	echo "#include \"../cmm/main.cpp\"" > $@

test6:	$(cmm) testdir
	$(cmm) -parse "int (x)[5];"
	@echo $@ succeeded

test7:	$(cmm) testdir
	$(cmm) -parse "int (foo::* x)( int y);"
	@echo $@ succeeded
	@echo $@ succeeded

test8:	$(cmm) testdir
	echo "class foo { int x};" > testdir/$@.$(top_host).cmm
	$(cmm) -c testdir/$@.$(top_host).cmm testdir/$@.$(top_host).cpp testdir/$@.$(top_host).cmmi
	@echo $@ succeeded

test9:	$(cmm) testdir
	@echo $@ succeeded

test10:	$(cmm) testdir
	$(cmm) -parse "inline int foo( double x);"
	@echo $@ succeeded

test11:	$(cmm) testdir
	$(cmm) -parse "template< class T> basic< T>::basic() {}"
	@echo $@ succeeded

test12:	$(cmm) testdir
	$(cmm) -parse "inline foo::foo() {}"
	@echo $@ succeeded

test13: testdir
	# $@: Demonstration that gcc's -dD flag doesn't do the right thing
	# when a macro is #undefined.
	echo "#define A" > testdir/$@.$(top_host).cpp
	echo "#undef A" >> testdir/$@.$(top_host).cpp
	gcc -E -dD testdir/$@.$(top_host).cpp > testdir/$@.$(top_host)-2.cpp
	echo "#define A" >> testdir/$@.$(top_host)-2.cpp
	cat testdir/$@.$(top_host)-2.cpp
	@echo $@ succeeded

test14: testdir
	# $@: demonstration that gcc's -dM flag can be used to append
	# correct #defines to a preprocessed C++ file.
	echo "#define A" > testdir/$@.$(top_host).cpp
	echo "#undef A" >> testdir/$@.$(top_host).cpp
	gcc -E testdir/$@.$(top_host).cpp > testdir/$@.$(top_host)-2.cpp
	gcc -E -dM testdir/$@.$(top_host).cpp >> testdir/$@.$(top_host)-2.cpp
	echo "#define A" >> testdir/$@.$(top_host)-2.cpp
	cat testdir/$@.$(top_host)-2.cpp
	@echo $@ succeeded

test15:	$(cmm) testdir
	echo "struct bar {}; void foo( virtual bar& x); void foo( virtual bar& x); void foo_( static bar&); void foo_( static bar&);" > testdir/$@.$(top_host).cmmpp
	$(cmm) -c testdir/$@.$(top_host).cmmpp testdir/$@.$(top_host).cpp testdir/$@.$(top_host).cmmi
	@echo $@ succeeded

test16:	$(cmm) testdir
	$(cmm) -parse "extern wchar_t *wcscpy (wchar_t *__restrict __dest, wchar_t *__restrict __src);"
	@echo $@ succeeded

test17:	$(cmm) testdir
	$(cmm) -newdeclsyntax -parse "char* argv[];"
	$(cmm) -newdeclsyntax -parse "argv: []->char;"
	$(cmm) -newdeclsyntax -parse "void ( *signal( int sig, void (*func) ( int sig )) ) ( int sig );"
	$(cmm) -newdeclsyntax -parse "signal: ( sig: int, func:-> ( sig: int) void ) -> (sig:int) void;"
	@echo $@ succeeded

test18:	$(cmm) testdir
	$(cmm) -parse "int x[4][5];"
	@echo $@ succeeded

test19:	examples/withnamespace/main.cmm $(cmm) testdir
	mkdir -p examples/withnamespace
	$(cmm) -c $< examples/withnamespace/main.cmm.$(top_host).cpp examples/withnamespace/main.cmm.$(top_host).cmmi
	$(cmm) -l examples/withnamespace/main.cmm.$(top_host).cmmi
	@echo $@ succeeded

test20:	testdir/test20.$(top_host).cmm.ii-$(top_build2).cmm $(cmm) testdir
	$(cmm)	-errorlevel 000 -detailedparse -parsefile $<
	@echo $@ succeeded

.PRECIOUS:  testdir/test20.$(top_host).cmm.ii-$(top_build2).cmm
testdir/test20.$(top_host).cmm:	always testdir
	echo "#include \"../cmm/main.cpp\"" > $@
	@echo $@ succeeded

test21:	testdir/test21.cmm.ii-$(top_build2).cmm $(cmm) testdir
	$(cmm)	-detailedparse -c $< testdir/$@.$(top_host).cpp testdir/$@.$(top_host).cmmi -shownumnodes
	tail	+2 testdir/$@.$(top_host).cpp > testdir/$@.$(top_host).cpp-2
	diff	$< testdir/$@.$(top_host).cpp-2 > testdir/$@.$(top_host).diff
	rm	testdir/$@.*
	@echo $@ succeeded
testdir/test21.cmm: always testdir
	echo "#include \"../cmm/main.cpp\"" > $@
# Tests that cmm can parse its own preprocessed source, and output
# identical text (apart from initial #line).


test22:	$(cmm) testdir
	echo "namespace foo {" >testdir/$@.$(top_host).cmm
	echo "int main() {" >>testdir/$@.$(top_host).cmm
	echo "std::find( a, b, int bar(){ return 45;});" >>testdir/$@.$(top_host).cmm
	echo "}" >>testdir/$@.$(top_host).cmm
	echo "}" >>testdir/$@.$(top_host).cmm
	$(cmm) -detailedparse -embeddedfns -v -parsefile testdir/$@.$(top_host).cmm
	@echo $@ succeeded

test23:	$(cmm) testdir
	echo "namespace top { int main() { find( a, b, int bar(){ return 45;}); } }" >testdir/$@.$(top_host).cmm
	$(cmm) -detailedparse -embeddedfns -v -parsefile testdir/$@.$(top_host).cmm
	@echo $@ succeeded

test24:	$(cmm) testdir
	$(cmm) -parse "fpos_t (*seekfn)(void * _cookie, fpos_t _off, int _whence);"
	@echo $@ succeeded

test25:	$(cmm) testdir
	echo " difference_type* __distance_type (const _Iter& x){}" > testdir/$@.$(top_host).cpp
	$(cmm) -parsefile testdir/$@.$(top_host).cpp
	@echo $@ succeeded

test26:	$(cmm) testdir
	echo "inline mem_fun_t<_Ret,_Tp> mem_fun(_Ret (_Tp::*__f)());" > testdir/$@.$(top_host).cpp
	$(cmm) -parsefile testdir/$@.$(top_host).cpp
	@echo $@ succeeded

test27:	$(cmm) testdir
	$(cmm) -detailedparse -parse "reverse_bidirectional_iterator() {}"
	@echo $@ succeeded

test28:	examples/embeddedfns/simple.cmm $(cmm) testdir
	mkdir -p examples/embeddedfns
	$(cmm) -newdeclsyntax -detailedparse -embeddedfns -c $< examples/embeddedfns/simple.$@.$(top_build2).cpp ""
	g++ examples/embeddedfns/simple.$@.$(top_build2).cpp -o examples/embeddedfns/simple.$@.$(top_build2).exe
	./examples/embeddedfns/simple.$@.$(top_build2).exe
	@echo $@ succeeded

test29:	$(cmm) testdir
	$(cmm) -detailedparse -embeddedfns -v -parse "x=find ( a, b, int ()( int* x){});"		
	@echo $@ succeeded

test30:	examples/overlap/main.$(top_build2).exe $(cmm) testdir
	./$<
	@echo $@ succeeded

test31:	examples/overlap/main-s.$(top_build2).exe $(cmm) testdir
	./$<
	@echo $@ succeeded

test32:	examples/overlap2/main.$(top_build2).exe testdir
	./$<
	@echo $@ succeeded

test33:	$(cmm) testdir
	$(cmm) -detailedparse -checkindent -parse "operator _STL ::ostream* () const {}"
	@echo $@ succeeded

test34:	examples/autoblocks/main.cmm $(cmm) testdir
	$(cmm) -autoblocks -testlexer $<
	@echo $@ succeeded

test35:	examples/autoblocks/main.cmm $(cmm) testdir
	mkdir -p examples/autoblocks
	$(cmm) -autoblocks -c $< examples/autoblocks/main.$@.$(top_host).cpp ""
	g++ examples/autoblocks/main.$@.$(top_host).cpp -o examples/autoblocks/main.$@.$(top_host).exe
	examples/autoblocks/main.$@.$(top_host).exe
	@echo $@ succeeded
	
test36:	examples/autoblocks/main2.cmm $(cmm) testdir
	mkdir -p examples/autoblocks
	$(cmm) -newdeclsyntax -autoblocks -c $< examples/autoblocks/main2.$@.$(top_host).cpp ""
	g++ examples/autoblocks/main2.$@.$(top_host).cpp -o examples/autoblocks/main2.$@.$(top_host).exe
	examples/autoblocks/main2.$@.$(top_host).exe
	@echo $@ succeeded

test37: $(cmm) testdir
	echo ";" > testdir/$@.$(top_host).cmmpp
	echo "# 180 "/usr/include/sys/endian.h" 3 4" >> testdir/$@.$(top_host).cmmpp
	#echo "typedef	unsigned char	u_char;" >> testdir/$@.$(top_host).cmmpp
	$(cmm) -c testdir/$@.$(top_host).cmmpp testdir/$@.$(top_host).cpp ""
	tail +2 testdir/$@.$(top_host).cpp > testdir/$@.$(top_host).cpp-2
	diff testdir/$@.$(top_host).cmmpp testdir/$@.$(top_host).cpp-2
	@echo $@ succeeded

test38:	$(cmm) testdir
	$(cmm) -detailedparse -parse "static long double ( min) () { return (__extension__ ((union __convert_long_double) {__convert_long_double_i: {0x0, 0x80000000, 0x1, 0x0}}).__convert_long_double_d) ; }"
	@echo $@ succeeded

test39: $(cmm) testdir
	$(cmm) -parse "__extension__ extern template class smanip<int>;"
	@echo $@ succeeded

test40: $(cmm) testdir
	echo "for(;;){}" > testdir/$@.$(top_host).cmm
	$(cmm) -detailedparse -c testdir/$@.$(top_host).cmm testdir/$@.$(top_host).cpp testdir/$@.$(top_host).cmmi
	@echo $@ succeeded

test41: $(cmm) testdir
	echo "class numeric_limits<char> : public _Integer_limits<char, (-127), CHAR_MAX, -1, true> {};" > testdir/$@.$(top_host).cmm
	$(cmm)	-no-hash-line -detailedparse -c testdir/$@.$(top_host).cmm testdir/$@.$(top_host).cpp ""
	diff testdir/$@.$(top_host).cmm testdir/$@.$(top_host).cpp
	@echo $@ succeeded

test42: $(cmm) testdir
	echo "f: & const int;" > testdir/$@.$(top_host).cmm
	echo "const int & f;" > testdir/$@.$(top_host).cpp0
	$(cmm)	-newdeclsyntax -no-hash-line -detailedparse -c testdir/$@.$(top_host).cmm testdir/$@.$(top_host).cpp ""
	diff -b testdir/$@.$(top_host).cpp0 testdir/$@.$(top_host).cpp
	@echo $@ succeeded

test43: $(cmm) testdir
	$(cmm) -parse "extern \"C\" void* construct();"
	@echo $@ succeeded

test44: $(cmm) testdir
	$(cmm) -parse "//\nint x;"
	@echo $@ succeeded

test45: $(cmm) testdir
	$(cmm) -parse "int foo() volatile;"
	$(cmm) -parse "int foo() mutable;"
	$(cmm) -parse "int foo() mutable volatile const;"
	@echo $@ succeeded

test46: $(cmm) testdir
	# This test is a demonstration of cmm's different error-reporting
	# levels.
	-$(cmm) -parse "int foo() int;"
	-$(cmm) -errorlevel 000 -parse "int foo() int;"
	-$(cmm) -errorlevel 001 -parse "int foo() int;"
	-$(cmm) -errorlevel 010 -parse "int foo() int;"
	-$(cmm) -errorlevel 011 -parse "int foo() int;"
	-$(cmm) -errorlevel 100 -parse "int foo() int;"
	-$(cmm) -errorlevel 101 -parse "int foo() int;"
	-$(cmm) -errorlevel 110 -parse "int foo() int;"
	-$(cmm) -errorlevel 111 -parse "int foo() int;"
	@echo $@ succeeded

test47: $(cmm) testdir
	echo "int cmm_pragma_autoblocks_on;" >testdir/$@.$(top_host).cmm
	echo "int* cmm_pragma_autoblocks_on;" >>testdir/$@.$(top_host).cmm
	echo "int (*cmm_pragma_autoblocks_on)();" >>testdir/$@.$(top_host).cmm
	echo "int foo()" >>testdir/$@.$(top_host).cmm
	echo " int bar;" >>testdir/$@.$(top_host).cmm
	$(cmm) -verbose -c testdir/$@.$(top_host).cmm testdir/$@.$(top_host).cpp ""
	@echo $@ succeeded

test48: $(cmm) testdir
	echo > testdir/$@.$(top_host).cmm
	echo "struct	Item {};" >> testdir/$@.$(top_host).cmm
	echo "struct	DirItem : Item {};" >> testdir/$@.$(top_host).cmm
	echo "int&	MakeItemWidget( virtual Item& item);" >> testdir/$@.$(top_host).cmm
	echo "int&	MakeItemWidget_( static Item& item)" >> testdir/$@.$(top_host).cmm
	echo "{	GtkWidget*	widget = gtk_label_new( typeid( item).name());" >> testdir/$@.$(top_host).cmm
	echo "	gtk_widget_show( widget);" >> testdir/$@.$(top_host).cmm
	echo "	return widget;}" >> testdir/$@.$(top_host).cmm
	echo "int&	MakeItemWidget_( static DirItem& item)" >> testdir/$@.$(top_host).cmm
	echo "{	GtkWidget*	widget = gtk_label_new( item.dirent.d_name);" >> testdir/$@.$(top_host).cmm
	echo "	gtk_widget_show( widget);" >> testdir/$@.$(top_host).cmm
	echo "	return widget;}" >> testdir/$@.$(top_host).cmm
	$(cmm) -c testdir/$@.$(top_host).cmm testdir/$@.$(top_host).cpp ""
	@echo $@ succeeded
# This test doesn't fails properly - the generated file $@.cpp should be
# vaguely legal C++

test49: $(cmm) testdir
	echo "template <class T, size_t N> struct Traits<T[N]> : Traits<T*>"\
		"{ static size_t get_size(const T*) { return N - 1; }};"\
		>testdir/$@.$(top_host).cmm
	$(cmm) -detailedparse -c testdir/$@.$(top_host).cmm testdir/$@.$(top_host).cpp ""
	@echo $@ succeeded

test50: $(cmm) testdir
	echo "operator const char** () const;" > testdir/$@.$(top_host).cmm
	$(cmm) -detailedparse -c testdir/$@.$(top_host).cmm testdir/$@.$(top_host).cpp ""
	@echo $@ succeeded

test51: $(cmm) testdir
	echo > testdir/$@.$(top_host).cmm
	echo "enum"	>> testdir/$@.$(top_host).cmm
	echo " foo,"	>> testdir/$@.$(top_host).cmm
	echo " bar"	>> testdir/$@.$(top_host).cmm
	echo "abc;"	>> testdir/$@.$(top_host).cmm
	$(cmm) -v -errorlevel 111 -lexer-verbose -detailedparse -autoblocks -parsefile testdir/$@.$(top_host).cmm
	@echo $@ succeeded
 
# this test fails at the moment.
test52: $(cmm) testdir
	echo "foo( bar( a.b));" > testdir/$@.$(top_host).cmm
	$(cmm) -v -detailedparse -no-hash-line -c testdir/$@.$(top_host).cmm testdir/$@.$(top_host).cpp ""
	-diff testdir/$@.$(top_host).cmm testdir/$@.$(top_host).cpp
	@echo $@ succeeded
	
test53: examples/overlap/main.cmm $(cmm) testdir
	$(cmm) -c $< $<.$@.$(top_host).cpp $<.$@.$(top_host).cmmi
	$(cmm) -no-exceptions -l $<.$@.$(top_host).cmmi
	$(call top_g++_link,$<.$@.$(top_host).cpp,testdir/$@.$(top_host).exe,$(top_libs))
	$(top_gcc$(top_gcc_version)-link.$(top_host)) -o testdir/$@.$(top_host).exe $<.$@.$(top_host).cpp $(top_libs)
	if ./testdir/$@.$(top_host).exe; then\
	    echo "Error - executable didn't return error"; exit 1;\
	    else echo exe returned error, as it should; fi
	@echo $@ succeeded
# The executable should return an error.
	
test54: $(cmm) testdir
	echo > testdir/$@.$(top_host).cmm
	echo "#include <stdexcept>" >> testdir/$@.$(top_host).cmm
	echo "#include <iostream>" >> testdir/$@.$(top_host).cmm
	echo "int main()" >> testdir/$@.$(top_host).cmm
	echo '{ try { std::cerr << "started\\n"; throw std::runtime_error( "foo");} catch( exception& e) {bar();} return 0;}' >> testdir/$@.$(top_host).cmm
	$(cmm) -detailedparse -exceptions-exit -c testdir/$@.$(top_host).cmm testdir/$@.$(top_host).cpp ""
	-diff testdir/$@.$(top_host).cmm testdir/$@.$(top_host).cpp
	g++ -o testdir/$@-.exe testdir/$@.$(top_host).cpp
	if ./testdir/$@-.exe; then exit 1; fi
	@echo $@ succeeded

test55: $(cmm) testdir
	echo > testdir/$@.$(top_host).cmm
	echo "int foo(...);" >> testdir/$@.$(top_host).cmm
	$(cmm) -detailedparse -c testdir/$@.$(top_host).cmm testdir/$@.$(top_host).cpp ""

test56: $(cmm) testdir
	echo "struct	Item	{protected: Item() {} public: virtual ~Item() {;}};" > testdir/$@.$(top_host).cmm
	$(cmm) -newdeclsyntax -detailedparse -exceptions-exit -c testdir/$@.$(top_host).cmm testdir/$@.$(top_host).cpp ""
	@echo $@ succeeded
	
test57: testdir/test57.$(top_host).cpp.ii-$(top_build2).cpp $(cmm) testdir
	$(cmm) -exceptions-exit -detailedparse -newdeclsyntax -c $< testdir/$@.$(top_host).cpp ""
	$(call top_g++_compile,testdir/$@.$(top_host).cpp,test57.o)
	@echo $@ succeeded
# Tests that removing exception code produces valid c++.
	
testdir/test57.$(top_host).cpp:	always testdir
	@echo $@
	echo	> $@
	echo	'void cmm_pragma_autoblocks_off();' >> $@
	echo	'#include <memory>' >> $@
	echo	'void cmm_pragma_autoblocks_off();' >> $@
	echo	'int	main()' >> $@
	echo	'{	return 0;' >> $@
	echo	'}' >> $@
	@echo $@ succeeded
# this checks that when cmm removes cmm_pragma_ decls, it preserves
# preprocessor items and line feeds. If cmm fails, gcc will say something
# like:
#	/usr/include/g++/stl_config.h:149: #-lines for entering and leaving files don't match
#	/usr/include/g++/stl_algobase.h:34: #-lines for entering and leaving files don't match
#	/usr/include/g++/memory:16: #-lines for entering and leaving files don't match
#	test57.cpp:1: #-lines for entering and leaving files don't match
# Note that cmm will output bad code if the cmm_pragma_ decl spans more than
# one line - it assumes that the last item in the decl (`;' or `{...}')
# starts on the first line of the decl.

test58: $(cmm) testdir
	echo "int x;" > testdir/$@.$(top_host).cmm
	echo "#define foo bar" >> testdir/$@.$(top_host).cmm
	$(cmm) -c testdir/$@.$(top_host).cmm testdir/$@.$(top_host).cpp ""
	@echo $@ succeeded

# This fails - cmm thinks <...> is template params...
test59: $(cmm) testdir
	-$(cmm) -parse "if ( c<127 && c>2);"
	@echo Ignoring testdir/$@ failure - cmm thinks 'if ( c<127 && c>2);' is template params...
	@echo $@ succeeded

test60: $(cmm) testdir
	echo "void foo()" > testdir/$@.$(top_host).cpp
	echo "{" >> testdir/$@.$(top_host).cpp
	echo " asm (foo);" >>testdir/$@.$(top_host).cpp
	echo " asm (".byte 0xf0, 0x0f, 0xc1, 0x02" // lock; xaddl %eax, (%edx)" >> testdir/$@.$(top_host).cpp
	echo "    : "=a" (__val)" >> testdir/$@.$(top_host).cpp
	echo "    : "0" (-1), "m" (ref), "d" (&ref)" >> testdir/$@.$(top_host).cpp
	echo "    : "memory");" >> testdir/$@.$(top_host).cpp
	echo "}" >> testdir/$@.$(top_host).cpp
	$(cmm) -no-hash-line -c testdir/$@.$(top_host).cpp testdir/$@.$(top_host).cpp2 ""
	diff testdir/$@.$(top_host).cpp testdir/$@.$(top_host).cpp2
	@echo $@ succeeded

test61: cmm/lexer.cpp $(cmm) testdir
	$(cmm) -detailedparse -no-hash-line -c $< testdir/$@.$(top_host).cpp ""
	@echo $@ succeeded
#	diff $< $@.$(top_host).cpp
# disable the diff - cmm renames cmm_pragma_* identifiers.

test62: $(cmm) testdir
	echo "{" > testdir/$@.$(top_host).cpp
	echo " #if 1" >> testdir/$@.$(top_host).cpp
	echo " // FOO" >> testdir/$@.$(top_host).cpp
	echo " FILE* in2;" >> testdir/$@.$(top_host).cpp
	echo "}" >> testdir/$@.$(top_host).cpp
	$(cmm) -detailedparse -no-hash-line -c testdir/$@.$(top_host).cpp testdir/$@.$(top_host).cpp2 ""
	diff testdir/$@.$(top_host).cpp testdir/$@.$(top_host).cpp2
	@echo $@ succeeded

test63:	test63-in $(cmm) testdir
	$(cmm)	-detailedparse -no-hash-line -c $< testdir/$@.$(top_host).cpp /dev/null
	diff $< testdir/$@.$(top_host).cpp
	@echo $@ succeeded

test64:	$(cmm) testdir
	$(cmm)	-detailedparse -parse "u_int16_t bswap16(u_int16_t) __asm__("__bswap16");"
	@echo $@ succeeded
# this text occurs in netbsd/gcc's /usr/include/machine/byte_swap.h

test65:	$(cmm) testdir
	$(cmm)	-parse "typedef __signed char __int8_t;"
	@echo $@ succeeded
# this text occurs in netbsd/gcc


test66: $(cmm) testdir
	echo "Error_destroy(Ole_stream_close(+fo));" > testdir/$@.$(top_host).cpp
	$(cmm) -no-hash-line -c testdir/$@.$(top_host).cpp testdir/$@.$(top_host).cpp2 ""
	diff testdir/$@.$(top_host).cpp testdir/$@.$(top_host).cpp2
	@echo $@ succeeded

test67:	$(cmm) testdir
	echo > testdir/$@.$(top_host).c
	echo "sheet = NULL;" >>testdir/$@.$(top_host).c
	echo "style_cleanup:">>testdir/$@.$(top_host).c
	echo "if (tagsdict)" >>testdir/$@.$(top_host).c
	echo "Ustrdict_destroy(tagsdict);" >> testdir/$@.$(top_host).c
	$(cmm) -errorlevel 111 -parsefile testdir/$@.$(top_host).c
	@echo $@ succeeded
	
test68:	$(cmm) testdir
	echo 'z = (const bbb *)&c->d;' >testdir/$@.$(top_host).c
	$(cmm) -detailedparse -no-hash-line -c testdir/$@.$(top_host).c testdir/$@.$(top_host).c2 ""
	diff testdir/$@.$(top_host).c testdir/$@.$(top_host).c2
	@echo $@ succeeded

test69:	$(cmm) testdir
	echo 'assert(obj->data.operator < sizeof operators / sizeof operators[0]);' >testdir/$@.$(top_host).c
	$(cmm) -detailedparse -no-hash-line -c testdir/$@.$(top_host).c testdir/$@.$(top_host).c2 ""
	diff testdir/$@.$(top_host).c testdir/$@.$(top_host).c2

test70:	$(cmm) testdir
	echo 'if ((operators[obj->operator].valid) == 0)' >testdir/$@.$(top_host).c
	$(cmm) -no-c++ -detailedparse -no-hash-line -c testdir/$@.$(top_host).c testdir/$@.$(top_host).c2 ""
	diff testdir/$@.$(top_host).c testdir/$@.$(top_host).c2
	@echo $@ succeeded

test71:	$(cmm) testdir
	$(cmm) -parse 'extern inline int sigaddset(sigset_t *set, int signo);'
	@echo $@ succeeded

test72: $(cmm) testdir
	$(cmm) -lexer-verbose -detailedparse -parse "c = (0xFFFDU);"
	@echo $@ succeeded

test73: $(cmm) testdir
	echo "int cmm_pragma_autoblocks_on();" > testdir/$@.$(top_host).cpp
	echo "struct Item" >> testdir/$@.$(top_host).cpp
	echo "	protected:" >> testdir/$@.$(top_host).cpp
	echo "	Item() {}" >> testdir/$@.$(top_host).cpp
	echo "	public:" >> testdir/$@.$(top_host).cpp
	echo "	virtual ~Item()" >> testdir/$@.$(top_host).cpp
	echo "	  ;" >> testdir/$@.$(top_host).cpp
	echo ";" >> testdir/$@.$(top_host).cpp
	$(cmm) -detailedparse -newdeclsyntax -c testdir/$@.$(top_host).cpp testdir/$@.$(top_host).cpp2 ""
	$(cmm) -detailedparse -newdeclsyntax -c testdir/$@.$(top_host).cpp2 testdir/$@.$(top_host).cpp3 ""

test74: $(cmm) testdir
	echo > testdir/$@.$(top_host).cmm
	echo "struct Base { int x; };" >> testdir/$@.$(top_host).cmm
	echo "struct Derived : Base, Base0 { double y, &z; std::string (*foo)(int); };" >> testdir/$@.$(top_host).cmm
	echo "@cmm_memberrecursivefn void Serialise( Derived& x, XDR& xdr);" >> testdir/$@.$(top_host).cmm
	$(cmm) -detailedparse -c testdir/$@.$(top_host).cmm testdir/$@.$(top_host).cpp testdir/$@.$(top_host).cmmi -l testdir/$@.$(top_host).cmmi
	
test75: $(cmm) testdir
	echo > testdir/$@.$(top_host).cmm
	echo "struct Base { int x; };" >> testdir/$@.$(top_host).cmm
	echo "struct Derived : Base, Base0 { double y, &z; std::string (*foo)(int); };" >> testdir/$@.$(top_host).cmm
	echo "@cmm_memberrecursivefn void Serialise( virtual Base& x, XDR& xdr);" >> testdir/$@.$(top_host).cmm
	-$(cmm) -detailedparse -c testdir/$@.$(top_host).cmm testdir/$@.$(top_host).cpp testdir/$@.$(top_host).cmmi -l testdir/$@.$(top_host).cmmi
	@echo Ignoring testdir/$@ failure - not implemented cmm_memberrecursivefn multimethods yet.

test76: $(cmm) testdir
	echo '__asm__ __volatile__ ("lock; addl %0,%1" : : "ir" (__val), "m" (*__mem) : "memory");' > testdir/$@.$(top_host).cmm
	$(cmm) -detailedparse -c testdir/$@.$(top_host).cmm testdir/$@.$(top_host).cpp testdir/$@.$(top_host).cmmi
	@echo $@ succeeded

test77: $(cmm) testdir
	echo 'switch foo { int x; case 45: break; }' > testdir/$@.$(top_host).cmm
	$(cmm) -detailedparse -c testdir/$@.$(top_host).cmm testdir/$@.$(top_host).cpp testdir/$@.$(top_host).cmmi
	@echo $@ succeeded

test78:	$(cmm) testdir
	echo 'class foo { int x; int y;} __attribute__ ((__unused__));' > testdir/$@.$(top_host).cmm
	$(cmm) -detailedparse -c testdir/$@.$(top_host).cmm testdir/$@.$(top_host).cpp testdir/$@.$(top_host).cmmi
	tail +2 testdir/$@.$(top_host).cpp > testdir/$@.$(top_host).cpp-2
	diff testdir/$@.$(top_host).cmm testdir/$@.$(top_host).cpp-2
	rm testdir/$@.$(top_host).*
	@echo $@ succeeded

iiiitest79: examples/overlap/main-s.cmm $(cmm) testdir
	mkdir -p examples/overlap
	$(cmm) -s $< examples/overlap/main-s.cpp
	g++ -o examples/overlap/main-s.exe examples/overlap/main-s.cpp cmm/dispatch.cpp
	./examples/overlap/main-s.exe

test79:	examples/overlap/main-s.$(top_build2).exe testdir
	./$<
	@echo $@ succeeded

#$(call top_exe,examples/overlap/main-s,examples/overlap/main-s.cmm cmm/dispatch.cpp)
#$(call top_exe_include,examples/overlap/main-s.cmm cmm/dispatch.cpp)

test80:	examples/overlap2/overlap-s.$(top_build2).exe testdir
	./$<
	@echo $@ succeeded

test81: testdir
	:
	@echo $@ succeeded

test82: testdir/test82.$(top_host).cmm.ii-$(top_build2).cmm $(cmm)
	$(cmm) -detailedparse -exceptions-exit -s $< testdir/$@.$(top_host).cpp
testdir/test82.$(top_host).cmm:	testdir always
	@echo "std::auto_ptr< Set>	MakeOpenBSDFSSet();" > $@
	@echo "#include <list>" >> $@
#	@echo "typedef typename _Alloc_traits<_Tp, _Allocator>::allocator_type allocator;" > $@.$(top_host).cmm
	@echo $@ succeeded

test83:	$(cmm) testdir
	@echo > testdir/$@.$(top_host).cmm
	@echo "struct	base    {};" >> testdir/$@.$(top_host).cmm
	@echo "struct	derived :base    {};" >> testdir/$@.$(top_host).cmm
	@echo "void    foo( virtual const base& item, XDR& xdr);" >> testdir/$@.$(top_host).cmm
	@echo "void    foo_( static const derived& item, XDR& xdr) {}" >> testdir/$@.$(top_host).cmm
	$(cmm) -detailedparse -exceptions-exit -s testdir/$@.$(top_host).cmm testdir/$@.$(top_host).cpp
	if grep itemXDR testdir/$@.$(top_host).cpp ; then exit 1 ; fi
	@echo $@ succeeded

test84:	$(cmm) testdir testdir/test84.$(top_build2).exe testdir/test84.exit.$(top_build2).exe testdir/test84.fudge.$(top_build2).exe
	@echo running testdir/test84.$(top_build2).exe
	if ! ./testdir/test84.$(top_build2).exe ; then exit 0 ; else exit 1 ; fi
	@echo running testdir/test84.exit.$(top_build2).exe
	if ! ./testdir/test84.exit.$(top_build2).exe ; then exit 0 ; else exit 1 ; fi
	@echo running testdir/test84.fudge.$(top_build2).exe
	if ! ./testdir/test84.fudge.$(top_build2).exe ; then exit 0 ; else exit 1 ; fi
	@echo $@ succeeded
# all the test84 executables should return an error.

.PRECIOUS: test84.cmm.cpp test84.cmm.except-exit.cpp test84.cmm.except-fudge.cpp

testdir/test84.cmm.$(top_build2).cpp:	examples/excepttest.cmm.ii-$(top_build2).cmm $(cmm)
	@echo running cmm to create $@
	$(cmm)	-no-hash-line -detailedparse -s $< $@
testdir/test84.cmm.$(top_build2).except-exit.cpp:	examples/excepttest.cmm.ii-$(top_build2).cmm $(cmm)
	@echo running cmm to create $@
	$(cmm) -exceptions-exit -no-hash-line -detailedparse -s $< $@
testdir/test84.cmm.$(top_build2).except-fudge.cpp:	examples/excepttest.cmm.ii-$(top_build2).cmm $(cmm)
	@echo running cmm to create $@
	$(cmm) -exceptions-fudge -no-hash-line -detailedparse -s $< $@

$(call top_exe,testdir/test84,	    testdir/test84.cmm.$(top_build2).cpp              cmm/dispatch.cpp)
$(call top_exe,testdir/test84.exit, testdir/test84.cmm.$(top_build2).except-exit.cpp  cmm/dispatch.cpp)
$(call top_exe,testdir/test84.fudge,testdir/test84.cmm.$(top_build2).except-fudge.cpp cmm/dispatch.cpp)


test85: $(cmm) testdir
	echo > testdir/$@.$(top_host).cmm
	echo "typedef struct { int x; } mytype;" >> testdir/$@.$(top_host).cmm
	echo "struct mytype2 { int y; };" >> testdir/$@.$(top_host).cmm
	echo "@cmm_memberrecursivefn void foo( const mytype&);" >> testdir/$@.$(top_host).cmm
	echo "@cmm_memberrecursivefn void foo( const mytype2&);" >> testdir/$@.$(top_host).cmm
	$(cmm) -detailedparse -s testdir/$@.$(top_host).cmm testdir/$@.$(top_host).cpp
	@echo $@ succeeded

test86: $(cmm) testdir
	echo > testdir/$@.$(top_host).cmm
	echo "typedef struct { int* (*x)( double); } mytype;" >> testdir/$@.$(top_host).cmm
	echo "struct mytype2 { int y; };" >> testdir/$@.$(top_host).cmm
	echo "@cmm_memberreflectfn void foo( const mytype&);" >> testdir/$@.$(top_host).cmm
	$(cmm) -detailedparse -s testdir/$@.$(top_host).cmm testdir/$@.$(top_host).cpp
	@echo $@ succeeded

test87: $(cmm) testdir
	echo > testdir/$@.$(top_host).cmm
	echo "struct base1 {}; struct base2 {};" >> testdir/$@.$(top_host).cmm
	echo "struct derived : base1,base2 {};" >> testdir/$@.$(top_host).cmm
	echo "void foo( virtual base2& b);" >> testdir/$@.$(top_host).cmm
	echo "void foo_( static derived& d){}" >> testdir/$@.$(top_host).cmm
	$(cmm) -s testdir/$@.$(top_host).cmm testdir/$@.$(top_host).cpp
	@echo $@ succeeded

test88:	examples/overlap/main-s.$(top_build2).exe testdir
	./$<
	@echo $@ succeeded

test89:	examples/multiinherittest.$(top_build2).exe testdir
	./$<
	@echo $@ succeeded
	
test90:	$(cmm) testdir
	echo > testdir/$@.$(top_host).cmm
	echo "struct base {};" >> testdir/$@.$(top_host).cmm
	echo "void foo( virtual base& b);" >> testdir/$@.$(top_host).cmm
	echo "void bar( virtual base& b);" >> testdir/$@.$(top_host).cmm
	$(cmm) -s testdir/$@.$(top_host).cmm testdir/$@.$(top_host).cpp 2>testdir/$@.$(top_host).out
	-rm testdir/$@.$(top_host).out0
	touch testdir/$@.$(top_host).out0
	diff -I "set owner/group" testdir/$@.$(top_host).out0 testdir/$@.$(top_host).out
	@echo $@ succeeded
# Tests that no diagnostics are generated - specifically interested in
# warnings about duplicate virtual fn decls.

test91:	$(cmm) testdir
	echo > testdir/$@.$(top_host).cmm
	echo "struct base {};" >> testdir/$@.$(top_host).cmm
	echo "void foo( virtual base& b);" >> testdir/$@.$(top_host).cmm
	echo "void foo( virtual base& b);" >> testdir/$@.$(top_host).cmm
	$(cmm) -s testdir/$@.$(top_host).cmm testdir/$@.$(top_host).cpp 2>testdir/$@.$(top_host).out
	grep 'Found duplicate virtual function declaration' testdir/$@.$(top_host).out
	@echo $@ succeeded
# Checks that duplicate virtual fn decls are found.


test92: $(cmm) testdir
	@echo >testdir/$@.$(top_host).cmm
	echo "struct base {};" >> testdir/$@.$(top_host).cmm
	@echo "void foo( virtual base& x);">>testdir/$@.$(top_host).cmm
	@echo "void foo_( void* x);" >> testdir/$@.$(top_host).cmm
	$(cmm) -s testdir/$@.$(top_host).cmm testdir/$@.$(top_host).cpp
	@echo $@ succeeded
# checks that implementations with non-class derived types don't crash cmm.

test93:	testdir testdir/test93.$(top_build2).exe
	@echo $@ succeeded

testdir/test93.$(top_host).cmm:	$(cmm) testdir always
	@echo >$@
	echo "#include <typeinfo>">>$@
	echo "int main() {}" >> $@
	echo "struct base { int x; };" >> $@
	echo "struct derived : base {};" >> $@
	echo "void foo( const base&) {};" >> $@
	@echo "@cmm_memberrecursivefn void foo( const derived& x);">>$@
	@echo "void barcmm_reflect( const base&, const std::type_info&, const char*){}">>$@
	@echo "@cmm_memberreflectfn void bar( const derived& x);">>$@
	@echo "void foo_( void* x);" >> $@
# Checks that @cmm_memberrecursivefn inserts appropriate modifiers 
# when using static_cast< -base->( param).

test94: $(cmm) testdir
	echo >testdir/$@.$(top_host).cmm
	echo "void release () { gbl_FortifyMagic = 0, delete this; }" >> testdir/$@.$(top_host).cmm
	$(cmm) -no-hash-line -s testdir/$@.$(top_host).cmm testdir/$@.$(top_host).cpp
	diff testdir/$@.$(top_host).cpp testdir/$@.$(top_host).cpp
	@echo $@ succeeded

test95: $(cmm) testdir
	echo >testdir/$@.$(top_host).cmm
	echo "if ( ~ios_base::ate & ~ios_base::binary);">> testdir/$@.$(top_host).cmm
	$(cmm) -no-hash-line -s testdir/$@.$(top_host).cmm testdir/$@.$(top_host).cpp
	diff testdir/$@.$(top_host).cpp testdir/$@.$(top_host).cpp
	@echo $@ succeeded

test96:	$(cmm) testdir
	echo "if (__this->_M_mmap_len > 0x100000UL ) {}" > testdir/$@.$(top_host).cmm
	$(cmm) -no-hash-line -s testdir/$@.$(top_host).cmm testdir/$@.$(top_host).cpp
	diff testdir/$@.$(top_host).cpp testdir/$@.$(top_host).cpp
	@echo $@ succeeded
	
test97:	$(cmm) testdir
	echo 'wstring _S_empty_wstring(L"", L'');'>testdir/$@.$(top_host).cmm
	$(cmm) -no-hash-line -s testdir/$@.$(top_host).cmm testdir/$@.$(top_host).cpp
	diff testdir/$@.$(top_host).cpp testdir/$@.$(top_host).cpp
	@echo $@ succeeded

test98: $(cmm) testdir
	echo > testdir/$@.$(top_host).cmm
	echo 'struct cmm_small_integer_typeid {};' >> testdir/$@.$(top_host).cmm
	echo 'struct base1 {' >> testdir/$@.$(top_host).cmm
	echo '};' >> testdir/$@.$(top_host).cmm
	echo 'struct base2 : cmm_small_integer_typeid {' >> testdir/$@.$(top_host).cmm
	echo '};' >> testdir/$@.$(top_host).cmm
	echo 'void foo1( virtual base1& x);' >> testdir/$@.$(top_host).cmm
	echo 'void foo2( virtual base2& x);' >> testdir/$@.$(top_host).cmm
	$(cmm) -s testdir/$@.$(top_host).cmm testdir/$@.$(top_host).cpp
#	cat $@.$(top_host).cpp
	@echo $@ succeeded

test99:	$(cmm) testdir examples/speed/main2.$(top_build2).exe
	./examples/speed/main2.$(top_build2).exe
	@echo $@ succeeded
# tests lookup speed with no cache, typeids and small ints.

test99-:	examples/speed/main2.cmm.ii-$(top_build2).cmm $(cmm) testdir
	echo 'run -detailedparse -s $< $<.cmm-s.cpp' > testdir/$@.$(top_host).gdb
	gdb -command testdir/$@.$(top_host).gdb $(cmm)
	@echo $@ succeeded

test100:	speedtests testdir
	@echo $@ succeeded


test101:        $(cmm) testdir
	echo "int* foo( void) throw() __attribute__( sdf);" > testdir/$@.$(top_host).cmm
	$(cmm) -no-hash-line -lexer-verbose -s testdir/$@.$(top_host).cmm testdir/$@.$(top_host).cpp
	diff testdir/$@.$(top_host).cmm testdir/$@.$(top_host).cpp
	@echo $@ succeeded

test102:        $(cmm) testdir examples/impl_fnptr.$(top_build2).exe
	./examples/impl_fnptr.$(top_build2).exe
	@echo $@ succeeded


$(call top_exe,examples/impl_fnptr,examples/impl_fnptr.cmm cmm/dispatch.cpp)
-include $(call top_exe_include,examples/impl_fnptr.cmm)

$(call top_exe,examples/speed/main2,examples/speed/main2.cmm cmm/dispatch.cpp)
-include $(call top_exe_include,examples/speed/main2.cmm cmm/dispatch.cpp)

$(call top_exe,testdir/test93,testdir/test93.$(top_host).cmm)

$(call top_exe,examples/multiinherittest, examples/multiinherittest.cmm cmm/dispatch.cpp)


# test103 tests dynamic linking. there is a separate version of this test for
# when running under cygwin, which needs a slightly different shared-library
# build process because shared libraries under windows can't have unresolved
# symbols.
test103-app-src	:=	examples/dynamic-linking/application.cmm cmm/dispatch.cpp utils/exceptionstream.cpp utils/debug.cpp
test103-lib-src	:=	examples/dynamic-linking/sharedlib.cmm
test103-app     :=      examples/dynamic-linking/application.$(top_build2).exe
test103-lib     :=      examples/dynamic-linking/sharedlib.$(top_build).$(top_host).exe


ifeq ($(findstring CYGWIN,$(top_hostos)),)
$(call top_exe.$(top_build)-shared.$(top_host),examples/dynamic-linking/sharedlib,$(test103-lib-src))

$(call top_exe,examples/dynamic-linking/application,$(test103-app-src),,-rdynamic)
# gcc -rdynamic is required for slackware, to put exported symbols in the executable.

-include $(call top_exe_include.gcc$(top_gcc_version)-debug-shared.$(top_host),$(test103-lib-src) $(test103-app-src))

test103:        $(cmm) testdir $(test103-app) $(test103-lib)
	./$(test103-app) $(test103-lib)
	@echo $@ succeeded

else

define top_cygwin_dll
$(strip $1).$(top_build2).dll $(strip $1).$(top_build2).lib:	$(call top_src2objs_1,$2,$(top_build2)) $3;
	@echo calling dlltool for:
	@echo "    $1.$(top_build2).dll"
	@echo "    $1.$(top_build2).lib"
	dlltool --export-all-symbols\
		-e $(strip $1).$(top_build2).export.o\
		-l $(strip $1).$(top_build2).lib\
		-D $(strip $1).$(top_build2).dll\
		-z $(strip $1).$(top_build2).def\
		$$^
	@echo Linking to $(strip $1).$(top_build2).dll
	g++ -o $(strip $1).$(top_build2).dll -shared $$^ $(strip $1).$(top_build2).export.o
endef
# $1=base-name,
# $2=source files
# $3=extra objects
# generates windows DLL and stubs library for the specified source files.
# No support for automatically using different linker flags for
# different builds.
# this should probably be moved to common.mk at some stage.

$(call top_cygwin_dll,test103-dispatch,cmm/dispatch.cpp)
$(call top_cygwin_dll,test103,examples/dynamic-linking/sharedlib.cmm,test103-dispatch.$(top_build2).lib)
$(call top_exe,test103-cygwin,\
	examples/dynamic-linking/application.cmm utils/exceptionstream.cpp utils/debug.cpp\
	,test103.$(top_build2).lib test103-dispatch.$(top_build2).lib)
test103-cygwin:	test103-cygwin.$(top_build2).exe
	./$< test103.$(top_build2).dll
test103:    test103-cygwin
	@echo $@ succeeded
endif



$(call top_exe,examples/dynamic-linking/app-small,\
	examples/dynamic-linking/app-small.cmm cmm/dispatch.cpp,\
	,\
        -rdynamic)
-include (top_exe_include,examples/dynamic-linking/app-small.cmm)
test103-unix-small:	$(cmm) examples/dynamic-linking/app-small.$(top_build2).exe $(test103-lib)
	./examples/dynamic-linking/app-small.$(top_build2).exe $(test103-lib)
	@echo $@ succeeded

test104: $(cmm)
	echo >testdir/$@.$(top_build2).cmm
	echo '#include <iostream>' >> testdir/$@.$(top_build2).cmm
	echo '#include <typeinfo>' >> testdir/$@.$(top_build2).cmm
	echo 'void output( int x, std::ostream& out) { out << x; }' >> testdir/$@.$(top_build2).cmm
	echo 'void output( double x, std::ostream& out) { out << x; }' >> testdir/$@.$(top_build2).cmm
	echo 'template< class T> void outputcmm_reflect( T& x, const std::type_info& ti, const char* name, std::ostream& out)' >> testdir/$@.$(top_build2).cmm
	echo '{ out << name << "(" << ti.name() << ")="; output( x, out); out << "\n"; }' >> testdir/$@.$(top_build2).cmm
	echo 'struct foo { int x; double y; };' >>testdir/$@.$(top_build2).cmm
	echo '@cmm_memberreflectfn void output(  struct foo& f, std::ostream& out);' >>testdir/$@.$(top_build2).cmm
	echo 'int main() { foo f; output( f, std::cout);  return 0;}' >>testdir/$@.$(top_build2).cmm
	$(cmm) -detailedparse -s testdir/$@.$(top_build2).cmm testdir/$@.$(top_build2).cpp
	g++ -o testdir/$@.$(top_build2).exe -Wall testdir/$@.$(top_build2).cpp
	./testdir/$@.$(top_build2).exe
	@echo $@ succeeded
# tests that @cmm_memberreflectfn works for fn parameter `struct foo'.

ifeq ($(top_hostos),OpenBSD)
test105: examples/reflect.$(top_build2).exe $(cmm)
	./$<
	@echo $@ succeeded
# demonstrates use of @cmm_memberreflectfn, and tests that reflection
# works for `struct stat' and FILE. Specific to BSD's use of __sbuf
# in FILE struct.
else
test105:
	:
	@echo $@ succeeded
endif
$(call top_exe,examples/reflect,examples/reflect.cmm)
-include $(call top_exe_include,examples/reflect.cmm)


$(call top_exe,examples/overlap2/overlap-s,\
	examples/overlap2/factories.cmm\
	examples/overlap2/main.cmm\
	examples/overlap2/shapesquare.cmm\
	examples/overlap2/squareshape.cmm\
	examples/overlap2/squaretriangle.cmm\
	examples/overlap2/trianglesquare.cmm\
	cmm/dispatch.cpp\
	)


test106: $(cmm)
	echo >$@.$(top_build2).cmm
	echo "int foo() { if ( text[0]==0) return;" >>$@.$(top_build2).cmm
	echo "extern void cmm_pragma_detailedparse_off();" >>$@.$(top_build2).cmm
	echo "{  }" >>$@.$(top_build2).cmm
	echo "extern void cmm_pragma_detailedparse_on();" >>$@.$(top_build2).cmm
	echo "}" >> $@.$(top_build2).cmm
	$(cmm) -detailedparse -parsefile $@.$(top_build2).cmm
	@echo $@ succeeded


test107:    examples/caller-dispatch.$(top_build2).exe
	./$<
	@echo $@ succeeded
test107_src :=\
    examples/caller-dispatch.cmm.ii-$(top_build2).cmm.cmm-sc.cpp\
    cmm/dispatch.cpp\

$(call top_exe,examples/caller-dispatch,$(test107_src))
-include $(call top_exe_include,$(test107_src))\
    examples/caller-dispatch.cmm.ii-$(top_build2).cmm.d

%.cmm.cmm-sc.cpp:	%.cmm $(top_cmm_prerequisite)
	@echo cmm -caller-dispatch: $@
	$(top_cmm) -caller-dispatch -detailedparse -s $< $@
# test for caller-dispatch.


test108:    $(cmm)
	echo "struct { unsigned int f :(8 / 2) * (16 - (int)sizeof(struct timespec));};" > $@.$(top_build2)
	echo "struct { unsigned int   :(8 / 2) * (16 - (int)sizeof(struct timespec));};" > $@.$(top_build2)
	$(cmm) -detailedparse -s $@.$(top_build2) $@.$(top_build2)2
	tail +2 $@.$(top_build2)2 >$@.$(top_build2)3
	diff $@.$(top_build2) $@.$(top_build2)3
	@echo $@ succeeded

test109:    $(cmm)
	echo >$@.$(top_build2).1.cmm
	echo "struct base{};" >>$@.$(top_build2).1.cmm
	echo "struct base2{};" >>$@.$(top_build2).1.cmm
	echo "void foo( virtual const base&);" >>$@.$(top_build2).1.cmm
	echo "void foo_( static base&){ abcdef;}" >>$@.$(top_build2).1.cmm
	$(cmm) -s $@.$(top_build2).1.cmm $@.$(top_build2).1.cpp 2>&1 | tee $@.$(top_build2).1.out
	if ! grep -i "cmm: Error" $@.$(top_build2).1.out >/dev/null; then\
	    echo cmm should have given warning because fn looks like implementation with wrong const ;\
	    exit 1 ;\
	    fi
	@echo $@ succeeded

test110:    $(cmm)
	$(cmm) -s examples/template-virtual.cmm examples/template-virtual.cmm.$@.$(top_build2).cmm-s.cpp
	@echo $@ succeeded
test111:    test111.$(top_build2).exe
	./$<
	@echo $@ succeeded

test112:    $(cmm)
	$(cmm) -detailedparse -parse "if ( i < 2 || __sign.size() > 1)  {}"
	@echo $@ succeeded

test113:    $(cmm)
	echo >$@.$(top_build2).1.cmm
	echo "struct base{};" >>$@.$(top_build2).1.cmm
	echo "struct base2{};" >>$@.$(top_build2).1.cmm
	echo "void foo( virtual base&);" >>$@.$(top_build2).1.cmm
	echo "void foo_( base&){ abcdef;}" >>$@.$(top_build2).1.cmm
	$(cmm) -s $@.$(top_build2).1.cmm $@.$(top_build2).1.cpp 2>&1 | tee $@.$(top_build2).1.out
	if ! grep -i "cmm: warning" $@.$(top_build2).1.out >/dev/null; then\
	    echo "cmm should have given warning because fn looks like implementation but with no static parameter prefix" ;\
	    exit 1 ;\
	    fi
	@echo $@ succeeded

test114:    $(cmm)
	echo >$@.$(top_build2).1.cmm
	echo "struct base{};" >>$@.$(top_build2).1.cmm
	echo "struct base2{};" >>$@.$(top_build2).1.cmm
	echo "void foo_( virtual base&){ abcdef;}" >>$@.$(top_build2).1.cmm
	$(cmm) -s $@.$(top_build2).1.cmm $@.$(top_build2).1.cpp 2>&1 | tee $@.$(top_build2).1.out
	if ! grep -i "cmm: warning" $@.$(top_build2).1.out >/dev/null; then\
	    echo "cmm should have given warning because fn looks like implementation but with no static parameter prefix" ;\
	    exit 1 ;\
	    fi
	@echo $@ succeeded

test115:    $(cmm)
	echo >$@.$(top_build2).1.cmm
	echo "struct base{};" >>$@.$(top_build2).1.cmm
	echo "void foo_( static base&){}" >>$@.$(top_build2).1.cmm
	$(cmm) -s $@.$(top_build2).1.cmm $@.$(top_build2).1.cpp 2>&1 | tee $@.$(top_build2).1.out
	if ! grep -i "cmm: Error" $@.$(top_build2).1.out >/dev/null; then\
	    echo "cmm should have given warning because fn looks like implementation but with no prior multimethod decl" ;\
	    exit 1 ;\
	    fi
	@echo $@ succeeded

$(call top_exe,test111,examples/template-virtual.cmm cmm/dispatch.cpp)


test116:    $(cmm)
	echo >$@.$(top_build2).cmm
	echo "struct base {};" >>$@.$(top_build2).cmm
	echo "void foo( shared_ptr< virtual base> x);" >>$@.$(top_build2).cmm
	echo "void foo_( static base& x){}" >>$@.$(top_build2).cmm
	$(cmm) -detailedparse -s $@.$(top_build2).cmm $@.$(top_build2).cpp 2>&1 |tee $@.$(top_build2).out
	if ! grep -i "cmm: Error" $@.$(top_build2).out >/dev/null; then\
	    echo "cmm should have given warning because fn looks like implementation but with differnet prior multimethod decl" ;\
	    exit 1 ;\
	    fi
	@echo $@ succeeded


test117:    $(cmm)
	echo >$@.$(top_build2).cmm
	echo "struct base {};" >> $@.$(top_build2).cmm
	echo "struct derived : derived {};" >> $@.$(top_build2).cmm
	echo "void fn( virtual base& x);" >> $@.$(top_build2).cmm
	echo "void fn_( static derived& y){}" >> $@.$(top_build2).cmm
	$(cmm) -detailedparse -s $@.$(top_build2).cmm $@.$(top_build2).cpp 2>&1 |tee $@.$(top_build2).out
	if ! grep -i "cmm: Error" $@.$(top_build2).out >/dev/null; then\
	    echo "cmm should have given warning because fn looks like implementation but with differnet prior multimethod decl" ;\
	    exit 1 ;\
	    fi
	@echo $@ succeeded
# this causes cmm version 0.25 to go into infinite loop.

test118:    $(cmm)
	echo >$@.$(top_build2).cmm
	echo "struct base :base {};" >> $@.$(top_build2).cmm
	echo "void fn( virtual base& x);" >> $@.$(top_build2).cmm
	echo "void fn_( static base& y){}" >> $@.$(top_build2).cmm
	$(cmm) -detailedparse -s $@.$(top_build2).cmm $@.$(top_build2).cpp
	@echo $@ succeeded
# this causes cmm version 0.25 to go into infinite loop.

test119:    testdir/test119.$(top_build2).exe
	./$< |tee testdir/$@.$(top_build2).out
	if ! grep 1006 testdir/$@.$(top_build2).out ;\
	    then echo failed, line number should be 1006; exit 1;\
	    else echo success, found correct line number 1006; exit 0;\
	fi
	@echo $@ succeeded
# checks that cmm's _get_impl function is generated with no newline
# characters, so line numbers that the compiler sees are correct.


testdir/test119.$(top_build2).cpp:    always $(cmm)
	@echo making $@.cmm
	echo >$@.cmm
	echo "#include <stdio.h>"                       >>$@.cmm
	echo "struct base {};"                          >>$@.cmm
	echo "void foo("                                >>$@.cmm
	echo "        virtual base& x"                  >>$@.cmm
	echo "        );"                               >>$@.cmm
	echo "#line 1000"                               >>$@.cmm
	echo "void foo_("                               >>$@.cmm
	echo "        static base& x"                   >>$@.cmm
	echo "        )"                                >>$@.cmm
	echo "{ (void) x;"                              >>$@.cmm
	echo "}"                                        >>$@.cmm
	echo "int main(){"                              >>$@.cmm
	echo "  printf( \"line %i\\\\n\", __LINE__);"   >>$@.cmm
	echo "  return 0;}"                             >>$@.cmm
	@echo calling cmm manually to make $@
	$(cmm) -s $@.cmm $@
	@echo $@ succeeded
	
$(call top_exe,testdir/test119,testdir/test119.$(top_build2).cpp cmm/dispatch.cpp)


test120:    testdir/test120.$(top_build2).exe
	./$<
	@echo $@ succeeded

testdir/test120.$(top_build2).cmm:  $(cmm)
	echo >$@
	echo '#include <iostream>' >>$@
	echo 'int sum( int x)' >>$@
	echo '{' >>$@
	echo '  if ( x<=1) return x;' >>$@
	echo '  return x + sum( x-1);' >>$@
	echo '}' >>$@
	echo 'int main()' >>$@
	echo '{' >>$@
	echo '  std::cout << sum( 7) << "\\n";' >>$@
	echo '  return 0;' >>$@
	echo '}' >>$@
testdir/test120.$(top_build2).cpp:  testdir/test120.$(top_build2).cmm $(cmm)
	$(cmm) -block 'std::cout << __FILE__ << ": " << __LINE__ << "\n"; '\
	    -s $< $@
	
$(call top_exe,testdir/test120,testdir/test120.$(top_build2).cpp)
-include $(call top_exe_include,testdir/test120.$(top_build2).cpp)



test121:    $(cmm) testdir/test121.$(top_build2).exe
	@echo $@ succeeded

# tests that virtual parameter types can be used from virtual fns in different namespaces.
testdir/test121.$(top_build2).cmm: top_always
	echo >$@
	echo 'namespace bar { struct base { virtual ~base(){};};}' >>$@
	echo 'namespace foo { void fn( virtual bar::base& b);}' >>$@
	echo 'namespace foo { void fn_( static bar::base& b){}}' >>$@
	echo 'int main(){ return 0;}' >>$@
$(call top_exe,testdir/test121,testdir/test121.$(top_build2).cmm cmm/dispatch.cpp)
-include $(call top_exe_include,testdir/test121.$(top_build2).cmm)


test122:    $(cmm)
	@echo >testdir/$@.$(top_build2).cmm
	@echo "struct base {}; struct derived : base{};"    >> testdir/$@.$(top_build2).cmm
	@echo "void cmm_pragma_newdeclsyntax_on();"         >> testdir/$@.$(top_build2).cmm
	@echo "foo: ( virtual x: &base) void;"              >> testdir/$@.$(top_build2).cmm
	@echo "foo_: ( static x: &derived) void {}"         >> testdir/$@.$(top_build2).cmm
	$(cmm) -s testdir/$@.$(top_build2).cmm testdir/$@.$(top_build2).cmm.cpp
	@echo $@ succeeded

test123:    $(cmm)
	@echo >testdir/$@.$(top_build2).cmm
	@echo "struct base {};"                                 >>testdir/$@.$(top_build2).cmm
	@echo "struct derived : base {};"                       >>testdir/$@.$(top_build2).cmm
	@echo "void foo( virtual base& a, virtual base& b);"    >>testdir/$@.$(top_build2).cmm
	@echo "void foo_( static derived& a, static base& b)"   >>testdir/$@.$(top_build2).cmm
	@echo "{}"                                              >>testdir/$@.$(top_build2).cmm
	$(cmm) -s testdir/$@.$(top_build2).cmm testdir/$@.$(top_build2).cpp
# test diagnostics when base class is not defined.


$(call top_exe,examples/template-multimethod,examples/template-multimethod.cmm)
-include $(call top_exe_include,examples/template-multimethod.cmm)


test124:    $(cmm)
	echo "void bar( void) {}" >> testdir/$@.$(top_build2).cmm
	$(cmm) -caller-dispatch -detailedparse\
            -s\
                testdir/$@.$(top_build2).cmm\
                testdir/$@.$(top_build2).cpp
	cat testdir/$@.$(top_build2).cpp


# profiling tests:

testprof1:
	$(MAKE) testdir/test20.$(top_host).cmm.ii-gcc$(top_gcc_version)-release.$(top_host).cmm top_build=gcc-release
	$(MAKE) cmm.gcc$(top_gcc_version)-release-hierprof.$(top_host).exe top_build=gcc-release-hierprof -s
	@echo running cmm.gcc$(top_gcc_version)-release-hierprof.$(top_host).exe...
	time ./cmm.gcc$(top_gcc_version)-release-hierprof.$(top_host).exe\
            -errorlevel 000\
            -detailedparse\
            -parsefile testdir/test20.$(top_host).cmm.ii-gcc$(top_gcc_version)-release.$(top_host).cmm\
            -shownumnodes\
            2> testdir/hp-out.gcc$(top_gcc_version)-release-hierprof.$(top_host)
	time ./cmm.gcc$(top_gcc_version)-release.$(top_host).exe\
            -errorlevel 000\
            -detailedparse \
            -parsefile testdir/test20.$(top_host).cmm.ii-gcc$(top_gcc_version)-release.$(top_host).cmm\
            -shownumnodes

testprof2:  testdir/test20.$(top_host).cmm.ii-gcc2.95.3-debug.$(top_host).cmm
	$(MAKE) cmm.gcc$(top_gcc_version)-releaseO3.$(top_host).exe top_build=gcc-releaseO3 -s
	time ./cmm.gcc$(top_gcc_version)-releaseO3.$(top_host).exe\
            -errorlevel 000\
            -detailedparse \
            -profil testdir/$@.$(top_build2).prof-out\
            -parsefile $<\
            -shownumnodes
	@echo profil stats are in testdir/$@.$(top_build2).prof-out


testprof3:
	$(MAKE) testdir/test20.$(top_host).cmm.ii-gcc$(top_gcc_version)-release.$(top_host).cmm top_build=gcc-release
	$(MAKE) cmm.gcc$(top_gcc_version)-releaseO3-hierprof.$(top_host).exe top_build=gcc-releaseO3-hierprof -s
	@echo running cmm.gcc$(top_gcc_version)-releaseO3-hierprof.$(top_host).exe...
	time ./cmm.gcc$(top_gcc_version)-releaseO3-hierprof.$(top_host).exe\
            -errorlevel 000\
            -detailedparse\
            -parsefile testdir/test20.$(top_host).cmm.ii-gcc$(top_gcc_version)-release.$(top_host).cmm\
            -shownumnodes\
            2> testdir/hp-out.gcc$(top_gcc_version)-releaseO3-hierprof.$(top_host)

testprof4:
	$(MAKE) test20 top_build=gcc-releaseO3

testprof5:  cmm.$(top_build2).exe
	$(MAKE) testdir/test20.$(top_host).cmm.ii-gcc$(top_gcc_version)-release.$(top_host).cmm top_build=gcc-release
	./cmm.$(top_build2).exe\
            -errorlevel 000\
            -detailedparse \
            -profil testdir/$@.$(top_build2).prof-out\
            -parsefile testdir/test20.$(top_host).cmm.ii-gcc$(top_gcc_version)-release.$(top_host).cmm\
            -shownumnodes
	@echo profil stats are in testdir/$@.$(top_build2).prof-out
            



examples/overlap/main.cppextra: examples/overlap/main.cmm.cmm-s.cpp
	-diff\
            examples/overlap/main.cmm\
            examples/overlap/main.cmm.cmm-s.cpp\
            >$@

	

top_objs2iis =\
    $(patsubst\
        %.cpp.$(top_build2).o,\
        %.cpp.ii-$(top_build2).cpp,\
        $(filter %.cpp.$(top_build2).o,$1))\
    $(patsubst\
        %.c.$(top_build2).o,\
        %.c.ii-$(top_build2).c,\
        $(filter %.cpp.$(top_build2).o,$1))\

top_srcs2objs2iis = $(call top_objs2iis, $(call top_srcs2objs, $1))

define top_exe_ii
$(strip $1).$(top_build2).ii:   $(call top_srcs2objs2iis,$2);
	@echo $^
	@echo $2
endef

%.ii:   %.$(top_build2).ii

$(call top_exe_ii,cmm,$(top_cmm_src))

# Things for cmm2

buildcmm2:	always
	@echo "Making cmm (gcc-release build)"
	$(MAKE) -f makefile top_build=gcc-release -s
	@echo "Making cmm2 (gcc-release build)"
	$(MAKE) top_srcdir= -C cmm2 cmm2 top_build=gcc-release -s
	




# Next bit is for making releases of Cmm.

# Things for building file containing text from cmm -h.

cmmhelp.text:	always $(top_cmm)
	@echo Making $@
	$(top_cmm) -h >$@

top_version :=0.28

ifneq ($(findstring openbsd,$(OSTYPE)),)
tar	:=	gtar
else
tar	:=	tar
endif


filelist0	:=\
	building.html\
	cmm/msvc/cmm.dsp\
	cmm/msvc/cmm.dsw\
	cmm/readme.html\
	cmm/cmm.cpp\
        cmm/cmm-thirdpass.cpp\
	cmm/cmm.h\
	cmm/dispatch.cpp\
	cmm/dispatch.h\
	cmm/lexer.cpp\
	cmm/lexer.h\
	cmm/lexer.h\
        cmm/lookup.cpp\
	cmm/main.cpp\
	cmm/parser.cpp\
	cmm/parser.h\
	cmm/parser2.cpp\
	cmm/parser2.h\
	cmm/parser-nodemethods.cpp\
	cmm2/exceptionstream.cmm\
	cmm2/lexer.cmm\
	cmm2/main.cmm\
	cmm2/makefile\
	cmm2/parser.cmm\
	cmm2/parser2.cmm\
	cmm2/parser-nodemethods.cmm\
	cmm2/readme.html\
	cmmhelp.text\
	common.mk\
	examples/autoblocks/main2.cmm\
	examples/autoblocks/main.cmm\
	examples/caller-dispatch.cmm\
        examples/dynamic-linking/application.cmm\
	examples/dynamic-linking/application.h\
	examples/dynamic-linking/sharedlib.cmm\
	examples/embeddedfns/simple.cmm\
	examples/excepttest.cmm\
	examples/impl_fnptr.cmm\
	examples/multiinherittest.cmm\
	examples/overlap/main-s.cmm\
	examples/overlap/main.cmm\
        examples/overlap/main.cppextra\
	examples/overlap2/factories.cmm\
	examples/overlap2/trianglesquare.cmm\
	examples/overlap2/squaretriangle.cmm\
	examples/overlap2/main.cmm\
	examples/overlap2/squareshape.cmm\
	examples/overlap2/shapesquare.cmm\
	examples/overlap2/triangle.h\
	examples/overlap2/square.h\
	examples/overlap2/shape.h\
	examples/overlap2/factories.h\
	examples/reflect.cmm\
        examples/speed/main.cmm\
	examples/speed/main2.cmm\
        examples/template-virtual.cmm\
	examples/withnamespace/main.cmm\
	history.html\
	makefile\
	otherextensions.html\
	readme.html\
	test63-in\
	users.html\
        utils/debug.cpp\
        utils/debug.h\
        utils/debug-internal.h\
	utils/tempfile.cpp\
	utils/tempfile.h\
        utils/commandbuf.cpp\
        utils/commandbuf.h\
        utils/exceptionstream.cpp\
        utils/exceptionstream.h\
        utils/profil.cpp\
        utils/profil.h\
        utils/regexwrap.cpp\
        utils/regexwrap.h\


filelist	:=	$(patsubst %,cmm-$(top_version)/%,$(filelist0))

cmm-$(top_version)/%:	%
	mkdir -p $(dir $@)
	cp -p $< $@

test-cvs:	always
	cvs -nq update $(patsubst cmm-$(top_version)/%,%,$(filelist))

cmm-$(top_version)-files:	$(filelist)

	
cmm-$(top_version)-rm:	always
	@echo removing any existing cmm-$(top_version) directory
	-rm -rf cmm-$(top_version)

cmm-$(top_version):	always htmlcontents cmm-$(top_version)-rm cmm-$(top_version)-files
	chmod -R a+rw $@
	@echo have made $@
# Makes a directory cmm-<version> populated with all files in the release.
# copy specific .cpp files because other .cpp files can be created sometimes...

cmm-$(top_version)-website:	cmm-$(top_version) cmm-$(top_version).tar.gz\
				cmmhelp.text\
				always
	@echo Making $@
	-rm -r $@
	cp -pr $< $@
	cp -p cmm-$(top_version).tar.gz $@
	cp -p cmmhelp.text $@
# The website is the release plus a couple of extra files, including the release tar.gz
# file itself.

htmlcontents    := htmlcontents
.PHONEY:	htmlcontents
htmlcontents:   top_always
	$(htmlcontents) -s building.html
	$(htmlcontents) -s cmm2/readme.html
	$(htmlcontents) -s otherextensions.html
	$(htmlcontents) -s users.html
	$(htmlcontents) -s cmm/readme.html
	$(htmlcontents) -s history.html
	$(htmlcontents) -s readme.html
# updates contents of all .html files.

%.tar.gz:	%	always
	@echo Creating $@ from $<
	$(tar)	-czf $@ $<
	ls -l $@
# Generic rule for creating tar.gz file from a directory.

.PHONEY:	release
release:	cmm-$(top_version).tar.gz

.PHONEY:	website
website:	cmm-$(top_version)-website.tar.gz


.PHONEY:	testallbuilds
testallbuilds:
	@echo $@
	@echo Testing gcc-release build
	$(MAKE) -f makefile test top_build=gcc-release
	@echo Testing gcc-debug build
	$(MAKE) -f makefile test top_build=gcc-debug
	echo All tests for all builds passed.

testallbuilds-egcc:
	@echo Testing gcc-release build egcc
	$(MAKE) -f makefile test top_build=gcc-release top_gcc=egcc top_g++:=eg++
	@echo Testing gcc-debug build egcc
	$(MAKE) -f makefile test top_build=gcc-debug top_gcc=egcc top_g++:=eg++
	echo All tests for all egcc builds passed.
# this is openbsd-specific. uses egcc/eg++ as name of compiler to test
# things with latest gcc (currently version 3.2).

testrelease-%:  cmm-$(top_version).tar.gz
	@echo $@
	-rm -r testrelease
	mkdir testrelease
	cd testrelease; $(tar) -xzf ../$<
	@echo running make $* in $@ - this may take a few minutes.
	cd testrelease/cmm-* && echo running make in `pwd` && $(MAKE) $* top_srcdir=
# This takes the release tar.gz file, unpacks it into temp dir, and runs
# make in the temp dir.
#   make testrelease-test
# - is a useful sanity check that everything works.

testrelease:	checkversion testrelease-test always cmm-$(top_version)-website
	@echo Release archive tests passed for all builds.
	@echo Release website is $(CURDIR)/cmm-$(top_version)-website
# As testrelease-test, but also creates the website directory and checks
# the version. Is used to make a final release.


.PHONEY:	always
always:


top_versionfiles	:=	readme.html\
				cmm/main.cpp\
				makefile\

versionnumbers:	always $(top_versionfiles)
	echo >$@
	cat $(top_versionfiles) | grep "cmm-[0-9]\.[0-9][0-9]" >>$@
	cat $(top_versionfiles) | grep "cmm [0-9]\.[0-9][0-9]" >>$@
	cat $(top_versionfiles) | grep "Cmm [0-9]\.[0-9][0-9]" >>$@
# Extracts all version numbers from specified files into
# `versionnumbers'
versionnumber:	always $(top_versionfiles)
	echo >$@
	cat $(top_versionfiles) | grep "cmm-$(top_version)" >>$@
	cat $(top_versionfiles) | grep "cmm $(top_version)" >>$@
	cat $(top_versionfiles) | grep "Cmm $(top_version)" >>$@
# Extracts all correct version numbers from specified files
# into `versionnumber'.

checkversion:	versionnumbers versionnumber
	@echo Checking version numbers
	diff $^
	grep "$(top_version)" history.html
	cat versionnumber | wc -l > $@.l1
	echo " 4 " >$@.l2
	if ! diff -b $@.l1 $@.l2 ; then\
	    echo number of occurrencies of version number is incorrect.\
	    diff versionnumber versionnumbers ; exit 1;\
	else exit 0 ; fi
	@echo Version numbers are correct.
# Checks that particular files contain $(top_version), and not any other
# version numbers. Also checks that history.html contains $(top_version),
# allowing other version numbers.
# Finally checks the number of lines containing the version number.
# Hopefully these rather messy tests are sufficient to prevent releases
# with incorrect documentation.

