From 0bbe5b44dc2145d45c7a7545d8d6ddf6838b5875 Mon Sep 17 00:00:00 2001 From: Frederik Hennig <frederik.hennig@fau.de> Date: Fri, 20 Dec 2024 13:33:16 +0100 Subject: [PATCH] add a CONFIG_MODULE parameter to SFG cmake registration function --- docs/source/usage/project_integration.md | 15 +++++++-- .../cmake/modules/PystencilsSfg.cmake | 32 +++++++++++-------- 2 files changed, 31 insertions(+), 16 deletions(-) diff --git a/docs/source/usage/project_integration.md b/docs/source/usage/project_integration.md index 19809a6..de3aa0c 100644 --- a/docs/source/usage/project_integration.md +++ b/docs/source/usage/project_integration.md @@ -74,6 +74,7 @@ find_package( PystencilsSfg ) Make sure to set the `Python_ROOT_DIR` cache variable to point to the correct Python interpreter (i.e. the virtual environment you have installed *pystencils-sfg* into). +(cmake_add_generator_scripts)= ### Add generator scripts The primary interaction point in CMake is the function `pystencilssfg_generate_target_sources`, @@ -85,6 +86,7 @@ pystencilssfg_generate_target_sources( <target> [DEPENDS dependency1.py [dependency2.py...]] [FILE_EXTENSIONS <header-extension> <impl-extension>] [OUTPUT_MODE <standalone|inline|header-only>] + [CONFIG_MODULE <path-to-config-module.py>] ) ``` @@ -97,6 +99,9 @@ The function takes the following options: - `DEPENDS`: A list of dependencies for the generator scripts - `FILE_EXTENSION`: The desired extensions for the generated files - `OUTPUT_MODE`: Sets the output mode of the code generator; see {any}`SfgConfig.output_mode`. + - `CONFIG_MODULE`: Set the configuration module for all scripts registered with this call. + If set, this overrides the value of `PystencilsSfg_CONFIG_MODULE` + in the current scope (see [](#cmake_set_config_module)) ### Include generated files @@ -110,9 +115,13 @@ path, such that generated header files for a target `<target>` may be included v (cmake_set_config_module)= ### Set a Configuration Module -To specify a [configuration module](#config_module) for your project, -set the scoped variable `PystencilsSfg_CONFIG_MODULE` to point at the respective Python file. -The pystencils-sfg CMake system will then pass that module to each generator script invocation. +There are two ways of specifying a [configuration module](#config_module) for generator scripts +registered with CMake: +- To set a configuration module for scripts registered with a single call to `pystencilssfg_generate_target_sources`, + use the `CONFIG_MODULE` function parameter (see [](#cmake_add_generator_scripts)). +- To set a config module for all generator scripts within the current CMake directory and its subdirectories, + set the scoped variable `PystencilsSfg_CONFIG_MODULE` to point at the respective Python file, e.g. + `set( PystencilsSfg_CONFIG_MODULE ProjectConfig.py )`. You might want to populate your configuration module with information about the current build setup and environment. diff --git a/src/pystencilssfg/cmake/modules/PystencilsSfg.cmake b/src/pystencilssfg/cmake/modules/PystencilsSfg.cmake index c42a251..e58a853 100644 --- a/src/pystencilssfg/cmake/modules/PystencilsSfg.cmake +++ b/src/pystencilssfg/cmake/modules/PystencilsSfg.cmake @@ -43,7 +43,7 @@ endfunction() function(pystencilssfg_generate_target_sources TARGET) set(options) - set(oneValueArgs OUTPUT_MODE) + set(oneValueArgs OUTPUT_MODE CONFIG_MODULE) set(multiValueArgs SCRIPTS DEPENDS FILE_EXTENSIONS) cmake_parse_arguments(_pssfg "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) @@ -53,21 +53,27 @@ function(pystencilssfg_generate_target_sources TARGET) list(APPEND generatorArgs "--sfg-output-mode=${_pssfg_OUTPUT_MODE}") endif() - if(DEFINED PystencilsSfg_CONFIGURATOR_SCRIPT) - message(AUTHOR_WARNING "The variable PystencilsSfg_CONFIGURATOR_SCRIPT is deprecated. Set PystencilsSfg_CONFIG_MODULE instead.") - cmake_path(ABSOLUTE_PATH PystencilsSfg_CONFIGURATOR_SCRIPT OUTPUT_VARIABLE configscript) - list(APPEND generatorArgs "--sfg-config-module=${configscript}") - list(APPEND _pssfg_DEPENDS ${configscript}) - endif() - - if(DEFINED PystencilsSfg_CONFIG_MODULE) - if(DEFINED PystencilsSfg_CONFIGURATOR_SCRIPT) - message(FATAL_ERROR "At most one of PystencilsSfg_CONFIGURATOR_SCRIPT and PystencilsSfg_CONFIG_MODULE may be set.") - endif() - + if(DEFINED _pssfg_CONFIG_MODULE) cmake_path(ABSOLUTE_PATH PystencilsSfg_CONFIG_MODULE OUTPUT_VARIABLE config_module) list(APPEND generatorArgs "--sfg-config-module=${config_module}") list(APPEND _pssfg_DEPENDS ${config_module}) + else() + if(DEFINED PystencilsSfg_CONFIGURATOR_SCRIPT) + message(AUTHOR_WARNING "The variable PystencilsSfg_CONFIGURATOR_SCRIPT is deprecated. Set PystencilsSfg_CONFIG_MODULE instead.") + cmake_path(ABSOLUTE_PATH PystencilsSfg_CONFIGURATOR_SCRIPT OUTPUT_VARIABLE configscript) + list(APPEND generatorArgs "--sfg-config-module=${configscript}") + list(APPEND _pssfg_DEPENDS ${configscript}) + endif() + + if(DEFINED PystencilsSfg_CONFIG_MODULE) + if(DEFINED PystencilsSfg_CONFIGURATOR_SCRIPT) + message(FATAL_ERROR "At most one of PystencilsSfg_CONFIGURATOR_SCRIPT and PystencilsSfg_CONFIG_MODULE may be set.") + endif() + + cmake_path(ABSOLUTE_PATH PystencilsSfg_CONFIG_MODULE OUTPUT_VARIABLE config_module) + list(APPEND generatorArgs "--sfg-config-module=${config_module}") + list(APPEND _pssfg_DEPENDS ${config_module}) + endif() endif() if(DEFINED _pssfg_FILE_EXTENSIONS) -- GitLab