Skip to content

Commit

Permalink
Ship .pyc files along with all .py files. Refs #5192
Browse files Browse the repository at this point in the history
  • Loading branch information
martyngigg committed Aug 17, 2012
1 parent de5403c commit 306f3e9
Show file tree
Hide file tree
Showing 9 changed files with 161 additions and 104 deletions.
61 changes: 56 additions & 5 deletions Code/Mantid/Build/CMake/MantidUtils.cmake
@@ -1,10 +1,9 @@
#######################################################################
#
# Define some useful macros/functions
#
# Define some useful macros/functions that are use throughout
# the cmake scripts
#######################################################################

#######################################################################
# NAME: SET_TARGET_OUTPUT_DIRECTORY
# Change the output directory of a library target. Handles the incorrect
# behaviour of MSVC
# Parameters:
Expand All @@ -15,7 +14,7 @@
function( SET_TARGET_OUTPUT_DIRECTORY TARGET OUTPUT_DIR )
get_target_property(TARGET_NAME ${TARGET} OUTPUTNAME)
if ( MSVC )
# For some reason when the generator is MSVC 10 it ignores the LIBRARY_OUTPUT_DIRECTORY
# For some reason when the generator is MSVC 10 it ignores the LIBRARY_OUTPUT_DIRECTORY
# property so we have to do something slightly different
if ( ${ARGC} STREQUAL 3 )
set ( LIB_EXT ${ARGV2} )
Expand All @@ -39,3 +38,55 @@ function( SET_TARGET_OUTPUT_DIRECTORY TARGET OUTPUT_DIR )
set_target_properties ( ${TARGET} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${OUTPUT_DIR} )
endif ( MSVC )
endfunction( SET_TARGET_OUTPUT_DIRECTORY )

#######################################################################

#
# NAME: ADD_COMPILE_PY_TARGET
# Adds a target that will compile the .py files into .pyc
# bytecode in the given directory. This is done recursively and
# the pyc files appear in the same directory as the source file
# The target is NOT added to ALL.
# - TARGET_NAME :: The name of the target
# - SOURCE_DIR :: A directory containing python files
# - An optional regex to exclude certain files can be included as the final argument
function( ADD_COMPILE_PY_TARGET TARGET_NAME SOURCE_DIR )
if( ARGC EQUAL 3 )
set( EXCLUDE_REGEX -x ${ARGV2} )
endif()
add_custom_target( ${TARGET_NAME}
COMMAND ${PYTHON_EXECUTABLE} -m compileall ${EXCLUDE_REGEX} -q ${SOURCE_DIR}
COMMENT "Byte-compiling python scripts" )
endfunction( ADD_COMPILE_PY_TARGET )

#######################################################################

#
# NAME: COPY_PYTHON_FILES_TO_DIR
# Adds a set of custom commands for each python file to copy
# the given file along with its .pyc file, that is assumed to exist
# by the side of the py file.
# - PY_FILES :: A list of python files to copy. Note you will have
# to quote an expanded list
# - SRC_DIR :: The src directory of the files to be copied
# - DEST_DIR :: The final directory for the copied files
# - INSTALLED_FILES :: An output variable containing the list of copied
# files including their full paths
function( COPY_PYTHON_FILES_TO_DIR PY_FILES SRC_DIR DEST_DIR INSTALLED_FILES )
foreach ( PYFILE ${PY_FILES} )
get_filename_component( _basefilename ${PYFILE} NAME_WE )
set( _py_src ${SRC_DIR}/${PYFILE} )
set( _py_bin ${DEST_DIR}/${PYFILE} )
set( _pyc_src ${SRC_DIR}/${_basefilename}.pyc )
set( _pyc_bin ${DEST_DIR}/${_basefilename}.pyc )
add_custom_command ( OUTPUT ${_py_bin} ${_pyc_bin}
DEPENDS ${SRC_DIR}/${PYFILE}
COMMAND ${CMAKE_COMMAND} ARGS -E copy_if_different
${_py_src} ${_py_bin}
COMMAND ${CMAKE_COMMAND} ARGS -E copy_if_different
${_pyc_src} ${_pyc_bin} )
set ( COPIED_FILES ${COPIED_FILES} ${_py_bin} )
set ( COPIED_FILES ${COPIED_FILES} ${_pyc_bin} )
endforeach ( PYFILE )
set ( ${INSTALLED_FILES} ${COPIED_FILES} PARENT_SCOPE )
endfunction( COPY_PYTHON_FILES_TO_DIR )
10 changes: 7 additions & 3 deletions Code/Mantid/Framework/CMakeLists.txt
Expand Up @@ -116,6 +116,11 @@ set ( MANTIDLIBS ${MANTIDLIBS} API )

