# IDE_VC5.PM
#
# Copyright (c) 1997-1999 Symbian Ltd.  All rights reserved.
#


# Makmake-module for creating MAKMAKE MSVC5 IDE makefiles shared by windows platform modules

package Ide_vc5;

# declare variables global for module
my $BaseAddressFlag;
my @CppCall;
my %IdeBlds=();
my %PrjHdrs=();
my @Win32LibList=();
my $Win32Resrc;


require Exporter;
@ISA=qw(Exporter);

@EXPORT=qw(
	PMHelp_Mmp
	PMPlatProcessMmp

		PMGetCleanFileList
		PMGetWorkPathList

	PMStartBldList
		PMBld
	PMEndBldList
	PMStartSubPrjList
			PMStartSubPrjSrc
			PMSubPrjSrcDepend
			PMSubPrjDoc
			PMStartSubPrjResrc
	PMEndSubPrjList
);

use Winutl;

sub PMHelp_Mmp {
# get the windows help from WINUTL
	&Winutl_Help_Mmp;
}

sub PMPlatProcessMmp {
# get WINUTL to do the windows mmp file processing
	&Winutl_DoMmp;
	$BaseAddressFlag=&Winutl_BaseAddress;
	$BaseAddressFlag=~s/^(.+$)$/ \/base:$1/o;
	@Win32LibList=&Winutl_Win32LibList;
	$Win32Resrc=&Winutl_Win32Resrc;
}

sub PMGetCleanFileList {
	&Winutl_GetCleanFileList;
}

sub PMGetWorkPathList {
	&Winutl_GetWorkPathList;
}

sub PMStartBldList {
	my $BaseTrg=&main::BaseTrg;
	my @BldList=&main::BldList;
	my $GenTrgType=&main::GenTrgType;
	my $TrgType=&main::TrgType;

	#create the required deffile
	if ($TrgType=~/^APP$/o) {
		my $ExtraFile=join '',&main::SysDefDir,'APP.DEF';
		&main::CreateExtraFile($ExtraFile,'EXPORTS ?NewApplication@@YAPAVCApaApplication@@XZ @1 NONAME');
	}
	if ($TrgType=~/^FSY$/o) {
		my $ExtraFile=join '',&main::SysDefDir,'FSY.DEF';
		&main::CreateExtraFile($ExtraFile,'EXPORTS CreateFileSystem @1 NONAME');
	}
	elsif ($TrgType=~/^LDD$/o) {
		my $ExtraFile=join '',&main::SysDefDir,'LDD.DEF';
		&main::CreateExtraFile($ExtraFile,'EXPORTS ?CreateLogicalDevice@@YAPAVDLogicalDevice@@XZ @1 NONAME');
	}
	elsif ($TrgType=~/^PDD$/o) {
		my $ExtraFile=join '',&main::SysDefDir,'PDD.DEF';
		&main::CreateExtraFile($ExtraFile,'EXPORTS ?CreatePhysicalDevice@@YAPAVDPhysicalDevice@@XZ @1 NONAME');
	}

	# set up global IDE builds variable
	%IdeBlds= (
		REL	=> "$BaseTrg - Win32 Release",
		DEB => "$BaseTrg - Win32 Debug",
		UREL=> "$BaseTrg - Win32 Uni Release",
		UDEB=> "$BaseTrg - Win32 Uni Debug",
	);

	&main::SetUserHdrsOnly;

	&main::Output(
		"# Microsoft Developer Studio Project File - Name=\"$BaseTrg\" - Package Owner=<4>\n",
		"# Microsoft Developer Studio Generated Build File, Format Version 5.00\n",
		"# ** DO NOT EDIT **\n",
		"\n"
	);

	if ($GenTrgType=~/^DLL$/o) {
		&main::Output(
			"# TARGTYPE \"Win32 (x86) Dynamic-Link Library\" 0x0102\n"
		);
	}
	elsif ($GenTrgType=~/^EXE$/o) {
		if (not &main::Plat=~/^WINC$/o) {
			&main::Output(
				"# TARGTYPE \"Win32 (x86) Application\" 0x0101\n"
			);
		}
		else {
			&main::Output(
				"# TARGTYPE \"Win32 (x86) Console Application\" 0x0103\n"
			);
		}
	}
	elsif ($GenTrgType=~/^LIB$/o) {
		&main::Output(
			"# TARGTYPE \"Win32 (x86) Static Library\" 0x0104\n"
		);
	}
	
	&main::Output(
		"\n",
		"CFG=",$IdeBlds{DEB},"\n",
		"!MESSAGE This is not a valid makefile. To build this project using NMAKE,\n",
		"!MESSAGE use the Export Makefile command and run\n",
		"!MESSAGE \n",
		"!MESSAGE NMAKE /f \"",ucfirst lc $BaseTrg,".mak\".\n",
		"!MESSAGE \n",
		"!MESSAGE You can specify a configuration when running NMAKE\n",
		"!MESSAGE by defining the macro CFG on the command line. For example:\n",
		"!MESSAGE \n",
		"!MESSAGE NMAKE /f \"",ucfirst lc $BaseTrg,".mak\" CFG=\"".$IdeBlds{DEB}."\"\n",
		"!MESSAGE \n",
		"!MESSAGE Possible choices for configuration are:\n",
		"!MESSAGE \n"
	);
	if ($GenTrgType=~/^DLL$/o) {
		foreach (@BldList) {
			&main::Output(
				"!MESSAGE \"",$IdeBlds{$_},"\" (based on \"Win32 (x86) Dynamic-Link Library\")\n"
			);
		}
	}
	elsif ($GenTrgType=~/^EXE$/o) {
		if (not &main::Plat=~/^WINC$/o) {
			foreach (@BldList) {
				&main::Output(
					"!MESSAGE \"",$IdeBlds{$_},"\" (based on \"Win32 (x86) Application\")\n"
				);
			}
		}
		else {
			foreach (@BldList) {
				&main::Output(
					"!MESSAGE \"",$IdeBlds{$_},"\" (based on \"Win32 (x86) Console Application\")\n"
				);
			}
		}
	}
	&main::Output(
		"!MESSAGE \n",
		"\n"
	);

	# BEGIN THE PROJECT
	#------------------
	&main::Output(
		"# Begin Project\n",
		"# PROP Scc_ProjName \"\"\n",
		"# PROP Scc_LocalPath \"\"\n",
		"CPP=cl.exe\n",
		"MTL=midl.exe\n",
		"RSC=rc.exe\n",
		"\n"
	);
}

