fpintf.pas 7.7 KB

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