cmake_minimum_required(VERSION 3.7)

project(fispro-jni CXX)

get_filename_component(CMAKE_MODULE_PATH "${CMAKE_MODULE_PATH}" ABSOLUTE)
get_filename_component(CMAKE_EXTERNALS "${CMAKE_EXTERNALS}" ABSOLUTE)

include(Util)
include(FISPro)
include(JNI)

if(BUILD_OPENSUSE)
    require_util()
    require_fispro()
else()
    find_util()
    find_fispro()
endif()
require_jni()

#file(GLOB_RECURSE UTIL_JNI_CPP_FILES *.cpp)
#foreach(UTIL_JNI_CPP_FILE ${UTIL_JNI_CPP_FILES})
#    file(RELATIVE_PATH UTIL_JNI_RELATIVE_CPP_FILE ${CMAKE_CURRENT_SOURCE_DIR} ${UTIL_JNI_CPP_FILE}) 
#    message(${UTIL_JNI_RELATIVE_CPP_FILE})
#endforeach()

set(FISPRO_JNI_SOURCES
    ${FISPRO_JNI_SOURCE_DIR}/cpp/fispro/fis/input/input_wrap.cpp
    ${FISPRO_JNI_SOURCE_DIR}/cpp/fispro/fis/output/output_wrap.cpp
    ${FISPRO_JNI_SOURCE_DIR}/cpp/fispro/fis/rule/rule_wrap.cpp
    ${FISPRO_JNI_SOURCE_DIR}/cpp/fispro/fis/mf/mf_wrap.cpp
    ${FISPRO_JNI_SOURCE_DIR}/cpp/fispro/fis/fis_wrap.cpp
    ${FISPRO_JNI_SOURCE_DIR}/cpp/fispro/fispro_wrap.cpp) 

# see http://www.swig.org/Doc1.3/Java.html#compiling_dynamic for no-strict-aliasing option
set(COMPILE_FLAGS_PROPERTIES "-fno-strict-aliasing -Wno-unused-function -Wno-maybe-uninitialized")
if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 6)
    # gnu++14 is now the default standard 
    set(COMPILE_FLAGS_PROPERTIES "${COMPILE_FLAGS_PROPERTIES} -std=gnu++14")
endif()
if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 6)
    set(COMPILE_FLAGS_PROPERTIES "${COMPILE_FLAGS_PROPERTIES} -Wno-address -Wno-nonnull-compare")
endif()
message(STATUS "CXX compile flags: ${COMPILE_FLAGS_PROPERTIES}")
set_source_files_properties(${FISPRO_JNI_SOURCES} PROPERTIES COMPILE_FLAGS ${COMPILE_FLAGS_PROPERTIES})
add_library(fispro-jni MODULE ${FISPRO_JNI_SOURCES})
target_link_libraries(fispro-jni ${FISPro_LIBRARY_NAME} ${Util_LIBRARY_NAME})
if(MSYS)
    set_target_properties(fispro-jni PROPERTIES LINK_FLAGS "-Wl,--kill-at")
endif()    
	message(STATUS "FisPro JNI Dependencies = ${DEPENDENCIES}")
if(DEPENDENCIES)
	add_dependencies(fispro-jni ${DEPENDENCIES})
endif()

if(UNIX)
    set_target_properties(fispro-jni PROPERTIES INSTALL_RPATH "$ORIGIN/")
endif()

if(BUILD_OPENSUSE)
    install(TARGETS fispro-jni 
        	RUNTIME DESTINATION bin
            LIBRARY DESTINATION lib
            ARCHIVE DESTINATION lib)
endif()
