Skip to content

Commit 3f54a86

Browse files
authored
Merge pull request godotengine#1707 from enetheru/cmake_module
CMake: GodotCPPModule.cmake
2 parents 847dca4 + 35469fd commit 3f54a86

File tree

4 files changed

+55
-21
lines changed

4 files changed

+55
-21
lines changed

CMakeLists.txt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,6 @@ include( cmake/godotcpp.cmake )
4141

4242
godotcpp_options()
4343

44-
#[[ Python is required for code generation ]]
45-
find_package(Python3 3.4 REQUIRED) # pathlib should be present
46-
4744
# Define our project.
4845
project( godot-cpp
4946
VERSION 4.4

cmake/python_callouts.cmake renamed to cmake/GodotCPPModule.cmake

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
11
#[=======================================================================[.rst:
2-
python_callouts.cmake
2+
GodotCPPModule.cmake
33
---------------------
44
5-
This file contains functions which which rely on calling Python
5+
This file contains functions and tests which may be needed by consumers.
66
77
* Generate Trimmed API
88
* Generate File List
99
* Generate Bindings
10-
]=======================================================================]
1110
11+
If you want to use these functions in your project extend the CMAKE_MODULE_PATH
12+
by adding these two lines into your CMakeLists.txt after the inclusion
13+
godot-cpp
14+
15+
.. highlight:: cmake
16+
17+
list(APPEND CMAKE_MODULE_PATH "${godot-cpp_SOURCE_DIR}/cmake")
18+
include( GodotCPPModule )
19+
20+
]=======================================================================]
21+
find_package(Python3 3.4 REQUIRED) # pathlib should be present
1222

1323
#[[ Generate Trimmed API
1424
@@ -106,22 +116,51 @@ endfunction( )
106116
The documentation displayed in the Godot editor is compiled into the extension.
107117
It takes a list of XML source files, and transforms them into a cpp file that
108118
is added to the sources list.]]
109-
function( generate_doc_source OUTPUT_PATH XML_SOURCES )
110-
# Transform the CMake list into the content of a python list
111-
# quote and join to form the interior of a python array
112-
list( TRANSFORM XML_SOURCES REPLACE "(.*\.xml)" "'\\1'" )
113-
list( JOIN XML_SOURCES "," XML_SOURCES )
119+
function( generate_doc_source OUTPUT_PATH SOURCES )
120+
# Transform SOURCES CMake LIST
121+
# quote each path with ''
122+
# join with , to transform into a python list minus the surrounding []
123+
set( PYTHON_LIST "${SOURCES}")
124+
list( TRANSFORM PYTHON_LIST REPLACE "(.*\.xml)" "'\\1'" )
125+
list( JOIN PYTHON_LIST "," PYTHON_LIST )
114126

115127
# Python one-liner to run our command
116128
# lists in CMake are just strings delimited by ';', so this works.
117129
set( PYTHON_SCRIPT "from doc_source_generator import generate_doc_source"
118-
"generate_doc_source( '${OUTPUT_PATH}', [${XML_SOURCES}] )" )
130+
"generate_doc_source( '${OUTPUT_PATH}', [${PYTHON_LIST}] )" )
119131

