fpintf.pas 7.7 KB

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