pas2jni.pas 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. procedure ParseCmdLine;
  60. var
  61. i: integer;
  62. s, ss: string;
  63. sl: TStringList;
  64. begin
  65. if ParamCount = 0 then begin
  66. ShowUsage;
  67. Halt(1);
  68. end;
  69. for i:=1 to Paramcount do begin
  70. s:=ParamStr(i);
  71. if Copy(s, 1, 1) = '-' then begin
  72. Delete(s, 1, 1);
  73. if s = '' then
  74. continue;
  75. case s[1] of
  76. 'U':
  77. begin
  78. Delete(s, 1, 1);
  79. if s = '' then
  80. continue;
  81. if w.SearchPath <> '' then
  82. w.SearchPath:=w.SearchPath + ';';
  83. w.SearchPath:=w.SearchPath + s;
  84. end;
  85. 'L':
  86. begin
  87. Delete(s, 1, 1);
  88. if s = '' then
  89. continue;
  90. w.LibName:=s;
  91. end;
  92. 'P':
  93. begin
  94. Delete(s, 1, 1);
  95. if s = '' then
  96. continue;
  97. w.JavaPackage:=s;
  98. end;
  99. 'O':
  100. begin
  101. Delete(s, 1, 1);
  102. if s = '' then
  103. continue;
  104. w.OutPath:=s;
  105. if w.JavaOutPath = '' then
  106. w.JavaOutPath:=s;
  107. end;
  108. 'J':
  109. begin
  110. Delete(s, 1, 1);
  111. if s = '' then
  112. continue;
  113. w.JavaOutPath:=s;
  114. end;
  115. 'D':
  116. begin
  117. Delete(s, 1, 1);
  118. if s = '' then
  119. continue;
  120. ppudumpprog:=s;
  121. end;
  122. 'I':
  123. begin
  124. Delete(s, 1, 1);
  125. if s = '' then
  126. continue;
  127. sl:=GetListParam(s);
  128. w.IncludeList.AddStrings(sl);
  129. sl.Free;
  130. end;
  131. 'E':
  132. begin
  133. Delete(s, 1, 1);
  134. if s = '' then
  135. continue;
  136. sl:=GetListParam(s);
  137. w.ExcludeList.AddStrings(sl);
  138. sl.Free;
  139. end;
  140. '?', 'H':
  141. begin
  142. ShowUsage;
  143. Halt(0);
  144. end;
  145. else
  146. begin
  147. writeln('Illegal parameter: -', s);
  148. Halt(1);
  149. end;
  150. end;
  151. end
  152. else begin
  153. ss:=ExtractFilePath(s);
  154. if ss <> '' then begin
  155. if w.SearchPath <> '' then
  156. w.SearchPath:=w.SearchPath + ';';
  157. w.SearchPath:=w.SearchPath + ss;
  158. end;
  159. w.Units.Add(ExtractFileName(s));
  160. end;
  161. end;
  162. end;
  163. begin
  164. try
  165. w:=TWriter.Create;
  166. try
  167. ParseCmdLine;
  168. w.ProcessUnits;
  169. finally
  170. w.Free;
  171. end;
  172. except
  173. writeln(Exception(ExceptObject).Message);
  174. Halt(2);
  175. end;
  176. end.