Java development with CEDA

Proposal

The purpose is to allow software developers to develop Java applications that use CEDA, even though CEDA is implemented in C++.

To allow this, Java Native Interface (JNI) is used. See the JNI example.

At least intially we will ignore IDEs such as Eclipse. Instead, we emphasise command line build tools, such as CMake.

Since we currently build the CEDA SDK under Windows using either the MSVC compiler cl, or clang-cl, we will also use that to build the application defined JNI dlls. This is controlled by CMake variables when CMake is invoked on the command line for the configure phase.

The PyCeda python package comes with a python script called generate_jni.py which is used to generate the .java and .cpp JNI files for a given CEDA based library. See Generating JNI with a python script.

We need to deal with JNI wrappers for everything in the ceda namespace. See JNI for the ceda namespace.

It would be interesting to see if we can make the ceda-samples project automatically run the generate_jni.py script, then build the JNI library. Running the samples would include running sample Java programs.

But that means a python script needs to execute as part of the build process. CMake can do this of course, but maybe to get going we just use a batch file. We already have a batch file windows_build_samples.bat which can be extended for this purpose.

The following folder structure is proposed:

  (root)
    ├── CedaExamples
    └── MyCompany
        └── Java
            ├── cpp
            └── java

Building of the shared library: This requires the following to be done:

  1. Download and install Java JDK
  2. CEDA SDK has been downloaded and installed
  3. pyceda package wheel file has been downloaded and installed
  4. ceda-samples has been downloaded
  5. windows_build_samples.bat has been run to build the Time, Shapes and Pizza Xc++ projects. This produces binaries Time.dll, Shapes.dll and Pizza.dll in C:\cedanet\build-ceda-samples\Win64-Release-dll\bin
  6. The pypizza python package is created and installed. This contains the Time.dll, Shapes.dll and Pizza.dll binaries. This is a prerequisite to using the generate_jni.py script.
  7. The generate_jni.py script is run to generate a bunch of .cpp and .java files. A path to the folder for where to put the .cpp files is given. A path to the folder for where to put the .java files is given. All generated files need to appear somewhere under C:\cedanet\repos\_build\ceda-samples\Windows-x64-RelWithDebInfo
  8. We need a CMakeLists.txt file which:
    • Uses find_package(Java), and find_package(JNI) to find Java/JNI stuff
    • Calls add_library() to build shared libraries named Time_jni.dll, Shapes_jni.dll and Pizza_jni.dll
    • Adds all the relevant .cpp files to the library. A file glob command can be used
    • Links Time_jni.dll against Time.dll etc
  9. Maybe? The generated .java files are compiled using the Java compiler (javac) and added to a generated JAR file, so they can easily be used from sample Java projects inside ceda-samples
  10. The Java compiler (javac) is used to build the sample Java projects inside ceda-samples

CMake toolchain file for MINGW

See CMake documentation: How to use MinGW to cross compile software for Windows . This describes how to make a CMake toolchain file.

This is not really necessary anymore - we have shown that it is possible to use cl or clang-cl to build JNI shared libs.

CMake commands to find Java

CMake provides the module FindJava to search for Java on the host. It also provides the module FindJNI to find the Java Native Interface (JNI) libraries.

Example usage:


find_package(Java)