cmake_minimum_required(VERSION 3.7)

project(geofis-jni CXX)

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

include(FISPro)
include(GeoFIS)
include(JNI)

if(BUILD_OPENSUSE)
    require_util()
    require_fispro()
    require_geofis()
else()
    find_util()
    find_fispro()
    find_geofis()
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(GEOFIS_JNI_SOURCES
    ${GEOFIS_JNI_SOURCE_DIR}/cpp/geofis/geofis_wrap.cpp
    ${GEOFIS_JNI_SOURCE_DIR}/cpp/geofis/data/data_wrap.cpp
    ${GEOFIS_JNI_SOURCE_DIR}/cpp/geofis/geometry/geometry_wrap.cpp
    ${GEOFIS_JNI_SOURCE_DIR}/cpp/geofis/process/zoning/fusion/aggregation/aggregation_wrap.cpp
    ${GEOFIS_JNI_SOURCE_DIR}/cpp/geofis/process/zoning/fusion/distance/distance_wrap.cpp
    ${GEOFIS_JNI_SOURCE_DIR}/cpp/geofis/process/zoning/merge/merge_wrap.cpp
    ${GEOFIS_JNI_SOURCE_DIR}/cpp/geofis/process/zoning/fusion/fusion_wrap.cpp
    ${GEOFIS_JNI_SOURCE_DIR}/cpp/geofis/process/zoning/neighborhood/neighborhood_wrap.cpp
    ${GEOFIS_JNI_SOURCE_DIR}/cpp/geofis/process/zoning/voronoi/voronoi_wrap.cpp
    ${GEOFIS_JNI_SOURCE_DIR}/cpp/geofis/process/zoning/zoning_wrap.cpp) 

#add_dependencies(${UTIL_EXTERNAL} ${GEOFIS_EXTERNAL})

# the SWIG %template directive only accepts one parameter substitution, see http://www.swig.org/Doc3.0/CPlusPlus11.html#CPlusPlus11_variadic_templates for BOOST_VARIANT_DO_NOT_USE_VARIADIC_TEMPLATES preprocessor option
# see https://www.boost.org/doc/libs/master/libs/config/doc/html/boost_config/boost_macro_reference.html
# see http://www.swig.org/Doc1.3/Java.html#compiling_dynamic for no-strict-aliasing option
set(COMPILE_FLAGS_PROPERTIES "-DBOOST_VARIANT_DO_NOT_USE_VARIADIC_TEMPLATES -DBOOST_NO_AUTO_PTR -Wno-maybe-uninitialized -fno-strict-aliasing -Wno-unused-function -Wno-nonnull")

if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 6)
    # gnu++14 is now the default standard 
    # see https://gcc.gnu.org/gcc-6/changes.html
    set(COMPILE_FLAGS_PROPERTIES "${COMPILE_FLAGS_PROPERTIES} -std=gnu++14")
endif()

if(CMAKE_CXX_COMPILER_VERSION GREATER_EQUAL 12)
	# The standard library no longer supports std::unary_function and std::binary_function. They were deprecated in C++11 and is removed from C++14.
	# see https://www.boost.org/doc/libs/1_69_0/libs/config/doc/html/boost_config/boost_macro_reference.html
	# gcc 12 warns deprecated-declarations
	# /CMakeExternals-12.0.1/Install/boost/1.78.0/x86_64-w64-mingw32/include/boost/numeric/ublas/detail/iterator.hpp:111:21: warning: 'template<class _Category, class _Tp, class _Distance, class _Pointer, class _Reference> struct std::iterator' is deprecated [-Wdeprecated-declarations]
    set(COMPILE_FLAGS_PROPERTIES "${COMPILE_FLAGS_PROPERTIES} -DBOOST_NO_CXX98_FUNCTION_BASE -Wno-deprecated-declarations")
endif()

if(BOOST_VERSION VERSION_LESS 1.67)
    set(COMPILE_FLAGS_PROPERTIES "${COMPILE_FLAGS_PROPERTIES} -Wno-parentheses -Wno-deprecated-declarations -Wno-misleading-indentation")
elseif(BOOST_VERSION VERSION_LESS 1.68)
    set(COMPILE_FLAGS_PROPERTIES "${COMPILE_FLAGS_PROPERTIES} -Wno-parentheses -Wno-misleading-indentation")
