From 004e29b5bcd8a146d69125caa9b943cbc6aee44c Mon Sep 17 00:00:00 2001
From: Lars Bilke <lars.bilke@ufz.de>
Date: Thu, 13 Apr 2023 11:17:11 +0200
Subject: [PATCH] [py] Fix and instructions on importating simulator modules.

---
 Applications/Python/ogs.mesh/CMakeLists.txt             | 5 +----
 Applications/Python/ogs.mesh/OGSMesh.h                  | 4 +++-
 Applications/Python/ogs.simulator/CMakeLists.txt        | 2 +-
 Applications/Python/ogs.simulator/ogs_python_module.cpp | 7 ++++++-
 4 files changed, 11 insertions(+), 7 deletions(-)

diff --git a/Applications/Python/ogs.mesh/CMakeLists.txt b/Applications/Python/ogs.mesh/CMakeLists.txt
index 5aa2e91c554..4cdc635a427 100644
--- a/Applications/Python/ogs.mesh/CMakeLists.txt
+++ b/Applications/Python/ogs.mesh/CMakeLists.txt
@@ -1,8 +1,6 @@
 # Create OpenGeoSys python module
 # https://pybind11.readthedocs.io/en/stable/compiling.html#building-with-cmake
-pybind11_add_module(
-    mesh MODULE ogs_mesh_python_module.cpp OGSMesh.cpp
-)
+pybind11_add_module(mesh MODULE ogs_mesh_python_module.cpp OGSMesh.cpp)
 
 # lld linker strips out PyInit_OpenGeoSys symbol. Use standard linker.
 get_target_property(_link_options mesh LINK_OPTIONS)
@@ -14,7 +12,6 @@ endif()
 target_link_libraries(
     mesh PRIVATE MeshLib BaseLib CMakeInfoLib GitInfoLib tclap
 )
-target_include_directories(mesh  PRIVATE ../../../MeshLib)
 
 # Install into Python module root dir (enables 'import ogs.simulator')
 install(TARGETS mesh LIBRARY DESTINATION ogs)
diff --git a/Applications/Python/ogs.mesh/OGSMesh.h b/Applications/Python/ogs.mesh/OGSMesh.h
index 4b8326efa30..965f7c02dfe 100644
--- a/Applications/Python/ogs.mesh/OGSMesh.h
+++ b/Applications/Python/ogs.mesh/OGSMesh.h
@@ -29,7 +29,9 @@
 #include "MeshLib/Mesh.h"
 #include "MeshLib/Node.h"
 
-class OGSMesh
+// Needs to be exported, see
+// https://pybind11.readthedocs.io/en/stable/advanced/misc.html#partitioning-code-over-multiple-extension-modules
+class PYBIND11_EXPORT OGSMesh
 {
 public:
     explicit OGSMesh(MeshLib::Mesh* mesh);
diff --git a/Applications/Python/ogs.simulator/CMakeLists.txt b/Applications/Python/ogs.simulator/CMakeLists.txt
index 61d9ef51b85..daf04fcd5e2 100644
--- a/Applications/Python/ogs.simulator/CMakeLists.txt
+++ b/Applications/Python/ogs.simulator/CMakeLists.txt
@@ -15,7 +15,7 @@ endif()
 target_link_libraries(
     simulator PRIVATE ApplicationsLib BaseLib CMakeInfoLib GitInfoLib tclap
 )
-target_include_directories(simulator PRIVATE ../../CLI ../ogs.mesh)
+target_include_directories(simulator PRIVATE ../../CLI)
 
 # Install into Python module root dir (enables 'import ogs.simulator')
 install(TARGETS simulator LIBRARY DESTINATION ogs)
diff --git a/Applications/Python/ogs.simulator/ogs_python_module.cpp b/Applications/Python/ogs.simulator/ogs_python_module.cpp
index c6ce2ac2ddf..d99fe166184 100644
--- a/Applications/Python/ogs.simulator/ogs_python_module.cpp
+++ b/Applications/Python/ogs.simulator/ogs_python_module.cpp
@@ -182,7 +182,12 @@ void finalize()
     BaseLib::unsetProjectDirectory();
 }
 
-/// python module name is OpenGeoSys
+/// To use this module import dependencies first:
+///   import ogs.mesh as mesh
+///   import ogs.simulator as sim
+///
+/// See also
+/// https://github.com/pybind/pybind11/issues/1391#issuecomment-912642979
 PYBIND11_MODULE(simulator, m)
 {
     m.attr("__name__") = "ogs.simulator";
-- 
GitLab