Converter: Treat a console application in a special way.

git-svn-id: trunk@26355 -
This commit is contained in:
juha 2010-06-30 15:53:15 +00:00
parent 5206990f73
commit 4755ecab2b
2 changed files with 51 additions and 5 deletions

View File

@ -50,6 +50,7 @@ type
constructor Create(Code: TCodeBuffer); constructor Create(Code: TCodeBuffer);
destructor Destroy; override; destructor Destroy; override;
function Convert: TModalResult; function Convert: TModalResult;
function FindApptypeConsole: boolean;
function RemoveUnits: boolean; function RemoveUnits: boolean;
function RenameUnits: boolean; function RenameUnits: boolean;
function UsesSectionsToUnitnames: TStringList; function UsesSectionsToUnitnames: TStringList;
@ -155,6 +156,22 @@ begin
end; end;
end; end;
function TConvDelphiCodeTool.FindApptypeConsole: boolean;
// Return true if there is {$APPTYPE CONSOLE} directive.
var
ParamPos, ACleanPos: Integer;
begin
Result:=false;
ACleanPos:=0;
with fCodeTool do begin
BuildTree(true);
ACleanPos:=FindNextCompilerDirectiveWithName(Src, 1, 'Apptype',
Scanner.NestedComments, ParamPos);
if (ACleanPos>0) and (ACleanPos<=SrcLen) and (ParamPos>0) then
Result:=LowerCase(copy(Src,ParamPos,7))='console';
end;
end;
function TConvDelphiCodeTool.AddDelphiAndLCLSections: boolean; function TConvDelphiCodeTool.AddDelphiAndLCLSections: boolean;
// add, remove and rename units for desired target. // add, remove and rename units for desired target.

View File

