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} ${-debug-symbols?1?${mam_cc_DEBUG} -D_BLD_DEBUG?${mam_cc_OPTIMIZE}?} ${mam_cc_NOSTRICTALIASING} -DERROR_CATALOG=\""libcmd"\" -DHOSTTYPE=\""${mam_cc_HOSTTYPE}"\" -D_BLD_cmd
setv CCFLAGS
setv IFFEFLAGS
setv LDFLAGS
setv CCLDFLAGS
setv DYLIB_VERSION 2.0

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}/fts.h implicit
		prev ${PACKAGE_ast_INCLUDE}/ast_mode.h implicit
		prev ${PACKAGE_ast_INCLUDE}/ast_fs.h implicit
		make ${PACKAGE_ast_INCLUDE}/ast_std.h implicit
			make ${PACKAGE_ast_INCLUDE}/regex.h implicit
				prev ${PACKAGE_ast_INCLUDE}/ast_api.h implicit
				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 depencency
								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
			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}/ls.h implicit
		prev ${PACKAGE_ast_INCLUDE}/ast_mode.h
		prev ${PACKAGE_ast_INCLUDE}/ast_fs.h
		prev ${PACKAGE_ast_INCLUDE}/ast_std.h
	done
	make ${PACKAGE_ast_INCLUDE}/cdt.h implicit
		prev ${PACKAGE_ast_INCLUDE}/ast_common.h
		prev ${PACKAGE_ast_INCLUDE}/ast_std.h
	done
	make ${PACKAGE_ast_INCLUDE}/error.h implicit
		make ${PACKAGE_ast_INCLUDE}/option.h implicit
			make ${PACKAGE_ast_INCLUDE}/ast.h implicit
				prev ${PACKAGE_ast_INCLUDE}/ast_api.h
				make ${PACKAGE_ast_INCLUDE}/vmalloc.h implicit
					prev ${PACKAGE_ast_INCLUDE}/ast_common.h
					prev ${PACKAGE_ast_INCLUDE}/ast_std.h
				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
		done
		prev ${PACKAGE_ast_INCLUDE}/ast.h
	done
	make ${PACKAGE_ast_INCLUDE}/modex.h implicit
		prev ${PACKAGE_ast_INCLUDE}/modecanon.h implicit
		prev ${PACKAGE_ast_INCLUDE}/ast_fs.h
	done
	make ${PACKAGE_ast_INCLUDE}/sum.h implicit
		prev ${PACKAGE_ast_INCLUDE}/ast.h
	done
	make ${PACKAGE_ast_INCLUDE}/ccode.h implicit
		prev ${PACKAGE_ast_INCLUDE}/ast_ccode.h implicit
		prev ${PACKAGE_ast_INCLUDE}/ast_common.h
	done
	make ${PACKAGE_ast_INCLUDE}/tmx.h implicit
		make ${PACKAGE_ast_INCLUDE}/tv.h implicit
			prev ${PACKAGE_ast_INCLUDE}/ast.h
		done
		make ${PACKAGE_ast_INCLUDE}/tm.h implicit
			make ${PACKAGE_ast_INCLUDE}/times.h implicit
				prev ${PACKAGE_ast_INCLUDE}/ast_time.h implicit
				prev ${PACKAGE_ast_INCLUDE}/ast.h
			done
			prev ${PACKAGE_ast_INCLUDE}/ast.h
		done
	done
	make ${PACKAGE_ast_INCLUDE}/stk.h implicit
		prev ${PACKAGE_ast_INCLUDE}/sfio.h
	done
	make ${PACKAGE_ast_INCLUDE}/proc.h implicit
		prev ${PACKAGE_ast_INCLUDE}/ast.h
	done
	make ${PACKAGE_ast_INCLUDE}/sfdisc.h implicit
		prev ${PACKAGE_ast_INCLUDE}/ast.h
	done
	make ${PACKAGE_ast_INCLUDE}/lc.h implicit
		prev ${PACKAGE_ast_INCLUDE}/ast.h
	done
	prev ${PACKAGE_ast_INCLUDE}/shcmd.h implicit
	prev ${PACKAGE_ast_INCLUDE}/hashkey.h implicit
	prev ${PACKAGE_ast_INCLUDE}/ast_tty.h implicit
	prev ${PACKAGE_ast_INCLUDE}/sig.h implicit

	note *
	note * Library dependencies
	note *

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

	note *
	note * Header dependencies and feature tests
	note *

	setv run_iffe iffe ${IFFEFLAGS} -v -c "${CC} ${mam_cc_FLAGS} ${CCFLAGS} ${mam_cc_NOSTRICTALIASING} ${LDFLAGS}" ref ${mam_cc_L+-L${INSTALLROOT}/lib} -I${PACKAGE_ast_INCLUDE} -I${INSTALLROOT}/include ${mam_libutil} ${mam_libast} : run

	make deps virtual
		make cmdinit.c
			prev ${PACKAGE_ast_INCLUDE}/shcmd.h
			make cmd.h implicit
				prev ${PACKAGE_ast_INCLUDE}/shcmd.h
				prev ${PACKAGE_ast_INCLUDE}/stk.h
				prev ${PACKAGE_ast_INCLUDE}/error.h
				prev ${PACKAGE_ast_INCLUDE}/ast.h
			done cmd.h
		done cmdinit.c
		make basename.c
			prev cmd.h
		done
		make cat.c
			prev ${PACKAGE_ast_INCLUDE}/endian.h
			prev cmd.h
		done
		make chgrp.c
			make FEATURE/symlink implicit
				prev features/symlink
				exec - ${run_iffe} ${<}
			done
			prev ${PACKAGE_ast_INCLUDE}/fts.h
			prev ${PACKAGE_ast_INCLUDE}/endian.h
			prev ${PACKAGE_ast_INCLUDE}/ls.h
			prev ${PACKAGE_ast_INCLUDE}/cdt.h
			prev cmd.h
		done
		make chmod.c
			prev FEATURE/symlink
			prev ${PACKAGE_ast_INCLUDE}/fts.h
			prev ${PACKAGE_ast_INCLUDE}/ls.h
			prev cmd.h
		done
		make chown.c
			prev cmd.h
		done
		make cksum.c
			prev ${PACKAGE_ast_INCLUDE}/error.h
			prev ${PACKAGE_ast_INCLUDE}/fts.h
			prev ${PACKAGE_ast_INCLUDE}/modex.h
			prev ${PACKAGE_ast_INCLUDE}/ls.h
			prev ${PACKAGE_ast_INCLUDE}/sum.h
			prev cmd.h
		done
		make cmp.c
			prev ${PACKAGE_ast_INCLUDE}/ccode.h
			prev ${PACKAGE_ast_INCLUDE}/ls.h
			prev cmd.h
		done
		make comm.c
			prev cmd.h
		done
		make cp.c
			prev ${PACKAGE_ast_INCLUDE}/tmx.h
			prev ${PACKAGE_ast_INCLUDE}/stk.h
			prev ${PACKAGE_ast_INCLUDE}/hashkey.h
			prev ${PACKAGE_ast_INCLUDE}/fts.h
			prev ${PACKAGE_ast_INCLUDE}/times.h
			prev ${PACKAGE_ast_INCLUDE}/ls.h
			prev cmd.h
		done
		make cut.c
			prev cmd.h
		done
		make dirname.c
			prev cmd.h
		done
		make date.c
			prev ${PACKAGE_ast_INCLUDE}/times.h
			prev ${PACKAGE_ast_INCLUDE}/tmx.h
			prev ${PACKAGE_ast_INCLUDE}/proc.h
			prev ${PACKAGE_ast_INCLUDE}/ls.h
			prev cmd.h
		done
		make expr.c
			prev ${PACKAGE_ast_INCLUDE}/regex.h
			prev cmd.h
		done
		make fds.c
			prev ${PACKAGE_ast_INCLUDE}/endian.h
			make FEATURE/sockets implicit
				prev features/sockets
				exec - ${run_iffe} ${<}
			done
			prev ${PACKAGE_ast_INCLUDE}/ls.h
			prev cmd.h
		done
		make fmt.c
			prev cmd.h
		done
		make fold.c
			prev cmd.h
		done
		make getconf.c
			prev ${PACKAGE_ast_INCLUDE}/ls.h
			prev ${PACKAGE_ast_INCLUDE}/proc.h
			prev cmd.h
		done
		make head.c
			prev cmd.h
		done
		make id.c
			prev ${PACKAGE_ast_INCLUDE}/stdio.h
			make FEATURE/ids implicit
				prev features/ids
				exec - ${run_iffe} ${<}
			done
			prev cmd.h
		done
		make join.c
			prev ${PACKAGE_ast_INCLUDE}/wctype.h
			prev ${PACKAGE_ast_INCLUDE}/wchar.h
			prev ${PACKAGE_ast_INCLUDE}/sfdisc.h
			prev cmd.h
		done
		make ln.c
			prev cmd.h
		done
		make logname.c
			prev cmd.h
		done
		make md5sum.c
			prev cmd.h
		done
		make mkdir.c
			prev ${PACKAGE_ast_INCLUDE}/ls.h
			prev cmd.h
		done
		make mkfifo.c
			prev ${PACKAGE_ast_INCLUDE}/ls.h
			prev cmd.h
		done
		make mktemp.c
			prev ${PACKAGE_ast_INCLUDE}/ls.h
			prev cmd.h
		done
		make mv.c
			prev cmd.h
		done
		make paste.c
			prev cmd.h
		done
		make pathchk.c
			prev ${PACKAGE_ast_INCLUDE}/ls.h
			prev cmd.h
		done
		make pids.c
			prev ${PACKAGE_ast_INCLUDE}/sfdisc.h
			prev ${PACKAGE_ast_INCLUDE}/ast_tty.h
			prev cmd.h
		done
		make rev.c
			prev rev.h implicit
			prev cmd.h
		done
		make rm.c
			prev ${PACKAGE_ast_INCLUDE}/fts.h
			prev ${PACKAGE_ast_INCLUDE}/ls.h
			prev cmd.h
		done
		make rmdir.c
			prev cmd.h
		done
		make stty.c
			prev ${PACKAGE_ast_INCLUDE}/ast_tty.h
			prev ${PACKAGE_ast_INCLUDE}/ccode.h
			prev cmd.h
		done
		make sum.c
			prev cmd.h
		done
		make sync.c
			prev ${PACKAGE_ast_INCLUDE}/ls.h
			prev cmd.h
		done
		make tail.c
			prev rev.h
			prev ${PACKAGE_ast_INCLUDE}/tv.h
			prev ${PACKAGE_ast_INCLUDE}/ls.h
			prev cmd.h
		done
		make tee.c
			prev ${PACKAGE_ast_INCLUDE}/sig.h
			prev ${PACKAGE_ast_INCLUDE}/ls.h
			prev cmd.h
		done
		make tty.c
			prev cmd.h
		done
		make uname.c
			make FEATURE/utsname implicit
				prev features/utsname
				exec - ${run_iffe} ${<}
			done
			prev ${PACKAGE_ast_INCLUDE}/proc.h
			prev cmd.h
		done
		make uniq.c
			prev cmd.h
		done
		make vmstate.c
			prev ${PACKAGE_ast_INCLUDE}/sfdisc.h
			prev ${PACKAGE_ast_INCLUDE}/vmalloc.h
			prev cmd.h
		done
		make wc.c
			prev ${PACKAGE_ast_INCLUDE}/ls.h
			make wc.h implicit
				prev ${PACKAGE_ast_INCLUDE}/ast.h
			done
			prev cmd.h
		done
		make revlib.c
			prev rev.h
			prev cmd.h
		done
		make wclib.c
			prev ${PACKAGE_ast_INCLUDE}/lc.h
			prev ${PACKAGE_ast_INCLUDE}/wctype.h
			prev ${PACKAGE_ast_INCLUDE}/wchar.h
			prev wc.h
			prev cmd.h
		done
		make lib.c
			prev cmd.h
		done
	done deps

	make cmdext.h implicit
		prev cmdinit.c
		prev basename.c
		prev cat.c
		prev chgrp.c
		prev chmod.c
		prev chown.c
		prev cksum.c
		prev cmp.c
		prev comm.c
		prev cp.c
		prev cut.c
		prev dirname.c
		prev date.c
		prev expr.c
		prev fds.c
		prev fmt.c
		prev fold.c
		prev getconf.c
		prev head.c
		prev id.c
		prev join.c
		prev ln.c
		prev logname.c
		prev md5sum.c
		prev mkdir.c
		prev mkfifo.c
		prev mktemp.c
		prev mv.c
		prev paste.c
		prev pathchk.c
		prev pids.c
		prev rev.c
		prev rm.c
		prev rmdir.c
		prev stty.c
		prev sum.c
		prev sync.c
		prev tail.c
		prev tee.c
		prev tty.c
		prev uname.c
		prev uniq.c
		prev wc.c
		prev revlib.c
		prev wclib.c
		prev lib.c
		exec - {
		exec - cat <<!
		exec - /*
		exec -  * -lcmd extern function prototypes
		exec -  */
		exec -
		exec - #include <shcmd.h>
		exec -
		exec - !
		exec - sed \
		exec -	-e '/^b_[a-z_][a-z_0-9]*(/!d' \
		exec -	-e 's/^b_//' \
		exec -	-e 's/(.*//' \
		exec -	-e 's/.*/extern int	b_&(int, char**, Shbltin_t*);/' \
		exec -	${^} |
		exec -	sort -u
		exec - } > cmdext.h
		prev vmstate.c
		exec - {
		exec - cat <<!
		exec - #if _AST_vmalloc
		exec - extern int	b_vmstate(int, char**, Shbltin_t*);
		exec - #endif
		exec - !
		exec - } >> cmdext.h
		prev ${PACKAGE_ast_INCLUDE}/shcmd.h
	done

	note *
	note * Build the library
	note *

	make libcmd.a
		loop OBJ cmdinit basename cat chgrp chmod chown cksum cmp comm cp cut dirname date expr fds fmt fold getconf head id join ln logname md5sum mkdir mkfifo mktemp mv paste pathchk pids rev rm rmdir stty sum sync tail tee tty uname uniq vmstate wc revlib wclib lib
			make ${OBJ}.o
				prev ${OBJ}.c
				exec - ${CC} ${mam_cc_FLAGS} ${CCFLAGS} ${mam_cc_NOSTRICTALIASING} -I. -I${PACKAGE_ast_INCLUDE} -DERROR_CATALOG=\""libcmd"\" -DHOSTTYPE=\""${mam_cc_HOSTTYPE}"\" -D_BLD_cmd -c ${<}
			done
		done
		make sumlib.o
			bind -lsum
			exec - set -- ${mam_libsum}
			exec - ${AR} x "$1" sumlib.o
		done
		exec - if	test -f ${@}
		exec - then	${AR} rc ${@} ${?}  # add changed *.o
		exec - else	${AR} rc ${@} ${^}  # add all *.o
		exec - fi || exit
		exec - ranlib ${@} >/dev/null 2>&1 || true

		note * save for dylink
		setv _libcmd_object_files_ ${^}
	done libcmd.a

	note *
	note * Dynamically linked version, if supported
	note *

	make ${INSTALLROOT}/dyn/lib/libcmd${mam_cc_SUFFIX_SHARED} dontcare
		prev libcmd.a
		prev cmd.req
		exec - export LDFLAGS='${LDFLAGS} ${CCLDFLAGS} ${mam_cc_LD_NOASNEEDED}'
		exec - dylink -m cmd -v ${DYLIB_VERSION} -s ${mam_cc_SUFFIX_SHARED} $(sed 1d cmd.req) ${_libcmd_object_files_}
	done

	note *
	note * pre-install
	note *

	loop DIR make ${INSTALLROOT}/lib/lib ${PACKAGE_ast_INCLUDE}
		make ${DIR}
			exec - mkdir -p ${@}
		done
	done
	make ${INSTALLROOT}/lib/libcmd.a
		prev libcmd.a
		exec - cp -f ${<} ${@} || exit
		exec - ranlib ${@} >/dev/null 2>&1 || true
	done
	make ${INSTALLROOT}/lib/lib/cmd
		prev cmd.req
		exec - cp -f ${<} ${@}
	done
	make ${PACKAGE_ast_INCLUDE}/cmd.h
		prev cmd.h
		exec - cp -f ${<} ${@}
	done
	make ${PACKAGE_ast_INCLUDE}/cmdext.h
		prev cmdext.h
		exec - cp -f ${<} ${@}
	done
	make ${PACKAGE_ast_INCLUDE}/cmdlist.h
		make cmdlist.h
			prev cmdinit.c
			prev basename.c
			prev cat.c
			prev chgrp.c
			prev chmod.c
			prev chown.c
			prev cksum.c
			prev cmp.c
			prev comm.c
			prev cp.c
			prev cut.c
			prev dirname.c
			prev date.c
			prev expr.c
			prev fds.c
			prev fmt.c
			prev fold.c
			prev getconf.c
			prev head.c
			prev id.c
			prev join.c
			prev ln.c
			prev logname.c
			prev md5sum.c
			prev mkdir.c
			prev mkfifo.c
			prev mktemp.c
			prev mv.c
			prev paste.c
			prev pathchk.c
			prev pids.c
			prev rev.c
			prev rm.c
			prev rmdir.c
			prev stty.c
			prev sum.c
			prev sync.c
			prev tail.c
			prev tee.c
			prev tty.c
			prev uname.c
			prev uniq.c
			prev wc.c
			prev revlib.c
			prev wclib.c
			prev lib.c
			exec - {
			exec - cat <<!
			exec - /*
			exec -  * -lcmd function list -- define your own CMDLIST()
			exec -  */
			exec -
			exec - !
			exec - sed \
			exec -	-e '/^b_[a-z_][a-z_0-9]*(/!d' \
			exec -	-e 's/^b_//' \
			exec -	-e 's/(.*//' \
			exec -	-e 's/.*/CMDLIST(&)/' \
			exec -	${^} |
			exec - sort -u
			exec - } > ${@}
			prev vmstate.c
			exec - {
			exec - cat <<!
			exec - #if _AST_vmalloc
			exec - CMDLIST(vmstate)
			exec - #endif
			exec - !
			exec - } >> ${@}
		done cmdlist.h
		exec - cp -f ${<} ${@}
	done ${PACKAGE_ast_INCLUDE}/cmdlist.h
done install

make test dontcare virtual
done test
