
set(PYTHON_LIBRARY_TO_LINK ${Python3_LIBRARY_RELEASE})
set(undefined_link_flags " ")
if(${CMAKE_SYSTEM_NAME} STREQUAL Darwin)
    # on osx wih conda python 3 we do not link python
    # https://github.com/shogun-toolbox/shogun/issues/4068
    # Ok having spent some time on that I finally found the culprit. I had to move some lines around in generated SWIG wrapper to realise that the true error masked by this segfault is:
    #
    # PyThreadState_Get: no current thread
    # Here is the discussion I found then: https://groups.google.com/a/continuum.io/forum/#!topic/anaconda/057P4uNWyCU
    #
    # Anaconda guys do link python statically. So we run against dynamic library 'python' and the interpreter runs against statically linked 'python'. This causes stuff like segfault and 'no current thread' as that dynamic python is not initialised.
    #
    # The solution is quite simple. In case of anaconda we should not link but add -undefined dynamic_lookup instead. Patch to come soon.


    set(PYTHON_LIBRARY_TO_LINK )
    set(undefined_link_flags "-undefined dynamic_lookup")
endif()



if(${CMAKE_SYSTEM_NAME} STREQUAL Windows)
SET(LIBS
  CC3DCompuCellLib
   CC3DXMLUtils
   ${Python3_LIBRARY_RELEASE}

)
else(${CMAKE_SYSTEM_NAME} STREQUAL Windows)
SET(LIBS
  CC3DCompuCellLib   
   CC3DXMLUtils   
   ${PYTHON_LIBRARY_TO_LINK}

)


endif(${CMAKE_SYSTEM_NAME} STREQUAL Windows)



SET(LIBS_AUX
   SimpleVolumeShared
   VolumeMeanShared                    
   PyPlugin
#AutogeneratedModules - DO NOT REMOVE THIS LINE IT IS USED BY TWEDIT TO LOCATE CODE INSERTION POINT
#CustomCellAttributeSteppable_autogenerated
CustomCellAttributeSteppableShared
#HeterotypicBoundaryLength_autogenerated
HeterotypicBoundaryLengthShared
#GrowthSteppable_autogenerated
GrowthSteppableShared
)

# we assume that CMAKE_INSTALL_PREFIX points to site packages then relative path from here
# to the place where things need to go is "cc3d/cpp/"
SET(COMPUCELL3D_INSTALL_SWIG_MODULES_DIR "cc3d/cpp/")
# if we use altermative install scheme - olde CC#D layout we woudl us this setting w.r.t. CMAKE_INSTALL_PREFIX
# SET(COMPUCELL3D_INSTALL_SWIG_MODULES_DIR "lib/site-packages/cc3d/cpp/")

MESSAGE(COMPUCELL3D_INCLUDE_PATH ${COMPUCELL3D_INCLUDE_PATH})

INCLUDE_DIRECTORIES(
${CMAKE_CURRENT_SOURCE_DIR}
${COMPUCELL3D_SOURCE_DIR}
${COMPUCELL3D_SOURCE_DIR}/core/CompuCell3D
${COMPUCELL3D_SOURCE_DIR}/core
${COMPUCELL3D_INCLUDE_PATH}
${COMPUCELL3D_INSTALL_PATH}/include
${COMPUCELL3D_SOURCE_DIR}/CompuCell3D
${COMPUCELL3D_SOURCE_DIR}/CompuCell3D/plugins
${COMPUCELL3D_INCLUDE_PATH}/CompuCell3D
${COMPUCELL3D_INCLUDE_PATH}/CompuCell3D/plugins
${PYINTERFACE_SRC_DIR}/PyPlugin
${PYINTERFACE_SRC_DIR}/..
)

message("PY INTERFACE COMPUCELL3D_SOURCE_DIR=" ${COMPUCELL3D_SOURCE_DIR})
message("PY INTERFACE COMPUCELL3D_INCLUDE_PATH=" ${COMPUCELL3D_INCLUDE_PATH})
message("PY INTERFACE COMPUCELL3D_INSTALL_PATH=" ${COMPUCELL3D_INSTALL_PATH})



SET_SOURCE_FILES_PROPERTIES(CompuCellExtraModules.i  PROPERTIES CPLUSPLUS ON)
SWIG_ADD_LIBRARY(CompuCellExtraModules LANGUAGE python SOURCES CompuCellExtraModules.i)
# SWIG_ADD_MODULE(CompuCellExtraModules python CompuCellExtraModules.i)


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

SWIG_LINK_LIBRARIES(CompuCellExtraModules ${LIBS} ${LIBS_AUX})
set_target_properties(CompuCellExtraModules PROPERTIES LINK_FLAGS ${undefined_link_flags})

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(CompuCellExtraModules PROPERTIES MACOSX_RPATH TRUE)

   # append directories in the linker search path and outside the project to the INSTALL_RPATH
   SET_TARGET_PROPERTIES(CompuCellExtraModules 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(CompuCellExtraModules PROPERTIES INSTALL_RPATH "@loader_path/CompuCell3DPlugins;@loader_path/CompuCell3DSteppables;@loader_path/lib")


endif()

# install_targets(/lib/python _CompuCellExtraModules)
install(TARGETS CompuCellExtraModules DESTINATION ${COMPUCELL3D_INSTALL_SWIG_MODULES_DIR} )

set(python_files_path ${CMAKE_BINARY_DIR}/pyinterface/CompuCellExtraModules)

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