浏览代码

Improve ctkMacroCompilePythonScript

To ease understanding of the parameters passed the the macro, it
now use ctkMacroParseArguments

Concept of Resources has been introduced, a python resources is a file
that will be installed along the python script without being compiled.

Using a configured script and a CustomCommand, LIBRARY_PATH is now
set before invoking python executable to compile the scripts.
Jean-Christophe Fillion-Robin 14 年之前
父节点
当前提交
52ca34d52d
共有 1 个文件被更改,包括 87 次插入25 次删除
  1. 87 25
      CMake/ctkMacroCompilePythonScript.cmake

+ 87 - 25
CMake/ctkMacroCompilePythonScript.cmake

@@ -5,49 +5,111 @@
 #          ParaView/VTK/Wrapping/Python/CMakeLists.txt
 #          ParaView/VTK/Wrapping/Python/CMakeLists.txt
 #
 #
 
 
-MACRO(ctkMacroCompilePythonScript kit_name python_source_files dest_dir)
-
+MACRO(ctkMacroCompilePythonScript)
+  ctkMacroParseArguments(MY
+    "TARGET_NAME;SCRIPTS;RESOURCES;DESTINATION_DIR;INSTALL_DIR"
+    ""
+    ${ARGN}
+    )
+  
   FIND_PACKAGE(PythonInterp REQUIRED)
   FIND_PACKAGE(PythonInterp REQUIRED)
+  FIND_PACKAGE(PythonLibs REQUIRED)
+
+  # Extract python lib path
+  get_filename_component(PYTHON_LIBRARY_PATH ${PYTHON_LIBRARY} PATH)
   
   
-  SET(copied_python_files)
+  # Sanity checks
+  FOREACH(varname TARGET_NAME SCRIPTS DESTINATION_DIR;INSTALL_DIR)
+    IF(NOT DEFINED MY_${varname})
+      MESSAGE(SEND_ERROR "${varname} is mandatory")
+    ENDIF()
+  ENDFOREACH()
   
   
-  FOREACH(file ${python_source_files})
-    GET_FILENAME_COMPONENT(file_we "${CMAKE_CURRENT_SOURCE_DIR}/${file}" NAME_WE)
-    SET(src "${CMAKE_CURRENT_SOURCE_DIR}/${file_we}.py")
-    SET(tgt "${dest_dir}/${file_we}.py")
+  set(copied_files)
+  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})
     SET(copied_python_files ${copied_python_files} ${tgt})
+    SET(copied_files ${copied_files} ${tgt})
     ADD_CUSTOM_COMMAND(DEPENDS ${src}
     ADD_CUSTOM_COMMAND(DEPENDS ${src}
                         COMMAND ${CMAKE_COMMAND} -E copy ${src} ${tgt}
                         COMMAND ${CMAKE_COMMAND} -E copy ${src} ${tgt}
                         OUTPUT ${tgt}
                         OUTPUT ${tgt}
-                        COMMENT "Copying ${file_we}")
+                        COMMENT "Copying python script: ${file}.py")
   ENDFOREACH()
   ENDFOREACH()
+
+  IF(DEFINED MY_RESOURCES)
+    FOREACH(file ${MY_RESOURCES})
+      SET(src "${CMAKE_CURRENT_SOURCE_DIR}/${file}")
+      SET(tgt "${MY_DESTINATION_DIR}/${file}")
+      SET(copied_files ${copied_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_files})
+  ENDIF()
+                    
   
   
   # Byte compile the Python files.
   # Byte compile the Python files.
-  SET(compile_all_script "${CMAKE_CURRENT_BINARY_DIR}/compile_all_${kit_name}.py")
+  SET(compile_all_script "${CMAKE_CURRENT_BINARY_DIR}/compile_${MY_TARGET_NAME}_python_scripts.py")
   
   
-  # Based on paraview/VTK/Wrapping/Python/compile_all_vtk.py.in
+  # Generate compile_${MY_TARGET_NAME}_python_scripts.py
   FILE(WRITE ${compile_all_script} "
   FILE(WRITE ${compile_all_script} "
-\# Auto-generated
+#
+# Generated by ctkMacroCompilePythonScript CMAKE macro
+#
+
+# Based on paraview/VTK/Wrapping/Python/compile_all_vtk.py.in
+
 import compileall
 import compileall
-compileall.compile_dir('${dest_dir}')
-file = open('${CMAKE_CURRENT_BINARY_DIR}/${kit_name}_python_compile_complete', 'w')
+compileall.compile_dir('@MY_DESTINATION_DIR@')
+file = open('@CMAKE_CURRENT_BINARY_DIR@/python_compile_@MY_TARGET_NAME@_complete', 'w')
 file.write('Done')
 file.write('Done')
 ")
 ")
 
 
+  # Configure cmake script associated with the custom command
+  # required to properly update the library path with PYTHON_LIBRARY_PATH
+  SET(compile_all_cmake_script "${CMAKE_CURRENT_BINARY_DIR}/compile_${MY_TARGET_NAME}_python_scripts.cmake")
+  FILE(WRITE ${compile_all_cmake_script} "
+#
+# Generated by ctkMacroCompilePythonScript CMAKE macro
+#
+
+IF(WIN32)
+    SET(ENV{PATH} \"@PYTHON_LIBRARY_PATH@;\$ENV{PATH}\")
+ELSEIF(APPLE)
+  SET(ENV{DYLD_LIBRARY_PATH} \"@PYTHON_LIBRARY_PATH@:\$ENV{DYLD_LIBRARY_PATH}\")
+ELSE()
+  SET(ENV{LD_LIBRARY_PATH} \"@PYTHON_LIBRARY_PATH@:\$ENV{LD_LIBRARY_PATH}\")
+ENDIF()
+
+EXECUTE_PROCESS(
+  COMMAND \"@PYTHON_EXECUTABLE@\" \"@compile_all_script@\"
+  )
+")
+
   ADD_CUSTOM_COMMAND(
   ADD_CUSTOM_COMMAND(
-    COMMAND ${PYTHON_EXECUTABLE} ${compile_all_script}
+    COMMAND ${CMAKE_COMMAND} -P ${compile_all_cmake_script}
     DEPENDS ${copied_python_files}  ${compile_all_script}
     DEPENDS ${copied_python_files}  ${compile_all_script}
-    OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${kit_name}_python_compile_complete"
-    COMMENT "Compiling ${kit_name} python files"
+    OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/python_compile_${MY_TARGET_NAME}_complete"
+    COMMENT "Compiling python scripts: ${MY_TARGET_NAME}"
     )
     )
-  
-  ADD_CUSTOM_TARGET(${kit_name}_pyc ALL
-    DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/${kit_name}_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)
 
 
+  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()
 ENDMACRO()
+