SET(LIBS
    cc3d::CompuCellLib
    ${PYTHON_LIBRARY_TO_LINK}
)

if(${CMAKE_SYSTEM_NAME} STREQUAL Windows)

    if(${MSVC})
        # have to add /bigobj to visual studion compilation of hte CompuCell python module
        add_definitions(/bigobj)
                
    endif()

else(${CMAKE_SYSTEM_NAME} STREQUAL Windows)

    INCLUDE_DIRECTORIES(/usr/include)

    if(USE_DOLFIN)
        LIST(APPEND LIBS dolfin)
    endif()

endif(${CMAKE_SYSTEM_NAME} STREQUAL Windows)


SET(LIBS_AUX
    PyPlugin
    cc3d::BiasVectorSteppable
    cc3d::ImplicitMotility
    cc3d::Chemotaxis
    cc3d::FocalPointPlasticity
    cc3d::PlasticityTracker
    cc3d::Plasticity
    cc3d::NeighborTracker
    cc3d::VolumeTracker
    cc3d::CenterOfMass
    cc3d::PixelTracker
    cc3d::BoundaryPixelTracker
    cc3d::Mitosis
    cc3d::MitosisSteppable
    cc3d::ClusterSurface
    cc3d::ClusterSurfaceTracker
    cc3d::ElasticityTracker
    cc3d::Elasticity
    cc3d::ContactLocalFlex
    cc3d::ContactLocalProduct
    cc3d::ContactMultiCad
    cc3d::AdhesionFlex
    cc3d::ConnectivityLocalFlex
    cc3d::ConnectivityGlobal
    cc3d::LengthConstraint
    cc3d::BoundaryMonitor
    cc3d::MomentOfInertia
    cc3d::Secretion
    cc3d::CellOrientation
    cc3d::PolarizationVector
    cc3d::Polarization23
    cc3d::OrientedGrowth
    cc3d::OrientedGrowth2
    cc3d::ContactOrientation
    # cc3d::CleaverMeshDumper

    # AutogeneratedModules - DO NOT REMOVE THIS LINE IT IS USED BY TWEDIT TO LOCATE CODE INSERTION POINT
    # CurvatureCalculator_autogenerated
    cc3d::CurvatureCalculator
    cc3d::PDESolvers
    cc3d::VectorFieldPolarization
    cc3d::FieldManager

)

SET_SOURCE_FILES_PROPERTIES(CompuCell.i PROPERTIES CPLUSPLUS ON)

if(${CMAKE_SYSTEM_NAME} STREQUAL Windows)

    SET_SOURCE_FILES_PROPERTIES(CompuCell.i  PROPERTIES SWIG_FLAGS "-DCC3D_ISWIN")

elseif(${CMAKE_SYSTEM_NAME} STREQUAL Darwin)

    SET_SOURCE_FILES_PROPERTIES(CompuCell.i  PROPERTIES SWIG_FLAGS "-DCC3D_ISMAC")

endif()

# SET_SOURCE_FILES_PROPERTIES(CompuCellPython.i CompuCellAuxPython.i PROPERTIES SWIG_FLAGS "-includeall")
SWIG_ADD_LIBRARY(CompuCell LANGUAGE python SOURCES CompuCell.i)

if(${CMAKE_SYSTEM_NAME} STREQUAL Windows)
  # changes dll name to pyd sop that it is compatible with new Python standard
  SET_TARGET_PROPERTIES(CompuCell PROPERTIES SUFFIX ".pyd")
endif(${CMAKE_SYSTEM_NAME} STREQUAL Windows)

target_include_directories(CompuCell PUBLIC
    ${PYTHON_DIRS_TO_INCLUDE}
    $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
    $<BUILD_INTERFACE:${COMPUCELL3D_SOURCE_DIR}/core/CompuCell3D>
    $<BUILD_INTERFACE:${COMPUCELL3D_SOURCE_DIR}/core/CompuCell3D/plugins>
    $<BUILD_INTERFACE:${COMPUCELL3D_SOURCE_DIR}/core/pyinterface>
    $<BUILD_INTERFACE:${COMPUCELL3D_SOURCE_DIR}/core/pyinterface/PyPlugin>
    $<INSTALL_INTERFACE:${CMAKE_INSTALL_PREFIX}/${COMPUCELL3D_INSTALL_INCLUDE_DIR}/CompuCell3D>
    $<INSTALL_INTERFACE:${CMAKE_INSTALL_PREFIX}/${COMPUCELL3D_INSTALL_INCLUDE_DIR}/CompuCell3D/plugins>
    $<INSTALL_INTERFACE:${CMAKE_INSTALL_PREFIX}/${COMPUCELL3D_INSTALL_INCLUDE_DIR}/pyinterface/PyPlugin>
)
set_target_properties(CompuCell PROPERTIES
    LINK_FLAGS ${undefined_link_flags}
    SWIG_USE_TARGET_INCLUDE_DIRECTORIES TRUE
)
SWIG_LINK_LIBRARIES(CompuCell ${LIBS} ${LIBS_AUX})
target_link_directories(CompuCell PUBLIC ${Python_LIBRARY_DIRS})

if (APPLE)

   # interesting reading: https://blog.kitware.com/upcoming-in-cmake-2-8-12-osx-rpath-support/

   # INSTALL_NAME_DIR overrides MACOSX_RPATH.  Try not to do that.

   # Use rpath for CompuCell.so
   SET_TARGET_PROPERTIES(CompuCell PROPERTIES MACOSX_RPATH TRUE)

   # append directories in the linker search path and outside the project to the INSTALL_RPATH
   SET_TARGET_PROPERTIES(CompuCell PROPERTIES CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)

   # this is where libraries on which CompuCell.so depends on are
   # installed relative to where CompuCell.py is installed
   SET_TARGET_PROPERTIES(CompuCell PROPERTIES INSTALL_RPATH "@loader_path/CompuCell3DPlugins;@loader_path/CompuCell3DSteppables;@loader_path/lib;@loader_path/../../../")


endif()

install(TARGETS CompuCell DESTINATION ${COMPUCELL3D_INSTALL_SWIG_MODULES_DIR} )

set(python_files_path ${CMAKE_BINARY_DIR}/core/pyinterface/CompuCellPython)

INSTALL(FILES ${python_files_path}/CompuCell.py 
        DESTINATION ${COMPUCELL3D_INSTALL_SWIG_MODULES_DIR}
)
