From c9f8e8a098acaa172d6e54aacde9a66dbade02e9 Mon Sep 17 00:00:00 2001
From: Christoph Alt <christoph.alt@fau.de>
Date: Fri, 21 Feb 2025 12:16:55 +0100
Subject: [PATCH] Adding option to automatically installation

---
 .gitlab-ci.yml                                |  9 +++
 .../cmake/FindPystencilsSfg.cmake             |  2 +-
 standalone/CMakeLists.txt                     | 73 +++++++++++++++++++
 standalone/requirements.txt                   |  5 ++
 4 files changed, 88 insertions(+), 1 deletion(-)
 create mode 100644 standalone/CMakeLists.txt
 create mode 100644 standalone/requirements.txt

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 41e3a70..c430eab 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -71,3 +71,12 @@ pages:
     - docker
   only:
     - master@pycodegen/pystencils-sfg
+
+cmake-standalone:
+  stage: deploy
+  needs: []
+  script:
+    - echo "Publishing Cmake standalone"
+  artifacts:
+    paths:
+      - standalone
diff --git a/src/pystencilssfg/cmake/FindPystencilsSfg.cmake b/src/pystencilssfg/cmake/FindPystencilsSfg.cmake
index 20a3fd5..5efae7a 100644
--- a/src/pystencilssfg/cmake/FindPystencilsSfg.cmake
+++ b/src/pystencilssfg/cmake/FindPystencilsSfg.cmake
@@ -48,7 +48,7 @@ endif()
 if(${PystencilsSfg_FOUND})
     message( STATUS "Found pystencils Source File Generator (Version ${PystencilsSfg_VERSION})")
     message( STATUS "Using Python interpreter ${PystencilsSfg_PYTHON_INTERPRETER} for SFG generator scripts.")
-    
+
     execute_process(COMMAND ${PystencilsSfg_PYTHON_INTERPRETER} -m pystencilssfg cmake modulepath --no-newline
                     OUTPUT_VARIABLE _PystencilsSfg_CMAKE_MODULE_PATH)
 
diff --git a/standalone/CMakeLists.txt b/standalone/CMakeLists.txt
new file mode 100644
index 0000000..8963be3
--- /dev/null
+++ b/standalone/CMakeLists.txt
@@ -0,0 +1,73 @@
+cmake_minimum_required( VERSION 3.24 )
+project(PystencilsSfg_Standalone)
+
+if (DEFINED CACHE{PystencilsSfg_PYTHON_INTERPRETER})
+    set( _use_venv_init OFF)
+elseif(DEFINED PystencilsSfg_PYTHON_PATH)
+    set( _use_venv_init OFF)
+else()
+    set( _use_venv_init ON )
+endif()
+
+set(CODEGEN_PRIVATE_VENV ${_use_venv_init}
+    CACHE BOOL
+    "Create a private virtual Python environment inside the build tree for code generation"
+)
+
+function(codegen_venv_install)
+    if(NOT CODEGEN_PRIVATE_VENV)
+        return()
+    endif()
+
+    if(NOT _sfg_private_venv_done)
+        message( FATAL_ERROR "The private virtual environment for code generation was not initialized yet" )
+    endif()
+
+    execute_process(
+	COMMAND ${PystencilsSfg_PYTHON_INTERPRETER} -m pip install ${ARGV}
+    )
+endfunction()
+
+
+if (CODEGEN_PRIVATE_VENV)
+
+	set(CODEGEN_VENV_PATH ${CMAKE_CURRENT_BINARY_DIR}/codegen-venv CACHE PATH "Location of the virtual environment used for code generation")
+	set(_venv_python_exe ${CODEGEN_VENV_PATH}/bin/python)
+	set(CODEGEN_VENV_PYTHON ${_venv_python_exe})
+
+	find_package( Python COMPONENTS Interpreter REQUIRED )
+
+	if(NOT _sfg_private_venv_done)
+		message( STATUS "Setting up Python virtual environment at ${CODEGEN_VENV_PATH}" )
+
+		#   Create the venv and register its interpreter with pystencils-sfg
+		if(NOT EXISTS ${CODEGEN_VENV_PATH})
+			execute_process( COMMAND ${Python_EXECUTABLE} -m venv ${CODEGEN_VENV_PATH})
+		endif()
+
+
+		set(CODEGEN_VENV_REQUIREMENTS ${PROJECT_SOURCE_DIR}/requirements.txt CACHE FILEPATH "Location of the requirements installed in the virtual environment used for code generation")
+		if (EXISTS ${CODEGEN_VENV_REQUIREMENTS})
+			message( STATUS "Installing required Python packages from ${CODEGEN_VENV_REQUIREMENTS}" )
+			execute_process( COMMAND ${_venv_python_exe} -m pip install -r ${CODEGEN_VENV_REQUIREMENTS} OUTPUT_QUIET)
+		else()
+			message( WARNING "Could not find ${CODEGEN_VENV_REQUIREMENTS}" )
+		endif()
+
+		set( _sfg_private_venv_done TRUE CACHE BOOL "" )
+		mark_as_advanced(_sfg_private_venv_done)
+	endif()
+	set(_sfg_cache_python_init ${_venv_python_exe})
+	set(PystencilsSfg_PYTHON_INTERPRETER ${_sfg_cache_python_init} CACHE PATH "Path to the Python executable used to run pystencils-sfg")
+endif()
+
+# get the find pystencils-sfg file
+execute_process( 
+	COMMAND ${PystencilsSfg_PYTHON_INTERPRETER} -m pystencilssfg cmake make-find-module
+	WORKING_DIRECTORY ${CMAKE_FIND_PACKAGE_REDIRECTS_DIR}
+)
+# renaming it
+file(RENAME ${CMAKE_FIND_PACKAGE_REDIRECTS_DIR}/FindPystencilsSfg.cmake ${CMAKE_FIND_PACKAGE_REDIRECTS_DIR}/pystencilssfg-config.cmake)
+
+# Find it
+find_package( PystencilsSfg REQUIRED )
diff --git a/standalone/requirements.txt b/standalone/requirements.txt
new file mode 100644
index 0000000..82b06ee
--- /dev/null
+++ b/standalone/requirements.txt
@@ -0,0 +1,5 @@
+py-cpuinfo
+# pystencils 2.0 Development Branch
+git+https://i10git.cs.fau.de/pycodegen/pystencils.git@4876f0b774d38fe7644f2b64f80f1291ffa0fa08
+# pystencils-sfg
+git+https://i10git.cs.fau.de/pycodegen/pystencils-sfg.git@dcb77258670abe5bc87f2bd80761594eb304cb90
-- 
GitLab