Currently each foreign package is adorned with a small program "spkg-install" that automates the compilation/installation of the package by calling the parts of the packages build system accordingly.

most implementations of this program (for the various packages) are purely monolithic, sequential and stateless. these programs need to be extended to allow

a simple straigtforward replacement ("common practice") would be an executable makefile which has "install:" as its default target. such a makefile could look like very much like this

\#!/whatever/make
include /some/common/rules
PACKAGE_NAME=bla

configure:
    cd src; configure --prefix=... --something

install:
    $(MAKE) -Csrc install DESTDIR=$(DESTDIR) INSTALL=$(SAGE_INSTALL) -F $(FILELIST)

theoretically it can be implemented in any other language, we have interpreters for,

but this looks much more involved (example?)