Skip to content

Commit

Permalink
CMake: Support using build_profile.json
Browse files Browse the repository at this point in the history
Build Profile:
- Add missing VisualInstance3D class to build_profile.json
Python:
- Fix binding_generator.py print_file_list function so that random print statements don't pollute the list.
CMake:
- add GODOT_BUILD_PROFILE cache option
- Split file list generation into a function with args
- add status and debug messages
- re-arrange python commands to make reading them simpler
  • Loading branch information
enetheru committed Dec 11, 2024
1 parent 28ed5df commit 35ca242
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 9 deletions.
9 changes: 8 additions & 1 deletion binding_generator.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#!/usr/bin/env python

import io
import json
import re
import shutil
from contextlib import redirect_stdout
from pathlib import Path


Expand Down Expand Up @@ -268,7 +270,12 @@ def get_file_list(api_filepath, output_dir, headers=False, sources=False, profil


def print_file_list(api_filepath, output_dir, headers=False, sources=False, profile_filepath=""):
print(*get_file_list(api_filepath, output_dir, headers, sources, profile_filepath), sep=";", end=None)
trap = io.StringIO()
# suppress stdout messages from get_file_list
with redirect_stdout(trap):
file_list = get_file_list(api_filepath, output_dir, headers, sources, profile_filepath)

print(*file_list, sep=";", end=None)


def parse_build_profile(profile_filepath, api):
Expand Down
86 changes: 79 additions & 7 deletions cmake/godotcpp.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,53 @@ function( godot_arch_map ALIAS PROC )
endif ()
endfunction()


#[[ Generate File List
Use the binding_generator.py Python script to determine the list of files that
will be passed to the code generator using extension_api.json and
build_profile.json.
This happens for every configure.]]
function( generate_file_list OUT_VAR API_FILEPATH PROFILE_FILEPATH OUTPUT_DIR )
# This code snippet will be squashed into a single line
set( PRINT_FILE_LIST_PY [[
import binding_generator;
binding_generator.print_file_list(
api_filepath='${API_FILEPATH}',
profile_filepath='${PROFILE_FILEPATH}',
output_dir='${OUTPUT_DIR}',
headers=True,sources=True)
]])

# Reformat code to a single line, to make build tools happy.
string( CONFIGURE "${PRINT_FILE_LIST_PY}" PYTHON_SCRIPT )
message( DEBUG "Python:\n${PYTHON_SCRIPT}" )
string( REGEX REPLACE "\n *" " " PYTHON_SCRIPT "${PYTHON_SCRIPT}" )

execute_process( COMMAND "${Python3_EXECUTABLE}" "-c" "${PYTHON_SCRIPT}"
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
OUTPUT_VARIABLE GENERATED_FILES_LIST
OUTPUT_STRIP_TRAILING_WHITESPACE
)

# Debug output
message( DEBUG "FileList-Begin" )
foreach( PATH ${GENERATED_FILES_LIST} )
message( DEBUG ${PATH} )
endforeach( )
message( DEBUG "FileList-End" )

# Error out if the file list generator returned no files.
list( LENGTH GENERATED_FILES_LIST LIST_LENGTH )
if( NOT LIST_LENGTH GREATER 0 )
message( FATAL_ERROR "File List Generation Failed")
endif()
message( STATUS "There are ${LIST_LENGTH} Files to generate" )

set( ${OUT_VAR} ${GENERATED_FILES_LIST} PARENT_SCOPE )
endfunction( )


# Function to define all the options.
function( godotcpp_options )
#NOTE: platform is managed using toolchain files.
Expand Down Expand Up @@ -109,7 +156,9 @@ function( godotcpp_options )
#TODO threads
#TODO compiledb
#TODO compiledb_file
#TODO build_profile

set( GODOT_BUILD_PROFILE "" CACHE PATH
"Path to a file containing a feature build profile" )

set(GODOT_USE_HOT_RELOAD "" CACHE BOOL
"Enable the extra accounting required to support hot reload. (ON|OFF)")
Expand Down Expand Up @@ -205,6 +254,10 @@ function( godotcpp_generate )
if (NOT "${GODOT_CUSTOM_API_FILE}" STREQUAL "") # User-defined override.
set(GODOT_GDEXTENSION_API_FILE "${GODOT_CUSTOM_API_FILE}")
endif()
message( STATUS "GODOT_GDEXTENSION_API_FILE = '${GODOT_GDEXTENSION_API_FILE}'")
if( GODOT_BUILD_PROFILE )
message( STATUS "GODOT_BUILD_PROFILE = '${GODOT_BUILD_PROFILE}'")
endif( )

# Code Generation option
if(GODOT_GENERATE_TEMPLATE_GET_NODE)
Expand All @@ -213,14 +266,33 @@ function( godotcpp_generate )
set(GENERATE_BINDING_PARAMETERS "False")
endif()

execute_process(COMMAND "${Python3_EXECUTABLE}" "-c" "import binding_generator; binding_generator.print_file_list('${GODOT_GDEXTENSION_API_FILE}', '${CMAKE_CURRENT_BINARY_DIR}', headers=True, sources=True)"
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
OUTPUT_VARIABLE GENERATED_FILES_LIST
OUTPUT_STRIP_TRAILING_WHITESPACE
)
# generate the file list to use
generate_file_list( GENERATED_FILES_LIST
"${GODOT_GDEXTENSION_API_FILE}"
"${GODOT_BUILD_PROFILE}"
"${CMAKE_CURRENT_BINARY_DIR}" )

#[[ Generate Bindings
Using the generated file list, use the binding_generator.py to generate the
godot-cpp bindings. This will run at build time only if there are files
missing.
]]
set( GENERATE_BINDINGS_PY
[[import binding_generator;
binding_generator.generate_bindings(
api_filepath='${GODOT_GDEXTENSION_API_FILE}',
use_template_get_node='${GENERATE_BINDING_PARAMETERS}',
bits='${BITS}',
precision='${GODOT_PRECISION}',
output_dir='${CMAKE_CURRENT_BINARY_DIR}')
]])

string(CONFIGURE "${GENERATE_BINDINGS_PY}" PYTHON_SCRIPT )
message( DEBUG "Python:\n${PYTHON_SCRIPT}" )
string( REGEX REPLACE "\n *" " " PYTHON_SCRIPT "${PYTHON_SCRIPT}" )

add_custom_command(OUTPUT ${GENERATED_FILES_LIST}
COMMAND "${Python3_EXECUTABLE}" "-c" "import binding_generator; binding_generator.generate_bindings('${GODOT_GDEXTENSION_API_FILE}', '${GENERATE_BINDING_PARAMETERS}', '${BITS}', '${GODOT_PRECISION}', '${CMAKE_CURRENT_BINARY_DIR}')"
COMMAND "${Python3_EXECUTABLE}" "-c" "${PYTHON_SCRIPT}"
VERBATIM
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
MAIN_DEPENDENCY ${GODOT_GDEXTENSION_API_FILE}
Expand Down
3 changes: 2 additions & 1 deletion test/build_profile.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"Label",
"OS",
"TileMap",
"InputEventKey"
"InputEventKey",
"VisualInstance3D"
]
}

0 comments on commit 35ca242

Please sign in to comment.