Turn python_callouts.cmake into GodotCPPModule.cmake

Move the find_package for python into it.
Recommend adding godot-cpp/cmake to CMAKE_MODULE_PATH and using include( GodotCPPModule ) to use functions.
Add target_doc_sources function to simplify the addition of documentation to a binary.
pull/1707/head
Samuel Nicholas 2025-02-09 16:55:25 +10:30
parent f398ebb8ce
commit 35469fd839
4 changed files with 55 additions and 21 deletions

View File

@ -41,9 +41,6 @@ include( cmake/godotcpp.cmake )
godotcpp_options()
#[[ Python is required for code generation ]]
find_package(Python3 3.4 REQUIRED) # pathlib should be present
# Define our project.
project( godot-cpp
VERSION 4.4

View File

@ -1,14 +1,24 @@
#[=======================================================================[.rst:
python_callouts.cmake
GodotCPPModule.cmake
---------------------
This file contains functions which which rely on calling Python
This file contains functions and tests which may be needed by consumers.
* Generate Trimmed API
* Generate File List
* Generate Bindings
]=======================================================================]
If you want to use these functions in your project extend the CMAKE_MODULE_PATH
by adding these two lines into your CMakeLists.txt after the inclusion
godot-cpp
.. highlight:: cmake
list(APPEND CMAKE_MODULE_PATH "${godot-cpp_SOURCE_DIR}/cmake")
include( GodotCPPModule )
]=======================================================================]
find_package(Python3 3.4 REQUIRED) # pathlib should be present
#[[ Generate Trimmed API
@ -106,22 +116,51 @@ endfunction( )
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 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 )
function( generate_doc_source OUTPUT_PATH SOURCES )
# Transform SOURCES CMake LIST
# quote each path with ''
# join with , to transform into a python list minus the surrounding []
set( PYTHON_LIST "${SOURCES}")
list( TRANSFORM PYTHON_LIST REPLACE "(.*\.xml)" "'\\1'" )
list( JOIN PYTHON_LIST "," PYTHON_LIST )
# 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}] )" )
"generate_doc_source( '${OUTPUT_PATH}', [${PYTHON_LIST}] )" )
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"
DEPENDS
"${godot-cpp_SOURCE_DIR}/doc_source_generator.py"
"${SOURCES}"
COMMENT "Generating: ${OUTPUT_PATH}"
)
endfunction()
#[[ target_doc_sources
A simpler interface to add xml files as doc source to a output target.
TARGET: The gdexension library target
SOURCES: a list of xml files to use for source generation and inclusion.
This function also adds a doc_gen target to test source generation.]]
function( target_doc_sources TARGET SOURCES )
# set the generated file name
set( DOC_SOURCE_FILE "${CMAKE_CURRENT_BINARY_DIR}/gen/doc_source.cpp" )
# Create the file generation target, this won't be triggered unless a target
# that depends on DOC_SOURCE_FILE is built
generate_doc_source( "${DOC_SOURCE_FILE}" ${SOURCES} )
# Add DOC_SOURCE_FILE as a dependency to TARGET
target_sources( ${TARGET} PRIVATE "${DOC_SOURCE_FILE}" )
# Create a dummy target that depends on the source so that users can
# test the file generation task.
if( TARGET doc_gen )
else()
add_custom_target( doc_gen )
endif()
target_sources( doc_gen PRIVATE "${DOC_SOURCE_FILE}" )
endfunction()

View File

@ -23,6 +23,7 @@ project directive, it means that
directive was
]=======================================================================]
include( ${CMAKE_CURRENT_SOURCE_DIR}/cmake/GodotCPPModule.cmake)
include( ${CMAKE_CURRENT_SOURCE_DIR}/cmake/common_compiler_flags.cmake)
include( ${CMAKE_CURRENT_SOURCE_DIR}/cmake/android.cmake)
include( ${CMAKE_CURRENT_SOURCE_DIR}/cmake/ios.cmake)
@ -30,7 +31,7 @@ include( ${CMAKE_CURRENT_SOURCE_DIR}/cmake/linux.cmake)
include( ${CMAKE_CURRENT_SOURCE_DIR}/cmake/macos.cmake)
include( ${CMAKE_CURRENT_SOURCE_DIR}/cmake/web.cmake)
include( ${CMAKE_CURRENT_SOURCE_DIR}/cmake/windows.cmake)
include( ${CMAKE_CURRENT_SOURCE_DIR}/cmake/python_callouts.cmake)
# Detect number of processors
include(ProcessorCount)

View File

@ -13,9 +13,6 @@ file( GLOB_RECURSE DOC_XML
CONFIGURE_DEPENDS
"${CMAKE_CURRENT_SOURCE_DIR}/doc_classes/*.xml" )
set( DOC_DATA_SOURCE "${CMAKE_CURRENT_BINARY_DIR}/src/gen/doc_data.gen.cpp" )
generate_doc_source( "${DOC_DATA_SOURCE}" "${DOC_XML}" )
foreach( TARGET_ALIAS template_debug template_release editor )
set( TARGET_NAME "godot-cpp.test.${TARGET_ALIAS}" )
@ -30,13 +27,13 @@ foreach( TARGET_ALIAS template_debug template_release editor )
src/tests.h
)
set( OUTPUT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/project/bin/" )
# conditionally add doc data to compile output
if( TARGET_ALIAS MATCHES "editor|template_debug" )
target_sources( ${TARGET_NAME} PRIVATE "${DOC_DATA_SOURCE}" )
target_doc_sources( ${TARGET_NAME} ${DOC_XML} )
endif()
set( OUTPUT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/project/bin/" )
# Link to godot-cpp target
set( LINK_TARGET "godot-cpp::${TARGET_ALIAS}" )
target_link_libraries( ${TARGET_NAME} PRIVATE ${LINK_TARGET} )