From e0ffcc8776b62e5212575ade24b21f5970ff81eb Mon Sep 17 00:00:00 2001
From: Behzad Safaei <iwia103h@alex1.nhr.fau.de>
Date: Sat, 26 Apr 2025 20:03:17 +0200
Subject: [PATCH] Fix: expose waLBerla dependencies of P4IRS to consumer
 projects

---
 CMakeLists.txt              | 21 ++++++++++++++-------
 cmake/pairs-config.cmake.in |  1 -
 src/pairs/code_gen/cgen.py  |  3 +++
 src/pairs/sim/simulation.py |  2 ++
 4 files changed, 19 insertions(+), 8 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 50b9df3..3f60b34 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -64,7 +64,6 @@ set(PAIRS_TARGET "pairs")
 
 # PAIRS dependencies 
 set(PAIRS_LINK_LIBRARIES)
-set(PAIRS_LINK_DIRS ${CMAKE_CURRENT_BINARY_DIR})
 set(PAIRS_INCLUDE_DIRS ${CMAKE_CURRENT_BINARY_DIR})
 
 # The target can either be an executable or a static library
@@ -72,7 +71,9 @@ if(USER_SOURCE_FILES)
     add_executable(${PAIRS_TARGET} ${RUNTIME_COMMON_FILES})
 else()
     add_library(${PAIRS_TARGET} STATIC ${RUNTIME_COMMON_FILES})
-    list(APPEND PAIRS_LINK_LIBRARIES ${PAIRS_TARGET})
+
+    # TODO: Get the location with $<TARGET_FILE> generator expression (at build time)
+    list(APPEND PAIRS_LINK_LIBRARIES "${CMAKE_CURRENT_BINARY_DIR}/lib${PAIRS_TARGET}.a")
 endif()
 
 # Include P4IRS 'runtime' dir
@@ -119,12 +120,18 @@ if(USE_WALBERLA)
     waLBerla_import()
 
     # Recent issue from the waLBerla side: warnings about empty CUDA_ARCHITECTURES for walberla_gpu 
-    set(PAIRS_WALBERLA_DEPENDENCIES walberla::blockforest walberla::core)
-    target_link_libraries(${PAIRS_TARGET} PUBLIC ${PAIRS_WALBERLA_DEPENDENCIES})
+    target_link_libraries(${PAIRS_TARGET} PUBLIC 
+        walberla::blockforest 
+        walberla::domain_decomposition 
+        walberla::core 
+        stdc++fs
+    )
 
-    ## TODO: PAIRS_LINK_DIRS and PAIRS_LINK_LIBRARIES for walberla modules *AND* their dependencies
-    ## This implemention only works if the consumer of the library is itself a walberla app (made within the build system of walberla)
-    list(APPEND PAIRS_LINK_LIBRARIES ${PAIRS_WALBERLA_DEPENDENCIES})
+    # TODO: Get the location with $<TARGET_FILE> generator expression (at build time)
+    list(APPEND PAIRS_LINK_LIBRARIES ${CMAKE_CURRENT_BINARY_DIR}/external/walberla/src/blockforest/libwalberla_blockforest.a)
+    list(APPEND PAIRS_LINK_LIBRARIES ${CMAKE_CURRENT_BINARY_DIR}/external/walberla/src/domain_decomposition/libwalberla_domain_decomposition.a)
+    list(APPEND PAIRS_LINK_LIBRARIES ${CMAKE_CURRENT_BINARY_DIR}/external/walberla/src/core/libwalberla_core.a)
+    list(APPEND PAIRS_LINK_LIBRARIES stdc++fs)
 endif()
 
 #================================================================================
diff --git a/cmake/pairs-config.cmake.in b/cmake/pairs-config.cmake.in
index fdf10ef..db70813 100644
--- a/cmake/pairs-config.cmake.in
+++ b/cmake/pairs-config.cmake.in
@@ -2,5 +2,4 @@ set ( pairs_SOURCE_DIR @pairs_SOURCE_DIR@ )
 set ( pairs_BINARY_DIR @pairs_BINARY_DIR@ )
 
 set ( PAIRS_LINK_LIBRARIES @PAIRS_LINK_LIBRARIES@ )
-set ( PAIRS_LINK_DIRS @PAIRS_LINK_DIRS@ )
 set ( PAIRS_INCLUDE_DIRS @PAIRS_INCLUDE_DIRS@ )
diff --git a/src/pairs/code_gen/cgen.py b/src/pairs/code_gen/cgen.py
index 272e1a9..78c3958 100644
--- a/src/pairs/code_gen/cgen.py
+++ b/src/pairs/code_gen/cgen.py
@@ -147,6 +147,9 @@ class CGen:
             self.print("#define PAIRS_TARGET_OPENMP")
             self.print("#include <omp.h>")
 
+        if self.sim._use_walberla:
+            self.print("#define USE_WALBERLA")
+            
         self.print("#include <limits.h>")
         self.print("#include <math.h>")
         self.print("#include <stdbool.h>")
diff --git a/src/pairs/sim/simulation.py b/src/pairs/sim/simulation.py
index 0e1164a..3550a7a 100644
--- a/src/pairs/sim/simulation.py
+++ b/src/pairs/sim/simulation.py
@@ -93,6 +93,7 @@ class Simulation:
         # Domain partitioning
         self._dom_part = None
         self._partitioner = None
+        self._use_walberla = False
         self._comm = None
 
         # Contact history
@@ -122,6 +123,7 @@ class Simulation:
 
         elif partitioner == DomainPartitioners.BlockForest:
             self._dom_part = BlockForest(self)
+            self._use_walberla = True
 
         else:
             raise Exception("Invalid domain partitioner.")
-- 
GitLab