comphook.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. {
  2. Copyright (c) 1998-2002 by Peter Vreman
  3. This unit handles the compilerhooks for output to external programs
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  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. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit comphook;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. {$IFNDEF MACOS_USE_FAKE_SYSUTILS}
  22. SysUtils,
  23. {$ELSE}
  24. globals,
  25. {$ENDIF}
  26. finput;
  27. Const
  28. { Levels }
  29. V_None = $0;
  30. V_Fatal = $1;
  31. V_Error = $2;
  32. V_Normal = $4; { doesn't show a text like Error: }
  33. V_Warning = $8;
  34. V_Note = $10;
  35. V_Hint = $20;
  36. V_LineInfoMask = $fff;
  37. { From here by default no line info }
  38. V_Info = $1000;
  39. V_Status = $2000;
  40. V_Used = $4000;
  41. V_Tried = $8000;
  42. V_Conditional = $10000;
  43. V_Debug = $20000;
  44. V_Executable = $40000;
  45. V_LevelMask = $fffffff;
  46. V_All = V_LevelMask;
  47. V_Default = V_Fatal + V_Error + V_Normal;
  48. { Flags }
  49. V_LineInfo = $10000000;
  50. const
  51. { RHIDE expect gcc like error output }
  52. fatalstr : string[20] = 'Fatal:';
  53. errorstr : string[20] = 'Error:';
  54. warningstr : string[20] = 'Warning:';
  55. notestr : string[20] = 'Note:';
  56. hintstr : string[20] = 'Hint:';
  57. type
  58. PCompilerStatus = ^TCompilerStatus;
  59. TCompilerStatus = record
  60. { Current status }
  61. currentmodule,
  62. currentsourcepath,
  63. currentsource : string; { filename }
  64. currentline,
  65. currentcolumn : longint; { current line and column }
  66. { Total Status }
  67. compiledlines : longint; { the number of lines which are compiled }
  68. errorcount : longint; { number of generated errors }
  69. { program info }
  70. isexe,
  71. islibrary : boolean;
  72. { Settings for the output }
  73. verbosity : longint;
  74. maxerrorcount : longint;
  75. errorwarning,
  76. errornote,
  77. errorhint,
  78. skip_error,
  79. use_stderr,
  80. use_redir,
  81. use_bugreport,
  82. use_gccoutput,
  83. print_source_path,
  84. compiling_current : boolean;
  85. { Redirection support }
  86. redirfile : text;
  87. { Special file for bug report }
  88. reportbugfile : text;
  89. end;
  90. var
  91. status : tcompilerstatus;
  92. type
  93. EControlCAbort=class(Exception)
  94. constructor Create;
  95. end;
  96. ECompilerAbort=class(Exception)
  97. constructor Create;
  98. end;
  99. ECompilerAbortSilent=class(Exception)
  100. constructor Create;
  101. end;
  102. { Default Functions }
  103. Function def_status:boolean;
  104. Function def_comment(Level:Longint;const s:string):boolean;
  105. function def_internalerror(i:longint):boolean;
  106. procedure def_initsymbolinfo;
  107. procedure def_donesymbolinfo;
  108. procedure def_extractsymbolinfo;
  109. function def_openinputfile(const filename: string): tinputfile;
  110. Function def_getnamedfiletime(Const F : String) : Longint;
  111. { Function redirecting for IDE support }
  112. type
  113. tstopprocedure = procedure(err:longint);
  114. tstatusfunction = function:boolean;
  115. tcommentfunction = function(Level:Longint;const s:string):boolean;
  116. tinternalerrorfunction = function(i:longint):boolean;
  117. tinitsymbolinfoproc = procedure;
  118. tdonesymbolinfoproc = procedure;
  119. textractsymbolinfoproc = procedure;
  120. topeninputfilefunc = function(const filename: string): tinputfile;
  121. tgetnamedfiletimefunc = function(const filename: string): longint;
  122. const
  123. do_status : tstatusfunction = @def_status;
  124. do_comment : tcommentfunction = @def_comment;
  125. do_internalerror : tinternalerrorfunction = @def_internalerror;
  126. do_initsymbolinfo : tinitsymbolinfoproc = @def_initsymbolinfo;
  127. do_donesymbolinfo : tdonesymbolinfoproc = @def_donesymbolinfo;
  128. do_extractsymbolinfo : textractsymbolinfoproc = @def_extractsymbolinfo;
  129. do_openinputfile : topeninputfilefunc = @def_openinputfile;
  130. do_getnamedfiletime : tgetnamedfiletimefunc = @def_getnamedfiletime;
  131. implementation
  132. uses
  133. {$IFNDEF USE_SYSUTILS}
  134. dos,
  135. {$ENDIF USE_SYSUTILS}
  136. cutils
  137. ;
  138. {****************************************************************************
  139. Helper Routines
  140. ****************************************************************************}
  141. function gccfilename(const s : string) : string;
  142. var
  143. i : longint;
  144. begin
  145. for i:=1to length(s) do
  146. begin
  147. case s[i] of
  148. '\' : gccfilename[i]:='/';
  149. 'A'..'Z' : gccfilename[i]:=chr(ord(s[i])+32);
  150. else
  151. gccfilename[i]:=s[i];
  152. end;
  153. end;
  154. gccfilename[0]:=s[0];
  155. end;
  156. function tostr(i : longint) : string;
  157. var
  158. hs : string;
  159. begin
  160. str(i,hs);
  161. tostr:=hs;
  162. end;
  163. {****************************************************************************
  164. Stopping the compiler
  165. ****************************************************************************}
  166. constructor EControlCAbort.Create;
  167. begin
  168. {$IFNDEF MACOS_USE_FAKE_SYSUTILS}
  169. inherited Create('Ctrl-C Signaled!');
  170. {$ELSE}
  171. inherited Create;
  172. {$ENDIF}
  173. end;
  174. constructor ECompilerAbort.Create;
  175. begin
  176. {$IFNDEF MACOS_USE_FAKE_SYSUTILS}
  177. inherited Create('Compilation Aborted');
  178. {$ELSE}
  179. inherited Create;
  180. {$ENDIF}
  181. end;
  182. constructor ECompilerAbortSilent.Create;
  183. begin
  184. {$IFNDEF MACOS_USE_FAKE_SYSUTILS}
  185. inherited Create('Compilation Aborted');
  186. {$ELSE}
  187. inherited Create;
  188. {$ENDIF}
  189. end;
  190. {****************************************************************************
  191. Predefined default Handlers
  192. ****************************************************************************}
  193. function def_status:boolean;
  194. var
  195. hstatus : TFPCHeapStatus;
  196. begin
  197. def_status:=false; { never stop }
  198. { Status info?, Called every line }
  199. if ((status.verbosity and V_Status)<>0) then
  200. begin
  201. if (status.compiledlines=1) or
  202. (status.currentline mod 100=0) then
  203. begin
  204. if status.currentline>0 then
  205. Write(status.currentline,' ');
  206. hstatus:=GetFPCHeapStatus;
  207. WriteLn(DStr(hstatus.CurrHeapUsed shr 10),'/',DStr(hstatus.CurrHeapSize shr 10),' Kb Used');
  208. end;
  209. end;
  210. {$ifdef macos}
  211. Yield;
  212. {$endif}
  213. end;
  214. Function def_comment(Level:Longint;const s:string):boolean;
  215. const
  216. rh_errorstr = 'error:';
  217. rh_warningstr = 'warning:';
  218. var
  219. hs : string;
  220. begin
  221. def_comment:=false; { never stop }
  222. hs:='';
  223. if not(status.use_gccoutput) then
  224. begin
  225. if (status.verbosity and Level)=V_Hint then
  226. hs:=hintstr;
  227. if (status.verbosity and Level)=V_Note then
  228. hs:=notestr;
  229. if (status.verbosity and Level)=V_Warning then
  230. hs:=warningstr;
  231. if (status.verbosity and Level)=V_Error then
  232. hs:=errorstr;
  233. if (status.verbosity and Level)=V_Fatal then
  234. hs:=fatalstr;
  235. if (status.verbosity and Level)=V_Used then
  236. hs:=PadSpace('('+status.currentmodule+')',10);
  237. end
  238. else
  239. begin
  240. if (status.verbosity and Level)=V_Hint then
  241. hs:=rh_warningstr;
  242. if (status.verbosity and Level)=V_Note then
  243. hs:=rh_warningstr;
  244. if (status.verbosity and Level)=V_Warning then
  245. hs:=rh_warningstr;
  246. if (status.verbosity and Level)=V_Error then
  247. hs:=rh_errorstr;
  248. if (status.verbosity and Level)=V_Fatal then
  249. hs:=rh_errorstr;
  250. end;
  251. { Generate line prefix }
  252. if ((Level and V_LineInfo)=V_LineInfo) and
  253. (status.currentsource<>'') and
  254. (status.currentline>0) then
  255. begin
  256. {$ifndef macos}
  257. { Adding the column should not confuse RHIDE,
  258. even if it does not yet use it PM
  259. but only if it is after error or warning !! PM }
  260. if status.currentcolumn>0 then
  261. begin
  262. if status.use_gccoutput then
  263. hs:=gccfilename(status.currentsource)+':'+tostr(status.currentline)+': '+hs+' '+
  264. tostr(status.currentcolumn)+': '+s
  265. else
  266. begin
  267. hs:=status.currentsource+'('+tostr(status.currentline)+
  268. ','+tostr(status.currentcolumn)+') '+hs+' '+s;
  269. if status.print_source_path then
  270. hs:=status.currentsourcepath+hs;
  271. end;
  272. end
  273. else
  274. begin
  275. if status.use_gccoutput then
  276. hs:=gccfilename(status.currentsource)+': '+hs+' '+tostr(status.currentline)+': '+s
  277. else
  278. hs:=status.currentsource+'('+tostr(status.currentline)+') '+hs+' '+s;
  279. end;
  280. {$else}
  281. {MPW style error}
  282. if status.currentcolumn>0 then
  283. hs:='File "'+status.currentsourcepath+status.currentsource+'"; Line '+tostr(status.currentline)+
  284. ' #[' + tostr(status.currentcolumn) + '] ' +hs+' '+s
  285. else
  286. hs:='File "'+status.currentsourcepath+status.currentsource+'"; Line '+tostr(status.currentline)+' # '+hs+' '+s;
  287. {$endif}
  288. end
  289. else
  290. begin
  291. if hs<>'' then
  292. hs:=hs+' '+s
  293. else
  294. hs:=s;
  295. end;
  296. { Display line }
  297. if ((status.verbosity and (Level and V_LevelMask))=(Level and V_LevelMask)) then
  298. begin
  299. {$ifdef FPC}
  300. if status.use_stderr then
  301. begin
  302. writeln(stderr,hs);
  303. flush(stderr);
  304. end
  305. else
  306. {$endif}
  307. begin
  308. if status.use_redir then
  309. writeln(status.redirfile,hs)
  310. else
  311. writeln(hs);
  312. end;
  313. end;
  314. { include everything in the bugreport file }
  315. if status.use_bugreport then
  316. begin
  317. {$ifdef FPC}
  318. Write(status.reportbugfile,hexstr(level,8)+':');
  319. Writeln(status.reportbugfile,hs);
  320. {$endif}
  321. end;
  322. end;
  323. function def_internalerror(i : longint) : boolean;
  324. begin
  325. do_comment(V_Fatal+V_LineInfo,'Internal error '+tostr(i));
  326. {$ifdef EXTDEBUG}
  327. {$ifdef FPC}
  328. { Internalerror() and def_internalerror() do not
  329. have a stackframe }
  330. dump_stack(stdout,get_caller_frame(get_frame));
  331. {$endif FPC}
  332. {$endif EXTDEBUG}
  333. def_internalerror:=true;
  334. end;
  335. procedure def_initsymbolinfo;
  336. begin
  337. end;
  338. procedure def_donesymbolinfo;
  339. begin
  340. end;
  341. procedure def_extractsymbolinfo;
  342. begin
  343. end;
  344. function def_openinputfile(const filename: string): tinputfile;
  345. begin
  346. def_openinputfile:=tdosinputfile.create(filename);
  347. end;
  348. Function def_GetNamedFileTime (Const F : String) : Longint;
  349. var
  350. {$IFDEF USE_SYSUTILS}
  351. fh : THandle;
  352. {$ELSE USE_SYSUTILS}
  353. info : SearchRec;
  354. {$ENDIF USE_SYSUTILS}
  355. begin
  356. Result := -1;
  357. {$IFDEF USE_SYSUTILS}
  358. fh := FileOpen(f, faArchive+faReadOnly+faHidden);
  359. Result := FileGetDate(fh);
  360. FileClose(fh);
  361. {$ELSE USE_SYSUTILS}
  362. FindFirst (F,archive+readonly+hidden,info);
  363. if DosError=0 then
  364. Result := info.time;
  365. FindClose(info);
  366. {$ENDIF USE_SYSUTILS}
  367. end;
  368. end.