ctkScriptWrapPythonQt_Light.cmake 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. ###########################################################################
  2. #
  3. # Library: CTK
  4. #
  5. # Copyright (c) Kitware Inc.
  6. #
  7. # Licensed under the Apache License, Version 2.0 (the "License");
  8. # you may not use this file except in compliance with the License.
  9. # You may obtain a copy of the License at
  10. #
  11. # http://www.apache.org/licenses/LICENSE-2.0.txt
  12. #
  13. # Unless required by applicable law or agreed to in writing, software
  14. # distributed under the License is distributed on an "AS IS" BASIS,
  15. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. # See the License for the specific language governing permissions and
  17. # limitations under the License.
  18. #
  19. ###########################################################################
  20. #
  21. # ctkScriptWrapPythonQt_Light
  22. #
  23. #
  24. # This script should be invoked either as a CUSTOM_COMMAND
  25. # or from the command line using the following syntax:
  26. #
  27. # cmake -DWRAPPING_SCRIPT:FILEPATH=/path/to/ctkWrapPythonQt.py
  28. # -DWRAPPING_NAMESPACE:STRING=org.commontk -DTARGET:STRING=MyLib
  29. # -DSOURCES:STRING="file1^^file2"
  30. # -DOUTPUT_DIR:PATH=/path
  31. # -DPYTHON_EXECUTABLE:FILEPATH=/path/to/python
  32. # -DPYTHON_LIBRARY_PATH:PATH=/path/to/pythonlib
  33. # -DHAS_DECORATOR:BOOL=True
  34. # -P ctkScriptWrapPythonQt_Light.cmake
  35. #
  36. #
  37. # LOG FILE:
  38. # File ctkScriptWrapPythonQt_Light_log.txt will be created in the current directory.
  39. # It will contain the list of class and the constructor signature that will be wrapped.
  40. #
  41. # Check for non-defined var
  42. foreach(var WRAPPING_NAMESPACE TARGET SOURCES HAS_DECORATOR)
  43. if(NOT DEFINED ${var})
  44. message(FATAL_ERROR "${var} not specified when calling ctkScriptWrapPythonQt")
  45. endif()
  46. endforeach()
  47. # Check for non-existing ${var}
  48. foreach(var WRAPPING_SCRIPT OUTPUT_DIR PYTHON_EXECUTABLE PYTHON_LIBRARY_PATH)
  49. if(NOT EXISTS ${${var}})
  50. message(FATAL_ERROR "Failed to find ${var}=\"${${var}}\" when calling ctkScriptWrapPythonQt")
  51. endif()
  52. endforeach()
  53. # Convert ^^ separated string to list
  54. string(REPLACE "^^" ";" SOURCES "${SOURCES}")
  55. if(WIN32)
  56. set(ENV{PATH} ${PYTHON_LIBRARY_PATH};$ENV{PATH})
  57. elseif(APPLE)
  58. set(ENV{DYLD_LIBRARY_PATH} ${PYTHON_LIBRARY_PATH}:$ENV{DYLD_LIBRARY_PATH})
  59. else()
  60. set(ENV{LD_LIBRARY_PATH} ${PYTHON_LIBRARY_PATH}:$ENV{LD_LIBRARY_PATH})
  61. endif()
  62. set(verbose 0)
  63. set(extra_args)
  64. if(verbose)
  65. set(extra_args --extra-verbose)
  66. endif()
  67. execute_process(
  68. COMMAND ${PYTHON_EXECUTABLE} ${WRAPPING_SCRIPT}
  69. --target=${TARGET} --namespace=${WRAPPING_NAMESPACE} --output-dir=${OUTPUT_DIR} ${extra_args}
  70. ${SOURCES}
  71. RESULT_VARIABLE result
  72. OUTPUT_VARIABLE output
  73. ERROR_VARIABLE error
  74. OUTPUT_STRIP_TRAILING_WHITESPACE
  75. )
  76. if(NOT result EQUAL 0)
  77. message(FATAL_ERROR "Failed to wrap target: ${TARGET}\n${output}\n${error}")
  78. else()
  79. if(verbose)
  80. message(${output})
  81. endif()
  82. endif()
  83. # Convert wrapping namespace to subdir
  84. string(REPLACE "." "_" WRAPPING_NAMESPACE_UNDERSCORE ${WRAPPING_NAMESPACE})
  85. # Configure 'ctkMacroWrapPythonQtModuleInit.cpp.in' using TARGET, HAS_DECORATOR and
  86. # WRAPPING_NAMESPACE_UNDERSCORE.
  87. configure_file(
  88. ${CMAKE_CURRENT_LIST_DIR}/ctkMacroWrapPythonQtModuleInit.cpp.in
  89. ${OUTPUT_DIR}/${WRAP_INT_DIR}${WRAPPING_NAMESPACE_UNDERSCORE}_${TARGET}_module_init.cpp
  90. )