include_directories(
    ${CMAKE_CURRENT_SOURCE_DIR}/WrappingJava/src/main/java/org/geuz/gmsh/utils)
# flag used to compilation during Java wrapping
set(CMAKE_CXX_FLAGS "-fno-strict-aliasing")
# find SWIG and include the swig file
find_package(SWIG REQUIRED)
include(${SWIG_USE_FILE})
# find Java and JNI and include files required
find_package(Java REQUIRED)
find_package(JNI REQUIRED)
include_directories(${JNI_INCLUDE_DIRS})
include_directories(${JAVA_INCLUDE_PATH})
include_directories(${JAVA_INCLUDE_PATH2})
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
# define where the interface file (used by SWIG) are
set(SWIG_SOURCES WrapGmsh.i)
# let swig know that example.i is c++
set_source_files_properties(${SWIG_SOURCES} PROPERTIES CPLUSPLUS ON)
# define where the Java files generated by SWIG will be stored
set(CMAKE_SWIG_OUTDIR ${CMAKE_CURRENT_BINARY_DIR}/WrappingJava/org/geuz/gmsh/generated/)
# defines the packaging of Java files generated by SWIG
set(JAVAPACKAGENAME "org.geuz.gmsh.generated")
set(CMAKE_SWIG_FLAGS -package ${JAVAPACKAGENAME})
# create the swig module called WrapGmsh
# using the SWIG_SOURCE flag defined previously
# swig will be used to create WrapGmshJAVA_wrap.cxx from SWIG_SOURCE
swig_add_module(WrapGmsh java ${SWIG_SOURCES})
swig_link_libraries(WrapGmsh shared)

# link dynamic library called libWrapGmsh with the help of "sharedSrc" library

# define a good name for the dynamic libraty which depends on the OS
if(WIN32)
  set_target_properties(WrapGmsh PROPERTIES PREFIX "")
endif(WIN32)
if(UNIX)
  set_target_properties(WrapGmsh PROPERTIES PREFIX "lib")
endif(UNIX)
add_custom_command(TARGET WrapGmsh
POST_BUILD # do the rest of the command after the build period
# write on the output consol
  COMMAND cmake -E echo "Compiling Java files..."
  #compilation of Java files generated by SWIG
  COMMAND ${JAVA_COMPILE}
    ${CMAKE_CURRENT_BINARY_DIR}/WrappingJava/org/geuz/gmsh/generated/*.java
    -classpath ${CMAKE_CURRENT_BINARY_DIR}/WrappingJava/
    -d ${CMAKE_CURRENT_BINARY_DIR}/WrappingJava/
  # compilation of Java files used by wrapping
  COMMAND ${JAVA_COMPILE}
    ${CMAKE_CURRENT_SOURCE_DIR}/WrappingJava/src/main/java/org/geuz/gmsh/utils/*.java
    -classpath ${CMAKE_CURRENT_BINARY_DIR}/WrappingJava/
    -d ${CMAKE_CURRENT_BINARY_DIR}/WrappingJava/
  # compilation of Java files used as demonstration
  COMMAND ${JAVA_COMPILE}
    ${CMAKE_CURRENT_SOURCE_DIR}/WrappingJava/src/main/java/com/artenum/sample/*.java
    -classpath ${CMAKE_CURRENT_BINARY_DIR}/WrappingJava/
    -d ${CMAKE_CURRENT_BINARY_DIR}/WrappingJava/
  # create a jar file which contains the files with a .class extension
  COMMAND cmake -E echo "Creating jar file..."
  COMMAND ${JAVA_ARCHIVE} cvf ${CMAKE_CURRENT_BINARY_DIR}/WrappingJava/WrapGmsh.jar
    -C ${CMAKE_CURRENT_BINARY_DIR}/WrappingJava/ org
  COMMAND ${JAVA_ARCHIVE} cvf ${CMAKE_CURRENT_BINARY_DIR}/WrappingJava/Sample.jar
    -C ${CMAKE_CURRENT_BINARY_DIR}/WrappingJava/ com
  # create a jar from Sample
  COMMAND ${CMAKE_COMMAND} -E copy
    ${CMAKE_CURRENT_SOURCE_DIR}/WrappingJava/src/main/java/t5.geo
    ${CMAKE_CURRENT_BINARY_DIR}/WrappingJava/
  # copy the build.xml file to run the ant script
  COMMAND ${CMAKE_COMMAND} -E copy
    ${CMAKE_CURRENT_SOURCE_DIR}/WrappingJava/build.xml
    ${CMAKE_CURRENT_BINARY_DIR}/WrappingJava/)
install(TARGETS WrapGmsh DESTINATION ${GMSH_LIB} OPTIONAL)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/WrappingJava/WrapGmsh.jar
    DESTINATION ${GMSH_LIB})