add_subdirectory (PythonAPI)
add_subdirectory (PythonInterface)
# Compile python user scripts
add_compile_py_target( scripts ${CMAKE_CURRENT_SOURCE_DIR}/../scripts ".*_template" )
# Compile instrument python scripts
add_compile_py_target( instrument_scripts ${CMAKE_CURRENT_SOURCE_DIR}/../instrument )
add_dependencies (PythonInterface scripts instrument_scripts)

find_package ( Matlab )
if( MATLAB_FOUND )
Expand Down Expand Up @@ -183,6 +188,5 @@ install ( DIRECTORY ../instrument/ DESTINATION ${INBUNDLE}instrument
PATTERN "*UNIT_TESTING*" EXCLUDE
)

# Scripts
install ( DIRECTORY ../scripts/ DESTINATION ${INBUNDLE}scripts PATTERN ".svn" EXCLUDE
PATTERN "*.pyc" EXCLUDE )
# Ships both py & pyc files
install ( DIRECTORY ../scripts/ DESTINATION ${INBUNDLE}scripts PATTERN ".svn" EXCLUDE )
27 changes: 21 additions & 6 deletions Code/Mantid/Framework/PythonAPI/CMakeLists.txt
Expand Up @@ -77,19 +77,27 @@ include_directories ( ${PYTHON_NUMPY_INCLUDE_DIR} )
include_directories ( inc )

###########################################################################
# Copy the required python files into the build destination
# Build a pyc file in the binary directory for each python file
###########################################################################

