IDE/lazbuild: building IDE: fixed using lazarus.new.exe if lazarus.exe cannot be renamed

git-svn-id: branches/fixes_1_4@48608 -
This commit is contained in:
mattias 2015-04-03 09:47:25 +00:00
parent 980c56ebf5
commit 12296c5cd6

View File

@ -68,7 +68,6 @@ type
blfOnlyIDE, // skip all but IDE (for example build IDE, but not packages, not lazbuild, ...) blfOnlyIDE, // skip all but IDE (for example build IDE, but not packages, not lazbuild, ...)
blfDontClean, // ignore clean up option in profile blfDontClean, // ignore clean up option in profile
blfUseMakeIDECfg, // append @idemake.cfg blfUseMakeIDECfg, // append @idemake.cfg
blfReplaceExe, // ignore OSLocksExecutables and do not create lazarus.new.exe
blfBackupOldExe // rename existing lazarus exe to lazarus.old blfBackupOldExe // rename existing lazarus exe to lazarus.old
); );
TBuildLazarusFlags = set of TBuildLazarusFlag; TBuildLazarusFlags = set of TBuildLazarusFlag;
@ -410,6 +409,7 @@ var
var var
IdeBuildMode: TIdeBuildMode; IdeBuildMode: TIdeBuildMode;
s: String; s: String;
DefaultTargetFilename: String;
begin begin
// Get target files and directories. // Get target files and directories.
Result:=mrCancel; Result:=mrCancel;
@ -501,6 +501,14 @@ begin
if (not fOutputDirRedirected) and (not CheckDirectoryWritable(fWorkingDir)) then if (not fOutputDirRedirected) and (not CheckDirectoryWritable(fWorkingDir)) then
exit(mrCancel); exit(mrCancel);
// fTargetFilename may be lazarus.new.exe, append -o
// Note: FPC automatically changes the last extension (append or replace)
// For example under linux, where executables don't need any extension
// fpc removes the last extension of the -o option.
DefaultTargetFilename:='lazarus'+GetExecutableExt(fTargetOS);
if CreateRelativePath(fTargetFilename,fTargetDir) <> DefaultTargetFilename then
AppendExtraOption('-o'+fTargetFilename);
if fExtraOptions<>'' then if fExtraOptions<>'' then
EnvironmentOverrides.Values['OPT'] := fExtraOptions; EnvironmentOverrides.Values['OPT'] := fExtraOptions;
if not fUpdateRevInc then begin if not fUpdateRevInc then begin
@ -560,7 +568,6 @@ function TLazarusBuilder.CalcTargets(Flags: TBuildLazarusFlags): TModalResult;
var var
LazDir, TargetLCLPlatform: string; LazDir, TargetLCLPlatform: string;
DefaultTargetFilename: String;
IsCrossCompiling: Boolean; IsCrossCompiling: Boolean;
s: String; s: String;
begin begin
@ -724,18 +731,18 @@ begin
// so make sure the directory doesn't end with the path delimiter. // so make sure the directory doesn't end with the path delimiter.
AppendExtraOption('-FE'+ChompPathDelim(fTargetDir)); AppendExtraOption('-FE'+ChompPathDelim(fTargetDir));
// Note: FPC automatically changes the last extension (append or replace) // Important: Do not append -o here, because if the old exe cannot be
// For example under linux, where executables don't need any extension // renamed/deleted it needs to be changed.
// fpc removes the last extension of the -o option.
DefaultTargetFilename:='lazarus'+GetExecutableExt(fTargetOS);
if CreateRelativePath(fTargetFilename,fTargetDir) <> DefaultTargetFilename then
AppendExtraOption('-o'+fTargetFilename);
end; end;
//DebugLn(['CreateIDEMakeOptions ',MMDef.Name,' ',fExtraOptions]); //DebugLn(['CreateIDEMakeOptions ',MMDef.Name,' ',fExtraOptions]);
end; end;
procedure TLazarusBuilder.BackupExe(Flags: TBuildLazarusFlags); procedure TLazarusBuilder.BackupExe(Flags: TBuildLazarusFlags);
{ Try to delete old backups and try to rename old exe.
Some OS (Win) locks the exe while running, so it cannot be deleted.
Some OS (Win XP) forbids renaming while exe is running.
}
var var
Ext: String; Ext: String;
BackupFilename: String; BackupFilename: String;
@ -747,15 +754,14 @@ begin
Ext:=ExtractFileExt(fTargetFilename); Ext:=ExtractFileExt(fTargetFilename);
AltFilename:=LeftStr(fTargetFilename,length(fTargetFilename)-length(Ext))+'.new'+Ext; AltFilename:=LeftStr(fTargetFilename,length(fTargetFilename)-length(Ext))+'.new'+Ext;
if blfBackupOldExe in Flags then begin if blfBackupOldExe in Flags then begin
// always delete the lazarus.new exe, so that users/startlazarus are not // first try to delete the lazarus.new exe, so that users/startlazarus are
// confused which one is the newest // not confused which one is the newest.
// This may fail if OS has locked the exe.
if FileExistsUTF8(AltFilename) then begin if FileExistsUTF8(AltFilename) then begin
case DeleteFileInteractive(AltFilename,[mbIgnore]) of if DeleteFileUTF8(AltFilename) then
mrIgnore: debugln(['Note: (lazarus) deleted file "',AltFilename,'"'])
else
debugln(['Warning: (lazarus) unable to delete file "',AltFilename,'"']); debugln(['Warning: (lazarus) unable to delete file "',AltFilename,'"']);
mrOk:
debugln(['Note: (lazarus) deleted file "',AltFilename,'"']);
end;
end; end;
// try to rename the old exe // try to rename the old exe
@ -768,33 +774,37 @@ begin
// => try to backup the backup // => try to backup the backup
Backup2Filename:=LeftStr(fTargetFilename,length(fTargetFilename)-length(Ext))+'.old2'+Ext; Backup2Filename:=LeftStr(fTargetFilename,length(fTargetFilename)-length(Ext))+'.old2'+Ext;
if FileExistsUTF8(Backup2Filename) then begin if FileExistsUTF8(Backup2Filename) then begin
case DeleteFileInteractive(Backup2Filename) of if DeleteFileUTF8(Backup2Filename) then
mrOk:
debugln(['Note: deleted backup "',Backup2Filename,'"']) debugln(['Note: deleted backup "',Backup2Filename,'"'])
else else
debugln(['WARNING: unable to delete old backup file "'+Backup2Filename+'"']); debugln(['WARNING: unable to delete old backup file "'+Backup2Filename+'"']);
end;
end; end;
if not FileExistsUTF8(Backup2Filename) then begin if not FileExistsUTF8(Backup2Filename) then begin
case RenameFileWithErrorDialogs(BackupFilename,Backup2Filename) of if RenameFileUTF8(BackupFilename,Backup2Filename) then
mrOk:
debugln(['Note: renamed old backup file "'+BackupFilename+'" to "',Backup2Filename,'"']) debugln(['Note: renamed old backup file "'+BackupFilename+'" to "',Backup2Filename,'"'])
else else
debugln(['WARNING: unable to rename old backup file "'+BackupFilename+'" to "',Backup2Filename,'"']); debugln(['WARNING: unable to rename old backup file "'+BackupFilename+'" to "',Backup2Filename,'"']);
end;
end; end;
end; end;
end; end;
if not FileExistsUTF8(BackupFilename) then begin if not FileExistsUTF8(BackupFilename) then begin
case RenameFileWithErrorDialogs(fTargetFilename,BackupFilename) of if RenameFileUTF8(fTargetFilename,BackupFilename) then
mrOk:
debugln(['Note: renamed file "'+fTargetFilename+'" to "',BackupFilename,'"']) debugln(['Note: renamed file "'+fTargetFilename+'" to "',BackupFilename,'"'])
else else
debugln(['WARNING: unable to rename file "'+fTargetFilename+'" to "',BackupFilename,'"']); debugln(['WARNING: unable to rename file "'+fTargetFilename+'" to "',BackupFilename,'"']);
end; end;
if FileExistsUTF8(fTargetFilename)
and FileExistsUTF8(AltFilename) then begin
IDEMessageDialog('Delete Error','Unable to rename'#13
+fTargetFilename+#13
+'and unable to delete'#13
+AltFilename+#13
+'One of them must be gone, before building the IDE. Maybe you have another IDE still running?',mtError,[mbCancel]);
exit;
end; end;
end; end;
if (not (blfReplaceExe in Flags)) and FileExistsUTF8(fTargetFilename) then if FileExistsUTF8(fTargetFilename) then
fTargetFilename:=AltFilename; // backup didn't work => use another file name fTargetFilename:=AltFilename; // backup didn't work => use another file name
end; end;