note *
note * This build script is in an extended Make Abstract Machine (MAM)
note * language. Documentation is at: src/cmd/INIT/README-mamake.md
note *

setv MAMAKE_STRICT 3
setv INSTALLROOT ../../..
setv PACKAGE_ast_INCLUDE ${INSTALLROOT}/include/ast
setv CC cc
setv AR ${mam_cc_AR} ${mam_cc_AR_ARFLAGS}
setv mam_cc_FLAGS ${mam_cc_TARGET} ${mam_cc_DLL} ${mam_cc_PIC} ${-debug-symbols?1?${mam_cc_DEBUG} -D_BLD_DEBUG?${mam_cc_OPTIMIZE}?}
setv CCFLAGS
setv IFFEFLAGS
setv LDFLAGS

make install virtual
	note *
	note * AST header dependencies
	note * TODO: verify and centralise
	note *

	make ${PACKAGE_ast_INCLUDE}/endian.h implicit
		make ${PACKAGE_ast_INCLUDE}/bytesex.h implicit
			make ${PACKAGE_ast_INCLUDE}/ast_common.h implicit
				prev ${PACKAGE_ast_INCLUDE}/ast_map.h implicit
			done
		done
	done
	make ${PACKAGE_ast_INCLUDE}/fnv.h implicit
		prev ${PACKAGE_ast_INCLUDE}/ast_common.h
	done
	make ${PACKAGE_ast_INCLUDE}/swap.h implicit
		prev ${PACKAGE_ast_INCLUDE}/ast_common.h
	done
	make ${PACKAGE_ast_INCLUDE}/ast.h implicit
		prev ${PACKAGE_ast_INCLUDE}/ast_api.h implicit
		make ${PACKAGE_ast_INCLUDE}/vmalloc.h implicit
			prev ${PACKAGE_ast_INCLUDE}/ast_common.h
			make ${PACKAGE_ast_INCLUDE}/ast_std.h implicit
				make ${PACKAGE_ast_INCLUDE}/regex.h implicit
					prev ${PACKAGE_ast_INCLUDE}/ast_api.h
					make ${PACKAGE_ast_INCLUDE}/ast_wchar.h implicit
						make ${PACKAGE_ast_INCLUDE}/wctype.h implicit
							make ${PACKAGE_ast_INCLUDE}/ast_wctype.h implicit
								prev ${PACKAGE_ast_INCLUDE}/endian.h
								make ${PACKAGE_ast_INCLUDE}/wchar.h implicit
									note * TODO * circular dependency
									note # prev ${PACKAGE_ast_INCLUDE}/ast_wchar.h
								done
							done
						done
						make ${PACKAGE_ast_INCLUDE}/stdio.h implicit
							make ${PACKAGE_ast_INCLUDE}/ast_stdio.h implicit
								prev ${PACKAGE_ast_INCLUDE}/sfio_s.h implicit
								note * TODO * circular dependency
								note # prev ${PACKAGE_ast_INCLUDE}/ast_std.h
							done
						done
						prev ${PACKAGE_ast_INCLUDE}/ast_common.h
					done
					prev ${PACKAGE_ast_INCLUDE}/ast_common.h
				done
				make ${PACKAGE_ast_INCLUDE}/getopt.h implicit
					prev ${PACKAGE_ast_INCLUDE}/ast_getopt.h implicit
				done
				prev ${PACKAGE_ast_INCLUDE}/ast_map.h
				prev ${PACKAGE_ast_INCLUDE}/ast_botch.h implicit
				prev ${PACKAGE_ast_INCLUDE}/ast_limits.h implicit
				make ${PACKAGE_ast_INCLUDE}/ast_fcntl.h implicit
					prev ${PACKAGE_ast_INCLUDE}/ast_fs.h implicit
				done
				prev ${PACKAGE_ast_INCLUDE}/ast_getopt.h
				make ${PACKAGE_ast_INCLUDE}/ast_sys.h implicit
					prev ${PACKAGE_ast_INCLUDE}/getopt.h
					prev ${PACKAGE_ast_INCLUDE}/endian.h
				done
				prev ${PACKAGE_ast_INCLUDE}/ast_lib.h implicit
				prev ${PACKAGE_ast_INCLUDE}/ast_common.h
			done
		done
		make ${PACKAGE_ast_INCLUDE}/sfio.h implicit
			prev ${PACKAGE_ast_INCLUDE}/sfio_s.h
			prev ${PACKAGE_ast_INCLUDE}/ast_common.h
			prev ${PACKAGE_ast_INCLUDE}/ast_std.h
		done
		prev ${PACKAGE_ast_INCLUDE}/ast_std.h
	done
	prev ${PACKAGE_ast_INCLUDE}/hashpart.h implicit

	note *
	note * Library dependencies
	note *

	bind -last
	make sum.req
		prev ${INSTALLROOT}/bin/mkreq
		exec - mkreq ${CC} ${mam_cc_FLAGS} ${CCFLAGS} ${mam_cc_NOSTRICTALIASING} : ${LDFLAGS} : sum md ast
	done

	note *
	note * Build the library
	note *

	make libsum.a
		make sumlib.o
			make sumlib.c
				make sum-sha2.c implicit
					prev ${PACKAGE_ast_INCLUDE}/endian.h
				done
				prev sum-sha1.c implicit
				prev sum-md5.c implicit
				prev sum-lmd.c implicit
				make sum-prng.c implicit
					prev ${PACKAGE_ast_INCLUDE}/fnv.h
				done
				prev sum-bsd.c implicit
				prev sum-ast4.c implicit
				make FEATURE/sum implicit
					prev features/sum
					exec - iffe ${IFFEFLAGS} -v -c \
					exec -  '${CC} ${mam_cc_FLAGS} ${CCFLAGS} ${mam_cc_NOSTRICTALIASING} ${LDFLAGS}' \
					exec -	ref ${mam_cc_L+-L${INSTALLROOT}/lib} -I${PACKAGE_ast_INCLUDE} \
					exec -	-I${INSTALLROOT}/include ${mam_libast} : run ${<}
				done
				prev ${PACKAGE_ast_INCLUDE}/hashpart.h
				prev ${PACKAGE_ast_INCLUDE}/swap.h
				prev ${PACKAGE_ast_INCLUDE}/endian.h
				make sum.h implicit
					prev ${PACKAGE_ast_INCLUDE}/ast.h
				done
				make sum-att.c implicit
					prev ${PACKAGE_ast_INCLUDE}/ast.h
				done
				make sum-crc.c implicit
					prev ${PACKAGE_ast_INCLUDE}/ast.h
				done
			done
			exec - ${CC} ${mam_cc_FLAGS} ${CCFLAGS} ${mam_cc_NOSTRICTALIASING} -I. -I${PACKAGE_ast_INCLUDE} -c ${<}
		done sumlib.o
		exec - ${AR} rc ${@} ${<} || exit
		exec - ranlib ${@} >/dev/null 2>&1 || true
	done libsum.a

	note *
	note * pre-install
	note *

	loop DIR ${INSTALLROOT}/lib/lib ${PACKAGE_ast_INCLUDE}
		make ${DIR}
			exec - mkdir -p ${@}
		done
	done
	make ${INSTALLROOT}/lib/libsum.a
		prev libsum.a
		exec - cp -f ${<} ${@} || exit
		exec - ranlib ${@} >/dev/null 2>&1 || true
	done
	make ${INSTALLROOT}/lib/lib/sum
		prev sum.req
		exec - cp -f ${<} ${@}
	done
	make ${PACKAGE_ast_INCLUDE}/sum.h
		prev sum.h
		exec - cp -f ${<} ${@}
	done
done install

make test dontcare virtual
done test
