I am using Swig to produce a Java binding for a C library. The build system is written in CMake in order to achieve platform neutrality. I wish to produce a JAR file containing the Java bindings (i.e. the .class files resulting from the compilation of the .java files created by Swig). I am trying to use the Cmake add_jar()
command to perform the compilation and produce the JAR file.
My problem is that Swig produces a set of Java files at build time, but add_jar()
requires a list of source files at the time cmake is executed. I am currently working around the problem using a UNIX wildcard (which is passed literally to the javac command line).
# How do I avoid the shell wildcard?
add_jar(ExampleJNI ${CMAKE_SWIG_OUTDIR}/*.java)
Is there a better way?
I include below a complete example (comprising of three files) that illustrates my approach. If you want to try for yourself, put the three files in a directory, then invoke cmake . ; make VERBOSE=1
.
CMakeLists.txt
cmake_minimum_required (VERSION 2.8)
find_package(Swig REQUIRED)
find_package(Java REQUIRED)
find_package(JNI REQUIRED)
include(UseJava)
include(UseSWIG)
project (Example)
add_library (Example example.c)
set(CMAKE_SWIG_FLAGS -package org.mycompany)
set(CMAKE_SWIG_OUTDIR ${CMAKE_CURRENT_BINARY_DIR}/src/main/java/org/mycompany)
swig_add_module(ExampleSWIG java example.i)
include_directories(${JNI_INCLUDE_DIRS})
swig_link_libraries(ExampleSWIG Example)
# How do I avoid the wildcard?
add_jar(ExampleJNI ${CMAKE_SWIG_OUTDIR}/*.java)
add_dependencies(ExampleJNI ExampleSWIG)
example.c
int foo() {
return 0;
}
example.i
%module example
%inline %{
extern int foo();
%}
I am using:
- cmake version 2.8.10.2
- Java version "1.6.0_37"
- SWIG Version 2.0.9
- Mac OS X Darwin Kernel Version 12.2.0