elseif(BOOST_VERSION VERSION_LESS 1.69)
    set(COMPILE_FLAGS_PROPERTIES "${COMPILE_FLAGS_PROPERTIES} -Wno-misleading-indentation")
elseif(BOOST_VERSION VERSION_EQUAL 1.69)
    set(COMPILE_FLAGS_PROPERTIES "${COMPILE_FLAGS_PROPERTIES} -DBOOST_ALLOW_DEPRECATED_HEADERS -Wno-misleading-indentation")
elseif(BOOST_VERSION VERSION_EQUAL 1.74)
    set(COMPILE_FLAGS_PROPERTIES "${COMPILE_FLAGS_PROPERTIES} -DBOOST_ALLOW_DEPRECATED_HEADERS")
endif()

if(MINGW AND TARGET_32_BITS AND ((BOOST_VERSION VERSION_GREATER 1.72) AND (BOOST_VERSION VERSION_LESS 1.76)))
    # see boost/config/compiler/gcc.hpp
    # if __MINGW32__ BOOST_NO_CXX11_THREAD_LOCAL defined
    # so Boost.Multiprecision and Boost.Math deprecated
    set(COMPILE_FLAGS_PROPERTIES "${COMPILE_FLAGS_PROPERTIES} -DBOOST_MP_DISABLE_DEPRECATE_03_WARNING -DBOOST_MATH_DISABLE_DEPRECATED_03_WARNING")
endif()

if(CGAL_VERSION VERSION_LESS 4.9)
    # warn about catching polymorphic type 'class CGAL::Uncertain_conversion_exception' by value in CGAL/Lazy.h
    # uninitialized warning in CGAL/Arr_dcel_base.h default constructor of Arr_vertex and Arr_face classes
    set(COMPILE_FLAGS_PROPERTIES "${COMPILE_FLAGS_PROPERTIES} -Wno-attributes -Wno-catch-value -Wno-uninitialized")
elseif(CGAL_VERSION VERSION_LESS 4.10)
    set(COMPILE_FLAGS_PROPERTIES "${COMPILE_FLAGS_PROPERTIES} -Wno-catch-value -Wno-uninitialized")
elseif(CGAL_VERSION VERSION_LESS 4.12)
    set(COMPILE_FLAGS_PROPERTIES "${COMPILE_FLAGS_PROPERTIES} -Wno-uninitialized")
elseif((CGAL_VERSION VERSION_GREATER 4.12.2) AND (CGAL_VERSION VERSION_LESS 5.3))
    # see https://stackoverflow.com/questions/55732233/why-i-am-getting-boost-parameter-max-arity-redefined-warning-while-compiling for BOOST_PARAMETER_MAX_ARITY=12
    set(COMPILE_FLAGS_PROPERTIES "${COMPILE_FLAGS_PROPERTIES} -DBOOST_PARAMETER_MAX_ARITY=12")
endif()

if(COMPILE_FLAGS_PROPERTIES)
    message(STATUS "GeoFIS JNI CXX compile flags: ${COMPILE_FLAGS_PROPERTIES}")
    set_source_files_properties(${GEOFIS_JNI_SOURCES} PROPERTIES COMPILE_FLAGS ${COMPILE_FLAGS_PROPERTIES})
endif()

add_library(geofis-jni MODULE ${GEOFIS_JNI_SOURCES})
# TODO MPFR_LIBRARY_NAME GMP_LIBRARY_NAME MPFR et GMP en shared dll, et GeoFISBoost.cmake
target_link_libraries(geofis-jni ${GeoFIS_LIBRARY_NAME} ${CGAL_LIBRARIES_NAME} ${CGAL_BOOST_PREFIX_LIBRARIES} boost_filesystem ${MPFR_LIBRARY} ${GMP_LIBRARY} ${FISPro_LIBRARY_NAME} ${Util_LIBRARY_NAME})
if(MSYS)
    set_target_properties(geofis-jni PROPERTIES LINK_FLAGS "-Wl,--kill-at")
endif()    
if(DEPENDENCIES)
	message(STATUS "GeoFIS JNI Dependencies = ${DEPENDENCIES}")
	add_dependencies(geofis-jni ${DEPENDENCIES})
endif()

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

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