From 54032a43619b5453ee34c95da8160b6796e72490 Mon Sep 17 00:00:00 2001 From: Samuel Nicholas Date: Sat, 28 Dec 2024 18:29:24 +1030 Subject: [PATCH] Implement generate_doc_source for cmake --- cmake/python_callouts.cmake | 41 ++++++++++++++++++++++++++++++++----- doc_source_generator.py | 2 +- test/CMakeLists.txt | 14 ++++++++++++- tools/godotcpp.py | 3 +-- 4 files changed, 51 insertions(+), 9 deletions(-) diff --git a/cmake/python_callouts.cmake b/cmake/python_callouts.cmake index 8e2262112..34db0b0e4 100644 --- a/cmake/python_callouts.cmake +++ b/cmake/python_callouts.cmake @@ -18,8 +18,12 @@ Its usage is listed as: ]] function( build_profile_generate_trimmed_api BUILD_PROFILE INPUT_JSON OUTPUT_JSON ) execute_process( - COMMAND "${Python3_EXECUTABLE}" "build_profile.py" "${BUILD_PROFILE}" "${INPUT_JSON}" "${OUTPUT_JSON}" - WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} + COMMAND "${Python3_EXECUTABLE}" + "${godot-cpp_SOURCE_DIR}/build_profile.py" + "${BUILD_PROFILE}" + "${INPUT_JSON}" + "${OUTPUT_JSON}" + WORKING_DIRECTORY ${godot-cpp_SOURCE_DIR} ) endfunction( ) @@ -45,7 +49,7 @@ function( binding_generator_get_file_list OUT_VAR_NAME API_FILEPATH OUTPUT_DIR ) string( REGEX REPLACE "\n *" " " PYTHON_SCRIPT "${PYTHON_SCRIPT}" ) execute_process( COMMAND "${Python3_EXECUTABLE}" "-c" "${PYTHON_SCRIPT}" - WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} + WORKING_DIRECTORY "${godot-cpp_SOURCE_DIR}" OUTPUT_VARIABLE GENERATED_FILES_LIST OUTPUT_STRIP_TRAILING_WHITESPACE ) @@ -91,9 +95,36 @@ function( binding_generator_generate_bindings API_FILE USE_TEMPLATE_GET_NODE, BI add_custom_command(OUTPUT ${GENERATED_FILES_LIST} COMMAND "${Python3_EXECUTABLE}" "-c" "${PYTHON_SCRIPT}" VERBATIM - WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} + WORKING_DIRECTORY ${godot-cpp_SOURCE_DIR} MAIN_DEPENDENCY ${GODOT_GDEXTENSION_API_FILE} - DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/binding_generator.py + DEPENDS ${godot-cpp_SOURCE_DIR}/binding_generator.py COMMENT "Generating bindings" ) endfunction( ) + +#[[ Generate doc_data.cpp +The documentation displayed in the Godot editor is compiled into the extension. +It takes a list of XML source files, and transforms them into a cpp file that +is added to the sources list.]] +function( generate_doc_source OUT_VAR_NAME OUTPUT_PATH XML_SOURCES ) + # Transform the CMake list into the content of a python list + # quote and join to form the interior of a python array + list( TRANSFORM XML_SOURCES REPLACE "(.*\.xml)" "'\\1'" ) + list( JOIN XML_SOURCES "," XML_SOURCES ) + + # Python one-liner to run our command + # lists in CMake are just strings delimited by ';', so this works. + set( PYTHON_SCRIPT "from doc_source_generator import generate_doc_source" + "generate_doc_source( '${OUTPUT_PATH}', [${XML_SOURCES}] )" ) + + add_custom_command( OUTPUT "${OUTPUT_PATH}" + COMMAND "${Python3_EXECUTABLE}" "-c" "${PYTHON_SCRIPT}" + VERBATIM + WORKING_DIRECTORY "${godot-cpp_SOURCE_DIR}" + DEPENDS "${godot-cpp_SOURCE_DIR}/doc_source_generator.py" + COMMENT "Generating Doc Data" + ) + + # Propagate generated file to caller scope + set( ${OUT_VAR_NAME} "${OUTPUT_PATH}" PARENT_SCOPE ) +endfunction() diff --git a/doc_source_generator.py b/doc_source_generator.py index 6cf5d06e3..4f6efb1f3 100644 --- a/doc_source_generator.py +++ b/doc_source_generator.py @@ -1,7 +1,7 @@ #!/usr/bin/env python -import os import glob +import os import zlib diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 5526357e3..5d4958e6f 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -3,11 +3,18 @@ Integration Testing ------------------- The Test target used to validate changes in the GitHub CI. - ]=======================================================================] message( STATUS "Testing Integration targets are enabled.") +# Generate Doc Data +file( GLOB_RECURSE DOC_XML + LIST_DIRECTORIES NO + CONFIGURE_DEPENDS + ${CMAKE_CURRENT_SOURCE_DIR}/doc_classes/*.xml ) + +generate_doc_source( DOC_DATA "${CMAKE_CURRENT_BINARY_DIR}/gen/doc_data.cpp" "${DOC_XML}" ) + foreach( TARGET_ALIAS template_debug template_release editor ) set( TARGET_NAME "godot-cpp.test.${TARGET_ALIAS}" ) set( LINK_TARGET "godot-cpp::${TARGET_ALIAS}" ) @@ -23,6 +30,11 @@ foreach( TARGET_ALIAS template_debug template_release editor ) src/tests.h ) + # conditionally add doc data to compile output + if( TARGET_ALIAS MATCHES "editor|template_debug" ) + target_sources( ${TARGET_NAME} PRIVATE "${DOC_DATA}" ) + endif( ) + target_link_libraries( ${TARGET_NAME} PRIVATE ${LINK_TARGET} ) ### Get useful properties of the library diff --git a/tools/godotcpp.py b/tools/godotcpp.py index ca3f2c7de..8931e6dc7 100644 --- a/tools/godotcpp.py +++ b/tools/godotcpp.py @@ -12,10 +12,9 @@ from binding_generator import _generate_bindings, _get_file_list, get_file_list from build_profile import generate_trimmed_api - -from binding_generator import scons_generate_bindings, scons_emit_files from doc_source_generator import scons_generate_doc_source + def add_sources(sources, dir, extension): for f in os.listdir(dir): if f.endswith("." + extension):