From f89e71964ec15ea90d83684a9aab55dd2f902ebb Mon Sep 17 00:00:00 2001
From: "Joachim Wuttke (h)" <j.wuttke@fz-juelich.de>
Date: Mon, 5 Oct 2020 23:17:08 +0200
Subject: [PATCH] CMake: move code to SwigLib.cmake

---
 Core/CMakeLists.txt           | 43 +--------------------------------
 Fit/CMakeLists.txt            | 43 +--------------------------------
 cmake/BornAgain/SwigLib.cmake | 45 +++++++++++++++++++++++++++++++++--
 3 files changed, 45 insertions(+), 86 deletions(-)

diff --git a/Core/CMakeLists.txt b/Core/CMakeLists.txt
index 96fefc09ced..19262458906 100644
--- a/Core/CMakeLists.txt
+++ b/Core/CMakeLists.txt
@@ -28,48 +28,7 @@ set_target_properties(${lib} PROPERTIES PREFIX ${libprefix} SUFFIX ${libsuffix})
 set(${lib}_LIBRARY_TYPE SHARED)
 
 if(BORNAGAIN_PYTHON)
-
-    if(CONFIGURE_BINDINGS)
-
-        set(TMP_DIR ${CMAKE_CURRENT_BINARY_DIR}/Wrap)
-        file(MAKE_DIRECTORY ${TMP_DIR})
-
-        GeneratePythonDocs(${AUTO_DIR}/doxygen${name}.i ${WRAP_DIR}/swig)
-
-        set(swig_dependencies
-            ${WRAP_DIR}/swig/lib${lib}.i
-            ${WRAP_DIR}/swig/directors.i
-            ${WRAP_DIR}/swig/extend${name}.i
-            ${WRAP_DIR}/swig/ignores.i
-            ${WRAP_DIR}/swig/shared_pointers.i
-            ${WRAP_DIR}/swig/warnings.i
-            )
-        foreach(FNAM ${swig_dependencies})
-            if(NOT EXISTS ${FNAM})
-                message(FATAL_ERROR "Could NOT find SWIG input ${FNAM}")
-            endif()
-        endforeach()
-        list(APPEND swig_dependencies ${AUTO_DIR}/doxygen${name}.i)
-
-        set(SWIG_FLAGS "-c++;-python;-o;${AUTO_DIR}/lib${lib}_wrap.cpp;-outdir;${TMP_DIR}"
-                               ";-I${CMAKE_SOURCE_DIR};-I${CMAKE_BINARY_DIR}/inc")
-
-        add_custom_command(
-            OUTPUT ${AUTO_DIR}/lib${lib}.py
-            COMMAND ${Python3_EXECUTABLE} ${WRAP_DIR}/swig/tweaks.py
-                   ${TMP_DIR}/lib${lib}.py
-                   ${AUTO_DIR}/lib${lib}.py
-            DEPENDS ${TMP_DIR}/lib${lib}.py
-            )
-        add_custom_command(
-            OUTPUT ${TMP_DIR}/lib${lib}.py
-                   ${AUTO_DIR}/lib${lib}_wrap.h
-                   ${AUTO_DIR}/lib${lib}_wrap.cpp
-            COMMAND ${SWIG_EXECUTABLE} ${SWIG_FLAGS} ${WRAP_DIR}/swig/lib${lib}.i
-            DEPENDS ${swig_dependencies} ${include_files}
-            )
-
-    endif(CONFIGURE_BINDINGS)
+    SwigLib(${name} ${lib} ${CMAKE_CURRENT_BINARY_DIR}/Wrap)
 
     add_custom_target(
         ${lib}_python
diff --git a/Fit/CMakeLists.txt b/Fit/CMakeLists.txt
index d2b53a8e9bc..1e53df4f271 100644
--- a/Fit/CMakeLists.txt
+++ b/Fit/CMakeLists.txt
@@ -21,48 +21,7 @@ set_target_properties(${lib} PROPERTIES PREFIX ${libprefix} SUFFIX ${libsuffix})
 set(${lib}_LIBRARY_TYPE SHARED)
 
 if(BORNAGAIN_PYTHON)
-
-    if(CONFIGURE_BINDINGS)
-
-        set(TMP_DIR ${CMAKE_CURRENT_BINARY_DIR}/Wrap)
-        file(MAKE_DIRECTORY ${TMP_DIR})
-
-        GeneratePythonDocs(${AUTO_DIR}/doxygen${name}.i ${WRAP_DIR}/swig)
-
-        set(swig_dependencies
-            ${WRAP_DIR}/swig/lib${lib}.i
-            ${WRAP_DIR}/swig/directors.i
-            ${WRAP_DIR}/swig/extend${name}.i
-            ${WRAP_DIR}/swig/ignores.i
-            ${WRAP_DIR}/swig/shared_pointers.i
-            ${WRAP_DIR}/swig/warnings.i
-            )
-        foreach(FNAM ${swig_dependencies})
-            if(NOT EXISTS ${FNAM})
-                message(FATAL_ERROR "Could NOT find SWIG input ${FNAM}")
-            endif()
-        endforeach()
-        list(APPEND swig_dependencies ${AUTO_DIR}/doxygen${name}.i)
-
-        set(SWIG_FLAGS "-c++;-python;-o;${AUTO_DIR}/lib${lib}_wrap.cpp;-outdir;${TMP_DIR}"
-                               ";-I${CMAKE_SOURCE_DIR};-I${CMAKE_BINARY_DIR}/inc")
-
-        add_custom_command(
-            OUTPUT ${AUTO_DIR}/lib${lib}.py
-            COMMAND ${Python3_EXECUTABLE} ${WRAP_DIR}/swig/tweaks.py
-                   ${TMP_DIR}/lib${lib}.py
-                   ${AUTO_DIR}/lib${lib}.py
-            DEPENDS ${TMP_DIR}/lib${lib}.py
-            )
-        add_custom_command(
-            OUTPUT ${TMP_DIR}/lib${lib}.py
-                   ${AUTO_DIR}/lib${lib}_wrap.h
-                   ${AUTO_DIR}/lib${lib}_wrap.cpp
-            COMMAND ${SWIG_EXECUTABLE} ${SWIG_FLAGS} ${WRAP_DIR}/swig/lib${lib}.i
-            DEPENDS ${swig_dependencies} ${include_files}
-            )
-
-    endif(CONFIGURE_BINDINGS)
+    SwigLib(${name} ${lib} ${CMAKE_CURRENT_BINARY_DIR}/Wrap)
 
     add_custom_target(
         ${lib}_python
diff --git a/cmake/BornAgain/SwigLib.cmake b/cmake/BornAgain/SwigLib.cmake
index c5e41f8997a..dce997bc0f1 100644
--- a/cmake/BornAgain/SwigLib.cmake
+++ b/cmake/BornAgain/SwigLib.cmake
@@ -1,8 +1,49 @@
-function(SwigLib name lib)
+function(SwigLib name lib TMP_DIR)
 
     if(NOT BORNAGAIN_PYTHON)
-        return()
+        message(FATAL_ERROR "Function SwigLib called though BORNAGAIN_PYTHON=false")
     endif()
 
+    if(CONFIGURE_BINDINGS)
+
+        file(MAKE_DIRECTORY ${TMP_DIR})
+
+        GeneratePythonDocs(${AUTO_DIR}/doxygen${name}.i ${WRAP_DIR}/swig)
+
+        set(swig_dependencies
+            ${WRAP_DIR}/swig/lib${lib}.i
+            ${WRAP_DIR}/swig/directors.i
+            ${WRAP_DIR}/swig/extend${name}.i
+            ${WRAP_DIR}/swig/ignores.i
+            ${WRAP_DIR}/swig/shared_pointers.i
+            ${WRAP_DIR}/swig/warnings.i
+            )
+        foreach(FNAM ${swig_dependencies})
+            if(NOT EXISTS ${FNAM})
+                message(FATAL_ERROR "Could NOT find SWIG input ${FNAM}")
+            endif()
+        endforeach()
+        list(APPEND swig_dependencies ${AUTO_DIR}/doxygen${name}.i)
+
+        set(SWIG_FLAGS "-c++;-python;-o;${AUTO_DIR}/lib${lib}_wrap.cpp;-outdir;${TMP_DIR}"
+                               ";-I${CMAKE_SOURCE_DIR};-I${CMAKE_BINARY_DIR}/inc")
+
+        add_custom_command(
+            OUTPUT ${AUTO_DIR}/lib${lib}.py
+            COMMAND ${Python3_EXECUTABLE} ${WRAP_DIR}/swig/tweaks.py
+                   ${TMP_DIR}/lib${lib}.py
+                   ${AUTO_DIR}/lib${lib}.py
+            DEPENDS ${TMP_DIR}/lib${lib}.py
+            )
+        add_custom_command(
+            OUTPUT ${TMP_DIR}/lib${lib}.py
+                   ${AUTO_DIR}/lib${lib}_wrap.h
+                   ${AUTO_DIR}/lib${lib}_wrap.cpp
+            COMMAND ${SWIG_EXECUTABLE} ${SWIG_FLAGS} ${WRAP_DIR}/swig/lib${lib}.i
+            DEPENDS ${swig_dependencies} ${include_files}
+            )
+
+    endif(CONFIGURE_BINDINGS)
+
 
 endfunction()
-- 
GitLab