instantfpc.pas 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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.3';
  22. // 1.3 compile in a separate directory, so that parallel invocations do not overwrite link.res files
  23. Procedure Usage;
  24. begin
  25. writeln('instantfpc '+Version);
  26. writeln;
  27. writeln('Run pascal source files as scripts.');
  28. writeln('Normal usage is to add to a program source file a first line');
  29. writeln('("shebang") "#!/usr/bin/instantfpc".');
  30. writeln('Then you can execute the source directly in the terminal/console.');
  31. writeln;
  32. writeln('instantfpc -h');
  33. writeln(' Print this help message and exit.');
  34. writeln;
  35. writeln('instantfpc -v');
  36. writeln(' Print version and exit.');
  37. writeln;
  38. writeln('instantfpc [compiler options] <source file> [program parameters]');
  39. writeln(' Compiles source and runs program.');
  40. writeln(' Source is compared with the cache. If cache is not valid then');
  41. writeln(' source is copied to cache with the shebang line commented and');
  42. writeln(' cached source is compiled.');
  43. writeln(' If compilation fails the fpc output is written to stdout and');
  44. writeln(' instantfpc exits with error code 1.');
  45. writeln(' If compilation was successful the program is executed.');
  46. writeln(' If the compiler options contains -B the program is always');
  47. writeln(' compiled.');
  48. writeln(' If the environment option INSTANTFPCOPTIONS is set it is');
  49. writeln(' passed to the compiler as first parameters.');
  50. writeln;
  51. writeln('instantfpc --get-cache');
  52. writeln(' Prints current cache directory and exit.');
  53. writeln;
  54. writeln('Options:');
  55. writeln;
  56. writeln(' --set-cache=<path to cache>');
  57. writeln(' Set the cache to be used. Otherwise using environment variable');
  58. writeln(' INSTANTFPCCACHE.');
  59. writeln;
  60. writeln(' --compiler=<path to compiler>');
  61. writeln(' Normally fpc is searched in PATH and used as compiler.');
  62. writeln;
  63. writeln(' --skip-run');
  64. writeln(' Do not execute the program. Useful to test if script compiles.');
  65. writeln(' You probably want to combine it with -B.');
  66. writeln;
  67. writeln(' -B');
  68. writeln(' Always recompile.');
  69. Halt(0);
  70. end;
  71. Procedure DisplayCache;
  72. begin
  73. write(GetCacheDir);
  74. Halt(0);
  75. end ;
  76. var
  77. i,j: Integer;
  78. p: String;
  79. Filename: String;
  80. Src: TStringList;
  81. CacheDir: String;
  82. CacheFilename: String;
  83. OutputFilename: 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. OutputFilename:=CacheDir+ChangeFileExt(ExtractFileName(Filename),'');
  171. if not IsCacheValid(Src,CacheFilename,OutputFilename) then begin
  172. // save source in cache to find out next time if something changed
  173. Src.SaveToFile(CacheFilename);
  174. Compile(Filename,CacheFilename,OutputFilename);
  175. end;
  176. // run
  177. if RunIt then
  178. Run(OutputFilename);
  179. finally
  180. // memory is freed by OS, but for debugging puposes you can do it manually
  181. {$IFDEF IFFreeMem}
  182. Proc.Free;
  183. Src.Free;
  184. {$ENDIF}
  185. end;
  186. end.