sub PMBld {

	my $BaseFirstObj=&main::BaseFirstObj;
	my @BaseObjList=&main::BaseObjList;
	my @BaseSrcList=&main::BaseSrcList;
	my $BaseTrg=&main::BaseTrg;
	my $Bld=&main::Bld;
	my @BldList=&main::BldList;
	my $ChopBldPath=&main::Path_ChopPath(&main::BldPath);
	my $ChopRelPath=&main::Path_ChopPath(&main::RelPath);
	my @ChopUserIncPaths=&main::Path_ChopPath(&main::UserIncPaths);
	my @ChopSysIncPaths=&main::Path_ChopPath(&main::SysIncPaths);
	my $GenTrgType=&main::GenTrgType;
	my @MacroList=&main::MacroList;
	my @LibList=&main::LibList;
	my $Trg=&main::Trg;
	my $TrgPath=&main::TrgPath;
	my $TrgType=&main::TrgType;

	my $DefFile=&main::DefFile;

	my $NotUseWin32LibsFlag="";
	if (not @Win32LibList) {
		$NotUseWin32LibsFlag=" /X"; # this flag suppresses searching of the standard directories for header files
	}


	if ($Bld eq $BldList[0]) {
		&main::Output(
			"!IF  \"\$(CFG)\" == \"",$IdeBlds{$Bld},"\"\n",
			"\n"
		);
	}
	else {
		&main::Output(
			"!ELSEIF  \"\$(CFG)\" == \"",$IdeBlds{$Bld},"\"\n",
			"\n"
		);
	}
	&main::Output(
		"# PROP BASE Use_MFC 0\n"
	);
	if ($Bld=~/^U?REL$/o) {
		&main::Output(
			"# PROP BASE Use_Debug_Libraries 0\n"
		);
		if (not $Bld=~/^U/o) {
			&main::Output(
				"# PROP BASE Output_Dir \".\\WinRel\"\n",
				"# PROP BASE Intermediate_Dir \".\\WinRel\"\n"
			);
		}
		else {
			&main::Output(
				"# PROP BASE Output_Dir \".\\Win32_Un\"\n",
				"# PROP BASE Intermediate_Dir \".\\Win32_Un\"\n"
			);
		}
	}
	elsif ($Bld=~/^U?DEB$/o) {
		&main::Output(
			"# PROP BASE Use_Debug_Libraries 1\n"
		);
		if (not $Bld=~/^U/o) {
			&main::Output(
				"# PROP BASE Output_Dir \".\\WinDebug\"\n",
				"# PROP BASE Intermediate_Dir \".\\WinDebug\"\n"
			);
		}
		else {
			&main::Output(
				"# PROP BASE Output_Dir \".\\Win32_U0\"\n",
				"# PROP BASE Intermediate_Dir \".\\Win32_U0\"\n"
			);
		}
	}
	&main::Output(
			"# PROP Use_MFC 0\n"
	);
	if ($Bld=~/^U?REL$/o) {
		&main::Output(
			"# PROP Use_Debug_Libraries 0\n"
			);
	}
	elsif ($Bld=~/^U?DEB$/o) {
		&main::Output(
			"# PROP Use_Debug_Libraries 1\n"
		);
	}
	&main::Output(
		"# PROP Output_Dir \"$ChopRelPath\"\n",
		"# PROP Intermediate_Dir \"$ChopBldPath\"\n",
	);

	&main::Output(
		"# ADD CPP /nologo /Zp4"
	);
	if ($GenTrgType=~/^EXE$/o) {
		&main::Output(
			" /ML"
		);
	}
	elsif ($GenTrgType=~/^DLL$/o) {
		&main::Output(
			" /MD"
		);
	}
	if ($Bld=~/^U?DEB$/o) {
		&main::Output(
			"d"
		);
	}
	&main::Output(
		" /W4"
	);
	if ($Bld=~/^U?REL$/o) {
		&main::Output(
			" /O1 /Ob1"
		);
	}
	elsif ($Bld=~/^U?DEB$/o) {
		&main::Output(
			" /Zi /Od"
		);
	}
	&main::Output(
		"$NotUseWin32LibsFlag"
	);
	foreach (@ChopUserIncPaths,@ChopSysIncPaths) {
		&main::Output(
			" /I \"$_\""
		);
	}
	foreach (@MacroList) {
		&main::Output(
			" /D \"$_\""
		);
	}
	if ($Bld=~/^U?DEB$/o) {
		&main::Output(
			" /FR"
		);
	}
	&main::Output(
		" /FD /c\n",
	);

	if ($GenTrgType=~/^DLL$/o) {
		&main::Output(
			"# ADD MTL /nologo"
		);
		if ($Bld=~/^U?REL$/o) {
			&main::Output(
				" /mktyplib203 /D"
			);
		}
		elsif ($Bld=~/^U?DEB$/o) {
			&main::Output(
				" /D \"_DEBUG\" /mktyplib203"
			);
		}
		&main::Output(
			" /win32\n"
		);
	}

	&main::Output(
		"# ADD BASE RSC /l 0x809\n",
		"# ADD RSC /l 0x809 /d"
	);
	if ($Bld=~/^U?REL$/o) {
		&main::Output(
			" \"NDEBUG\""
		);
	}
	elsif ($Bld=~/^U?DEB$/o) {
		&main::Output(
			" \"_DEBUG\""
		);
	}
	&main::Output(
		"\n"
	);

	&main::Output(
		"BSC32=bscmake.exe\n",
		"# ADD BSC32 /nologo\n",
	);

	&main::Output(
		"LINK32=link.exe\n",
		"# ADD BASE LINK32 /machine:IX86\n",
		"# ADD LINK32"
	);
	&main::Output(
		" $ChopRelPath\\$BaseFirstObj.OBJ"
	);
	foreach (@BaseObjList) {
		&main::Output(
			" ",$ChopRelPath,"\\",$_,'.obj'
		);
	}
	foreach (@Win32LibList) {
		&main::Output(
			" ",lc $_
		);
	}
	foreach (@LibList) {
		&main::Output(
			" $ChopRelPath\\",lc $_
		);
	}
	&main::Output(
		" /nologo"
	);
	if ($GenTrgType=~/^EXE$/o) {
		&main::Output(
			" /entry:\"_E32Startup\""
		);
	}
	elsif ($GenTrgType=~/^DLL$/o) {
		&main::Output(
			"$BaseAddressFlag /entry:\"_E32Dll\""
		);
	}
	if (&main::Plat=~/^WINC$/o && $GenTrgType=~/^EXE$/o) { # shouldn't really have WINC mentioned here
		&main::Output(
			' /subsystem:console'
		);
	}
	else {
		&main::Output(
			' /subsystem:windows'
		);
	}
	if ($GenTrgType=~/^DLL$/o) {
		&main::Output(
			' /dll'
		);
	}
	if ($Bld=~/^U?DEB$/o) {
		&main::Output(
			" /incremental:yes /pdb:\"$ChopRelPath\\$BaseTrg.pdb\" /debug"
		);
	}
	&main::Output(
		' /machine:IX86 /nodefaultlib'
	);
# if there isn't a freeze file but the $TrgType is special put in the correct freeze file by default
	if ($DefFile) {
		&main::Output(
			" /def:\"$DefFile\""
		);
	}
	elsif ($TrgType=~/^APP$/o) {
		&main::Output(
			" /def:\"",&main::SysDefDir,"APP.DEF\""
		);
	}
	elsif ($TrgType=~/^FSY$/o) {
		&main::Output(
			" /def:\"",&main::SysDefDir,"FSY.DEF\""
		);
	}
	elsif ($TrgType=~/^LDD$/o) {
		&main::Output(
				" /def:\"",&main::SysDefDir,"LDD.DEF\""
		);
	}
	elsif ($TrgType=~/^PDD$/o) {
		&main::Output(
				" /def:\"",&main::SysDefDir,"PDD.DEF\""
		);
	}
	&main::Output(
		" /out:\"$ChopRelPath\\$TrgPath$Trg\""
	);
	&main::Output(
		' /WARN:3',
		"\n",
		"\n"
	);
}

