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