@@ -14,6 +14,7 @@ set(DSF_VERSION "${DSF_VERSION_MAJOR}.${DSF_VERSION_MINOR}.${DSF_VERSION_PATCH}"
1414project (dsf VERSION ${DSF_VERSION} LANGUAGES CXX)
1515
1616option (DSF_TEST "Build DSF tests" OFF )
17+ option (BUILD_PYTHON_BINDINGS "Build Python bindings" OFF )
1718
1819# Set the C++ standard
1920set (CMAKE_CXX_STANDARD 20)
@@ -45,6 +46,19 @@ endif()
4546
4647file (GLOB SOURCES "src/dsf/base/*.cpp" "src/dsf/mobility/*.cpp" "src/dsf/utility/*.cpp" "src/dsf/geometry/*.cpp" "src/dsf/mdt/*.cpp" )
4748
49+ # Determine library type based on whether Python bindings are enabled
50+ if (BUILD_PYTHON_BINDINGS)
51+ set (DSF_LIB_TYPE SHARED)
52+ # Ensure all dependencies are built with -fPIC for shared libraries
53+ set (CMAKE_POSITION_INDEPENDENT_CODE ON )
54+ # Set RPATH for the shared library to find dependencies
55+ set (CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE )
56+ set (CMAKE_BUILD_WITH_INSTALL_RPATH TRUE )
57+ set (CMAKE_INSTALL_RPATH "$ORIGIN" )
58+ else ()
59+ set (DSF_LIB_TYPE STATIC )
60+ endif ()
61+
4862include (FetchContent)
4963# Get rapidcsv
5064FetchContent_Declare(rapidcsv
@@ -84,7 +98,7 @@ if(NOT tbb_POPULATED)
8498 FetchContent_MakeAvailable(tbb)
8599endif ()
86100
87- add_library (dsf STATIC ${SOURCES} )
101+ add_library (dsf ${DSF_LIB_TYPE} ${SOURCES} )
88102target_include_directories (dsf PUBLIC
89103 $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR} /src>
90104 $<INSTALL_INTERFACE:include >
@@ -109,9 +123,6 @@ install(EXPORT dsfConfig
109123 NAMESPACE dsf::
110124 DESTINATION lib/cmake/dsf)
111125
112- # Optional Python bindings - only build if requested
113- option (BUILD_PYTHON_BINDINGS "Build Python bindings" OFF )
114-
115126if (BUILD_PYTHON_BINDINGS)
116127 include (FetchContent)
117128
@@ -143,8 +154,19 @@ if(BUILD_PYTHON_BINDINGS)
143154 "${tbb_SOURCE_DIR} /include"
144155 )
145156
146- # Link the pybind11 module with your static library and pybind11
147- target_link_libraries (dsf_python_module PRIVATE dsf pybind11::module pybind11::headers TBB::tbb simdjson::simdjson)
157+ # Link the pybind11 module with your shared library and pybind11
158+ target_link_libraries (dsf_python_module PRIVATE dsf pybind11::module pybind11::headers TBB::tbb simdjson::simdjson spdlog::spdlog)
159+
160+ # Copy TBB and dsf shared libraries next to the Python module for runtime access
161+ add_custom_command (TARGET dsf_python_module POST_BUILD
162+ COMMAND ${CMAKE_COMMAND} -E copy_if_different
163+ $<TARGET_FILE:TBB::tbb>
164+ $<TARGET_FILE_DIR:dsf_python_module>/$<TARGET_SONAME_FILE_NAME:TBB::tbb>
165+ COMMAND ${CMAKE_COMMAND} -E copy_if_different
166+ $<TARGET_FILE:dsf>
167+ $<TARGET_FILE_DIR:dsf_python_module>/
168+ COMMENT "Copying shared libraries to Python module directory"
169+ )
148170endif ()
149171
150172# Tests
0 commit comments