pas2jni.pas 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. {
  2. pas2jni - JNI bridge generator for Pascal.
  3. Copyright (c) 2013 by Yury Sidorov.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************}
  16. {$mode objfpc}{$H+}
  17. program pas2jni;
  18. uses SysUtils, Classes, writer, ppuparser;
  19. var
  20. w: TWriter;
  21. procedure ShowUsage;
  22. begin
  23. writeln('Usage: ', ChangeFileExt(ExtractFileName(ParamStr(0)), ''), ' [options] <unit> [<unit2> <unit3> ...]');
  24. writeln;
  25. writeln('Options:');
  26. writeln(' -U<path> - Unit search path, semicolon delimited. Wildcards are allowed.');
  27. writeln(' -L<name> - Set output library name.');
  28. writeln(' -P<name> - Set Java package name.');
  29. writeln(' -O<path> - Set output path for Pascal files.');
  30. writeln(' -J<path> - Set output path for Java files.');
  31. writeln(' -D<prog> - Set full path to the "ppudump" program.');
  32. writeln(' -I<list> - Include the list of specified objects in the output. The list is');
  33. writeln(' semicolon delimited. To read the list from a file use -I@<file>');
  34. writeln(' -E<list> - Exclude the list of specified objects from the output. The list is');
  35. writeln(' semicolon delimited. To read the list from a file use -E@<file>');
  36. writeln(' -? - Show this help information.');
  37. end;
  38. function GetListParam(const p: string): TStringList;
  39. var
  40. fs: TFileStream;
  41. r: string;
  42. begin
  43. if Copy(p, 1, 1) = '@' then begin
  44. fs:=TFileStream.Create(Copy(p, 2, MaxInt), fmOpenRead or fmShareDenyWrite);
  45. try
  46. SetLength(r, fs.Size);
  47. if r <> '' then
  48. fs.ReadBuffer(PChar(r)^, fs.Size);
  49. finally
  50. fs.Free;
  51. end;
  52. end
  53. else
  54. r:=p;
  55. r:=StringReplace(r, ';', LineEnding, [rfReplaceAll]);
  56. Result:=TStringList.Create;
  57. Result.Text:=r;
  58. end;
  59. function ParseCmdLine: boolean;
  60. var
  61. i: integer;
  62. s, ss: string;
  63. sl: TStringList;
  64. begin
  65. Result:=False;
  66. if ParamCount = 0 then begin
  67. ShowUsage;
  68. ErrorCode:=1;
  69. exit;
  70. end;
  71. for i:=1 to Paramcount do begin
  72. s:=ParamStr(i);
  73. if Copy(s, 1, 1) = '-' then begin
  74. Delete(s, 1, 1);
  75. if s = '' then
  76. continue;
  77. case s[1] of
  78. 'U':
  79. begin
  80. Delete(s, 1, 1);
  81. if s = '' then
  82. continue;
  83. if w.SearchPath <> '' then
  84. w.SearchPath:=w.SearchPath + ';';
  85. w.SearchPath:=w.SearchPath + s;
  86. end;
  87. 'L':
  88. begin
  89. Delete(s, 1, 1);
  90. if s = '' then
  91. continue;
  92. w.LibName:=s;
  93. end;
  94. 'P':
  95. begin
  96. Delete(s, 1, 1);
  97. if s = '' then
  98. continue;
  99. w.JavaPackage:=s;
  100. end;
  101. 'O':
  102. begin
  103. Delete(s, 1, 1);
  104. if s = '' then
  105. continue;
  106. w.OutPath:=s;
  107. if w.JavaOutPath = '' then
  108. w.JavaOutPath:=s;
  109. end;
  110. 'J':
  111. begin
  112. Delete(s, 1, 1);
  113. if s = '' then
  114. continue;
  115. w.JavaOutPath:=s;
  116. end;
  117. 'D':
  118. begin
  119. Delete(s, 1, 1);
  120. if s = '' then
  121. continue;
  122. ppudumpprog:=s;
  123. end;
  124. 'I':
  125. begin
  126. Delete(s, 1, 1);
  127. if s = '' then
  128. continue;
  129. sl:=GetListParam(s);
  130. w.IncludeList.AddStrings(sl);
  131. sl.Free;
  132. end;
  133. 'E':
  134. begin
  135. Delete(s, 1, 1);
  136. if s = '' then
  137. continue;
  138. sl:=GetListParam(s);
  139. w.ExcludeList.AddStrings(sl);
  140. sl.Free;
  141. end;
  142. '?', 'H':
  143. begin
  144. ShowUsage;
  145. exit;
  146. end;
  147. else
  148. begin
  149. writeln('Illegal parameter: -', s);
  150. ErrorCode:=1;
  151. exit;
  152. end;
  153. end;
  154. end
  155. else begin
  156. ss:=ExtractFilePath(s);
  157. if ss <> '' then begin
  158. if w.SearchPath <> '' then
  159. w.SearchPath:=w.SearchPath + ';';
  160. w.SearchPath:=w.SearchPath + ss;
  161. end;
  162. w.Units.Add(ExtractFileName(s));
  163. end;
  164. end;
  165. Result:=True;
  166. end;
  167. begin
  168. try
  169. w:=TWriter.Create;
  170. try
  171. if ParseCmdLine then
  172. w.ProcessUnits;
  173. finally
  174. w.Free;
  175. end;
  176. except
  177. writeln(Exception(ExceptObject).Message);
  178. ErrorCode:=2;
  179. end;
  180. end.