instantfpc.pas 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. { Compile and run a pascal program.
  2. Copyright (C) 2011 Mattias Gaertner [email protected]
  3. This source is free software; you can redistribute it and/or modify it under
  4. the terms of the GNU General Public License as published by the Free
  5. Software Foundation; either version 2 of the License, or (at your option)
  6. any later version.
  7. This code is distributed in the hope that it will be useful, but WITHOUT ANY
  8. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. details.
  11. A copy of the GNU General Public License is available on the World Wide Web
  12. at <http://www.gnu.org/copyleft/gpl.html>. You can also obtain it by writing
  13. to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
  14. MA 02111-1307, USA.
  15. }
  16. program instantfpc;
  17. {$mode objfpc}{$H+}
  18. uses
  19. Classes, SysUtils, InstantFPTools;
  20. const
  21. Version = '1.2';
  22. Procedure Usage;
  23. begin
  24. writeln('instantfpc '+Version);
  25. writeln;
  26. writeln('instantfpc -h');
  27. writeln(' This help message.');
  28. writeln;
  29. writeln('instantfpc -v');
  30. writeln(' Print version and exit.');
  31. writeln;
  32. writeln('instantfpc [compiler options] <source file> [program parameters]');
  33. writeln(' Compiles source and runs program.');
  34. writeln(' Source is compared with the cache. If cache is not valid then');
  35. writeln(' source is copied to cache with the shebang line commented and');
  36. writeln(' cached source is compiled.');
  37. writeln(' If compilation fails the fpc output is written to stdout and');
  38. writeln(' instantfpc exits with error code 1.');
  39. writeln(' If compilation was successful the program is executed.');
  40. writeln(' If the compiler options contains -B the program is always');
  41. writeln(' compiled.');
  42. writeln(' If the environment option INSTANTFPCOPTIONS is set it is');
  43. writeln(' passed to the compiler as first parameters.');
  44. writeln;
  45. writeln('instantfpc --get-cache');
  46. writeln(' Prints cache directory to stdout.');
  47. writeln;
  48. writeln('instantfpc --set-cache=<path to cache>');
  49. writeln(' Set the cache to be used. Otherwise using environment variable');
  50. writeln(' INSTANTFPCCACHE.');
  51. writeln;
  52. writeln('instantfpc --compiler=<path to compiler>');
  53. writeln(' Normally fpc is searched in PATH and used as compiler.');
  54. writeln;
  55. writeln('Normal usage is to add as first line ("shebang") "#!/usr/bin/instantfpc"');
  56. writeln('to a program source file. Then you can execute the source like a script.');
  57. Halt(0);
  58. end;
  59. Procedure DisplayCache;
  60. begin
  61. write(GetCacheDir);
  62. Halt(0);
  63. end ;
  64. var
  65. i,j: Integer;
  66. p: String;
  67. Filename: String;
  68. Src: TStringList;
  69. CacheDir: String;
  70. CacheFilename: String;
  71. OutputFilename: String;
  72. ExeExt: String;
  73. E : String;
  74. // Return true if filename found.
  75. Function InterpretParam(p : String) : boolean;
  76. begin
  77. Result:=False;
  78. if (P='') then exit;
  79. if p='-v' then
  80. begin
  81. writeln('instantfpc '+Version);
  82. Halt(1);
  83. end
  84. else if p='-h' then
  85. usage
  86. else if p='--get-cache' then
  87. DisplayCache
  88. else if copy(p,1,11)='--compiler=' then
  89. begin
  90. delete(P,1,11);
  91. SetCompiler(p);
  92. end
  93. else if copy(p,1,12)='--set-cache=' then
  94. begin
  95. delete(P,1,12);
  96. SetCacheDir(p);
  97. end
  98. else if (P<>'') and (p[1]<>'-') then
  99. begin
  100. Filename:=p;
  101. Result:=True;
  102. end;
  103. end;
  104. begin
  105. Filename:='';
  106. { For example:
  107. /usr/bin/instantfpc -MObjFpc -Sh ./envvars.pas param1
  108. }
  109. for i:=1 to Paramcount do
  110. begin
  111. p:=ParamStr(i);
  112. if p='' then
  113. continue
  114. else
  115. begin
  116. if (I<>1) then
  117. begin
  118. if InterpretParam(p) then
  119. Break;
  120. end
  121. else
  122. begin
  123. // The linux kernel passes the whole shebang line as 1 argument.
  124. // We must parse and split it ourselves.
  125. Repeat
  126. J:=Pos(' ',P);
  127. if (J=0) then
  128. J:=Length(P)+1;
  129. if InterpretParam(Copy(P,1,J-1)) then
  130. Break;
  131. Delete(P,1,J);
  132. Until (P='');
  133. if (FileName<>'') then
  134. Break;
  135. end;
  136. end;
  137. end;
  138. if (Filename='') then
  139. begin
  140. writeln('missing source file');
  141. Halt(1);
  142. end;
  143. CheckSourceName(Filename);
  144. Src:=TStringList.Create;
  145. try
  146. Src.LoadFromFile(Filename);
  147. CommentShebang(Src);
  148. CacheDir:=GetCacheDir;
  149. // check cache
  150. CacheFilename:=CacheDir+ExtractFileName(Filename);
  151. E:=LowerCase(ExtractFileExt(CacheFileName));
  152. if (E<>'.pp') and (E<>'.pas') and (E<>'.lpr') then
  153. CacheFileName:=CacheFileName+'.pas';
  154. ExeExt:='';
  155. OutputFilename:=CacheDir+ChangeFileExt(ExtractFileName(Filename),ExeExt);
  156. if not IsCacheValid(Src,CacheFilename,OutputFilename) then begin
  157. // save source in cache to find out next time if something changed
  158. Src.SaveToFile(CacheFilename);
  159. Compile(Filename,CacheFilename,OutputFilename);
  160. end;
  161. // run
  162. Run(OutputFilename);
  163. finally
  164. // memory is freed by OS, but for debugging puposes you can do it manually
  165. {$IFDEF IFFreeMem}
  166. Proc.Free;
  167. Src.Free;
  168. {$ENDIF}
  169. end;
  170. end.