instantfpc.pas 5.7 KB

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