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 4

setv INSTALLROOT ../../..
setv CC cc
setv mam_cc_FLAGS %{mam_cc_TARGET} %{mam_cc_DLL} %{-debug-symbols?1?%{mam_cc_DEBUG} -D_BLD_DEBUG?%{mam_cc_OPTIMIZE}?}
setv CCFLAGS
setv CCLDFLAGS %{-strip-symbols?1?%{mam_cc_LD_STRIP}??}
setv IFFEFLAGS
setv LDFLAGS

make install virtual
	bind -lcmd
	bind -lutil dontcare

	note *
	note * Build pty
	note *

	make pty
		make pty.o
			make pty.c
				make FEATURE/pty
					makp features/pty
					exec - iffe %{IFFEFLAGS} -v -c "%{CC} %{mam_cc_FLAGS} %{CCFLAGS} %{LDFLAGS}" ref \
					exec -	-I%{INCLUDE_AST} -I%{INSTALLROOT}/include %{mam_libast} %{mam_libcmd} : run %{<}
				done
				prev %{INCLUDE_AST}/ast_time.h
				prev %{INCLUDE_AST}/regex.h
				prev %{INCLUDE_AST}/proc.h
				prev %{INCLUDE_AST}/error.h
				prev %{INCLUDE_AST}/cmd.h
			done pty.c
			exec - %{CC} %{mam_cc_FLAGS} %{CCFLAGS} -I. -I%{INCLUDE_AST} \
			exec -	-DERROR_CATALOG=\""builtin"\" -DCMD_STANDALONE=b_pty -c %{<}
		done pty.o
		exec - %{CC} %{CCLDFLAGS} %{mam_cc_FLAGS} %{CCFLAGS} %{mam_cc_NOSTRICTALIASING} %{LDFLAGS} \
		exec -	-o pty pty.o %{mam_libutil} %{mam_libast} %{mam_libcmd}
	done pty

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

	make %{INSTALLROOT}/dyn/bin/pty dontcare
		prev pty
		makp %{INSTALLROOT}/lib/lib/cmd
		exec - export LDFLAGS='%{LDFLAGS} %{CCLDFLAGS} %{mam_cc_LD_NOASNEEDED}'
		exec - dylink -e pty %{mam_libutil} $(cat %{<}) pty.o
	done

	note *
	note * Pre-install
	note *

	make %{INSTALLROOT}/bin
		exec - mkdir -p %{@}
	done
	make %{INSTALLROOT}/bin/pty
		prev pty
		note * macOS throws code signature error if 'cp' overwrites Mach-O binary; must remove first
		exec - rm -f %{@} && cp %{<} %{@}
	done
done install

make test dontcare virtual
done test
