somexlab / fastddm

Python library for Differential Dynamic Microscopy analysis
https://fastddm.readthedocs.io/
GNU General Public License v3.0
3 stars 0 forks source link

[BUG]: Segmentation fault with conda on MacOS #206

Closed enrico-lattuada closed 5 months ago

enrico-lattuada commented 5 months ago

Description When using the package built with C++ enabled inside a conda environment on MacOS, I get a segmentation fault error when I import the library from Python.

To Reproduce Steps to reproduce the behavior:

  1. Create a conda environment configuration YAML file fastddm-test.yml.
    name: fddm-test
    channels:
      - defaults
    dependencies:
      - clang
      - clangxx
      - python>=3.8
  2. Create the environment
    conda env create -f fastddm-env.yml
  3. Activate the environment
    conda activate fddm-test
  4. Use the environment compilers by exporting the relevant variables
    conda env config vars set CC=$CONDA_PREFIX/bin/clang
    conda env config vars set CXX=$CONDA_PREFIX/bin/clang++
  5. Deactivate the environment
    conda deactivate
  6. Reactivate the environment
  7. Build and install the package from source with ENABLE_CPP=ON
  8. Import the package in Python

Expected behavior Import without errors.

Desktop (please complete the following information):

Additional context This might also be connected to the same problem (with or sometimes without conda environments) on other systems with multiple Python and C++ compiler versions installed.

enrico-lattuada commented 5 months ago

Possibly connected to this.

enrico-lattuada commented 5 months ago

The problem must be treated slightly differently for the different OSs.

The common part is using the MODULE option in the pybind11_add_module CMake function.

Linux

Linux automatically links the Python libraries when using the MODULE option. We don't need to make any changes.

MacOS

We must not explicitly link Python in the lower level libraries. We trust -undefined dynamic_lookup.

Add the following property for fddm (and for fddm_cuda as well, even though this should not be necessary since CUDA is not supported on macOS)

if(APPLE)
    set_target_properties(fddm PROPERTIES
        LINK_FLAGS "-undefined dynamic_lookup"
    )
endif(APPLE)

Windows

For Windows, we must explicitly link Python (at least, I found no source saying otherwise).

Add the following lines for both fddm and fddm_cuda

if(WIN32)
    target_link_libraries(fddm PUBLIC ${PYTHON_LIBRARIES})
endif(WIN32)

Also, since after pybind11_add_module the two options SHARED and MODULE are treated differently on Windows, we need to change the core and core_cuda target property from RUNTIME_OUTPUT_DIRECTORY_RELEASE to LIBRARY_OUTPUT_DIRECTORY_RELEASE.

enrico-lattuada commented 5 months ago

Closed by #207