Commit 1715bab3 authored by jcfr's avatar jcfr
Browse files

COMP: Added macro Slicer3_compile_python_scripts

Factored out CMake code from Base/Python/CMakeLists.txt for future re-use

git-svn-id: http://svn.slicer.org/Slicer4/trunk@14580 3bd1e089-480b-0410-8dfb-8563597acbee
parent 8aeea2a3
#
# Based on ParaView/VTK/Utilities/vtkTclTest2Py/CMakeLists.txt and
# ParaView/VTK/Wrapping/Python/CMakeLists.txt
#
FIND_PACKAGE(PythonInterp REQUIRED)
SET(Slicer_PYTHON_FILES
SET(Slicer_PYTHON_SCRIPTS
slicer/__init__
slicer/logic
slicer/slicerqt
......@@ -21,7 +16,7 @@ SET(Slicer_PYTHON_RESOURCES
)
IF(Slicer3_USE_PYTHONQT_WITH_TCL)
LIST(APPEND Slicer_PYTHON_FILES
LIST(APPEND Slicer_PYTHON_SCRIPTS
tpycl/__init__
tpycl/tpycl
)
......@@ -30,57 +25,14 @@ IF(Slicer3_USE_PYTHONQT_WITH_TCL)
)
ENDIF()
SET(copied_python_files)
SET(Slicer_PYTHON_MODULE_BINARY_DIR "${Slicer3_BINARY_DIR}/bin/Python")
FOREACH(file ${Slicer_PYTHON_FILES})
SET(src "${CMAKE_CURRENT_SOURCE_DIR}/${file}.py")
SET(tgt "${Slicer_PYTHON_MODULE_BINARY_DIR}/${file}.py")
SET(copied_python_files ${copied_python_files} ${tgt})
ADD_CUSTOM_COMMAND(DEPENDS ${src}
COMMAND ${CMAKE_COMMAND} -E copy ${src} ${tgt}
OUTPUT ${tgt}
COMMENT "source copy")
ENDFOREACH(file)
FOREACH(file ${Slicer_PYTHON_RESOURCES})
SET(src "${CMAKE_CURRENT_SOURCE_DIR}/${file}")
SET(tgt "${Slicer_PYTHON_MODULE_BINARY_DIR}/${file}")
SET(copied_python_files ${copied_python_files} ${tgt})
ADD_CUSTOM_COMMAND(DEPENDS ${src}
COMMAND ${CMAKE_COMMAND} -E copy ${src} ${tgt}
OUTPUT ${tgt}
COMMENT "resource copy")
ENDFOREACH(file)
ADD_CUSTOM_TARGET(CopyPythonFiles
ALL
DEPENDS ${copied_python_files})
# Byte compile the Python files.
SET(compile_all_script "${CMAKE_CURRENT_BINARY_DIR}/compile_all_slicer.py")
CONFIGURE_FILE("${CMAKE_CURRENT_SOURCE_DIR}/compile_all_slicer.py.in"
${compile_all_script}
@ONLY IMMEDIATE)
ADD_CUSTOM_COMMAND(
COMMAND ${PYTHON_EXECUTABLE}
ARGS "${compile_all_script}"
DEPENDS ${copied_python_files} ${compile_all_script}
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/slicer_python_compile_complete"
Slicer3_compile_python_scripts(
TARGET_NAME slicer
SCRIPTS "${Slicer_PYTHON_SCRIPTS}"
RESOURCES "${Slicer_PYTHON_RESOURCES}"
DESTINATION_DIR ${Slicer3_BINARY_DIR}/bin/Python
INSTALL_DIR ${Slicer3_INSTALL_BIN_DIR}
)
ADD_CUSTOM_TARGET(slicer_pyc ALL
DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/slicer_python_compile_complete")
# Install the slicer python module directory
INSTALL(DIRECTORY "${Slicer3_BINARY_DIR}/bin/Python"
DESTINATION "${Slicer3_INSTALL_BIN_DIR}" COMPONENT Runtime
USE_SOURCE_PERMISSIONS)
#-----------------------------------------------------------------------------
# Testing
#
......
# Based on paraview/VTK/Wrapping/Python/compile_all_vtk.py.in
import compileall
compileall.compile_dir('@Slicer_PYTHON_MODULE_BINARY_DIR@')
file = open('@CMAKE_CURRENT_BINARY_DIR@/slicer_python_compile_complete', 'w')
file.write('Done')
\ No newline at end of file
#
# Based on ParaView/VTK/Utilities/vtkTclTest2Py/CMakeLists.txt and
# ParaView/VTK/Wrapping/Python/CMakeLists.txt
#
MACRO(Slicer3_compile_python_scripts)
SLICER3_PARSE_ARGUMENTS(MY
"TARGET_NAME;SCRIPTS;RESOURCES;DESTINATION_DIR;INSTALL_DIR"
""
${ARGN}
)
FIND_PACKAGE(PythonInterp REQUIRED)
# Sanity checks
FOREACH(varname TARGET_NAME SCRIPTS DESTINATION_DIR;INSTALL_DIR)
IF(NOT DEFINED MY_${varname})
MESSAGE(SEND_ERROR "${varname} is mandatory")
ENDIF()
ENDFOREACH()
set(copied_python_files)
FOREACH(file ${MY_SCRIPTS})
SET(src "${CMAKE_CURRENT_SOURCE_DIR}/${file}.py")
SET(tgt "${MY_DESTINATION_DIR}/${file}.py")
SET(copied_python_files ${copied_python_files} ${tgt})
ADD_CUSTOM_COMMAND(DEPENDS ${src}
COMMAND ${CMAKE_COMMAND} -E copy ${src} ${tgt}
OUTPUT ${tgt}
COMMENT "Copying python script: ${file}.py")
ENDFOREACH()
IF(DEFINED MY_RESOURCES)
FOREACH(file ${MY_RESOURCES})
SET(src "${CMAKE_CURRENT_SOURCE_DIR}/${file}")
SET(tgt "${MY_DESTINATION_DIR}/${file}")
#SET(copied_python_files ${copied_python_files} ${tgt})
ADD_CUSTOM_COMMAND(DEPENDS ${src}
COMMAND ${CMAKE_COMMAND} -E copy ${src} ${tgt}
OUTPUT ${tgt}
COMMENT "Copying python resource: ${file}")
ENDFOREACH()
ADD_CUSTOM_TARGET(Copy${MY_TARGET_NAME}PythonFiles
ALL
DEPENDS ${copied_python_files})
ENDIF()
# Byte compile the Python files.
SET(compile_all_script "${CMAKE_CURRENT_BINARY_DIR}/compile_${MY_TARGET_NAME}_python_scripts.py")
# Generate compile_${MY_TARGET_NAME}_python_scripts.py
FILE(WRITE ${compile_all_script} "
#
# Generated by Slicer3_compile_python_scripts CMAKE macro
#
# Based on paraview/VTK/Wrapping/Python/compile_all_vtk.py.in
import compileall
compileall.compile_dir('@MY_DESTINATION_DIR@')
file = open('@CMAKE_CURRENT_BINARY_DIR@/python_compile_@MY_TARGET_NAME@_complete', 'w')
file.write('Done')
")
# Byte compile the Python files.
#SET(compile_all_script "${CMAKE_CURRENT_BINARY_DIR}/compile_${MY_TARGET_NAME}_python_scripts.py")
#CONFIGURE_FILE("${CMAKE_CURRENT_LIST_DIR}/compile_python_scripts.py.in"
# ${compile_all_script}
# @ONLY IMMEDIATE)
ADD_CUSTOM_COMMAND(
COMMAND ${PYTHON_EXECUTABLE}
ARGS "${compile_all_script}"
DEPENDS ${copied_python_files} ${compile_all_script}
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/python_compile_${MY_TARGET_NAME}_complete"
COMMENT "Compiling python scripts: ${MY_TARGET_NAME}"
)
ADD_CUSTOM_TARGET(Compile${MY_TARGET_NAME}PythonFiles ALL
DEPENDS
${CMAKE_CURRENT_BINARY_DIR}/python_compile_${MY_TARGET_NAME}_complete
${compile_all_script}
)
# Install python module directory
INSTALL(DIRECTORY "${MY_DESTINATION_DIR}"
DESTINATION "${MY_INSTALL_DIR}" COMPONENT Runtime
USE_SOURCE_PERMISSIONS)
ENDMACRO()
......@@ -337,6 +337,8 @@ IF(Slicer3_USE_QT)
IF(NOT VTK_WRAP_PYTHON)
MESSAGE(FATAL_ERROR "Since both Slicer3_USE_QT and Slicer3_USE_PYTHONQT are ON, VTK_WRAP_PYTHON is expected to be ON. Disable Slicer3_USE_PYTHONQT or re-configure VTK with python wrapping.")
ENDIF(NOT VTK_WRAP_PYTHON)
# Include convenient script allowing to copy and compile python scripts
INCLUDE(${Slicer3_CMAKE_DIR}/Slicer3CompilePythonScripts.cmake)
# Enable qMRMLWidgets python wrapping
SET(qMRMLWidgets_WRAP_PYTHON ON)
ENDIF(Slicer3_USE_PYTHONQT)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment