Skip to content

Commit c09f7a7

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 Install pkg-config file for interoperability with other build systems
1 parent c20a84e commit c09f7a7

File tree

3 files changed

+94
-6
lines changed

3 files changed

+94
-6
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/godot-cpp.pc.in

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
prefix=${pcfiledir}/../..
2+
exec_prefix=${prefix}
3+
includedir=${prefix}/@CMAKE_INSTALL_INCLUDEDIR@
4+
libdir=${exec_prefix}/@CMAKE_INSTALL_LIBDIR@
5+
6+
Name: @PROJECT_NAME@
7+
Description: C++ bindings for the Godot script API
8+
Version: @GODOT_API_VERSION@
9+
Libs: -L${libdir} -l@GODOTCPP_OUTPUT_NAME@
10+
Cflags: -I${includedir}

cmake/godotcpp.cmake

Lines changed: 82 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1+
include("GNUInstallDirs")
2+
13
function( godotcpp_options )
24

35
#TODO platform
46
#TODO target
57

68
# Input from user for GDExtension interface header and the API JSON file
7-
set(GODOT_GDEXTENSION_DIR "gdextension" CACHE PATH
9+
set(GODOT_GDEXTENSION_DIR "${PROJECT_SOURCE_DIR}/gdextension" CACHE PATH
810
"Path to a custom directory containing GDExtension interface header and API JSON file ( /path/to/gdextension_dir )" )
9-
set(GODOT_CUSTOM_API_FILE "" CACHE FILEPATH
11+
set(GODOT_CUSTOM_API_FILE "${GODOT_GDEXTENSION_DIR}/extension_api.json" CACHE FILEPATH
1012
"Path to a custom GDExtension API JSON file (takes precedence over `gdextension_dir`) ( /path/to/custom_api_file )")
1113

1214
#TODO generate_bindings
@@ -42,6 +44,12 @@ function( godotcpp_options )
4244
option(GODOT_SYSTEM_HEADERS "Expose headers as SYSTEM." ON)
4345
option(GODOT_WARNING_AS_ERROR "Treat warnings as errors" OFF)
4446

47+
set(GODOT_USE_HOT_RELOAD "" CACHE BOOL
48+
"Enable the extra accounting required to support hot reload. (ON|OFF)")
49+
50+
set(GODOT_INSTALL_CMAKEDIR "${CMAKE_INSTALL_LIBDIR}/cmake/godot" CACHE PATH
51+
"Install path to godot-cpp CMake folder (relative to install prefix)")
52+
4553
# Run options commands on the following to populate cache for all platforms.
4654
# This type of thing is typically done conditionally
4755
# But as scons shows all options so shall we.
@@ -203,10 +211,11 @@ function( godotcpp_generate )
203211
set(GODOT_SYSTEM_HEADERS_ATTRIBUTE SYSTEM)
204212
endif ()
205213

206-
target_include_directories(${PROJECT_NAME} ${GODOT_SYSTEM_HEADERS_ATTRIBUTE} PUBLIC
207-
include
208-
${CMAKE_CURRENT_BINARY_DIR}/gen/include
209-
${GODOT_GDEXTENSION_DIR}
214+
target_include_directories(${PROJECT_NAME} ${GODOT_CPP_SYSTEM_HEADERS_ATTRIBUTE} PUBLIC
215+
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
216+
$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/gen/include>
217+
$<BUILD_INTERFACE:${GODOT_GDEXTENSION_DIR}>
218+
$<INSTALL_INTERFACE:include>
210219
)
211220

212221
# Add the compile flags
@@ -235,6 +244,73 @@ function( godotcpp_generate )
235244
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/bin"
236245
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/bin"
237246
OUTPUT_NAME "${OUTPUT_NAME}"
247+
EXPORT_NAME "cpp" # This ensures that the exported target is godot::cpp
238248
)
239249

240250
endfunction()
251+
252+
function( godotcpp_installable )
253+
include("CMakePackageConfigHelpers")
254+
255+
# Install the library and headers to their respective install location
256+
# CMAKE_INSTALL_* is used to allow the package manager to chose the install location
257+
install(TARGETS "godot-cpp"
258+
EXPORT "godot-config"
259+
ARCHIVE
260+
DESTINATION "${CMAKE_INSTALL_LIBDIR}"
261+
COMPONENT "godot-cpp"
262+
)
263+
install(
264+
DIRECTORY
265+
"${PROJECT_SOURCE_DIR}/include/"
266+
"${PROJECT_BINARY_DIR}/gen/include/"
267+
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
268+
COMPONENT "godot-cpp"
269+
)
270+
install(FILES "${GODOT_GDEXTENSION_DIR}/gdextension_interface.h"
271+
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
272+
COMPONENT "godot-cpp"
273+
)
274+
275+
# Install the export config file
276+
# This allows this library to be easily consumed by cmake projects:
277+
# find_package("godot-cpp" CONFIG REQUIRED)
278+
# target_link_libaries("my-project" PRIVATE "godot::cpp")
279+
install(EXPORT "godot-config"
280+
NAMESPACE "godot::"
281+
DESTINATION "${GODOT_INSTALL_CMAKEDIR}"
282+
COMPONENT "godot-cpp"
283+
)
284+
285+
if(CMAKE_VERSION VERSION_GREATER_EQUAL "3.19") # string(JSON...) only available in cmake v3.19+
286+
# Use the JSON api file to get the version
287+
file(READ "${GODOT_GDEXTENSION_DIR}/extension_api.json" GODOT_GDEXTENSION_API_JSON)
288+
# GODOT_API_VERSION_MAJOR = GODOT_GDEXTENSION_API_JSON["header"]["version_major"]
289+
string(JSON GODOT_API_VERSION_MAJOR GET "${GODOT_GDEXTENSION_API_JSON}" "header" "version_major")
290+
string(JSON GODOT_API_VERSION_MINOR GET "${GODOT_GDEXTENSION_API_JSON}" "header" "version_minor")
291+
string(JSON GODOT_API_VERSION_PATCH GET "${GODOT_GDEXTENSION_API_JSON}" "header" "version_patch")
292+
set(GODOT_API_VERSION "${GODOT_API_VERSION_MAJOR}.${GODOT_API_VERSION_MINOR}.${GODOT_API_VERSION_PATCH}")
293+
# Install the config version file so that the gdextension version can be specified in find_package
294+
write_basic_package_version_file("${PROJECT_BINARY_DIR}/godot-config-version.cmake"
295+
VERSION "${GODOT_API_VERSION}"
296+
COMPATIBILITY SameMinorVersion
297+
)
298+
install(FILES "${PROJECT_BINARY_DIR}/godot-config-version.cmake"
299+
DESTINATION "${GODOT_INSTALL_CMAKEDIR}"
300+
COMPONENT "godot-cpp"
301+
)
302+
303+
#Configure and install the pkg-config file
304+
get_target_property(GODOTCPP_OUTPUT_NAME ${PROJECT_NAME} OUTPUT_NAME)
305+
configure_file(
306+
"${PROJECT_SOURCE_DIR}/cmake/godot-cpp.pc.in"
307+
"${PROJECT_BINARY_DIR}/godot-cpp.pc"
308+
@ONLY
309+
)
310+
install(
311+
FILES "${PROJECT_BINARY_DIR}/godot-cpp.pc"
312+
DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig"
313+
COMPONENT "godot-cpp"
314+
)
315+
endif()
316+
endfunction()

0 commit comments

Comments
 (0)