Index: src/tools/tspec/makefile.c
===================================================================
--- src/tools/tspec/makefile.c	(revision 368)
+++ src/tools/tspec/makefile.c	(working copy)
@@ -67,6 +67,7 @@
 #include "name.h"
 #include "type.h"
 #include "utility.h"
+#include "makefile.h"
 
 
 /*
@@ -99,8 +100,6 @@
     FILE *output;
     hash_elem *e;
     char *api2 = hack_name(api, "_Aa0");
-    int li = output_incl_len;
-    int ls = output_src_len;
 
     /* Open output file */
     nm = (whole ? MAKEFILE_API : MAKEFILE);
@@ -112,93 +111,20 @@
 	error(ERR_SERIOUS, "Can't open output file, %s", nm);
 	return;
     }
-
-    /* Print file header */
-    IGNORE fprintf(output, "# AUTOMATICALLY GENERATED BY %s %s\n",
-		     progname, progvers);
-    IGNORE fprintf(output, "# MAKEFILE FOR %s API\n\n", api);
-    if (whole) {
-	IGNORE fputs("TCC=tcc# Version of tcc\n", output);
-	IGNORE fprintf(output, "LIB=%s.tl# TDF library\n", api2);
-	IGNORE fputs("IMPL=/usr/include# API implementation directory\n",
-		       output);
-	IGNORE fputs("WORK=.# Work directory\n\n", output);
-	IGNORE fprintf(output, "INCL=%s# API header directory\n",
-			 output_incl_dir);
-	IGNORE fprintf(output, "SRC=%s# API auxiliary directory\n",
-			 output_src_dir);
-	IGNORE fputs("PRE_FLAGS=\n", output);
-	IGNORE fprintf(output, "INCL_FLAGS=-I${IMPL} -I${INCL}/%s%s\n",
-		         api, OUTPUT_SUFFIX);
-	IGNORE fputs("POST_FLAGS=\n", output);
-	IGNORE fputs("TDI=${TCC} -D", output);
-	IGNORE fputs(BUILDING_MACRO, output);
-	IGNORE fputs(" ${PRE_FLAGS} ${INCL_FLAGS}", output);
-	IGNORE fputs(" ${POST_FLAGS} -Fj\n", output);
-	IGNORE fputs("TDP=${TCC} -Ytdp ${INCL_FLAGS} -Fj\n", output);
-	IGNORE fputs("TNC=${TCC} -Ytnc -Fj\n", output);
-	IGNORE fputs("TLIB=${TCC} -Ymakelib\n\n", output);
-    }
-    IGNORE fputs("SHELL=/bin/sh\n\n", output);
-
-    /* Print the list of files */
-    IGNORE fputs("JFILES=", output);
-    for (e = f; e != null; e = e->next) {
-	info *i = e->obj->u.u_info;
-	if (need_info(i, api)) {
-	    int m;
-	    char *a = i->api;
-	    if (strneq(a, "shared/", 7))a += 7;
-	    a = hack_name(a, "_Aa0");
-	    IGNORE sprintf(buffer, "%s/%s", a, basename(i->src));
-	    m = (int)strlen(buffer) - 1;
-	    buffer [m] = 'j';
-	    IGNORE fprintf(output, "\\\n ${WORK}/%s", buffer);
-	}
-    }
-    IGNORE fputs("\n\nall : ${LIB}\n", output);
-    IGNORE fputs("\t@echo all done\n\n", output);
-    IGNORE fputs("${LIB} : ${JFILES}\n", output);
-    IGNORE fputs("\t@rm -f ${LIB}\n", output);
-    IGNORE fputs("\t${TLIB} -o ${LIB} ${JFILES}\n\n", output);
-
-    /* Print the construction for each file */
-    for (e = f; e != null; e = e->next) {
-	info *i = e->obj->u.u_info;
-	if (need_info(i, api)) {
-	    int m;
-	    char *a = i->api;
-	    if (strneq(a, "shared/", 7))a += 7;
-	    a = hack_name(a, "_Aa0");
-	    IGNORE sprintf(buffer, "%s/%s", a, basename(i->src));
-	    m = (int)strlen(buffer) - 1;
-	    buffer [m] = 'j';
-	    IGNORE fprintf(output, "${WORK}/%s : ${SRC}/%s",
-			     buffer, i->src + ls);
-	    if (whole && i->incl) {
-		IGNORE fprintf(output, "\\\n ${INCL}/%s ${IMPL}/%s",
-				 i->incl + li, i->file);
-	    }
-	    if (whole) {
-		IGNORE fprintf(output, "\n\t@ test -d ${WORK}/%s", a);
-		IGNORE fprintf(output, " || mkdir ${WORK}/%s", a);
-	    }
-	    if (i->method == null) {
-		IGNORE fputs("\n\t${TDI} ", output);
-		if (!whole) {
-		    IGNORE fprintf(output, "-f${STARTUP}/%s.h ", a);
+    fprintf(output, "APILIB=\t%s.tl\n\n", api);
+	fputs("APIOBJS=\t", output);
+	for ( e = f ; e != null ; e = e->next ) {
+		info *i = e->obj->u.u_info ;
+		if ( need_info ( i, api ) ) {
+			int m;
+			strcpy(buffer, basename(i->src));
+			m = strlen(buffer) - 1;
+			buffer[m] = 'j';
+			fprintf(output, "\\\n\t\tbuilding/%s.api/%s", api, basename(buffer));
 		}
-	    } else {
-		IGNORE fprintf(output, "\n\t${%s} ", i->method);
-	    }
-	    IGNORE fprintf(output, "-o ${WORK}/%s ${SRC}/%s\n\n",
-			     buffer, i->src + ls);
 	}
-    }
-
+	fputs("\n", output);
     /* End of makefile */
-    IGNORE fputs("clean :\n\trm -f ${JFILES}\n\n", output);
-    IGNORE fputs("clobber : clean\n\trm -f ${LIB}\n", output);
     IGNORE fclose(output);
     return;
 }
Index: src/lib/env/common/Makefile
===================================================================
--- src/lib/env/common/Makefile	(revision 368)
+++ src/lib/env/common/Makefile	(working copy)
@@ -6,6 +6,6 @@
 	pl_tdf posix posix1 posix2 status strict_mot_1_2 strict_posix\
 	strict_svid3 strict_xpg3 svid3 system system+ target_tok tcc_diag\
 	tcc_pp tcc_prof tcc_time tdf_ext tdp tnc tsimp unix95 wsl x5_aw x5_ext\
-	x5_lib x5_mit x5_mu x5_private x5_proto x5_t xpg3 xpg4
+	x5_lib x5_mit x5_mu x5_private x5_proto x5_t xpg3 xpg4 build
 
 .include "../Makefile.inc"
Index: src/lib/env/common/build
===================================================================
--- src/lib/env/common/build	(revision 0)
+++ src/lib/env/common/build	(revision 0)
@@ -0,0 +1,56 @@
+/* default environment for -MACH- */
+
++TENDRA_MACHDIR		"<TENDRA_BASEDIR>/lib/machines/-MACH-"
++TENDRA_BINDIR		"<TENDRA_BASEDIR>/bin"
++TENDRA_ENVDIR		"<TENDRA_MACHDIR>/env"
++TENDRA_LIBDIR		"<TENDRA_BASEDIR>/lib"
++TENDRA_INCLDIR		"<TENDRA_LIBDIR>/include"
++TENDRA_STARTUPDIR	"<TENDRA_BASEDIR>/../src/lib/startup"
++TENDRA_TMPDIR		"/tmp"
+
++TDFC			"<TENDRA_BASEDIR>/src/producers/c/tdfc2"
++TDFCPP			"<TENDRA_BASEDIR>/src/producers/c/tdfc2 -E"
++SPEC_LINK		"<TENDRA_BASEDIR>/src/producers/c/tdfc2 -S"
++TCPPLUS		"<TENDRA_BINDIR>/tcpplus"
++TCPPLUSPP		"<TENDRA_BINDIR>/tcpplus -E"
++CPP_SPEC_LINK		"<TENDRA_BINDIR>/tcpplus -S"
++TLD			"<TENDRA_BINDIR>/tld"
++TRANS			"<TENDRA_BINDIR>/trans"
++DISP			"<TENDRA_BINDIR>/disp"
++PL_TDF			"<TENDRA_BINDIR>/pl"
++TNC			"<TENDRA_BINDIR>/tnc"
+
++DUMP_ANAL		"<TENDRA_BINDIR>/dump_lint"
++DUMP_LINK		"<TENDRA_BINDIR>/dump_linker"
+
++AS			"/bin/as"
++AS1			"/bin/as"
++LD			"/bin/ld"
++DYN_LINK		"<TENDRA_LIBDIR>/sys/dyninit"
++CC			"/bin/cc"
+
++INCL			"-Nansi:<TENDRA_INCLDIR>/ansi.api"
++STARTUP_DIR		"-I<TENDRA_STARTUPDIR>/Bounds.ansi -I<TENDRA_STARTUPDIR>/Modes"
++STARTUP		"-f<TENDRA_STARTUPDIR>/Modes/Xc"
++STARTUP_CPP_DIR	"-I<TENDRA_STARTUPDIR>/Bounds.ansi -I<TENDRA_STARTUPDIR>/Modes.cpp"
++STARTUP_CPP		"-f<TENDRA_STARTUPDIR>/Modes.cpp/Xc"
++PORTABILITY		"<TENDRA_STARTUPDIR>/Bounds.ansi/default.pf"
++LINK			"-L<TENDRA_LIBDIR>"
++LIB			"-lansi"
+
++CRT0			"/lib/crt0.o"
++CRT1			""
++CRTN			""
++SYS_LIB		""
++SYS_LIBC		"-lc"
++LINK_ENTRY		""
+
++API_NAME		"-Aansi"
++API			"-R<TENDRA_INCLDIR>/ansi.d"
++ENVDIR			"<TENDRA_ENVDIR>"
++INFO			"ANSI@(X3.159)"
++TEMP			"<TENDRA_TMPDIR>"
++VERSION		""
+
+/* Extra information */
+
Index: src/lib/Makefile
===================================================================
--- src/lib/Makefile	(revision 368)
+++ src/lib/Makefile	(working copy)
@@ -1,5 +1,5 @@
 # $Id$
 
-SUBDIR = apis env startup
+SUBDIR = env startup apis
 
 .include "Makefile.inc"
Index: mk/tendra.build.mk
===================================================================
--- mk/tendra.build.mk	(revision 370)
+++ mk/tendra.build.mk	(working copy)
@@ -21,6 +21,12 @@
 	@${ECHO} "# Compiling ${PARTIMP}"
 	${CC} ${CCOPTS} -c ${.IMPSRC} -o ${.TARGET}
 
+.c.j:
+	@${ECHO} "# Compiling ${PARTIMP}"
+	${TCC} -yTENDRA_BASEDIR=${OBJ_DIR} -Y${OBJ_DIR}/src/lib/env/common/build \
+	       -o ${.TARGET} ${.IMPSRC} -I${.OBJDIR}/${API}.api/ \
+	       -f${BASE_DIR}/${STARTUP_MACH}/${API}.h -i
+
 .pl.j:
 	@${ECHO} "# Transforming ${PARTIMP}"
 	${TPL} ${.IMPSRC} ${.TARGET}
@@ -34,12 +40,28 @@
 
 .if "${API}" != ""
 #
-# Build API.
+# Build API dependencies.
 #
-_REALWORK: .USE
-	@${ECHO} "# Building ${API} API"
+
+api-depend:
+	@${ECHO} "# Creating API source for ${API}"
 	${TSPEC} -I${BASE_DIR}/${APIS} -O./ -S./building ${API}
 
+# Include Makefile generated by tspec during api-depend.
+.sinclude "./building/${API}.api/Makefile"
+
+#
+# Build API.
+#
+_REALWORK: api-depend
+	cd ${BASE_DIR}/${APIS}/${API}/ && ${.MAKE} makeapi
+makeapi: ${APIOBJS}
+	@${ECHO} "# Linking ${API} API"
+	${TLD} -o ${APILIB} ${APIOBJS}
+
+CLEAN_EXTRA+= ${APILIB} ${APIOBJS} ${APIOBJS:S/.j/.c/} \
+              ./building/${API}.api/Makefile
+
 _objdir= 	${OBJ_DIR}/${APIS}
 .elif "${ENVFILE}" != ""
 #
Index: mk/tendra.base.mk
===================================================================
--- mk/tendra.base.mk	(revision 368)
+++ mk/tendra.base.mk	(working copy)
@@ -51,6 +51,7 @@
 APIS=		src/lib/apis
 ENVIRONMENT=	src/lib/env
 STARTUP=	src/lib/startup
+STARTUP_MACH=	src/lib/machines/${OSFAM}/${BLDARCH}/startup
 TOOLS=		src/tools
 UTILITIES=	src/utilities
 
