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}?}
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}/error.h implicit
		make ${PACKAGE_ast_INCLUDE}/option.h implicit
			make ${PACKAGE_ast_INCLUDE}/ast.h implicit
				prev ${PACKAGE_ast_INCLUDE}/ast_api.h implicit
				make ${PACKAGE_ast_INCLUDE}/vmalloc.h implicit
					make ${PACKAGE_ast_INCLUDE}/ast_common.h implicit
						prev ${PACKAGE_ast_INCLUDE}/ast_map.h implicit
						make ${PACKAGE_ast_INCLUDE}/endian.h implicit
							make ${PACKAGE_ast_INCLUDE}/bytesex.h implicit
								note * TODO * circular dependency
								note # prev ${PACKAGE_ast_INCLUDE}/ast_common.h
							done
						done
					done
					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
		done
		prev ${PACKAGE_ast_INCLUDE}/ast.h
	done
	make ${PACKAGE_ast_INCLUDE}/fts.h implicit
		prev ${PACKAGE_ast_INCLUDE}/ast_mode.h implicit
		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

	note *
	note * Library dependencies
	note *

	bind -ldl dontcare
	bind -last
	make dll.req
		prev ${INSTALLROOT}/bin/mkreq
		exec - mkreq ${CC} ${mam_cc_FLAGS} ${CCFLAGS} ${mam_cc_NOSTRICTALIASING} : ${LDFLAGS} : dll dl dld ast
	done

	note *
	note * Header dependencies and feature tests
	note *

	make dlfcn.c
		prev ${PACKAGE_ast_INCLUDE}/error.h
		make dlldefs.h implicit
			make FEATURE/dll
				prev features/dll
				exec - iffe ${IFFEFLAGS} -v -c \
				exec -  '${CC} ${mam_cc_FLAGS} ${CCFLAGS} ${mam_cc_NOSTRICTALIASING} ${LDFLAGS}' \
				exec -	ref ${mam_cc_L+-L.} ${mam_cc_L+-L${INSTALLROOT}/lib} \
				exec -	-I${PACKAGE_ast_INCLUDE} -I${INSTALLROOT}/include \
				exec -	${mam_libdl} ${mam_libast} : run ${<}
			done
			exec - cp -f ${<} ${@}
		done
		prev ${PACKAGE_ast_INCLUDE}/ast.h
	done
	make dllopen.c
		make dlllib.h implicit
			prev ${PACKAGE_ast_INCLUDE}/error.h
			prev dlldefs.h
			prev ${PACKAGE_ast_INCLUDE}/ast.h
		done
	done
	make dllfind.c
		prev ${PACKAGE_ast_INCLUDE}/error.h
		prev dlldefs.h
		prev ${PACKAGE_ast_INCLUDE}/ast.h
	done
	make dllplug.c
		prev dlllib.h
	done
	make dll_lib.c
		prev dlllib.h
	done
	make dllnext.c
		prev dlldefs.h
		prev ${PACKAGE_ast_INCLUDE}/ast.h
	done
	make dlllook.c
		prev dlldefs.h
		prev ${PACKAGE_ast_INCLUDE}/error.h
		prev ${PACKAGE_ast_INCLUDE}/ast.h
	done
	make dllscan.c
		prev dlldefs.h
		prev ${PACKAGE_ast_INCLUDE}/vmalloc.h
		prev ${PACKAGE_ast_INCLUDE}/fts.h
		prev ${PACKAGE_ast_INCLUDE}/error.h
		prev ${PACKAGE_ast_INCLUDE}/endian.h
		prev ${PACKAGE_ast_INCLUDE}/cdt.h
		prev ${PACKAGE_ast_INCLUDE}/ast.h
	done
	make dllcheck.c
		prev dlllib.h
	done
	make dllerror.c
		prev dlllib.h
	done

	note *
	note * Make the library
	note *

	make libdll.a
		loop OBJ dlfcn dllopen dllfind dllplug dll_lib dllnext dlllook dllscan dllcheck dllerror
			make ${OBJ}.o
				prev ${OBJ}.c
				exec - ${CC} ${mam_cc_FLAGS} ${CCFLAGS} ${mam_cc_NOSTRICTALIASING} -I. -I${PACKAGE_ast_INCLUDE} -D_BLD_dll -c ${<}
			done
		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 _libdll_object_files_ ${^}
	done libdll.a

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

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

	note *
	note * pre-install
	note *

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

make test dontcare virtual
done test
