diff --git a/cmake/PrepareSFG.cmake b/cmake/PrepareSFG.cmake index 9c042b24d82e0c8291c0a6745588e38dba862f34..4a5431b4a30597f145128a302d1cd028d10ae33f 100644 --- a/cmake/PrepareSFG.cmake +++ b/cmake/PrepareSFG.cmake @@ -32,15 +32,40 @@ if( WALBERLA_CODEGEN_USE_PRIVATE_VENV ) OUTPUT_QUIET ) - set( - _sfg_private_venv_done TRUE CACHE BOOL "" - ) + set( _sfg_private_venv_done TRUE CACHE BOOL "" ) + set( _wlb_codegen_python_init ${_venv_python_exe} ) mark_as_advanced(_sfg_private_venv_done) endif() set(PystencilsSfg_PYTHON_PATH ${_venv_python_exe}) +else() + # Use the external Python environment, but check if all packages are installed + find_package( Python COMPONENTS Interpreter REQUIRED ) + + execute_process( + COMMAND ${Python_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/cmake/check_python_env.py + RESULT_VARIABLE _package_check_status + OUTPUT_VARIABLE _package_check_output + ERROR_VARIABLE _package_check_error + ) + + message( STATUS ${_package_check_output} ) + + if(NOT (${_package_check_status} EQUAL 0) ) + message( FATAL_ERROR ${_package_check_error} ) + endif() + + set( _wlb_codegen_python_init ${Python_EXECUTABLE} ) endif() +set( + WALBERLA_CODEGEN_PYTHON + ${_wlb_codegen_python_init} + CACHE PATH + "Path to the Python interpreter used for code generation" +) +mark_as_advanced(WALBERLA_CODEGEN_PYTHON) + # Find pystencils-sfg find_package( PystencilsSfg REQUIRED ) @@ -63,6 +88,23 @@ configure_file( message( STATUS "Wrote project-wide code generator configuration to ${WALBERLA_CODEGEN_CONFIG_MODULE}" ) +#[[ +Run `pip install` in the code generation environment with the given arguments. +#]] +function(walberla_codegen_venv_install) + if(NOT WALBERLA_CODEGEN_USE_PRIVATE_VENV) + message( FATAL_ERROR "The private virtual environment for code generation is disabled" ) + 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 ${WALBERLA_CODEGEN_PYTHON} -m pip install ${ARGV} + ) +endfunction() + # Code Generation Functions #[[ diff --git a/cmake/check_python_env.py b/cmake/check_python_env.py new file mode 100644 index 0000000000000000000000000000000000000000..7d00009b56992b3b16fb1dc07f2854e30e9d49b3 --- /dev/null +++ b/cmake/check_python_env.py @@ -0,0 +1,29 @@ +import sys + + +def error(msg: str): + msg = "Required Python packages could not be found:\n" + msg + print(msg, file=sys.stderr) + sys.exit(1) + + +print("Checking Python environment - ", end="") + +try: + import pystencils +except ImportError: + error("pystencils is not installed in the current Python environment.") + +try: + import pystencilssfg +except ImportError: + error("pystencils-sfg is not installed in the current Python environment.") + +try: + import sfg_walberla +except ImportError: + error("sfg_walberla is not installed in the current Python environment.") + +print("found required packages pystencils, pystencils-sfg, sfg-walberla", end="") + +sys.exit(0)