# WINUTL.PM
#
# Copyright (c) 1997-1999 Symbian Ltd.  All rights reserved.
#


# this package does various ancillary things for windows modules

package Winutl;

my $BaseAddress='';
my $NoBrowseInfo='';
my @Win32LibList=();
my $Win32Resrc='';

require Exporter;
@ISA=qw(Exporter);
@EXPORT=qw(
	Winutl_Help_Mmp
	Winutl_DoMmp

	Winutl_BaseAddress
	Winutl_NoBrowseInfo
	Winutl_Win32LibList
	Winutl_Win32Resrc

	Winutl_GetCleanFileList
	Winutl_GetWorkPathList
);


sub Winutl_Help_Mmp {
# provide the help text for START <windows platforms> END blocks

	print
		"BASEADDRESS    [base address for dll loading]\n",
		"NOBROWSEINFO   // relates to command-line makefiles only\n",
		"WIN32_LIBRARY  [win32 libraries]\n",
		"WIN32_RESOURCE  [win32 resource]\n"
	;
}

sub Winutl_DoMmp () {
# process the START <windows platforms> END blocks

	my $BaseTrg=&main::BaseTrg;
	my $GenTrgType=&main::GenTrgType;
	my $MakeFilePath=&main::MakeFilePath;
	my $MMPFILE=&main::MmpFile;
	my @UidList=&main::UidList;
	my @UnicodeUidList=&main::UnicodeUidList;

	# set up START WINS ... END block module variables
	my @MmpWarn=();
	my $Line;
	LINE: foreach $Line (&main::PlatTxt) {
		my $LineNum=shift @$Line;
		$_=shift @$Line;
		if (/^NOBROWSEINFO$/o) {
			if (@$Line) {
				push @MmpWarn, "$LineNum: Keyword NOBROWSEINFO takes no arguments\n";
			}
			$NoBrowseInfo='True';
			next LINE;
		}
		if (/^BASEADDRESS$/o) {
			if (@$Line) {
				$BaseAddress=shift @$Line;
				$BaseAddress=~s/X/x/o;
				next LINE;
			}
			push @MmpWarn, "$LineNum: No base address specified for keyword BASEADDRESS\n";
			next LINE;
		}
		if (/^WIN32_LIBRARY$/o) {
			if (push @Win32LibList, @$Line) {
				next LINE;
			}
			push @MmpWarn, "$LineNum: No libraries specified for keyword WIN32_LIBRARY\n";
			next LINE;
		}
		if (/^WIN32_RESOURCE$/o) {
			if (@$Line) {
				$Win32Resrc=shift @$Line;
				next LINE;
			}
			push @MmpWarn, "$LineNum: No resource specified for keyword WIN32RESOURCE\n";
			next LINE;
		}
		push @MmpWarn, "$LineNum: Unrecognised Keyword \"$_\"\n";
	}

	undef $Line;
	if (@MmpWarn) {
		warn
			"\nMMPFILE \"",$MMPFILE,"\"\n",
			"START .. END BLOCK WARNINGS(S)\n",
			@MmpWarn,
			"\n"
		;
	}
	undef @MmpWarn;

	if ($GenTrgType!~/^LIB$/o) {
		# create the UID source file
		my $UidText=join(
			"\n",
			'// Makmake-generated uid source file',
			'#include <E32STD.H>',
			'#pragma data_seg(".E32_UID")',
			"#if defined(_UNICODE)\n"
		);
		$UidText.='__WINS_UID(';
		foreach (@UnicodeUidList) {
			$UidText.="$_,";
		}
		chop $UidText;
		$UidText.=")\n";
		$UidText.="#else\n";
		$UidText.='__WINS_UID(';
		foreach (@UidList) {
			$UidText.="$_,";
		}
		chop $UidText;
		$UidText.=")\n";
		$UidText.="#endif\n";
		$UidText.="#pragma data_seg()\n";
		&main::AddSrc("$MakeFilePath$BaseTrg.UID.CPP", $UidText);
	}


	# if Win32 Libraries required - set the Windows standard include paths
	if (@Win32LibList  || $Win32Resrc) {	# show where to find win32 libraries
		# include env variable takes everything between ';', including spaces and '"', as part of path
		&main::SetStdIncPaths(split ';', uc $ENV{INCLUDE});
		&main::AddPlatMacros('WIN32','_WINDOWS');
	}
}

sub Winutl_BaseAddress () {
	$BaseAddress;
}

sub Winutl_NoBrowseInfo () {
	$NoBrowseInfo;
}

sub Winutl_Win32LibList () {
	@Win32LibList;
}

sub Winutl_Win32Resrc () {
	$Win32Resrc;
}

sub Winutl_GetCleanFileList () {
# return a list of files that all the windows modules will want to put in the clean list
	my @BaseSrcList=&main::BaseSrcList;
	my $BaseTrg=&main::BaseTrg;
	my $BldPath=&main::BldPath;
	my $GenTrgType=&main::GenTrgType;
	my $RelPath=&main::RelPath;
	my $Trg=&main::Trg;
	my $TrgPath=&main::TrgPath;
	my $TrgType=&main::TrgType;

	my @List=();
	push @List, "$RelPath$BaseTrg.BSC";
	foreach (@BaseSrcList) {
		push @List, "$BldPath$_.SBR";
	}
	push @List, "$RelPath$TrgPath$Trg";
	foreach (@BaseSrcList) {
		push @List, "$BldPath$_.OBJ";
	}	
	if ($TrgType=~/^LIB$/o) {
		push @List, "$RelPath$BaseTrg.OBJ";
	}
	if ($Win32Resrc) {
		my $BaseWin32Resrc=&main::Path_Split('Base',$Win32Resrc);
		push @List, "$BldPath$BaseWin32Resrc.RES";
	}
	if ($GenTrgType=~/^(DLL|LIB)$/o) {
		push @List, "$RelPath$BaseTrg.lib","$RelPath$BaseTrg.EXP";
	}
	push @List, "$RelPath$BaseTrg.ilk","$RelPath$BaseTrg.PDB";
	@List;
}

sub Winutl_GetWorkPathList () {
# return a list of directories that all the windows modules will want to create for -makework
	my $RelPath=&main::RelPath;

	my @List=();
	push @List, &main::BldPath;
	push @List, $RelPath.&main::TrgPath;
	if (&main::SysResrcList) {
		push @List, $RelPath.&main::DataPath;
	}
	@List;
}

1;
