python_callouts.cmake 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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}"
  17. "${godot-cpp_SOURCE_DIR}/build_profile.py"
  18. "${BUILD_PROFILE}"
  19. "${INPUT_JSON}"
  20. "${OUTPUT_JSON}"
  21. WORKING_DIRECTORY ${godot-cpp_SOURCE_DIR}
  22. )
  23. endfunction( )
  24. #[[ Generate File List
  25. Use the binding_generator.py Python script to determine the list of files that
  26. will be passed to the code generator using extension_api.json.
  27. NOTE: This happens for every configure.]]
  28. function( binding_generator_get_file_list OUT_VAR_NAME API_FILEPATH OUTPUT_DIR )
  29. # This code snippet will be squashed into a single line
  30. # The two strings make this a list, in CMake lists are semicolon delimited strings.
  31. set( PYTHON_SCRIPT
  32. "from binding_generator import print_file_list"
  33. "print_file_list( api_filepath='${API_FILEPATH}',
  34. output_dir='${OUTPUT_DIR}',
  35. headers=True,
  36. sources=True)")
  37. message( DEBUG "Python:\n${PYTHON_SCRIPT}" )
  38. # Strip newlines and whitespace to make it a one-liner.
  39. string( REGEX REPLACE "\n *" " " PYTHON_SCRIPT "${PYTHON_SCRIPT}" )
  40. execute_process( COMMAND "${Python3_EXECUTABLE}" "-c" "${PYTHON_SCRIPT}"
  41. WORKING_DIRECTORY "${godot-cpp_SOURCE_DIR}"
  42. OUTPUT_VARIABLE GENERATED_FILES_LIST
  43. OUTPUT_STRIP_TRAILING_WHITESPACE
  44. )
  45. # Debug output
  46. message( DEBUG "FileList-Begin" )
  47. foreach( PATH ${GENERATED_FILES_LIST} )
  48. message( DEBUG ${PATH} )
  49. endforeach()
  50. # Error out if the file list generator returned no files.
  51. list( LENGTH GENERATED_FILES_LIST LIST_LENGTH )
  52. if( NOT LIST_LENGTH GREATER 0 )
  53. message( FATAL_ERROR "File List Generation Failed")
  54. endif()
  55. message( STATUS "There are ${LIST_LENGTH} Files to generate" )
  56. set( ${OUT_VAR_NAME} ${GENERATED_FILES_LIST} PARENT_SCOPE )
  57. endfunction( )
  58. #[[ Generate Bindings
  59. Using the generated file list, use the binding_generator.py to generate the
  60. godot-cpp bindings. This will run at build time only if there are files
  61. missing. ]]
  62. function( binding_generator_generate_bindings API_FILE USE_TEMPLATE_GET_NODE, BITS, PRECISION, OUTPUT_DIR )
  63. # This code snippet will be squashed into a single line
  64. set( PYTHON_SCRIPT
  65. "from binding_generator import generate_bindings"
  66. "generate_bindings(
  67. api_filepath='${API_FILE}',
  68. use_template_get_node='${USE_TEMPLATE_GET_NODE}',
  69. bits='${BITS}',
  70. precision='${PRECISION}',
  71. output_dir='${OUTPUT_DIR}')")
  72. message( DEBUG "Python:\n${PYTHON_SCRIPT}" )
  73. # Strip newlines and whitespace to make it a one-liner.
  74. string( REGEX REPLACE "\n *" " " PYTHON_SCRIPT "${PYTHON_SCRIPT}" )
  75. add_custom_command(OUTPUT ${GENERATED_FILES_LIST}
  76. COMMAND "${Python3_EXECUTABLE}" "-c" "${PYTHON_SCRIPT}"
  77. VERBATIM
  78. WORKING_DIRECTORY ${godot-cpp_SOURCE_DIR}
  79. MAIN_DEPENDENCY ${GODOT_GDEXTENSION_API_FILE}
  80. DEPENDS ${godot-cpp_SOURCE_DIR}/binding_generator.py
  81. COMMENT "Generating bindings"
  82. )
  83. endfunction( )
  84. #[[ Generate doc_data.cpp
  85. The documentation displayed in the Godot editor is compiled into the extension.
  86. It takes a list of XML source files, and transforms them into a cpp file that
  87. is added to the sources list.]]
  88. function( generate_doc_source OUTPUT_PATH XML_SOURCES )
  89. # Transform the CMake list into the content of a python list
  90. # quote and join to form the interior of a python array
  91. list( TRANSFORM XML_SOURCES REPLACE "(.*\.xml)" "'\\1'" )
  92. list( JOIN XML_SOURCES "," XML_SOURCES )
  93. # Python one-liner to run our command
  94. # lists in CMake are just strings delimited by ';', so this works.
  95. set( PYTHON_SCRIPT "from doc_source_generator import generate_doc_source"
  96. "generate_doc_source( '${OUTPUT_PATH}', [${XML_SOURCES}] )" )
  97. add_custom_command( OUTPUT "${OUTPUT_PATH}"
  98. COMMAND "${Python3_EXECUTABLE}" "-c" "${PYTHON_SCRIPT}"
  99. VERBATIM
  100. WORKING_DIRECTORY "${godot-cpp_SOURCE_DIR}"
  101. DEPENDS "${godot-cpp_SOURCE_DIR}/doc_source_generator.py"
  102. COMMENT "Generating Doc Data"
  103. )
  104. endfunction()