Skip to content

Commit

Permalink
ENH: Support "compiling" source file exclusively to .pyc
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
MujassimJamal and jcfr committed Mar 20, 2024
1 parent fb72e1a commit cbf0111
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
1 change: 1 addition & 0 deletions CMake/CTKConfig.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
22 changes: 20 additions & 2 deletions CMake/ctkMacroCompilePythonScript.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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()
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit cbf0111

Please sign in to comment.