@ -126,6 +126,7 @@ type
fDelphiPFilename: string; // .dpr or .dpk file name fDelphiPFilename: string; // .dpr or .dpk file name
fLazPSuffix: string; // '.lpi' or '.lpk' fLazPSuffix: string; // '.lpi' or '.lpk'
fDelphiPSuffix: string; // '.dpr' or '.dpk' fDelphiPSuffix: string; // '.dpr' or '.dpk'
fIsConsoleApp: Boolean;
// Units found in user defined paths. // Units found in user defined paths.
fCachedUnitNames: TStringToStringTree; fCachedUnitNames: TStringToStringTree;
// Map of case incorrect unit name -> real unit name. // Map of case incorrect unit name -> real unit name.
@ -153,6 +154,7 @@ type
protected protected
function CreateInstance: TModalResult; virtual; abstract; function CreateInstance: TModalResult; virtual; abstract;
function CreateMainSourceFile: TModalResult; virtual; function CreateMainSourceFile: TModalResult; virtual;
function ScanMainSourceFile: TModalResult; virtual;
function ConvertMainSourceFile: TModalResult; virtual; function ConvertMainSourceFile: TModalResult; virtual;
function FindAllUnits: TModalResult; virtual; abstract; function FindAllUnits: TModalResult; virtual; abstract;
function ConvertAllUnits: TModalResult; virtual; abstract; function ConvertAllUnits: TModalResult; virtual; abstract;
@ -190,6 +192,7 @@ type
protected protected
function CreateInstance: TModalResult; override; function CreateInstance: TModalResult; override;
function CreateMainSourceFile: TModalResult; override; function CreateMainSourceFile: TModalResult; override;
function ScanMainSourceFile: TModalResult; override;
function ConvertMainSourceFile: TModalResult; override; function ConvertMainSourceFile: TModalResult; override;
function FindAllUnits: TModalResult; override; function FindAllUnits: TModalResult; override;
function ConvertAllUnits: TModalResult; override; function ConvertAllUnits: TModalResult; override;
@ -531,8 +534,8 @@ begin
if Result<>mrOk then exit; if Result<>mrOk then exit;
end; end;
// check LCL path only for projects/packages. // Check LCL path for single files. They are correct when converting projects.
if Assigned(fOwnerConverter) then begin if not Assigned(fOwnerConverter) then begin
Result:=CheckFilenameForLCLPaths(fLazUnitFilename); Result:=CheckFilenameForLCLPaths(fLazUnitFilename);
if Result<>mrOk then exit; if Result<>mrOk then exit;
end; end;
@ -759,6 +762,10 @@ begin
for i:=fMissingUnits.Count-1 downto 0 do begin for i:=fMissingUnits.Count-1 downto 0 do begin
UnitN:=fMissingUnits[i]; UnitN:=fMissingUnits[i];
if UnitUpdater.FindReplacement(UnitN, s) then begin if UnitUpdater.FindReplacement(UnitN, s) then begin
// Don't replace Windows with LCL units in a console application.
if Assigned(fOwnerConverter) and fOwnerConverter.fIsConsoleApp and
(LowerCase(UnitN)='windows') then
s:='';
if fSettings.AutoReplaceUnits then if fSettings.AutoReplaceUnits then
RenameOrRemoveUnit(UnitN, s) // Automatic rename / remove. RenameOrRemoveUnit(UnitN, s) // Automatic rename / remove.
else else
@ -835,6 +842,7 @@ end;
constructor TConvertDelphiPBase.Create(const AFilename, ADescription: string); constructor TConvertDelphiPBase.Create(const AFilename, ADescription: string);
begin begin
fOrigPFilename:=AFilename; fOrigPFilename:=AFilename;
fIsConsoleApp:=False; // Default = GUI app.
fCachedUnitNames:=TStringToStringTree.Create(false); fCachedUnitNames:=TStringToStringTree.Create(false);
fCachedRealFileNames:=TStringToStringTree.Create(true); fCachedRealFileNames:=TStringToStringTree.Create(true);
fSettings:=TConvertSettings.Create('Convert Delphi '+ADescription); fSettings:=TConvertSettings.Create('Convert Delphi '+ADescription);
@ -899,9 +907,12 @@ begin
RemoveNonExistingFiles(false); RemoveNonExistingFiles(false);
CleanUpCompilerOptionsSearchPaths(CompOpts); CleanUpCompilerOptionsSearchPaths(CompOpts);
// Scan LPR file for directives. Sets fIsConsoleApp flag.
// load required packages Result:=ScanMainSourceFile;
AddPackageDependency('LCL'); // Nearly all Delphi projects require it. if Result<>mrOK then exit;
// LCL dependency is added automatically later for GUI applications.
// AddPackageDependency('LCL');
// ToDo: make an option to add NoGUI to Project.CompilerOptions.LCLWidgetType.
if fProjPack is TProject then if fProjPack is TProject then
PkgBoss.AddDefaultDependencies(fProjPack as TProject); PkgBoss.AddDefaultDependencies(fProjPack as TProject);
CustomDefinesChanged; CustomDefinesChanged;
@ -1223,6 +1234,11 @@ begin
Result:=mrOK; // Do nothing. Overridden in project. Result:=mrOK; // Do nothing. Overridden in project.
end; end;
function TConvertDelphiPBase.ScanMainSourceFile: TModalResult;
begin
Result:=mrOK; // Do nothing. Overridden in project.
end;
function TConvertDelphiPBase.ConvertMainSourceFile: TModalResult; function TConvertDelphiPBase.ConvertMainSourceFile: TModalResult;
begin begin
Result:=mrOK; // Do nothing. Overridden in project. Result:=mrOK; // Do nothing. Overridden in project.
@ -1297,6 +1313,19 @@ begin
Result:=mrOk; Result:=mrOk;
end; end;
function TConvertDelphiProject.ScanMainSourceFile: TModalResult;
var
ConvTool: TConvDelphiCodeTool;
begin
Result:=mrOK;
ConvTool:=TConvDelphiCodeTool.Create(fMainUnitConverter.fPascalBuffer);
try
fIsConsoleApp:=ConvTool.FindApptypeConsole;
finally
ConvTool.Free;
end;
end;
function TConvertDelphiProject.ConvertMainSourceFile: TModalResult; function TConvertDelphiProject.ConvertMainSourceFile: TModalResult;
begin begin
// Loading was done earlier. Now just convert. // Loading was done earlier. Now just convert.