Skip to content

Commit 10df0ac

Browse files
committed
Make godot-cpp installable with cmake config
This is so this library can be used via a package manager The install destination uses CMAKE_INSTALL_ so that package managers can choose the best location for these artifacts godot-cpp-dev is the installed component this is so that if this library is a subproject, the user can install their project without installing this library by specifying the components As BUILD_INTERFACE requires absolute path, this means that GODOT_GDEXTENSION_DIR needs to be an absolute path config filename can either be PascalCase or kebab-case, the latter was chosen to be consistent with the package's name (godot-cpp) string(JSON ...) is only available in cmake v3.19, as it is used to obtain the version, this means that config-version file is only created when using cmake v3.19 or later
1 parent a62f633 commit 10df0ac

File tree

3 files changed

+81
-5
lines changed

3 files changed

+81
-5
lines changed

CMakeLists.txt

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@
3030
# -DANDROID_TOOLCHAIN_NAME=arm-linux-androideabi-4.9 -DANDROID_PLATFORM=android-23 -DCMAKE_BUILD_TYPE=Debug .
3131
# cmake --build .
3232
#
33+
# Installation (after the library is built):
34+
# cmake --install <insert_build_directory_here> --component godot-cpp-dev
35+
#
36+
#
3337
# Protip
3438
# Generate the buildfiles in a sub directory to not clutter the root directory with build files:
3539
# mkdir build && cd build && cmake -G "Unix Makefiles" .. && cmake --build .
@@ -65,9 +69,8 @@ if(NOT DEFINED BITS)
6569
endif()
6670

6771
# Input from user for GDExtension interface header and the API JSON file
68-
set(GODOT_GDEXTENSION_DIR "gdextension" CACHE STRING "")
72+
set(GODOT_GDEXTENSION_DIR "${CMAKE_CURRENT_SOURCE_DIR}/gdextension" CACHE STRING "")
6973
set(GODOT_CUSTOM_API_FILE "" CACHE STRING "")
70-
7174
set(GODOT_GDEXTENSION_API_FILE "${GODOT_GDEXTENSION_DIR}/extension_api.json")
7275
if (NOT "${GODOT_CUSTOM_API_FILE}" STREQUAL "") # User-defined override.
7376
set(GODOT_GDEXTENSION_API_FILE "${GODOT_CUSTOM_API_FILE}")
@@ -183,9 +186,10 @@ if (GODOT_CPP_SYSTEM_HEADERS)
183186
endif ()
184187

185188
target_include_directories(${PROJECT_NAME} ${GODOT_CPP_SYSTEM_HEADERS_ATTRIBUTE} PUBLIC
186-
include
187-
${CMAKE_CURRENT_BINARY_DIR}/gen/include
188-
${GODOT_GDEXTENSION_DIR}
189+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
190+
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/gen/include>
191+
$<BUILD_INTERFACE:${GODOT_GDEXTENSION_DIR}>
192+
$<INSTALL_INTERFACE:include>
189193
)
190194

191195
# Add the compile flags
@@ -213,4 +217,8 @@ set_target_properties(${PROJECT_NAME}
213217
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/bin"
214218
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/bin"
215219
OUTPUT_NAME "${OUTPUT_NAME}"
220+
EXPORT_NAME "cpp" # This ensures that the exported target is godot::cpp
216221
)
222+
223+
224+
include("cmake/install.cmake")

cmake/config.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
include("${CMAKE_CURRENT_LIST_DIR}/godot-cpp-target.cmake")

cmake/install.cmake

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
2+
include("CMakePackageConfigHelpers")
3+
include("GNUInstallDirs")
4+
5+
# Install the library and headers to their respective install location
6+
# CMAKE_INSTALL_ are used to allow the package manager to chose the install location
7+
# Components are used so that if godot-cpp is a subproject, the user can chose not to install it
8+
install(TARGETS "godot-cpp"
9+
EXPORT "godot-cpp-target"
10+
ARCHIVE
11+
DESTINATION "${CMAKE_INSTALL_LIBDIR}"
12+
COMPONENT "godot-cpp-dev"
13+
)
14+
install(
15+
DIRECTORY
16+
"${CMAKE_CURRENT_SOURCE_DIR}/include/"
17+
"${CMAKE_CURRENT_BINARY_DIR}/gen/include/"
18+
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
19+
COMPONENT "godot-cpp-dev"
20+
)
21+
# Install the gdextension files
22+
# The gdextension header is assumed to be the root include directory
23+
# As the JSON file is neither a header nor lib file it goes to the datadir
24+
install(FILES "${GODOT_GDEXTENSION_DIR}/gdextension_interface.h"
25+
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
26+
COMPONENT "godot-cpp-dev"
27+
)
28+
install(FILES "${GODOT_GDEXTENSION_API_FILE}"
29+
DESTINATION "${CMAKE_INSTALL_DATADIR}/godot-cpp"
30+
COMPONENT "godot-cpp-dev"
31+
)
32+
33+
# Install the export config file
34+
# This allows this library to be easily consumed by cmake projects:
35+
# find_package("godot-cpp" 4.2.0 CONFIG REQUIRED)
36+
# target_link_libaries("my-project" PRIVATE "godot::cpp")
37+
install(FILES "${CMAKE_CURRENT_SOURCE_DIR}/cmake/config.cmake"
38+
RENAME "godot-cpp-config.cmake"
39+
DESTINATION "${CMAKE_INSTALL_DATADIR}/godot-cpp"
40+
COMPONENT "godot-cpp-dev"
41+
)
42+
install(EXPORT "godot-cpp-target"
43+
NAMESPACE "godot::"
44+
DESTINATION "${CMAKE_INSTALL_DATADIR}/godot-cpp"
45+
COMPONENT "godot-cpp-dev"
46+
)
47+
48+
if(CMAKE_VERSION VERSION_GREATER_EQUAL "3.19") # string(JSON...) only available in cmake v3.19+
49+
# Use the JSON api file to get the version
50+
file(READ "${GODOT_GDEXTENSION_API_FILE}" GODOT_GDEXTENSION_API_JSON)
51+
# GODOT_API_VERSION_MAJOR = GODOT_GDEXTENSION_API_JSON["header"]["version_major"]
52+
string(JSON GODOT_API_VERSION_MAJOR GET "${GODOT_GDEXTENSION_API_JSON}" "header" "version_major")
53+
string(JSON GODOT_API_VERSION_MINOR GET "${GODOT_GDEXTENSION_API_JSON}" "header" "version_minor")
54+
string(JSON GODOT_API_VERSION_PATCH GET "${GODOT_GDEXTENSION_API_JSON}" "header" "version_patch")
55+
set(GODOT_API_VERSION "${GODOT_API_VERSION_MAJOR}.${GODOT_API_VERSION_MINOR}.${GODOT_API_VERSION_PATCH}")
56+
unset(GODOT_GDEXTENSION_API_JSON)
57+
58+
# Install the config version file so that the gdextension version can be specified in find_package
59+
write_basic_package_version_file("${CMAKE_CURRENT_BINARY_DIR}/godot-cpp-config-version.cmake"
60+
VERSION "${GODOT_API_VERSION}"
61+
COMPATIBILITY SameMinorVersion
62+
)
63+
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/godot-cpp-config-version.cmake"
64+
DESTINATION "${CMAKE_INSTALL_DATADIR}/godot-cpp"
65+
COMPONENT "godot-cpp-dev"
66+
)
67+
endif()

0 commit comments

Comments
 (0)