sub PMEndBldList {
	my $GenTrgType=&main::GenTrgType;

	# CLOSE THE !IF ... !ENDIF LOOP
	#------------------------------
	&main::Output(
		"!ENDIF \n"
	);
}

sub PMStartSubPrjList {

	my @BldList=&main::BldList;

	&main::Output(
		"# Begin Target\n",
		"\n"
	);
	foreach (@BldList) {
		&main::Output(
			"# Name \"".$IdeBlds{$_}."\"\n"
		);
	}
	&main::Output(
		"# Begin Group \"Source Files\"\n",
		"\n",
		"# PROP Default_Filter \"cpp;c;cxx;rc;def;r;odl;hpj;bat;for;f90\"\n"
	);
}

sub PMStartSubPrjSrc {    

	&main::Output(
		"# Begin Source File\n",
		"\n",
		"SOURCE=",&main::SubPrj,ucfirst lc &main::Src,"\n",
		"# End Source File\n"
	);
}

sub PMSubPrjSrcDepend {

	# Generate user header list for this src, merge with list for all sources
	foreach (&main::DepList) {
		$PrjHdrs{$_}='unusedval';
	}
}

sub PMSubPrjDoc {    

	my $SrcPath=&main::SubPrj;

	&main::Output(
		"# Begin Source File\n",
		"\n",
		"SOURCE=",$SrcPath,ucfirst lc &main::Doc,"\n",
		"# PROP Exclude_From_Build 1\n",
		"# End Source File\n"
	);
}

