pas2jni.pas 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 the output library name.');
  28. writeln(' -P<name> - Set the 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 the 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(' -N - Do not generate a Java code for auto-loading of the shared library.');
  37. writeln(' -? - Show this help information.');
  38. end;
  39. function GetListParam(const p: string): TStringList;
  40. var
  41. fs: TFileStream;
  42. r: string;
  43. begin
  44. if Copy(p, 1, 1) = '@' then begin
  45. fs:=TFileStream.Create(Copy(p, 2, MaxInt), fmOpenRead or fmShareDenyWrite);
  46. try
  47. SetLength(r, fs.Size);
  48. if r <> '' then
  49. fs.ReadBuffer(PChar(r)^, fs.Size);
  50. finally
  51. fs.Free;
  52. end;
  53. end
  54. else
  55. r:=p;
  56. r:=StringReplace(r, ';', LineEnding, [rfReplaceAll]);
  57. Result:=TStringList.Create;
  58. Result.Text:=r;
  59. end;
  60. function ParseCmdLine: boolean;
  61. var
  62. i: integer;
  63. s, ss: string;
  64. sl: TStringList;
  65. begin
  66. Result:=False;
  67. if ParamCount = 0 then begin
  68. ShowUsage;
  69. ExitCode:=1;
  70. exit;
  71. end;
  72. for i:=1 to Paramcount do begin
  73. s:=ParamStr(i);
  74. if Copy(s, 1, 1) = '-' then begin
  75. Delete(s, 1, 1);
  76. if s = '' then
  77. continue;
  78. case s[1] of
  79. 'U':
  80. begin
  81. Delete(s, 1, 1);
  82. if s = '' then
  83. continue;
  84. if w.SearchPath <> '' then
  85. w.SearchPath:=w.SearchPath + ';';
  86. w.SearchPath:=w.SearchPath + s;
  87. end;
  88. 'L':
  89. begin
  90. Delete(s, 1, 1);
  91. if s = '' then
  92. continue;
  93. w.LibName:=s;
  94. end;
  95. 'P':
  96. begin
  97. Delete(s, 1, 1);
  98. if s = '' then
  99. continue;
  100. w.JavaPackage:=s;
  101. end;
  102. 'O':
  103. begin
  104. Delete(s, 1, 1);
  105. if s = '' then
  106. continue;
  107. w.OutPath:=s;
  108. if w.JavaOutPath = '' then
  109. w.JavaOutPath:=s;
  110. end;
  111. 'J':
  112. begin
  113. Delete(s, 1, 1);
  114. if s = '' then
  115. continue;
  116. w.JavaOutPath:=s;
  117. end;
  118. 'D':
  119. begin
  120. Delete(s, 1, 1);
  121. if s = '' then
  122. continue;
  123. ppudumpprog:=s;
  124. end;
  125. 'I':
  126. begin
  127. Delete(s, 1, 1);
  128. if s = '' then
  129. continue;
  130. sl:=GetListParam(s);
  131. w.IncludeList.AddStrings(sl);
  132. sl.Free;
  133. end;
  134. 'E':
  135. begin
  136. Delete(s, 1, 1);
  137. if s = '' then
  138. continue;
  139. sl:=GetListParam(s);
  140. w.ExcludeList.AddStrings(sl);
  141. sl.Free;
  142. end;
  143. 'N':
  144. w.LibAutoLoad:=False;
  145. '?', 'H':
  146. begin
  147. ShowUsage;
  148. exit;
  149. end;
  150. else
  151. begin
  152. writeln('Illegal parameter: -', s);
  153. ExitCode:=1;
  154. exit;
  155. end;
  156. end;
  157. end
  158. else begin
  159. ss:=ExtractFilePath(s);
  160. if ss <> '' then begin
  161. if w.SearchPath <> '' then
  162. w.SearchPath:=w.SearchPath + ';';
  163. w.SearchPath:=w.SearchPath + ss;
  164. end;
  165. w.Units.Add(ExtractFileName(s));
  166. end;
  167. end;
  168. Result:=True;
  169. end;
  170. begin
  171. try
  172. w:=TWriter.Create;
  173. try
  174. if ParseCmdLine then
  175. w.ProcessUnits;
  176. finally
  177. w.Free;
  178. end;
  179. except
  180. writeln(Exception(ExceptObject).Message);
  181. ExitCode:=2;
  182. end;
  183. end.