Recent

Author Topic: pas2jni utility  (Read 3954 times)

yuriy_sydorov

  • Full Member
  • ***
  • Posts: 157
pas2jni utility
« on: June 11, 2013, 11:57:33 am »
I wish to announce the new pas2jni utility, which is useful for Android development with FPC.

The pas2jni utility generates a JNI (Java Native Interface) bridge for a Pascal code. Then the Pascal code (including classes and other advanced features) can be easily used in Java programs.

For example you can do the following in Java:

import pas.classes.*;

...

TStringList sl = TStringList.Create();
sl.Add("Hello.");
String s = sl.getStrings(0);
sl.Free();

...

The following Pascal features are supported by pas2jni:

- function/procedure;
- var/out parameters;
- class;
- record;
- property;
- constant;
- enum;
- TGuid type;
- pointer type;
- string types;
- all numeric types;

Shared libraries, generated by pas2jni were tested with Java on Windows and Android. It should work on other systems as well.

HOW TO USE

pas2jni uses the ppudump utility included with Free Pascal Compiler to read unit interfaces. Therefore your Pascal code must be first compiled with FPC.
When your units are compiled, you can run pas2jni. You need to specify a list of main units and units search path.
When you specify a main unit, all its interface declarations will be available in Java. For linked units only used declarations will be available. You can fine tune included/excluded declaration using -I and -E command line options.

The basic invocation of pas2jni:

pas2jni myunit -U/path/to/my/units;/path/to/FPC/units/*

Here you specify myunit as the main unit and provide path to your compiled units and FPC compiled units.

After successfull run of pas2jni you will get the following output files:
- file "myunitjni.pas" - a generated library unit to be compiled to a shared library. It will contain all your Pascal code to be used from Java.
- folder "pas" - generated Java package "pas" to be used in your Java program. Interface to each Pascal unit is placed to a separate Java public class.

Note: You need to use ppudump of the same version as the FPC compiler. Use the -D switch to specify correct ppudump if it is not in PATH.

COMMAND LINE OPTIONS

Usage: pas2jni [options] <unit> [<unit2> <unit3> ...]

Options:
  -U<path> - Unit search path, semicolon delimited. Wildcards are allowed.
  -L<name> - Set output library name.
  -P<name> - Set Java package name.
  -O<path> - Set output path for Pascal files.
  -J<path> - Set output path for Java files.
  -D<prog> - Set full path to the "ppudump" program.
  -I<list> - Include the list of specified objects in the output. The list is
             semicolon delimited. To read the list from a file use -I@<file>
  -E<list> - Exclude the list of specified objects from the output. The list is
             semicolon delimited. To read the list from a file use -E@<file>
  -?       - Show this help information.


The sources of pas2jni is located in the "utils/pas2jni" folder in the compiler sources (FPC 2.7.1 trunk sources).  pas2jni is installed with FPC 2.7.1 and requires the compiler and ppudump of the same version.

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: pas2jni utility
« Reply #1 on: June 11, 2013, 12:00:13 pm »
Thanks, Yury, that sounds very useful for integration with Java (not that I ever hope I need that, but I'm sure others will appreciate it).

Thanks for announcing this as well!
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: pas2jni utility
« Reply #2 on: June 11, 2013, 12:05:40 pm »
I took the liberty of adding
http://wiki.lazarus.freepascal.org/FPC_New_Features_Trunk#Pas2jni_utility_added

... of course, please feel free to modify ;)
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

yuriy_sydorov

  • Full Member
  • ***
  • Posts: 157
Re: pas2jni utility
« Reply #3 on: January 08, 2015, 02:08:06 pm »
Since the pas2jni utility will be available in the upcoming 3.0 release of FPC, I've added a wiki page about pas2jni: http://wiki.freepascal.org/pas2jni

 

TinyPortal © 2005-2018