sub PMStartSubPrjResrc {

	my $Resrc=&main::Resrc;
	my $SrcPath=&main::SubPrj;

	&main::Output(
		"# Begin Source File\n",
		"\n",
		"SOURCE=",$SrcPath,ucfirst lc $Resrc,"\n",
		"# End Source File\n"
	);
}

sub PMEndSubPrjList {

	my $BaseDsp=&main::BaseMak;
	my $PathBaseDsp=&main::MakeFilePath.$BaseDsp;

	&main::Output(
		"# End Group\n",
		"# Begin Group \"Resource Files\"\n",
		"\n",
		"# PROP Default_Filter \"ico;cur;bmp;dlg;rc2;rct;bin;cnt;rtf;gif;jpg;jpeg;jpe\"\n"
	);
	if ($Win32Resrc) {
		&main::Output(
			"# Begin Source File\n",
			"\n",
			"SOURCE=",ucfirst lc $Win32Resrc,"\n",
			"# End Source File\n"
		);
		# Generate user header list for this src, merge with list for all sources
		foreach (&main::Deps_GenDependsL($Win32Resrc)) {
			$PrjHdrs{$_}='unusedval';
		}
	}
	&main::Output(
		"# End Group\n"
	);

	# Use the global %PrjHdrs Hash to produce user header listing
	&main::Output(
		"# Begin Group \"Header Files\"\n",
		"\n",
		"# PROP Default_Filter \"h;hpp;hxx;hm;inl;fi;fd\"\n"
	);
	foreach (keys %PrjHdrs) {
		&main::Output(
			"# Begin Source File\n",
			"\n",
			"SOURCE=",&main::Path_Split('Path',$_),ucfirst lc &main::Path_Split('File',$_),"\n",
			"# End Source File\n"
		);
	}	
	&main::Output(
		"# End Group\n",
		"# End Target\n",
		"# End Project\n",
	);

	&main::Path_DelFiles("$PathBaseDsp.MAK","$PathBaseDsp.MDP","$PathBaseDsp.NCB","$PathBaseDsp.OPT","$PathBaseDsp.PLG");

# create the .DSW file
	my $DswText=join(
		"\n",
		'Microsoft Developer Studio Workspace File, Format Version 5.00',
		'# WARNING: DO NOT EDIT OR DELETE THIS WORKSPACE FILE!',
		'',
		'#'x79,
		'',
		"Project: \"$BaseDsp\"=.\\$BaseDsp.dsp - Package Owner=<4>",
		'',
		'Package=<5>',
		'{{{',
		'}}}',
		'',
		'Package=<4>',
		'{{{',
		'}}}',
		'',
		'#'x79,
		'',
		'Global:',
		'',
		'Package=<5>',
		'{{{',
		'}}}',
		'',
		'Package=<3>',
		'{{{',
		'}}}',
		'',
		'#'x79,
		''
	);

	&main::CreateExtraFile("$PathBaseDsp.DSW",$DswText);
}

1;