120132
add_custom_command( OUTPUT "${OUTPUT_PATH}"
121133
COMMAND "${Python3_EXECUTABLE}" "-c" "${PYTHON_SCRIPT}"
122134
VERBATIM
123135
WORKING_DIRECTORY "${godot-cpp_SOURCE_DIR}"
124-
DEPENDS "${godot-cpp_SOURCE_DIR}/doc_source_generator.py"
125-
COMMENT "Generating Doc Data"
136+
DEPENDS
137+
"${godot-cpp_SOURCE_DIR}/doc_source_generator.py"
138+
"${SOURCES}"
139+
COMMENT "Generating: ${OUTPUT_PATH}"
126140
)
127141
endfunction()
142+
143+
#[[ target_doc_sources
144+
A simpler interface to add xml files as doc source to a output target.
145+
TARGET: The gdexension library target
146+
SOURCES: a list of xml files to use for source generation and inclusion.
147+
This function also adds a doc_gen target to test source generation.]]
148+
function( target_doc_sources TARGET SOURCES )
149+
# set the generated file name
150+
set( DOC_SOURCE_FILE "${CMAKE_CURRENT_BINARY_DIR}/gen/doc_source.cpp" )
151+
152+
# Create the file generation target, this won't be triggered unless a target
153+
# that depends on DOC_SOURCE_FILE is built
154+
generate_doc_source( "${DOC_SOURCE_FILE}" ${SOURCES} )
155+
156+
# Add DOC_SOURCE_FILE as a dependency to TARGET
157+
target_sources( ${TARGET} PRIVATE "${DOC_SOURCE_FILE}" )
158+
159+
# Create a dummy target that depends on the source so that users can
160+
# test the file generation task.
161+
if( TARGET doc_gen )
162+
else()
163+
add_custom_target( doc_gen )
164+
endif()
165+
target_sources( doc_gen PRIVATE "${DOC_SOURCE_FILE}" )
166+
endfunction()

cmake/godotcpp.cmake

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,15 @@ project directive, it means that
2323
directive was
2424
2525
]=======================================================================]
26+
include( ${CMAKE_CURRENT_SOURCE_DIR}/cmake/GodotCPPModule.cmake)
2627
include( ${CMAKE_CURRENT_SOURCE_DIR}/cmake/common_compiler_flags.cmake)
2728
include( ${CMAKE_CURRENT_SOURCE_DIR}/cmake/android.cmake)
2829
include( ${CMAKE_CURRENT_SOURCE_DIR}/cmake/ios.cmake)
2930
include( ${CMAKE_CURRENT_SOURCE_DIR}/cmake/linux.cmake)
3031
include( ${CMAKE_CURRENT_SOURCE_DIR}/cmake/macos.cmake)
3132
include( ${CMAKE_CURRENT_SOURCE_DIR}/cmake/web.cmake)
3233
include( ${CMAKE_CURRENT_SOURCE_DIR}/cmake/windows.cmake)
33-
include( ${CMAKE_CURRENT_SOURCE_DIR}/cmake/python_callouts.cmake)
34+
3435

3536
# Detect number of processors
3637
include(ProcessorCount)

test/CMakeLists.txt

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ file( GLOB_RECURSE DOC_XML
1313
CONFIGURE_DEPENDS
1414
"${CMAKE_CURRENT_SOURCE_DIR}/doc_classes/*.xml" )
1515

16-
set( DOC_DATA_SOURCE "${CMAKE_CURRENT_BINARY_DIR}/src/gen/doc_data.gen.cpp" )
17-
generate_doc_source( "${DOC_DATA_SOURCE}" "${DOC_XML}" )
18-
1916
foreach( TARGET_ALIAS template_debug template_release editor )
2017
set( TARGET_NAME "godot-cpp.test.${TARGET_ALIAS}" )
2118

@@ -30,13 +27,13 @@ foreach( TARGET_ALIAS template_debug template_release editor )
3027
src/tests.h
3128
)
3229

33-
set( OUTPUT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/project/bin/" )
34-
3530
# conditionally add doc data to compile output
3631
if( TARGET_ALIAS MATCHES "editor|template_debug" )
37-
target_sources( ${TARGET_NAME} PRIVATE "${DOC_DATA_SOURCE}" )
32+
target_doc_sources( ${TARGET_NAME} ${DOC_XML} )
3833
endif()
3934

35+
set( OUTPUT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/project/bin/" )
36+
4037
# Link to godot-cpp target
4138
set( LINK_TARGET "godot-cpp::${TARGET_ALIAS}" )
4239
target_link_libraries( ${TARGET_NAME} PRIVATE ${LINK_TARGET} )

0 commit comments

Comments
 (0)