# Now the common files for all distributions
set ( PYTHON_INSTALL_FILES "" )
foreach ( PYFILE ${PY_FILES} )
add_custom_command ( OUTPUT ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/${CMAKE_CFG_INTDIR}/${PYFILE}
get_filename_component( _basefilename ${PYFILE} NAME_WE )
set( _py_src ${CMAKE_CURRENT_SOURCE_DIR}/${PYFILE} )
set( _py_bin ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/${CMAKE_CFG_INTDIR}/${PYFILE} )
set( _pyc_src ${CMAKE_CURRENT_SOURCE_DIR}/${_basefilename}.pyc )
set( _pyc_bin ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/${CMAKE_CFG_INTDIR}/${_basefilename}.pyc )
add_custom_command ( OUTPUT ${_py_bin} ${_pyc_bin}
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${PYFILE}
COMMAND ${CMAKE_COMMAND} ARGS -E copy_if_different
COMMAND ${PYTHON_EXECUTABLE} ARGS -m compileall
${CMAKE_CURRENT_SOURCE_DIR}/${PYFILE}
${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/${CMAKE_CFG_INTDIR}
COMMAND ${CMAKE_COMMAND} ARGS -E copy_if_different
${_py_src} ${_py_bin}
COMMAND ${CMAKE_COMMAND} ARGS -E copy_if_different
${_pyc_src} ${_pyc_bin}
)
set ( PYTHON_INSTALL_FILES ${PYTHON_INSTALL_FILES} ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/${CMAKE_CFG_INTDIR}/${PYFILE} )
set ( PYTHON_INSTALL_FILES ${PYTHON_INSTALL_FILES} ${_pyc_bin} )
endforeach ( PYFILE )

###########################################################################
Expand Down Expand Up @@ -257,6 +265,13 @@ install ( TARGETS PythonAPI ${SYSTEM_PACKAGE_TARGET} DESTINATION ${BIN_DIR} )
if ( UNIX )
install ( TARGETS dlopen DESTINATION ${BIN_DIR} )
endif ()

###########################################################################
# Ship .py & .pyc files
###########################################################################
install ( FILES ${PY_FILES} DESTINATION ${BIN_DIR} )
install ( FILES ${PYTHON_INSTALL_FILES} DESTINATION ${BIN_DIR} )

add_compile_py_target( PythonAlgorithms ${CMAKE_CURRENT_SOURCE_DIR}/PythonAlgorithms )
install ( DIRECTORY PythonAlgorithms/ DESTINATION ${PLUGINS_DIR}/PythonAlgs
PATTERN ".svn" EXCLUDE PATTERN "*.pyc" EXCLUDE )
PATTERN ".svn" EXCLUDE )
5 changes: 3 additions & 2 deletions Code/Mantid/Framework/PythonInterface/CMakeLists.txt
Expand Up @@ -164,7 +164,8 @@ endif ()
# Installation settings
###########################################################################

# Regex matches any .py or .pyc file. The $ ensures that this is the last
# part of the string
install ( DIRECTORY mantid DESTINATION ${BIN_DIR}
PATTERN "*.pyc" EXCLUDE
PATTERN "CMakeLists.txt" EXCLUDE
FILES_MATCHING REGEX ".*.pyc?$"
PATTERN "src" EXCLUDE )
34 changes: 14 additions & 20 deletions Code/Mantid/Framework/PythonInterface/mantid/CMakeLists.txt
Expand Up @@ -6,7 +6,7 @@
set ( PYTHON_PKG_ROOT ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/${CMAKE_CFG_INTDIR}/mantid )

####################################################################################
# Define a macro for generating the exports
# A macro for generating the exports
# - MODULE_TEMPLATE: The file containing the @EXPORT_FUNCTIONS@ and @EXPORT_DECALRE@ flags to replace
# - OUTPUT_FILE: The path to the generated output file
# - EXPORT_FILES: The variable containing the files to be processed
Expand Down Expand Up @@ -34,7 +34,7 @@ MACRO( CREATE_MODULE MODULE_TEMPLATE OUTPUT_FILE EXPORT_FILES SRCS )
ENDMACRO( )

####################################################################################
# Define a function for setting the correct properties on the individual targets
# A function for setting the correct properties on the individual targets
####################################################################################
FUNCTION( SET_PYTHON_PROPERTIES TARGET TARGET_NAME )
# Library name needs to end in .pyd for Windows
Expand All @@ -58,31 +58,25 @@ ENDFUNCTION()
####################################################################################
# Root module
####################################################################################
# Compile all python modules (including sub modules) to pyc
add_compile_py_target( CompilePythonInterfaceScripts ${CMAKE_CURRENT_SOURCE_DIR} )
set_property ( TARGET CompilePythonInterfaceScripts PROPERTY FOLDER "MantidFramework/Python" )

# The destination directory, used by sub modules
set ( OUTPUT_DIR ${PYTHON_PKG_ROOT} )

# Select the files to "build", i.e. copy to the bin directory
set ( PY_FILES
__init__.py
simpleapi.py
)

# CMake would not let this hang off the PythonInterface target because
# when adding PythonInterface to Framework in the parent directory
# it can't figure out what file to be dependent on as
# add_custom_target doesn't produce any output.
# For that reason these are added as dependencies to the _kernel module
set ( PYTHON_PKGROOT_INSTALL_FILES "" )
foreach ( PY_FILE ${PY_FILES} )
add_custom_command ( OUTPUT ${OUTPUT_DIR}/${PY_FILE}
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${PY_FILE}
COMMAND ${CMAKE_COMMAND} ARGS -E copy_if_different
${CMAKE_CURRENT_SOURCE_DIR}/${PY_FILE}
${OUTPUT_DIR}/${PY_FILE}
)
set ( PYTHON_PKGROOT_INSTALL_FILES ${PYTHON_PKGROOT_INSTALL_FILES} ${OUTPUT_DIR}/${PY_FILE} )
endforeach ( PY_FILE )
copy_python_files_to_dir ( "${PY_FILES}" ${CMAKE_CURRENT_SOURCE_DIR} ${OUTPUT_DIR}
PYTHON_PKGROOT_INSTALL_FILES )

add_custom_target ( PythonModuleFiles DEPENDS ${PYTHON_PKGROOT_INSTALL_FILES} SOURCES ${PY_FILES} )
set_property ( TARGET PythonModuleFiles PROPERTY FOLDER "MantidFramework/Python" )
# There is no library target for the top level package just the python files
add_custom_target ( PythonModule DEPENDS ${PYTHON_PKGROOT_INSTALL_FILES} SOURCES ${PY_FILES} )
set_property ( TARGET PythonModule PROPERTY FOLDER "MantidFramework/Python" )

####################################################################################
# Sub modules
Expand All @@ -91,7 +85,7 @@ set_property ( TARGET PythonModuleFiles PROPERTY FOLDER "MantidFramework/Python"
set ( CMAKE_SHARED_LIBRARY_PREFIX )

add_subdirectory ( kernel )
add_dependencies ( PythonKernelModule PythonModuleFiles ) # Ensure the module files are in place
add_dependencies ( PythonKernelModule CompilePythonInterfaceScripts PythonModule ) # Ensure the module files are in place
add_subdirectory ( geometry )
add_subdirectory ( api )

Expand Down
21 changes: 7 additions & 14 deletions Code/Mantid/Framework/PythonInterface/mantid/api/CMakeLists.txt
Expand Up @@ -74,27 +74,20 @@ set ( PY_FILES
_workspaceops.py
)

# Set the destination directory
set ( OUTPUT_DIR ${PYTHON_PKG_ROOT}/api )

#############################################################################################
# Generate a source file from the export definitions
#############################################################################################
CREATE_MODULE ( ${MODULE_TEMPLATE} ${CMAKE_CURRENT_BINARY_DIR}/api.cpp EXPORT_FILES SRC_FILES )
create_module ( ${MODULE_TEMPLATE} ${CMAKE_CURRENT_BINARY_DIR}/api.cpp EXPORT_FILES
SRC_FILES )

#############################################################################################
# Copy over the pure Python files for the module
#############################################################################################
set ( PYTHON_INSTALL_FILES "" )
foreach ( PYFILE ${PY_FILES} )
add_custom_command ( OUTPUT ${OUTPUT_DIR}/${PYFILE}
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${PYFILE}
COMMAND ${CMAKE_COMMAND} ARGS -E copy_if_different
${CMAKE_CURRENT_SOURCE_DIR}/${PYFILE}
${OUTPUT_DIR}/${PYFILE}
)
set ( PYTHON_INSTALL_FILES ${PYTHON_INSTALL_FILES} ${OUTPUT_DIR}/${PYFILE} )
endforeach ( PYFILE )
# Set the destination directory
set ( OUTPUT_DIR ${PYTHON_PKG_ROOT}/api )

copy_python_files_to_dir ( "${PY_FILES}" ${CMAKE_CURRENT_SOURCE_DIR} ${OUTPUT_DIR}
PYTHON_INSTALL_FILES )

#############################################################################################
# Create the target for this directory
Expand Down
Expand Up @@ -35,24 +35,18 @@ set ( PY_FILES
#############################################################################################
# Generate a source file from the export definitions and provided template
#############################################################################################
CREATE_MODULE ( ${MODULE_TEMPLATE} ${CMAKE_CURRENT_BINARY_DIR}/geometry.cpp EXPORT_FILES SRC_FILES )
create_module ( ${MODULE_TEMPLATE} ${CMAKE_CURRENT_BINARY_DIR}/geometry.cpp EXPORT_FILES
SRC_FILES )

#############################################################################################
# Copy over the pure Python files for the module
#############################################################################################
# Set the destination directory
set ( OUTPUT_DIR ${PYTHON_PKG_ROOT}/geometry )

set ( PYTHON_INSTALL_FILES "" )
foreach ( PYFILE ${PY_FILES} )
add_custom_command ( OUTPUT ${OUTPUT_DIR}/${PYFILE}
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${PYFILE}
COMMAND ${CMAKE_COMMAND} ARGS -E copy_if_different
${CMAKE_CURRENT_SOURCE_DIR}/${PYFILE}
${OUTPUT_DIR}/${PYFILE}
)
set ( PYTHON_INSTALL_FILES ${PYTHON_INSTALL_FILES} ${OUTPUT_DIR}/${PYFILE} )
endforeach ( PYFILE )

copy_python_files_to_dir ( "${PY_FILES}" ${CMAKE_CURRENT_SOURCE_DIR} ${OUTPUT_DIR}
PYTHON_INSTALL_FILES )

#############################################################################################
# Create the target for this directory
Expand Down
15 changes: 4 additions & 11 deletions Code/Mantid/Framework/PythonInterface/mantid/kernel/CMakeLists.txt
Expand Up @@ -99,24 +99,17 @@ set ( PY_FILES
#############################################################################################
# Generate a source file from the export definitions and provided template
#############################################################################################
CREATE_MODULE ( ${MODULE_TEMPLATE} ${CMAKE_CURRENT_BINARY_DIR}/kernel.cpp EXPORT_FILES SRC_FILES )
create_module ( ${MODULE_TEMPLATE} ${CMAKE_CURRENT_BINARY_DIR}/kernel.cpp EXPORT_FILES
SRC_FILES )

#############################################################################################
# Copy over the pure Python files for the module
#############################################################################################
# Set the destination directory
set ( OUTPUT_DIR ${PYTHON_PKG_ROOT}/kernel )

set ( PYTHON_INSTALL_FILES "" )
foreach ( PYFILE ${PY_FILES} )
add_custom_command ( OUTPUT ${OUTPUT_DIR}/${PYFILE}
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${PYFILE}
COMMAND ${CMAKE_COMMAND} ARGS -E copy_if_different
${CMAKE_CURRENT_SOURCE_DIR}/${PYFILE}
${OUTPUT_DIR}/${PYFILE}
)
set ( PYTHON_INSTALL_FILES ${PYTHON_INSTALL_FILES} ${OUTPUT_DIR}/${PYFILE} )
endforeach ( PYFILE )
copy_python_files_to_dir ( "${PY_FILES}" ${CMAKE_CURRENT_SOURCE_DIR} ${OUTPUT_DIR}
PYTHON_INSTALL_FILES )

#############################################################################################
# Create the target for this directory
Expand Down

0 comments on commit 306f3e9

Please sign in to comment.