MODULE  imgdef_write;

!+
! ABSTRACT:
!	Routines to write the LINK and BUILD MMS files.
!
! HISTORY:
!	March 1988	SPR 13180	Travisano
!		Creation.
!-

DECLARE  option_file_name 	: EXTERNAL STRING;
DECLARE  link_file_name 	: EXTERNAL STRING;
DECLARE  build_file_name 	: EXTERNAL STRING;
DECLARE  target_name 		: EXTERNAL STRING;

! MMS build and link statements are stored in trees for output at end.
DECLARE  mms_build 		: EXTERNAL TREE ( INTEGER ) OF STRING;
DECLARE  mms_build_cnt 		: EXTERNAL INTEGER;
DECLARE  mms_link 		: EXTERNAL TREE ( INTEGER ) OF STRING;
DECLARE  mms_link_cnt		: EXTERNAL INTEGER;
DECLARE  mms_files 		: EXTERNAL TREE ( INTEGER ) OF STRING;
DECLARE  mms_files_cnt		: EXTERNAL INTEGER;

! Options files that must precede or follow the generated options file.
DECLARE  require_before 	: EXTERNAL TREE ( INTEGER ) OF STRING;
DECLARE  require_before_cnt	: EXTERNAL INTEGER;
DECLARE  require_after 		: EXTERNAL TREE ( INTEGER ) OF STRING;
DECLARE  require_after_cnt	: EXTERNAL INTEGER;

! The link switches from every #LINK statement.
DECLARE  link_switch_tree 	: EXTERNAL TREE ( INTEGER ) OF STRING;
DECLARE  link_switch_cnt	: EXTERNAL INTEGER;

!
! PROCEDURE to write the LINK MMS file.
!
PROCEDURE  write_link_mms ( );

	CONSTANT  tab  =  '	';

	DECLARE  link_file 	: FILE;
	DECLARE  i		: INTEGER;

	!
	! Local PROCEDURE to provide a write-behind strategy for dealing
	! with MMS and LINK continuation lines.
	!
	PROCEDURE  out ( str: STRING, cont: STRING );

		DECLARE  savebuf : STATIC STRING;

		IF  savebuf <> ''
		THEN
			WRITE FILE ( link_file ) savebuf & cont;
		END IF;

		savebuf = str;

	END PROCEDURE; /* out */


	OPEN FILE ( link_file ) AS link_file_name FOR OUTPUT;

	! MMS_BEGIN block
	!
	FOR  i = 1 TO mms_link_cnt;
		WRITE FILE ( link_file ) mms_link( i );
	END FOR;

	! MMS Target
	!
	WRITE FILE ( link_file ) target_name & ' : -' ;

	! Dependencies, including options files.
	!
	FOR  i = 1 TO mms_files_cnt;
		CALL out( tab & mms_files( i ), ', -' );
	END FOR;

	FOR  i = 1 TO require_before_cnt;
		CALL out( tab & require_before( i ), ', -' );
	END FOR;

	CALL out( tab & option_file_name, ', -' );

	FOR  i = 1 TO require_after_cnt;
		CALL out( tab & require_after( i ), ', -' );
	END FOR;

	CALL out( '', '' );

	! LINK statement
	!
	WRITE FILE ( link_file ) '  !' ;
	WRITE FILE ( link_file ) '  $(LINK) $(LINKFLAGS) ' &
					'/EXECUTABLE=' & target_name & ' -' ;

	! LINK switches
	!
	FOR  i = 1 TO link_switch_cnt;
		CALL out( tab & link_switch_tree( i ), ' -' );
	END FOR;

	CALL out( '', ' -');

	! Options files -- don't forget the /OPTIONS qualifier!
	!
	FOR  i = 1 TO require_before_cnt;
		CALL out( tab & require_before( i ), '/OPTIONS, -' );
	END FOR;

	CALL out( tab & option_file_name, '/OPTIONS, -' );

	FOR  i = 1 TO require_after_cnt;
		CALL out( tab & require_after( i ), '/OPTIONS, -' );
	END FOR;

	CALL out( '', '/OPTIONS' );

	CLOSE FILE ( link_file );

END PROCEDURE;  /* write_link_mms */


!
! PROCEDURE to write the BUILD MMS file.
!
PROCEDURE  write_build_mms ( );

	DECLARE  build_file 	: FILE;
	DECLARE  i, ix		: INTEGER;
	DECLARE  module_name	: STRING;
	DECLARE  subsys		: STRING;

	OPEN FILE ( build_file ) AS build_file_name FOR OUTPUT;

	FOR  i = 1 TO mms_build_cnt;
		WRITE FILE ( build_file ) mms_build( i );
	END FOR;

	WRITE FILE ( build_file ) '';
	WRITE FILE ( build_file ) '.INCLUDE  ' & link_file_name;
	WRITE FILE ( build_file ) '';

	! Have the MMS include the LINK and COMPILE MMS files.  Search
	! the files in the list for any objects.  Typically, these will
	! be of the form:
	!			subsystem_OBJECT:NAME.OBJ
	! which we convert to:
	!			.INCLUDE  subsystem_MMS_BUILD:NAME_COMPILE.MMS
	!
	! If this form is not specified, don't use subsystem areas.

	FOR  i = 1 TO mms_files_cnt;

		ix = INDEX( UPPER( mms_files( i ) ), '.OBJ' );
		IF  ix <> 0
		THEN
		    mms_files( i ) = UPPER( mms_files( i ) );

                    IF  ( INDEX( mms_files( i ), '_' ) <> 0  AND
                          INDEX( mms_files( i ), ':' ) <> 0 )
		    THEN
			subsys = mms_files(i)[1..INDEX(mms_files(i),'_')-1];
			module_name = mms_files(i)[INDEX(mms_files(i),':')+1..];
			ix = INDEX( module_name, '.OBJ' );

			WRITE FILE ( build_file ) '';
			WRITE FILE ( build_file ) '.INCLUDE  ' &
				subsys & '_MMS_BUILD:' &
				module_name[1..ix-1] & '_COMPILE.MMS';
		    ELSE
			WRITE FILE ( build_file ) '';
			WRITE FILE ( build_file ) '.INCLUDE  ' &
				mms_files( i )[1..ix-1] & '_COMPILE.MMS';

		    END IF;
		END IF;
	END FOR;

	CLOSE FILE ( build_file );

END PROCEDURE;  /* write_build_mms */


END MODULE; /* imgdef_write */
