diff --git a/converter/convertdelphi.pas b/converter/convertdelphi.pas index 6d41f46ef9..ef9f28c40b 100644 --- a/converter/convertdelphi.pas +++ b/converter/convertdelphi.pas @@ -146,6 +146,7 @@ type // Units that are found and will be added to project and converted. fUnitsToAddToProject: TStringList; fSettings: TConvertSettings; + fUseThreads: boolean; // The project/package uses TThread. function ConvertSub: TModalResult; procedure CleanUpCompilerOptionsSearchPaths(Options: TBaseCompilerOptions); procedure SetCompilerModeForDefineTempl(DefTempl: TDefineTemplate); @@ -464,6 +465,7 @@ end; destructor TConvertDelphiUnit.Destroy; begin + fUsedUnitsTool.Free; fCTLink.Free; if fOwnerConverter=nil then fSettings.Free; @@ -676,7 +678,6 @@ begin if Assigned(fUsedUnitsTool) then begin Result:=fUsedUnitsTool.Convert; if Result<>mrOk then exit; - FreeAndNil(fUsedUnitsTool); end; Result:=mrOk; end; @@ -782,6 +783,7 @@ constructor TConvertDelphiPBase.Create(const AFilename, ADescription: string); begin fOrigPFilename:=AFilename; fIsConsoleApp:=False; // Default = GUI app. + fUseThreads:=True; // For testing. fUnitSearchPaths:=TStringList.Create; fUnitSearchPaths.Delimiter:=';'; fUnitSearchPaths.StrictDelimiter:=True; @@ -1463,8 +1465,13 @@ begin Result:=ConvertOne(CurUnitInfo); if Result<>mrOK then Break; end; - if Result=mrOK then + if Result=mrOK then begin + if fUseThreads then begin + Result:=fMainUnitConverter.fUsedUnitsTool.AddThreadSupport; + if Result<>mrOK then exit; + end; Result:=ConvertAllFormFiles(ConvUnits); + end; finally ConvUnits.Free; // Owns and frees converter objects. end; diff --git a/converter/usedunits.pas b/converter/usedunits.pas index 62a972599f..63ceffa8d8 100644 --- a/converter/usedunits.pas +++ b/converter/usedunits.pas @@ -121,6 +121,7 @@ type function Convert: TModalResult; procedure MoveMissingToComment(AAllCommentedUnits: TStrings); procedure AddUnitIfNeeded(AUnitName: string); + function AddThreadSupport: TModalResult; public property MainUsedUnits: TUsedUnits read fMainUsedUnits; property ImplUsedUnits: TUsedUnits read fImplUsedUnits; @@ -629,6 +630,25 @@ begin end; end; +function TUsedUnitsTool.AddThreadSupport: TModalResult; +// AddUnitToSpecificUsesSection would insert cthreads in the beginning automatically +// It doesn't work with {$IFDEF UNIX} directive -> use UsesInsertPolicy. +var + OldPolicy: TUsesInsertPolicy; +begin + Result:=mrCancel; + with fCTLink do + try + OldPolicy:=SrcCache.BeautifyCodeOptions.UsesInsertPolicy; + SrcCache.BeautifyCodeOptions.UsesInsertPolicy:=uipFirst; + if not CodeTool.AddUnitToSpecificUsesSection(fMainUsedUnits.fUsesSection, + '{$IFDEF UNIX}cthreads{$ENDIF}', '', SrcCache) then exit; + finally + SrcCache.BeautifyCodeOptions.UsesInsertPolicy:=OldPolicy; + end; + Result:=mrOK; +end; + function TUsedUnitsTool.GetMissingUnitCount: integer; begin Result:=fMainUsedUnits.fMissingUnits.Count+fImplUsedUnits.fMissingUnits.Count;