From cbf01119f3827954386441f39905452154c1e902 Mon Sep 17 00:00:00 2001 From: Mujassim Bhaijamal <67727415+MujassimJamal@users.noreply.github.com> Date: Sat, 17 Feb 2024 17:23:47 +0530 Subject: [PATCH 1/2] ENH: Support "compiling" source file exclusively to .pyc Introduce the option CTK_COMPILE_PYTHON_SCRIPT_SKIP_SCRIPT_COPY to support disabling the copy of `*.py` script into the build directory. Update CMake function `ctkFunctionAddCompilePythonScriptTargets` to accept the `SKIP_SCRIPT_COPY` option. Update CMake macro `ctkMacroCompilePythonScript` to call `ctkFunctionAddCompilePythonScriptTargets` passing SKIP_SCRIPT_COPY based on the value of CTK_COMPILE_PYTHON_SCRIPT_SKIP_SCRIPT_COPY Address warning when using CMake >= 3.13, setting CMP0077 to NEW. See https://cmake.org/cmake/help/latest/policy/CMP0077.html Co-authored-by: Jean-Christophe Fillion-Robin --- CMake/CTKConfig.cmake.in | 1 + CMake/ctkMacroCompilePythonScript.cmake | 22 ++++++++++++++++++++-- CMakeLists.txt | 7 +++++++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/CMake/CTKConfig.cmake.in b/CMake/CTKConfig.cmake.in index dc3e5f8cd8..a11737032e 100644 --- a/CMake/CTKConfig.cmake.in +++ b/CMake/CTKConfig.cmake.in @@ -40,6 +40,7 @@ set(CTK_LIBRARY_DIRS ${CTK_LIBRARY_DIR}) # CTK specific variables set(CTK_CMAKE_DEBUG_POSTFIX "@CMAKE_DEBUG_POSTFIX@") +set(CTK_COMPILE_PYTHON_SCRIPT_SKIP_SCRIPT_COPY "@CTK_COMPILE_PYTHON_SCRIPT_SKIP_SCRIPT_COPY@") # Import CTK targets if(NOT TARGET CTKCore) diff --git a/CMake/ctkMacroCompilePythonScript.cmake b/CMake/ctkMacroCompilePythonScript.cmake index 27992fd42f..c4f411cb48 100644 --- a/CMake/ctkMacroCompilePythonScript.cmake +++ b/CMake/ctkMacroCompilePythonScript.cmake @@ -21,6 +21,12 @@ include(${CTK_CMAKE_DIR}/ctkMacroParseArguments.cmake) set(CTK_PYTHON_COMPILE_FILE_SCRIPT_DIR "${CMAKE_BINARY_DIR}/CMakeFiles") +# Setting this option to TRUE disable the copy of ".py" files into the +# destination directory associated with ctkMacroCompilePythonScript. +if(NOT DEFINED CTK_COMPILE_PYTHON_SCRIPT_SKIP_SCRIPT_COPY) + set(CTK_COMPILE_PYTHON_SCRIPT_SKIP_SCRIPT_COPY FALSE) +endif() + #! \ingroup CMakeAPI macro(ctkMacroCompilePythonScript) ctkMacroParseArguments(MY @@ -98,7 +104,11 @@ macro(ctkMacroCompilePythonScript) USE_SOURCE_PERMISSIONS) if(NOT MY_GLOBAL_TARGET) - ctkFunctionAddCompilePythonScriptTargets(${target}) + set(_skip_script_copy_option) + if(CTK_COMPILE_PYTHON_SCRIPT_SKIP_SCRIPT_COPY) + set(_skip_script_copy_option SKIP_SCRIPT_COPY) + endif() + ctkFunctionAddCompilePythonScriptTargets(${target} ${_skip_script_copy_option}) endif() endmacro() @@ -183,7 +193,15 @@ function(_ctk_add_compile_python_directories_target target) endfunction() function(ctkFunctionAddCompilePythonScriptTargets target) - _ctk_add_copy_python_files_target(${target} Script ${ARGN}) + ctkMacroParseArguments(MY + "" + "SKIP_SCRIPT_COPY" + ${ARGN} + ) + # Skip defining the target CopySlicerPythonScriptFiles when the argument skip_script_copy is set to True + if(NOT MY_SKIP_SCRIPT_COPY) + _ctk_add_copy_python_files_target(${target} Script ${ARGN}) + endif() _ctk_add_copy_python_files_target(${target} Resource ${ARGN}) _ctk_add_compile_python_directories_target(${target}) endfunction() diff --git a/CMakeLists.txt b/CMakeLists.txt index 48c4ef83b9..99dd8b1b72 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,6 +22,7 @@ cmake_minimum_required(VERSION 3.0) foreach(p CMP0054 # CMake 3.1 + CMP0077 # CMake 3.13 ) if(POLICY ${p}) cmake_policy(SET ${p} NEW) @@ -744,6 +745,12 @@ ctk_enable_option(Python_Wrapping "Wrap CTK classes using Qt meta-object system CTK_LIB_Scripting/Python/Core) mark_as_superbuild(CTK_ENABLE_Python_Wrapping) +include(CMakeDependentOption) +cmake_dependent_option( + CTK_COMPILE_PYTHON_SCRIPT_SKIP_SCRIPT_COPY "Disable copy of .py files when using ctkMacroCompilePythonScript()" OFF + "CTK_ENABLE_Python_Wrapping" OFF) +mark_as_superbuild(CTK_COMPILE_PYTHON_SCRIPT_SKIP_SCRIPT_COPY) + # Build examples # Create the logical expression containing the minimum set of required options # for the CTK_BUILD_EXAMPLES option to be ON From 103fc972002a46634d1744ea7dd69be576d0b04d Mon Sep 17 00:00:00 2001 From: Jean-Christophe Fillion-Robin Date: Wed, 20 Mar 2024 14:38:45 -0400 Subject: [PATCH 2/2] ENH: Support "compiling" source file exclusively to .pyc Introduce the option CTK_COMPILE_PYTHON_SCRIPT_KEEP_ONLY_PYC to support remove `*.py` scripts once the corresponding `.pyc` file has been generated in the destination directory. Update `_ctk_add_compile_python_directories_target` to accept `keep_only_pyc` argument. Update CMake function `ctkFunctionAddCompilePythonScriptTargets` to accept the `KEEP_ONLY_PYC` option and call `_ctk_add_compile_python_directories_target` accordingly. Update CMake macro `ctkMacroCompilePythonScript` to call `ctkFunctionAddCompilePythonScriptTargets` passing KEEP_ONLY_PYC based on the value of `CTK_COMPILE_PYTHON_SCRIPT_KEEP_ONLY_PYC`. Address warning when using CMake >= 3.13, setting CMP0077 to NEW. See https://cmake.org/cmake/help/latest/policy/CMP0077.html Co-authored-by: Jean-Christophe Fillion-Robin --- CMake/ctkMacroCompilePythonScript.cmake | 47 +++++++++++++---------- CMake/ctk_compile_python_scripts.cmake.in | 2 + CMakeLists.txt | 4 +- 3 files changed, 31 insertions(+), 22 deletions(-) diff --git a/CMake/ctkMacroCompilePythonScript.cmake b/CMake/ctkMacroCompilePythonScript.cmake index c4f411cb48..b734a3ea4a 100644 --- a/CMake/ctkMacroCompilePythonScript.cmake +++ b/CMake/ctkMacroCompilePythonScript.cmake @@ -18,13 +18,16 @@ # include(${CTK_CMAKE_DIR}/ctkMacroParseArguments.cmake) +if(${CMAKE_VERSION} VERSION_LESS "3.5") + include(CMakeParseArguments) +endif() set(CTK_PYTHON_COMPILE_FILE_SCRIPT_DIR "${CMAKE_BINARY_DIR}/CMakeFiles") -# Setting this option to TRUE disable the copy of ".py" files into the -# destination directory associated with ctkMacroCompilePythonScript. -if(NOT DEFINED CTK_COMPILE_PYTHON_SCRIPT_SKIP_SCRIPT_COPY) - set(CTK_COMPILE_PYTHON_SCRIPT_SKIP_SCRIPT_COPY FALSE) +# Setting this option to TRUE remove the relevant ".py" files from the +# destination directory after they have been byte-compiled in ctkMacroCompilePythonScript. +if(NOT DEFINED CTK_COMPILE_PYTHON_SCRIPT_KEEP_ONLY_PYC) + set(CTK_COMPILE_PYTHON_SCRIPT_KEEP_ONLY_PYC FALSE) endif() #! \ingroup CMakeAPI @@ -104,11 +107,11 @@ macro(ctkMacroCompilePythonScript) USE_SOURCE_PERMISSIONS) if(NOT MY_GLOBAL_TARGET) - set(_skip_script_copy_option) - if(CTK_COMPILE_PYTHON_SCRIPT_SKIP_SCRIPT_COPY) - set(_skip_script_copy_option SKIP_SCRIPT_COPY) + set(_keep_only_pyc_option) + if(CTK_COMPILE_PYTHON_SCRIPT_KEEP_ONLY_PYC) + set(_keep_only_pyc_option KEEP_ONLY_PYC) endif() - ctkFunctionAddCompilePythonScriptTargets(${target} ${_skip_script_copy_option}) + ctkFunctionAddCompilePythonScriptTargets(${target} ${_keep_only_pyc_option}) endif() endmacro() @@ -141,7 +144,7 @@ function(_ctk_add_copy_python_files_target target type) endfunction() -function(_ctk_add_compile_python_directories_target target) +function(_ctk_add_compile_python_directories_target target keep_only_pyc) set(target_name Compile${target}PythonFiles) if(NOT TARGET ${target_name}) # Byte compile the Python files. @@ -155,7 +158,10 @@ function(_ctk_add_compile_python_directories_target target) list(GET tuple 1 tgt_file) list(GET tuple 2 dest_dir) set(tgt ${dest_dir}/${tgt_file}) - set(_compileall_code "${_compileall_code}\nctk_compile_file('${tgt}', force=1)") + set(_compileall_code "${_compileall_code}\nsuccess = ctk_compile_file('${tgt}', force=1)") + if(keep_only_pyc) + set(_compileall_code "${_compileall_code}\nif success: Path('${tgt}').unlink()") + endif() endforeach() if(NOT PYTHONINTERP_FOUND) @@ -193,15 +199,16 @@ function(_ctk_add_compile_python_directories_target target) endfunction() function(ctkFunctionAddCompilePythonScriptTargets target) - ctkMacroParseArguments(MY - "" - "SKIP_SCRIPT_COPY" - ${ARGN} + set(options + KEEP_ONLY_PYC ) - # Skip defining the target CopySlicerPythonScriptFiles when the argument skip_script_copy is set to True - if(NOT MY_SKIP_SCRIPT_COPY) - _ctk_add_copy_python_files_target(${target} Script ${ARGN}) - endif() - _ctk_add_copy_python_files_target(${target} Resource ${ARGN}) - _ctk_add_compile_python_directories_target(${target}) + set(oneValueArgs + ) + set(multiValueArgs + ) + cmake_parse_arguments(MY "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) + + _ctk_add_copy_python_files_target(${target} Script ${MY_UNPARSED_ARGUMENTS}) + _ctk_add_copy_python_files_target(${target} Resource ${MY_UNPARSED_ARGUMENTS}) + _ctk_add_compile_python_directories_target(${target} ${MY_KEEP_ONLY_PYC}) endfunction() diff --git a/CMake/ctk_compile_python_scripts.cmake.in b/CMake/ctk_compile_python_scripts.cmake.in index 211da0da1c..470b0bad36 100644 --- a/CMake/ctk_compile_python_scripts.cmake.in +++ b/CMake/ctk_compile_python_scripts.cmake.in @@ -21,6 +21,8 @@ import sys import py_compile import struct +from pathlib import Path + def ctk_compile_file(fullname, ddir=None, force=0, rx=None, quiet=0): \"\"\"Byte-compile one file. diff --git a/CMakeLists.txt b/CMakeLists.txt index 99dd8b1b72..41946940a1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -747,9 +747,9 @@ mark_as_superbuild(CTK_ENABLE_Python_Wrapping) include(CMakeDependentOption) cmake_dependent_option( - CTK_COMPILE_PYTHON_SCRIPT_SKIP_SCRIPT_COPY "Disable copy of .py files when using ctkMacroCompilePythonScript()" OFF + CTK_COMPILE_PYTHON_SCRIPT_KEEP_ONLY_PYC "Remove .py scripts from destination directory after compiling to .pyc" OFF "CTK_ENABLE_Python_Wrapping" OFF) -mark_as_superbuild(CTK_COMPILE_PYTHON_SCRIPT_SKIP_SCRIPT_COPY) +mark_as_superbuild(CTK_COMPILE_PYTHON_SCRIPT_KEEP_ONLY_PYC) # Build examples # Create the logical expression containing the minimum set of required options