Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Support "compiling" source files exclusively to .pyc #1192

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
37 changes: 31 additions & 6 deletions CMake/ctkMacroCompilePythonScript.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,18 @@
#

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 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
macro(ctkMacroCompilePythonScript)
ctkMacroParseArguments(MY
Expand Down Expand Up @@ -98,7 +107,11 @@ macro(ctkMacroCompilePythonScript)
USE_SOURCE_PERMISSIONS)

if(NOT MY_GLOBAL_TARGET)
ctkFunctionAddCompilePythonScriptTargets(${target})
set(_keep_only_pyc_option)
if(CTK_COMPILE_PYTHON_SCRIPT_KEEP_ONLY_PYC)
set(_keep_only_pyc_option KEEP_ONLY_PYC)
endif()
ctkFunctionAddCompilePythonScriptTargets(${target} ${_keep_only_pyc_option})
endif()
endmacro()

Expand Down Expand Up @@ -131,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.
Expand All @@ -145,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)
Expand Down Expand Up @@ -183,7 +199,16 @@ function(_ctk_add_compile_python_directories_target target)
endfunction()

function(ctkFunctionAddCompilePythonScriptTargets target)
_ctk_add_copy_python_files_target(${target} Script ${ARGN})
_ctk_add_copy_python_files_target(${target} Resource ${ARGN})
_ctk_add_compile_python_directories_target(${target})
set(options
KEEP_ONLY_PYC
)
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()
2 changes: 2 additions & 0 deletions CMake/ctk_compile_python_scripts.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
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_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_KEEP_ONLY_PYC)

# Build examples
# Create the logical expression containing the minimum set of required options
# for the CTK_BUILD_EXAMPLES option to be ON
Expand Down
Loading