instantfpc.pas 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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.0';
  22. var
  23. i: Integer;
  24. p: String;
  25. Filename: String;
  26. Src: TStringList;
  27. CacheDir: String;
  28. CacheFilename: String;
  29. OutputFilename: String;
  30. ExeExt: String;
  31. begin
  32. Filename:='';
  33. { For example:
  34. /usr/bin/instantfpc -MObjFpc -Sh ./envvars.pas param1
  35. }
  36. for i:=1 to Paramcount do begin
  37. p:=ParamStr(i);
  38. //writeln('Param: ',i,' ',p);
  39. if p='' then
  40. continue
  41. else if p='-v' then begin
  42. writeln('instantfpc '+Version);
  43. Halt(1);
  44. end
  45. else if p='-h' then begin
  46. writeln('instantfpc '+Version);
  47. writeln;
  48. writeln('instantfpc -h');
  49. writeln(' This help message.');
  50. writeln;
  51. writeln('instantfpc -v');
  52. writeln(' Print version and exit.');
  53. writeln;
  54. writeln('instantfpc [compiler options] <source file> [program parameters]');
  55. writeln(' Compiles source and runs program.');
  56. writeln(' Source is compared with the cache. If cache is not valid then');
  57. writeln(' source is copied to cache with the shebang line commented and');
  58. writeln(' cached source is compiled.');
  59. writeln(' If compilation fails the fpc output is written to stdout and');
  60. writeln(' instantfpc exits with error code 1.');
  61. writeln(' If compilation was successful the program is executed.');
  62. writeln(' If the compiler options contains -B the program is always');
  63. writeln(' compiled.');
  64. writeln;
  65. writeln('instantfpc --get-cache');
  66. writeln(' Prints cache directory to stdout.');
  67. writeln;
  68. writeln('instantfpc --compiler=<path to compiler>');
  69. writeln(' Normally fpc is searched in PATH and used as compiler.');
  70. writeln;
  71. writeln('Normal usage is to add as first line ("shebang") "#!/usr/bin/instantfpc"');
  72. writeln('to a program source file. Then you can execute the source like a script.');
  73. Halt(0);
  74. end else if p='--get-cache' then begin
  75. CacheDir:=GetCacheDir;
  76. write(CacheDir);
  77. Halt(0);
  78. end else if (p[1]<>'-') then begin
  79. // the first non flag parameter is the file name of the script
  80. // followed by the parameters for the script
  81. Filename:=p;
  82. break;
  83. end;
  84. end;
  85. if Filename='' then begin
  86. writeln('missing source file');
  87. Halt(1);
  88. end;
  89. CheckSourceName(Filename);
  90. Src:=TStringList.Create;
  91. try
  92. Src.LoadFromFile(Filename);
  93. CommentShebang(Src);
  94. CacheDir:=GetCacheDir;
  95. // check cache
  96. CacheFilename:=CacheDir+ExtractFileName(Filename);
  97. ExeExt:='';
  98. OutputFilename:=CacheDir+ChangeFileExt(ExtractFileName(Filename),ExeExt);
  99. if not IsCacheValid(Src,CacheFilename,OutputFilename) then begin
  100. // save source in cache to find out next time if something changed
  101. Src.SaveToFile(CacheFilename);
  102. Compile(CacheFilename,OutputFilename);
  103. end;
  104. // run
  105. Run(OutputFilename);
  106. finally
  107. // memory is freed by OS, but for debugging puposes you can do it manually
  108. {$IFDEF IFFreeMem}
  109. Proc.Free;
  110. Src.Free;
  111. {$ENDIF}
  112. end;
  113. end.