pas2jni.pas 5.1 KB

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