Skip to content

Commit 0434a63

Browse files
committed
Make godot-cpp installable with cmake config
godot-cpp can be installed like this: cmake && make && make install It can be used like this: find_pacakge("godot") target_link_libraries("my_gdextension_project" PRIVATE "godot::cpp") The install destination uses CMAKE_INSTALL_ so that package managers can choose the best location for these artifacts As BUILD_INTERFACE requires absolute path, this means that GODOT_GDEXTENSION_DIR needs to be an absolute path
1 parent c20a84e commit 0434a63

File tree

2 files changed

+57
-4
lines changed

2 files changed

+57
-4
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,5 @@ include( ${PROJECT_SOURCE_DIR}/cmake/godotcpp.cmake )
2222
godotcpp_options()
2323

2424
godotcpp_generate()
25+
26+
godotcpp_installable()

cmake/godotcpp.cmake

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
include("CMakePackageConfigHelpers")
2+
include("GNUInstallDirs")
3+
14
function( godotcpp_options )
25

36
#TODO platform
47
#TODO target
58

69
# Input from user for GDExtension interface header and the API JSON file
7-
set(GODOT_GDEXTENSION_DIR "gdextension" CACHE PATH
10+
set(GODOT_GDEXTENSION_DIR "${CMAKE_CURRENT_SOURCE_DIR}/gdextension" CACHE PATH
811
"Path to a custom directory containing GDExtension interface header and API JSON file ( /path/to/gdextension_dir )" )
912
set(GODOT_CUSTOM_API_FILE "" CACHE FILEPATH
1013
"Path to a custom GDExtension API JSON file (takes precedence over `gdextension_dir`) ( /path/to/custom_api_file )")
@@ -204,9 +207,10 @@ function( godotcpp_generate )
204207
endif ()
205208

206209
target_include_directories(${PROJECT_NAME} ${GODOT_SYSTEM_HEADERS_ATTRIBUTE} PUBLIC
207-
include
208-
${CMAKE_CURRENT_BINARY_DIR}/gen/include
209-
${GODOT_GDEXTENSION_DIR}
210+
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
211+
"$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/gen/include>"
212+
"$<BUILD_INTERFACE:${GODOT_GDEXTENSION_DIR}>"
213+
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
210214
)
211215

212216
# Add the compile flags
@@ -235,6 +239,53 @@ function( godotcpp_generate )
235239
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/bin"
236240
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/bin"
237241
OUTPUT_NAME "${OUTPUT_NAME}"
242+
EXPORT_NAME "cpp" # This ensures that the exported target is godot::cpp when installed
238243
)
239244

240245
endfunction()
246+
247+
function( godotcpp_installable )
248+
# Install the library
249+
install(TARGETS "godot-cpp"
250+
EXPORT "godot-config"
251+
ARCHIVE
252+
DESTINATION "${CMAKE_INSTALL_LIBDIR}"
253+
COMPONENT "godot-cpp"
254+
)
255+
#Install the headers
256+
install(
257+
DIRECTORY
258+
"${PROJECT_SOURCE_DIR}/include/"
259+
"${PROJECT_BINARY_DIR}/gen/include/"
260+
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
261+
COMPONENT "godot-cpp"
262+
)
263+
install(FILES "${GODOT_GDEXTENSION_DIR}/gdextension_interface.h"
264+
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
265+
COMPONENT "godot-cpp"
266+
)
267+
# Install the export config file, so the library can be found via find_package
268+
install(EXPORT "godot-config"
269+
NAMESPACE "godot::"
270+
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/godot"
271+
COMPONENT "godot-cpp"
272+
)
273+
274+
if(CMAKE_VERSION VERSION_GREATER_EQUAL "3.19") # string(JSON...) only available in cmake v3.19+
275+
# Use the JSON api file to get the version
276+
file(READ "${GODOT_GDEXTENSION_DIR}/extension_api.json" GODOT_GDEXTENSION_API_JSON)
277+
string(JSON GODOT_API_VERSION_MAJOR GET "${GODOT_GDEXTENSION_API_JSON}" "header" "version_major") # GODOT_API_VERSION_MAJOR = GODOT_GDEXTENSION_API_JSON["header"]["version_major"]
278+
string(JSON GODOT_API_VERSION_MINOR GET "${GODOT_GDEXTENSION_API_JSON}" "header" "version_minor")
279+
string(JSON GODOT_API_VERSION_PATCH GET "${GODOT_GDEXTENSION_API_JSON}" "header" "version_patch")
280+
set(GODOT_API_VERSION "${GODOT_API_VERSION_MAJOR}.${GODOT_API_VERSION_MINOR}.${GODOT_API_VERSION_PATCH}")
281+
# Install the config version file so that the gdextension version can be specified in find_package
282+
write_basic_package_version_file("${PROJECT_BINARY_DIR}/godot-config-version.cmake"
283+
VERSION "${GODOT_API_VERSION}"
284+
COMPATIBILITY SameMinorVersion # https://docs.godotengine.org/en/stable/tutorials/scripting/gdextension/what_is_gdextension.html#version-compatibility
285+
)
286+
install(FILES "${PROJECT_BINARY_DIR}/godot-config-version.cmake"
287+
DESTINATION "${GODOT_INSTALL_CMAKEDIR}"
288+
COMPONENT "godot-cpp"
289+
)
290+
endif()
291+
endfunction()

0 commit comments

Comments
 (0)