 {
 Compile .DPR project file from Object Pascal (Delphi) source.
 ProjectFile is DPR or EXE file name, they must be homonymous.
 Timeout, ms is deadline time to compile project .DPR to .EXE.
 Forced=False means recompile only if EXE file not exists yet.
 Forced=True  means recompile even if EXE file already exists.
 Note: call the procedure at Polling, but not at Starting run.
 For example, if (RunCount=10) then CompileDprDroject(...);
 }
 procedure CompileDprProject(ProjectFile:String; Timeout:Integer; Forced:Boolean);
 var ms,Deadline:Real; Dpr,Exe,Map,TmpExe,TmpMap:String;
  procedure Waiting(Delay:Integer);
  begin
   rNul(wdt_Reset(True));
   bNul(Sleep(Delay));
  end;
 begin
  ms:=mSecNow;
  Exe:=''; Dpr:=''; Map:=''; TmpExe:=''; TmpMap:='';
  if not IsEmptyStr(ProjectFile) then begin
   ProjectFile:=DaqFileRef(Trim(ProjectFile),'');
   Dpr:=ForceExtension(ProjectFile,'.DPR');
   Exe:=ForceExtension(ProjectFile,'.EXE');
   Map:=ForceExtension(ProjectFile,'.MAP');
   TmpExe:=DaqFileRef('~~\Resource\Dcc32\exe\'+ExtractFileName(Exe),'.EXE');
   TmpMap:=DaqFileRef('~~\Resource\Dcc32\exe\'+ExtractFileName(Exe),'.MAP');
   if IsSameText(ExtractFileExt(ProjectFile),'.DLL') then begin
    TmpExe:=ForceExtension(TmpExe,'.DLL');
    Exe:=ForceExtension(Exe,'.DLL');
   end;
   if FileExists(Dpr) and (Forced or not FileExists(Exe)) then begin
    if not FileErase(TmpExe) then Problem('Could no erase '+TmpExe);
    if not FileErase(TmpMap) then Problem('Could no erase '+TmpMap);
    rNul(Eval('@system @async @compile '+Dpr));
    Deadline:=ms+Timeout;
    while (mSecNow<Deadline) do begin
     if FileExists(TmpExe) then
     if FileExists(TmpMap) then Deadline:=0;
     if Deadline>0 then Waiting(10);
    end;
    Deadline:=ms+Timeout;
    while (mSecNow<Deadline) do begin
     if FileRename(URL_Packed(TmpMap)+' '+URL_Packed(Map)) then Deadline:=0;
     if Deadline>0 then Waiting(10);
    end;
    Waiting(100);
    Deadline:=ms+Timeout;
    while (mSecNow<Deadline) do begin
     if FileRename(URL_Packed(TmpExe)+' '+URL_Packed(Exe)) then Deadline:=0;
     if Deadline>0 then Waiting(10);
    end;
    if FileExists(TmpExe) then
    if not FileRename(URL_Packed(TmpExe)+' '+URL_Packed(Exe))
    then Problem('Could not rename "'+TmpExe+'" to "'+Exe+'".');
    if FileExists(Dpr) and FileExists(Exe) and not FileExists(TmpExe)
    then Success('Project "'+Dpr+'" compiled to "'+Exe+'", '+Str(mSecNow-ms)+' ms elapsed.');
   end;
  end;
  Exe:=''; Dpr:=''; Map:=''; TmpExe:=''; TmpMap:='';
 end;
