Browse Source

* pas2jni: Added option: -N - Do not generate a Java code for auto-loading of the shared library.
* pas2jni: Do not generate TClass helper code if no classes are used at all.

git-svn-id: trunk@40179 -

yury 6 years ago
parent
commit
bfbe50bc04
2 changed files with 31 additions and 19 deletions
  1. 6 3
      utils/pas2jni/pas2jni.pas
  2. 25 16
      utils/pas2jni/writer.pas

+ 6 - 3
utils/pas2jni/pas2jni.pas

@@ -33,15 +33,16 @@ begin
   writeln;
   writeln('Options:');
   writeln('  -U<path> - Unit search path, semicolon delimited. Wildcards are allowed.');
-  writeln('  -L<name> - Set output library name.');
-  writeln('  -P<name> - Set Java package name.');
+  writeln('  -L<name> - Set the output library name.');
+  writeln('  -P<name> - Set the Java package name.');
   writeln('  -O<path> - Set output path for Pascal files.');
   writeln('  -J<path> - Set output path for Java files.');
-  writeln('  -D<prog> - Set full path to the "ppudump" program.');
+  writeln('  -D<prog> - Set the full path to the "ppudump" program.');
   writeln('  -I<list> - Include the list of specified objects in the output. The list is');
   writeln('             semicolon delimited. To read the list from a file use -I@<file>');
   writeln('  -E<list> - Exclude the list of specified objects from the output. The list is');
   writeln('             semicolon delimited. To read the list from a file use -E@<file>');
+  writeln('  -N       - Do not generate a Java code for auto-loading of the shared library.');
   writeln('  -?       - Show this help information.');
 end;
 
@@ -150,6 +151,8 @@ begin
             w.ExcludeList.AddStrings(sl);
             sl.Free;
           end;
+        'N':
+          w.LibAutoLoad:=False;
         '?', 'H':
           begin
             ShowUsage;

+ 25 - 16
utils/pas2jni/writer.pas

@@ -155,6 +155,7 @@ type
     JavaOutPath: string;
     IncludeList: TStringList;
     ExcludeList: TStringList;
+    LibAutoLoad: boolean;
 
     constructor Create;
     destructor Destroy; override;
@@ -1628,13 +1629,15 @@ begin
         end;
       end;
 
-      Fjs.WriteLn('static private boolean _JniLibLoaded = false;');
-      Fjs.WriteLn('public static void InitJni() {');
-      Fjs.WriteLn('if (!_JniLibLoaded) {', 1);
-      Fjs.WriteLn('_JniLibLoaded=true;', 2);
-      Fjs.WriteLn(Format('System.loadLibrary("%s");', [LibName]), 2);
-      Fjs.WriteLn('}', 1);
-      Fjs.WriteLn('}');
+      if LibAutoLoad then begin
+        Fjs.WriteLn('static private boolean _JniLibLoaded = false;');
+        Fjs.WriteLn('public static void InitJni() {');
+        Fjs.WriteLn('if (!_JniLibLoaded) {', 1);
+        Fjs.WriteLn('_JniLibLoaded=true;', 2);
+        Fjs.WriteLn(Format('System.loadLibrary("%s");', [LibName]), 2);
+        Fjs.WriteLn('}', 1);
+        Fjs.WriteLn('}');
+      end;
 
       // Support functions
       Fjs.WriteLn('public native static long AllocMemory(int Size);');
@@ -1644,7 +1647,8 @@ begin
       Fjs.WriteLn;
       Fjs.WriteLn('public static class PascalObject {');
       Fjs.IncI;
-      Fjs.WriteLn(Format('static { %s.system.InitJni(); }', [JavaPackage]));
+      if LibAutoLoad then
+        Fjs.WriteLn(Format('static { %s.system.InitJni(); }', [JavaPackage]));
       Fjs.WriteLn('protected long _pasobj = 0;');
       Fjs.WriteLn('protected PascalObject() { }');
       Fjs.WriteLn('protected PascalObject(PascalObject obj) { if (obj != null) _pasobj=obj._pasobj; }');
@@ -1676,12 +1680,6 @@ begin
       Fjs.DecI;
       Fjs.WriteLn('}');
 
-      // Class
-      Fjs.WriteLn;
-      Fjs.WriteLn('native static long GetClassRef(int index);');
-      AddNativeMethod(u, '_GetClassRef', 'GetClassRef', '(I)J');
-      Fjs.WriteLn('static TClass GetTClass(int index) { TClass c = new TClass(null); c._pasobj=GetClassRef(index); return c; }');
-
       // Record
       Fjs.WriteLn;
       Fjs.WriteLn('public static class Record extends PascalObjectEx {');
@@ -2025,8 +2023,10 @@ begin
       Fjs.WriteLn;
 
     end;
-    Fjs.WriteLn(Format('static { %s.system.InitJni(); }', [JavaPackage]));
-    Fjs.WriteLn;
+    if LibAutoLoad then begin
+      Fjs.WriteLn(Format('static { %s.system.InitJni(); }', [JavaPackage]));
+      Fjs.WriteLn;
+    end;
 
     // First pass
     for i:=0 to u.Count - 1 do begin
@@ -2074,6 +2074,14 @@ begin
       end;
     end;
 
+    // Class ref helpers
+    if FClasses.IndexOf('system.TClass', nil) >= 0 then begin
+      Fjs.WriteLn('native static long GetClassRef(int index);');
+      AddNativeMethod(u, '_GetClassRef', 'GetClassRef', '(I)J');
+      Fjs.WriteLn('static TClass GetTClass(int index) { TClass c = new TClass(null); c._pasobj=GetClassRef(index); return c; }');
+      Fjs.WriteLn;
+    end;
+
     Fjs.DecI;
     Fjs.WriteLn('}');
   finally
@@ -2729,6 +2737,7 @@ begin
   FThisUnit:=TUnitDef.Create(nil, dtUnit);
   FRecords:=TObjectList.Create(False);
   FRealClasses:=TObjectList.Create(False);
+  LibAutoLoad:=True;
 end;
 
 function DoCanUseDef(def, refdef: TDef): boolean;