comphook.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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. compiling_current : boolean;
  84. { Redirection support }
  85. redirfile : text;
  86. { Special file for bug report }
  87. reportbugfile : text;
  88. end;
  89. var
  90. status : tcompilerstatus;
  91. type
  92. EControlCAbort=class(Exception)
  93. constructor Create;
  94. end;
  95. ECompilerAbort=class(Exception)
  96. constructor Create;
  97. end;
  98. ECompilerAbortSilent=class(Exception)
  99. constructor Create;
  100. end;
  101. { Default Functions }
  102. Function def_status:boolean;
  103. Function def_comment(Level:Longint;const s:string):boolean;
  104. function def_internalerror(i:longint):boolean;
  105. procedure def_initsymbolinfo;
  106. procedure def_donesymbolinfo;
  107. procedure def_extractsymbolinfo;
  108. function def_openinputfile(const filename: string): tinputfile;
  109. Function def_getnamedfiletime(Const F : String) : Longint;
  110. { Function redirecting for IDE support }
  111. type
  112. tstopprocedure = procedure(err:longint);
  113. tstatusfunction = function:boolean;
  114. tcommentfunction = function(Level:Longint;const s:string):boolean;
  115. tinternalerrorfunction = function(i:longint):boolean;
  116. tinitsymbolinfoproc = procedure;
  117. tdonesymbolinfoproc = procedure;
  118. textractsymbolinfoproc = procedure;
  119. topeninputfilefunc = function(const filename: string): tinputfile;
  120. tgetnamedfiletimefunc = function(const filename: string): longint;
  121. const
  122. do_status : tstatusfunction = @def_status;
  123. do_comment : tcommentfunction = @def_comment;
  124. do_internalerror : tinternalerrorfunction = @def_internalerror;
  125. do_initsymbolinfo : tinitsymbolinfoproc = @def_initsymbolinfo;
  126. do_donesymbolinfo : tdonesymbolinfoproc = @def_donesymbolinfo;
  127. do_extractsymbolinfo : textractsymbolinfoproc = @def_extractsymbolinfo;
  128. do_openinputfile : topeninputfilefunc = @def_openinputfile;
  129. do_getnamedfiletime : tgetnamedfiletimefunc = @def_getnamedfiletime;
  130. implementation
  131. uses
  132. {$IFDEF USE_SYSUTILS}
  133. SysUtils,
  134. {$ELSE USE_SYSUTILS}
  135. dos,
  136. {$ENDIF USE_SYSUTILS}
  137. cutils
  138. ;
  139. {****************************************************************************
  140. Helper Routines
  141. ****************************************************************************}
  142. function gccfilename(const s : string) : string;
  143. var
  144. i : longint;
  145. begin
  146. for i:=1to length(s) do
  147. begin
  148. case s[i] of
  149. '\' : gccfilename[i]:='/';
  150. 'A'..'Z' : gccfilename[i]:=chr(ord(s[i])+32);
  151. else
  152. gccfilename[i]:=s[i];
  153. end;
  154. end;
  155. gccfilename[0]:=s[0];
  156. end;
  157. function tostr(i : longint) : string;
  158. var
  159. hs : string;
  160. begin
  161. str(i,hs);
  162. tostr:=hs;
  163. end;
  164. {****************************************************************************
  165. Stopping the compiler
  166. ****************************************************************************}
  167. constructor EControlCAbort.Create;
  168. begin
  169. {$IFNDEF MACOS_USE_FAKE_SYSUTILS}
  170. inherited Create('Ctrl-C Signaled!');
  171. {$ELSE}
  172. inherited Create;
  173. {$ENDIF}
  174. end;
  175. constructor ECompilerAbort.Create;
  176. begin
  177. {$IFNDEF MACOS_USE_FAKE_SYSUTILS}
  178. inherited Create('Compilation Aborted');
  179. {$ELSE}
  180. inherited Create;
  181. {$ENDIF}
  182. end;
  183. constructor ECompilerAbortSilent.Create;
  184. begin
  185. {$IFNDEF MACOS_USE_FAKE_SYSUTILS}
  186. inherited Create('Compilation Aborted');
  187. {$ELSE}
  188. inherited Create;
  189. {$ENDIF}
  190. end;
  191. {****************************************************************************
  192. Predefined default Handlers
  193. ****************************************************************************}
  194. function def_status:boolean;
  195. var
  196. hstatus : TFPCHeapStatus;
  197. begin
  198. def_status:=false; { never stop }
  199. { Status info?, Called every line }
  200. if ((status.verbosity and V_Status)<>0) then
  201. begin
  202. if (status.compiledlines=1) or
  203. (status.currentline mod 100=0) then
  204. begin
  205. if status.currentline>0 then
  206. Write(status.currentline,' ');
  207. hstatus:=GetFPCHeapStatus;
  208. WriteLn(DStr(hstatus.CurrHeapUsed shr 10),'/',DStr(hstatus.CurrHeapSize shr 10),' Kb Used');
  209. end;
  210. end;
  211. {$ifdef macos}
  212. Yield;
  213. {$endif}
  214. end;
  215. Function def_comment(Level:Longint;const s:string):boolean;
  216. const
  217. rh_errorstr = 'error:';
  218. rh_warningstr = 'warning:';
  219. var
  220. hs : string;
  221. begin
  222. def_comment:=false; { never stop }
  223. hs:='';
  224. if not(status.use_gccoutput) then
  225. begin
  226. if (status.verbosity and Level)=V_Hint then
  227. hs:=hintstr;
  228. if (status.verbosity and Level)=V_Note then
  229. hs:=notestr;
  230. if (status.verbosity and Level)=V_Warning then
  231. hs:=warningstr;
  232. if (status.verbosity and Level)=V_Error then
  233. hs:=errorstr;
  234. if (status.verbosity and Level)=V_Fatal then
  235. hs:=fatalstr;
  236. if (status.verbosity and Level)=V_Used then
  237. hs:=PadSpace('('+status.currentmodule+')',10);
  238. end
  239. else
  240. begin
  241. if (status.verbosity and Level)=V_Hint then
  242. hs:=rh_warningstr;
  243. if (status.verbosity and Level)=V_Note then
  244. hs:=rh_warningstr;
  245. if (status.verbosity and Level)=V_Warning then
  246. hs:=rh_warningstr;
  247. if (status.verbosity and Level)=V_Error then
  248. hs:=rh_errorstr;
  249. if (status.verbosity and Level)=V_Fatal then
  250. hs:=rh_errorstr;
  251. end;
  252. { Generate line prefix }
  253. if ((Level and V_LineInfo)=V_LineInfo) and
  254. (status.currentsource<>'') and
  255. (status.currentline>0) then
  256. begin
  257. {$ifndef macos}
  258. { Adding the column should not confuse RHIDE,
  259. even if it does not yet use it PM
  260. but only if it is after error or warning !! PM }
  261. if status.currentcolumn>0 then
  262. begin
  263. if status.use_gccoutput then
  264. hs:=gccfilename(status.currentsource)+':'+tostr(status.currentline)+': '+hs+' '+
  265. tostr(status.currentcolumn)+': '+s
  266. else
  267. hs:=status.currentsource+'('+tostr(status.currentline)+
  268. ','+tostr(status.currentcolumn)+') '+hs+' '+s;
  269. end
  270. else
  271. begin
  272. if status.use_gccoutput then
  273. hs:=gccfilename(status.currentsource)+': '+hs+' '+tostr(status.currentline)+': '+s
  274. else
  275. hs:=status.currentsource+'('+tostr(status.currentline)+') '+hs+' '+s;
  276. end;
  277. {$else}
  278. {MPW style error}
  279. if status.currentcolumn>0 then
  280. hs:='File "'+status.currentsourcepath+status.currentsource+'"; Line '+tostr(status.currentline)+
  281. ' #[' + tostr(status.currentcolumn) + '] ' +hs+' '+s
  282. else
  283. hs:='File "'+status.currentsourcepath+status.currentsource+'"; Line '+tostr(status.currentline)+' # '+hs+' '+s;
  284. {$endif}
  285. end
  286. else
  287. begin
  288. if hs<>'' then
  289. hs:=hs+' '+s
  290. else
  291. hs:=s;
  292. end;
  293. { Display line }
  294. if ((status.verbosity and (Level and V_LevelMask))=(Level and V_LevelMask)) then
  295. begin
  296. {$ifdef FPC}
  297. if status.use_stderr then
  298. begin
  299. writeln(stderr,hs);
  300. flush(stderr);
  301. end
  302. else
  303. {$endif}
  304. begin
  305. if status.use_redir then
  306. writeln(status.redirfile,hs)
  307. else
  308. writeln(hs);
  309. end;
  310. end;
  311. { include everything in the bugreport file }
  312. if status.use_bugreport then
  313. begin
  314. {$ifdef FPC}
  315. Write(status.reportbugfile,hexstr(level,8)+':');
  316. Writeln(status.reportbugfile,hs);
  317. {$endif}
  318. end;
  319. end;
  320. function def_internalerror(i : longint) : boolean;
  321. begin
  322. do_comment(V_Fatal+V_LineInfo,'Internal error '+tostr(i));
  323. {$ifdef EXTDEBUG}
  324. {$ifdef FPC}
  325. { Internalerror() and def_internalerror() do not
  326. have a stackframe }
  327. dump_stack(stdout,get_caller_frame(get_frame));
  328. {$endif FPC}
  329. {$endif EXTDEBUG}
  330. def_internalerror:=true;
  331. end;
  332. procedure def_initsymbolinfo;
  333. begin
  334. end;
  335. procedure def_donesymbolinfo;
  336. begin
  337. end;
  338. procedure def_extractsymbolinfo;
  339. begin
  340. end;
  341. function def_openinputfile(const filename: string): tinputfile;
  342. begin
  343. def_openinputfile:=tdosinputfile.create(filename);
  344. end;
  345. Function def_GetNamedFileTime (Const F : String) : Longint;
  346. var
  347. {$IFDEF USE_SYSUTILS}
  348. fh : THandle;
  349. {$ELSE USE_SYSUTILS}
  350. info : SearchRec;
  351. {$ENDIF USE_SYSUTILS}
  352. begin
  353. Result := -1;
  354. {$IFDEF USE_SYSUTILS}
  355. fh := FileOpen(f, faArchive+faReadOnly+faHidden);
  356. Result := FileGetDate(fh);
  357. FileClose(fh);
  358. {$ELSE USE_SYSUTILS}
  359. FindFirst (F,archive+readonly+hidden,info);
  360. if DosError=0 then
  361. Result := info.time;
  362. FindClose(info);
  363. {$ENDIF USE_SYSUTILS}
  364. end;
  365. end.