instantfpc.pas 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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('Run pascal source files as scripts.');
  27. writeln('Normal usage is to add to a program source file a first line');
  28. writeln('("shebang") "#!/usr/bin/instantfpc".');
  29. writeln('Then you can execute the source directly in the terminal/console.');
  30. writeln;
  31. writeln('instantfpc -h');
  32. writeln(' Print this help message and exit.');
  33. writeln;
  34. writeln('instantfpc -v');
  35. writeln(' Print version and exit.');
  36. writeln;
  37. writeln('instantfpc [compiler options] <source file> [program parameters]');
  38. writeln(' Compiles source and runs program.');
  39. writeln(' Source is compared with the cache. If cache is not valid then');
  40. writeln(' source is copied to cache with the shebang line commented and');
  41. writeln(' cached source is compiled.');
  42. writeln(' If compilation fails the fpc output is written to stdout and');
  43. writeln(' instantfpc exits with error code 1.');
  44. writeln(' If compilation was successful the program is executed.');
  45. writeln(' If the compiler options contains -B the program is always');
  46. writeln(' compiled.');
  47. writeln(' If the environment option INSTANTFPCOPTIONS is set it is');
  48. writeln(' passed to the compiler as first parameters.');
  49. writeln;
  50. writeln('instantfpc --get-cache');
  51. writeln(' Prints current cache directory and exit.');
  52. writeln;
  53. writeln('Options:');
  54. writeln;
  55. writeln(' --set-cache=<path to cache>');
  56. writeln(' Set the cache to be used. Otherwise using environment variable');
  57. writeln(' INSTANTFPCCACHE.');
  58. writeln;
  59. writeln(' --compiler=<path to compiler>');
  60. writeln(' Normally fpc is searched in PATH and used as compiler.');
  61. writeln;
  62. writeln(' --skip-run');
  63. writeln(' Do not execute the program. Useful to test if script compiles.');
  64. writeln(' You probably want to combine it with -B.');
  65. writeln;
  66. writeln(' -B');
  67. writeln(' Always recompile.');
  68. Halt(0);
  69. end;
  70. Procedure DisplayCache;
  71. begin
  72. write(GetCacheDir);
  73. Halt(0);
  74. end ;
  75. var
  76. i,j: Integer;
  77. p: String;
  78. Filename: String;
  79. Src: TStringList;
  80. CacheDir: String;
  81. CacheFilename: String;
  82. OutputFilename: String;
  83. ExeExt: String;
  84. E : String;
  85. RunIt: boolean = true;
  86. // Return true if filename found.
  87. Function InterpretParam(p : String) : boolean;
  88. begin
  89. Result:=False;
  90. if (P='') then exit;
  91. if p='-v' then
  92. begin
  93. writeln('instantfpc '+Version);
  94. Halt(1);
  95. end
  96. else if p='-h' then
  97. usage
  98. else if p='--get-cache' then
  99. DisplayCache
  100. else if copy(p,1,11)='--compiler=' then
  101. begin
  102. delete(P,1,11);
  103. SetCompiler(p);
  104. end
  105. else if copy(p,1,12)='--set-cache=' then
  106. begin
  107. delete(P,1,12);
  108. SetCacheDir(p);
  109. end
  110. else if p='--skip-run' then
  111. begin
  112. RunIt:=false;
  113. end
  114. else if (P<>'') and (p[1]<>'-') then
  115. begin
  116. Filename:=p;
  117. Result:=True;
  118. end;
  119. end;
  120. begin
  121. Filename:='';
  122. { For example:
  123. /usr/bin/instantfpc -MObjFpc -Sh ./envvars.pas param1
  124. }
  125. for i:=1 to Paramcount do
  126. begin
  127. p:=ParamStr(i);
  128. if p='' then
  129. continue
  130. else
  131. begin
  132. if (I<>1) then
  133. begin
  134. if InterpretParam(p) then
  135. Break;
  136. end
  137. else
  138. begin
  139. // The linux kernel passes the whole shebang line as 1 argument.
  140. // We must parse and split it ourselves.
  141. Repeat
  142. J:=Pos(' ',P);
  143. if (J=0) then
  144. J:=Length(P)+1;
  145. if InterpretParam(Copy(P,1,J-1)) then
  146. Break;
  147. Delete(P,1,J);
  148. Until (P='');
  149. if (FileName<>'') then
  150. Break;
  151. end;
  152. end;
  153. end;
  154. if (Filename='') then
  155. begin
  156. writeln('missing source file');
  157. Halt(1);
  158. end;
  159. CheckSourceName(Filename);
  160. Src:=TStringList.Create;
  161. try
  162. Src.LoadFromFile(Filename);
  163. CommentShebang(Src);
  164. CacheDir:=GetCacheDir;
  165. // check cache
  166. CacheFilename:=CacheDir+ExtractFileName(Filename);
  167. E:=LowerCase(ExtractFileExt(CacheFileName));
  168. if (E<>'.pp') and (E<>'.pas') and (E<>'.lpr') then
  169. CacheFileName:=CacheFileName+'.pas';
  170. ExeExt:='';
  171. OutputFilename:=CacheDir+ChangeFileExt(ExtractFileName(Filename),ExeExt);
  172. if not IsCacheValid(Src,CacheFilename,OutputFilename) then begin
  173. // save source in cache to find out next time if something changed
  174. Src.SaveToFile(CacheFilename);
  175. Compile(Filename,CacheFilename,OutputFilename);
  176. end;
  177. // run
  178. if RunIt then
  179. Run(OutputFilename);
  180. finally
  181. // memory is freed by OS, but for debugging puposes you can do it manually
  182. {$IFDEF IFFreeMem}
  183. Proc.Free;
  184. Src.Free;
  185. {$ENDIF}
  186. end;
  187. end.