python_callouts.cmake 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #[=======================================================================[.rst:
  2. python_callouts.cmake
  3. ---------------------
  4. This file contains functions which which rely on calling Python
  5. * Generate Trimmed API
  6. * Generate File List
  7. * Generate Bindings
  8. ]=======================================================================]
  9. #[[ Generate Trimmed API
  10. The build_profile.py has a __main__ and is used as a tool
  11. Its usage is listed as:
  12. $ python build_profile.py BUILD_PROFILE INPUT_JSON [OUTPUT_JSON]
  13. ]]
  14. function( build_profile_generate_trimmed_api BUILD_PROFILE INPUT_JSON OUTPUT_JSON )
  15. execute_process(
  16. COMMAND "${Python3_EXECUTABLE}" "build_profile.py" "${BUILD_PROFILE}" "${INPUT_JSON}" "${OUTPUT_JSON}"
  17. WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
  18. )
  19. endfunction( )
  20. #[[ Generate File List
  21. Use the binding_generator.py Python script to determine the list of files that
  22. will be passed to the code generator using extension_api.json.
  23. NOTE: This happens for every configure.]]
  24. function( binding_generator_get_file_list OUT_VAR_NAME API_FILEPATH OUTPUT_DIR )
  25. # This code snippet will be squashed into a single line
  26. # The two strings make this a list, in CMake lists are semicolon delimited strings.
  27. set( PYTHON_SCRIPT
  28. "from binding_generator import print_file_list"
  29. "print_file_list( api_filepath='${API_FILEPATH}',
  30. output_dir='${OUTPUT_DIR}',
  31. headers=True,
  32. sources=True)")
  33. message( DEBUG "Python:\n${PYTHON_SCRIPT}" )
  34. # Strip newlines and whitespace to make it a one-liner.
  35. string( REGEX REPLACE "\n *" " " PYTHON_SCRIPT "${PYTHON_SCRIPT}" )
  36. execute_process( COMMAND "${Python3_EXECUTABLE}" "-c" "${PYTHON_SCRIPT}"
  37. WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
  38. OUTPUT_VARIABLE GENERATED_FILES_LIST
  39. OUTPUT_STRIP_TRAILING_WHITESPACE
  40. )
  41. # Debug output
  42. message( DEBUG "FileList-Begin" )
  43. foreach( PATH ${GENERATED_FILES_LIST} )
  44. message( DEBUG ${PATH} )
  45. endforeach()
  46. # Error out if the file list generator returned no files.
  47. list( LENGTH GENERATED_FILES_LIST LIST_LENGTH )
  48. if( NOT LIST_LENGTH GREATER 0 )
  49. message( FATAL_ERROR "File List Generation Failed")
  50. endif()
  51. message( STATUS "There are ${LIST_LENGTH} Files to generate" )
  52. set( ${OUT_VAR_NAME} ${GENERATED_FILES_LIST} PARENT_SCOPE )
  53. endfunction( )
  54. #[[ Generate Bindings
  55. Using the generated file list, use the binding_generator.py to generate the
  56. godot-cpp bindings. This will run at build time only if there are files
  57. missing. ]]
  58. function( binding_generator_generate_bindings API_FILE USE_TEMPLATE_GET_NODE, BITS, PRECISION, OUTPUT_DIR )
  59. # This code snippet will be squashed into a single line
  60. set( PYTHON_SCRIPT
  61. "from binding_generator import generate_bindings"
  62. "generate_bindings(
  63. api_filepath='${API_FILE}',
  64. use_template_get_node='${USE_TEMPLATE_GET_NODE}',
  65. bits='${BITS}',
  66. precision='${PRECISION}',
  67. output_dir='${OUTPUT_DIR}')")
  68. message( DEBUG "Python:\n${PYTHON_SCRIPT}" )
  69. # Strip newlines and whitespace to make it a one-liner.
  70. string( REGEX REPLACE "\n *" " " PYTHON_SCRIPT "${PYTHON_SCRIPT}" )
  71. add_custom_command(OUTPUT ${GENERATED_FILES_LIST}
  72. COMMAND "${Python3_EXECUTABLE}" "-c" "${PYTHON_SCRIPT}"
  73. VERBATIM
  74. WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
  75. MAIN_DEPENDENCY ${GODOT_GDEXTENSION_API_FILE}
  76. DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/binding_generator.py
  77. COMMENT "Generating bindings"
  78. )
  79. endfunction( )