fpintf.pas 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. {
  2. $Id$
  3. This file is part of the Free Pascal Integrated Development Environment
  4. Copyright (c) 1998 by Berczi Gabor
  5. Misc routines for the IDE
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. {$i globdir.inc}
  13. unit FPIntf;
  14. {$ifdef FPC}
  15. {$ifndef COMPILER_1_0}
  16. {$mode objfpc}
  17. {$endif COMPILER_1_0}
  18. {$endif FPC}
  19. interface
  20. { Run }
  21. function GetRunParameters: string;
  22. procedure SetRunParameters(const Params: string);
  23. { Compile }
  24. procedure Compile(const FileName, ConfigFile: string);
  25. procedure SetPrimaryFile(const fn:string);
  26. function LinkAfter : boolean;
  27. {$ifdef USE_EXTERNAL_COMPILER}
  28. function version_string : string;
  29. {$endif USE_EXTERNAL_COMPILER}
  30. implementation
  31. uses
  32. Compiler,Comphook,
  33. {$ifdef FPC}
  34. {$ifndef COMPILER_1_0}
  35. sysutils,
  36. {$endif COMPILER_1_0}
  37. {$endif FPC}
  38. {$ifndef NODEBUG}
  39. FPDebug,
  40. {$endif NODEBUG}
  41. FPRedir,FPVars,FpCompil,
  42. FPUtils,FPSwitch,WUtils;
  43. {****************************************************************************
  44. Run
  45. ****************************************************************************}
  46. var
  47. RunParameters : string;
  48. function LinkAfter : boolean;
  49. begin
  50. LinkAfter:=LinkAfterSwitches^.GetBooleanItem(0);
  51. end;
  52. function GetRunParameters: string;
  53. begin
  54. GetRunParameters:=RunParameters;
  55. end;
  56. procedure SetRunParameters(const Params: string);
  57. begin
  58. RunParameters:=Params;
  59. {$ifndef NODEBUG}
  60. If assigned(Debugger) then
  61. Debugger^.SetArgs(RunParameters);
  62. {$endif}
  63. end;
  64. {****************************************************************************
  65. Compile
  66. ****************************************************************************}
  67. var
  68. CatchErrorLongJumpBuffer : jmp_buf;
  69. procedure CatchCompilationErrors;
  70. begin
  71. LongJmp(CatchErrorLongJumpBuffer,1);
  72. end;
  73. procedure Compile(const FileName, ConfigFile: string);
  74. var
  75. cmd : string;
  76. ExitReason : integer;
  77. ExitAddr,StoreExitProc : pointer;
  78. {$ifdef USE_EXTERNAL_COMPILER}
  79. CompilerOut : Text;
  80. CompilerOutputLine : longint;
  81. V,p,p1,p2,lineNb,ColumnNb : longint;
  82. error : word;
  83. ModuleName,Line : string;
  84. error_in_reading : boolean;
  85. {$endif USE_EXTERNAL_COMPILER}
  86. begin
  87. {$ifndef USE_EXTERNAL_COMPILER}
  88. cmd:='-d'+SwitchesModeStr[SwitchesMode];
  89. if ConfigFile<>'' then
  90. cmd:='['+ConfigFile+'] '+cmd;
  91. {$else USE_EXTERNAL_COMPILER}
  92. cmd:='-n -d'+SwitchesModeStr[SwitchesMode];
  93. if ConfigFile<>'' then
  94. cmd:='@'+ConfigFile+' '+cmd;
  95. if not UseExternalCompiler then
  96. {$endif USE_EXTERNAL_COMPILER}
  97. { Add the switches from the primary file }
  98. if PrimaryFileSwitches<>'' then
  99. cmd:=cmd+' '+PrimaryFileSwitches;
  100. cmd:=cmd+' '+FileName;
  101. { call the compiler }
  102. {$ifdef USE_EXTERNAL_COMPILER}
  103. if UseExternalCompiler then
  104. begin
  105. If not LocateExeFile(ExternalCompilerExe) then
  106. begin
  107. CompilerMessageWindow^.AddMessage(
  108. 0,ExternalCompilerExe+' not found','',0,0);
  109. exit;
  110. end;
  111. CompilerMessageWindow^.AddMessage(
  112. 0,'Running: '+ExternalCompilerExe+' '+cmd,'',0,0);
  113. if not ExecuteRedir(ExternalCompilerExe,cmd,'','ppc___.out','ppc___.err') then
  114. begin
  115. CompilerMessageWindow^.AddMessage(
  116. V_error,msg_errorinexternalcompilation,'',0,0);
  117. CompilerMessageWindow^.AddMessage(
  118. V_error,FormatStrInt(msg_iostatusis,IOStatus),'',0,0);
  119. CompilerMessageWindow^.AddMessage(
  120. V_error,FormatStrInt(msg_executeresultis,ExecuteResult),'',0,0);
  121. if IOStatus<>0 then
  122. exit;
  123. end;
  124. Assign(CompilerOut,'ppc___.out');
  125. Reset(CompilerOut);
  126. error_in_reading:=false;
  127. CompilerOutputLine:=0;
  128. While not eof(CompilerOut) do
  129. begin
  130. readln(CompilerOut,Line);
  131. Inc(CompilerOutputLine);
  132. p:=pos('(',line);
  133. if p>0 then
  134. begin
  135. ModuleName:=copy(Line,1,p-1);
  136. Line:=Copy(Line,p+1,255);
  137. p1:=pos(',',Line);
  138. val(copy(Line,1,p1-1),lineNb,error);
  139. Line:=Copy(Line,p1+1,255);
  140. p2:=pos(')',Line);
  141. if error=0 then
  142. val(copy(Line,1,p2-1),ColumnNb,error);
  143. Line:=Copy(Line,p2+1,255);
  144. V:=0;
  145. { using constants here isn't a good idea, because this won't
  146. work with localized versions of the compiler - Gabor }
  147. If Pos(' Error:',line)=1 then
  148. begin
  149. V:=V_error;
  150. Line:=Copy(Line,8,Length(Line));
  151. end
  152. else if Pos(' Fatal:',line)=1 then
  153. begin
  154. V:=V_fatal;
  155. Line:=Copy(Line,8,Length(Line));
  156. end
  157. else if Pos(' Hint:',line)=1 then
  158. begin
  159. V:=V_hint;
  160. Line:=Copy(Line,7,Length(Line));
  161. end
  162. else if Pos(' Note:',line)=1 then
  163. begin
  164. V:=V_note;
  165. Line:=Copy(Line,7,Length(Line));
  166. end;
  167. if error=0 then
  168. CompilerMessageWindow^.AddMessage(V,Line,ModuleName,LineNb,ColumnNb)
  169. else
  170. error_in_reading:=true;
  171. end
  172. else
  173. CompilerMessageWindow^.AddMessage(0,Line,'',0,0);
  174. ;
  175. end;
  176. Close(CompilerOut);
  177. end
  178. else
  179. {$endif USE_EXTERNAL_COMPILER}
  180. begin
  181. {$ifdef COMPILER_1_0}
  182. storeexitproc:=exitproc;
  183. if SetJmp(CatchErrorLongJumpBuffer)=0 then
  184. begin
  185. exitproc:=@CatchCompilationErrors;
  186. {$else : not COMPILER_1_0}
  187. try
  188. {$endif COMPILER_1_0}
  189. Compiler.Compile(cmd);
  190. {$ifdef COMPILER_1_0}
  191. end
  192. else
  193. begin
  194. ExitReason:=ExitCode;
  195. ExitCode:=0;
  196. ErrorCode:=0;
  197. ExitAddr:=ErrorAddr;
  198. ErrorAddr:=nil;
  199. CompilationPhase:=cpFailed;
  200. { FIXME: this is not 64bit compatible PM }
  201. CompilerMessageWindow^.AddMessage(V_Error,
  202. 'Compiler exited with error '+inttostr(ExitReason)+
  203. ' at addr '+inttohex(longint(ExitAddr),8),'',0,0);
  204. end;
  205. exitproc:=storeexitproc;
  206. {$else : not COMPILER_1_0}
  207. except
  208. on e : exception do
  209. begin
  210. CompilationPhase:=cpFailed;
  211. CompilerMessageWindow^.AddMessage(V_Error,
  212. 'Compiler exited','',0,0);
  213. CompilerMessageWindow^.AddMessage(V_Error,
  214. e.message,'',0,0);
  215. end;
  216. end;
  217. {$endif COMPILER_1_0}
  218. end;
  219. end;
  220. {$ifdef USE_EXTERNAL_COMPILER}
  221. function version_string : string;
  222. begin
  223. if not ExecuteRedir(ExternalCompilerExe,'-iV','','ppc___.out','ppc___.err') then
  224. version_string:=version.version_string;
  225. Assign(CompilerOut,'ppc___.out');
  226. Reset(CompilerOut);
  227. Readln(CompilerOut,s);
  228. Close(CompilerOut);
  229. version_string:=s;
  230. end;
  231. {$endif USE_EXTERNAL_COMPILER}
  232. procedure SetPrimaryFile(const fn:string);
  233. var
  234. t : text;
  235. begin
  236. PrimaryFile:='';
  237. PrimaryFileMain:='';
  238. PrimaryFileSwitches:='';
  239. PrimaryFilePara:='';
  240. if UpcaseStr(ExtOf(fn))='.PRI' then
  241. begin
  242. assign(t,fn);
  243. {$I-}
  244. reset(t);
  245. if ioresult=0 then
  246. begin
  247. PrimaryFile:=fn;
  248. readln(t,PrimaryFileMain);
  249. readln(t,PrimaryFileSwitches);
  250. readln(t,PrimaryFilePara);
  251. close(t);
  252. end;
  253. {$I+}
  254. EatIO;
  255. end
  256. else
  257. begin
  258. PrimaryFile:=fn;
  259. PrimaryFileMain:=fn;
  260. end;
  261. if PrimaryFilePara<>'' then
  262. SetRunParameters(PrimaryFilePara);
  263. end;
  264. end.
  265. {
  266. $Log$
  267. Revision 1.5 2002-10-04 15:23:46 pierre
  268. * don't use tpexcept anymore
  269. Revision 1.4 2002/09/07 15:40:43 peter
  270. * old logs removed and tabs fixed
  271. }