instantfpc.pas 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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.1';
  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. Halt(0);
  65. end;
  66. Procedure DisplayCache;
  67. begin
  68. write(GetCacheDir);
  69. Halt(0);
  70. end ;
  71. var
  72. i,j: Integer;
  73. p: String;
  74. Filename: String;
  75. Src: TStringList;
  76. CacheDir: String;
  77. CacheFilename: String;
  78. OutputFilename: String;
  79. ExeExt: String;
  80. E : String;
  81. RunIt: boolean = true;
  82. // Return true if filename found.
  83. Function InterpretParam(p : String) : boolean;
  84. begin
  85. Result:=False;
  86. if (P='') then exit;
  87. if p='-v' then
  88. begin
  89. writeln('instantfpc '+Version);
  90. Halt(1);
  91. end
  92. else if p='-h' then
  93. usage
  94. else if p='--get-cache' then
  95. DisplayCache
  96. else if copy(p,1,11)='--compiler=' then
  97. begin
  98. delete(P,1,11);
  99. SetCompiler(p);
  100. end
  101. else if copy(p,1,12)='--set-cache=' then
  102. begin
  103. delete(P,1,12);
  104. SetCacheDir(p);
  105. end
  106. else if p='--skip-run' then
  107. begin
  108. RunIt:=false;
  109. end
  110. else if (P<>'') and (p[1]<>'-') then
  111. begin
  112. Filename:=p;
  113. Result:=True;
  114. end;
  115. end;
  116. begin
  117. Filename:='';
  118. { For example:
  119. /usr/bin/instantfpc -MObjFpc -Sh ./envvars.pas param1
  120. }
  121. for i:=1 to Paramcount do
  122. begin
  123. p:=ParamStr(i);
  124. if p='' then
  125. continue
  126. else
  127. begin
  128. if (I<>1) then
  129. begin
  130. if InterpretParam(p) then
  131. Break;
  132. end
  133. else
  134. begin
  135. // The linux kernel passes the whole shebang line as 1 argument.
  136. // We must parse and split it ourselves.
  137. Repeat
  138. J:=Pos(' ',P);
  139. if (J=0) then
  140. J:=Length(P)+1;
  141. if InterpretParam(Copy(P,1,J-1)) then
  142. Break;
  143. Delete(P,1,J);
  144. Until (P='');
  145. if (FileName<>'') then
  146. Break;
  147. end;
  148. end;
  149. end;
  150. if (Filename='') then
  151. begin
  152. writeln('missing source file');
  153. Halt(1);
  154. end;
  155. CheckSourceName(Filename);
  156. Src:=TStringList.Create;
  157. try
  158. Src.LoadFromFile(Filename);
  159. CommentShebang(Src);
  160. CacheDir:=GetCacheDir;
  161. // check cache
  162. CacheFilename:=CacheDir+ExtractFileName(Filename);
  163. E:=LowerCase(ExtractFileExt(CacheFileName));
  164. if (E<>'.pp') and (E<>'.pas') and (E<>'.lpr') then
  165. CacheFileName:=CacheFileName+'.pas';
  166. ExeExt:='';
  167. OutputFilename:=CacheDir+ChangeFileExt(ExtractFileName(Filename),ExeExt);
  168. if not IsCacheValid(Src,CacheFilename,OutputFilename) then begin
  169. // save source in cache to find out next time if something changed
  170. Src.SaveToFile(CacheFilename);
  171. Compile(CacheFilename,OutputFilename);
  172. end;
  173. // run
  174. if RunIt then
  175. Run(OutputFilename);
  176. finally
  177. // memory is freed by OS, but for debugging puposes you can do it manually
  178. {$IFDEF IFFreeMem}
  179. Proc.Free;
  180. Src.Free;
  181. {$ENDIF}
  182. end;
  183. end.