Skip to content

Commit 76b3663

Browse files
committed
Make godot-cpp installable with cmake config
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 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 Edited the docs to include install examples
1 parent 6facde3 commit 76b3663

File tree

3 files changed

+78
-12
lines changed

3 files changed

+78
-12
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_install()

cmake/godotcpp.cmake

Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ function( godotcpp_options )
44
#TODO target
55

66
# Input from user for GDExtension interface header and the API JSON file
7-
set(GODOT_GDEXTENSION_DIR "gdextension" CACHE PATH
7+
set(GODOT_GDEXTENSION_DIR "${PROJECT_SOURCE_DIR}/gdextension" CACHE PATH
88
"Path to a custom directory containing GDExtension interface header and API JSON file ( /path/to/gdextension_dir )" )
99
set(GODOT_CUSTOM_API_FILE "" CACHE FILEPATH
1010
"Path to a custom GDExtension API JSON file (takes precedence over `gdextension_dir`) ( /path/to/custom_api_file )")
@@ -203,10 +203,11 @@ function( godotcpp_generate )
203203
set(GODOT_SYSTEM_HEADERS_ATTRIBUTE SYSTEM)
204204
endif ()
205205

206-
target_include_directories(${PROJECT_NAME} ${GODOT_SYSTEM_HEADERS_ATTRIBUTE} PUBLIC
207-
include
208-
${CMAKE_CURRENT_BINARY_DIR}/gen/include
209-
${GODOT_GDEXTENSION_DIR}
206+
target_include_directories(${PROJECT_NAME} ${GODOT_CPP_SYSTEM_HEADERS_ATTRIBUTE} PUBLIC
207+
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
208+
$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/gen/include>
209+
$<BUILD_INTERFACE:${GODOT_GDEXTENSION_DIR}>
210+
$<INSTALL_INTERFACE:include>
210211
)
211212

212213
# Add the compile flags
@@ -235,6 +236,63 @@ function( godotcpp_generate )
235236
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/bin"
236237
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/bin"
237238
OUTPUT_NAME "${OUTPUT_NAME}"
239+
EXPORT_NAME "cpp" # This ensures that the exported target is godot::cpp
238240
)
239241

240242
endfunction()
243+
244+
function( godotcpp_install )
245+
include("CMakePackageConfigHelpers")
246+
include("GNUInstallDirs")
247+
248+
# Install the library and headers to their respective install location
249+
# CMAKE_INSTALL_ are used to allow the package manager to chose the install location
250+
install(TARGETS "godot-cpp"
251+
EXPORT "godot-cpp-config"
252+
ARCHIVE
253+
DESTINATION "${CMAKE_INSTALL_LIBDIR}"
254+
)
255+
install(
256+
DIRECTORY
257+
"${PROJECT_SOURCE_DIR}/include/"
258+
"${PROJECT_BINARY_DIR}/gen/include/"
259+
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
260+
)
261+
262+
# Install the gdextension files
263+
# The gdextension header is assumed to be the root include directory
264+
# As the JSON file is neither a header nor lib file it goes to the datadir
265+
install(FILES "${GODOT_GDEXTENSION_DIR}/gdextension_interface.h"
266+
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
267+
)
268+
install(FILES "${GODOT_GDEXTENSION_DIR}/extension_api.json"
269+
DESTINATION "${CMAKE_INSTALL_DATADIR}/godot-cpp"
270+
)
271+
272+
# Install the export config file
273+
# This allows this library to be easily consumed by cmake projects:
274+
# find_package("godot-cpp" CONFIG REQUIRED)
275+
# target_link_libaries("my-project" PRIVATE "godot::cpp")
276+
install(EXPORT "godot-cpp-config"
277+
NAMESPACE "godot::"
278+
DESTINATION "${CMAKE_INSTALL_DATADIR}/godot-cpp"
279+
)
280+
281+
if(CMAKE_VERSION VERSION_GREATER_EQUAL "3.19") # string(JSON...) only available in cmake v3.19+
282+
# Use the JSON api file to get the version
283+
file(READ "${GODOT_GDEXTENSION_DIR}/extension_api.json" GODOT_GDEXTENSION_API_JSON)
284+
# GODOT_API_VERSION_MAJOR = GODOT_GDEXTENSION_API_JSON["header"]["version_major"]
285+
string(JSON GODOT_API_VERSION_MAJOR GET "${GODOT_GDEXTENSION_API_JSON}" "header" "version_major")
286+
string(JSON GODOT_API_VERSION_MINOR GET "${GODOT_GDEXTENSION_API_JSON}" "header" "version_minor")
287+
string(JSON GODOT_API_VERSION_PATCH GET "${GODOT_GDEXTENSION_API_JSON}" "header" "version_patch")
288+
set(GODOT_API_VERSION "${GODOT_API_VERSION_MAJOR}.${GODOT_API_VERSION_MINOR}.${GODOT_API_VERSION_PATCH}")
289+
# Install the config version file so that the gdextension version can be specified in find_package
290+
write_basic_package_version_file("${PROJECT_BINARY_DIR}/godot-cpp-config-version.cmake"
291+
VERSION "${GODOT_API_VERSION}"
292+
COMPATIBILITY SameMinorVersion
293+
)
294+
install(FILES "${PROJECT_BINARY_DIR}/godot-cpp-config-version.cmake"
295+
DESTINATION "${CMAKE_INSTALL_DATADIR}/godot-cpp"
296+
)
297+
endif()
298+
endfunction()

doc/cmake.md

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,28 +23,34 @@
2323
## Examples
2424
```shell
2525
Builds a debug version:
26-
cmake .
27-
cmake --build .
26+
cmake -B build/
27+
cmake --build build/
2828
```
2929
Builds a release version with clang
3030

3131
```shell
32-
CC=/usr/bin/clang CXX=/usr/bin/clang++ cmake -DCMAKE_BUILD_TYPE=Release -G "Unix Makefiles" .
33-
cmake --build .
32+
CC=/usr/bin/clang CXX=/usr/bin/clang++ cmake -DCMAKE_BUILD_TYPE=Release -G "Unix Makefiles" -B build/
33+
cmake --build build/
3434
```
3535
Builds an android armeabi-v7a debug version:
3636

3737
``` shell
3838
cmake -DCMAKE_TOOLCHAIN_FILE=$ANDROID_NDK/build/cmake/android.toolchain.cmake -DANDROID_NDK=$ANDROID_NDK \
39-
-DANDROID_TOOLCHAIN_NAME=arm-linux-androideabi-4.9 -DANDROID_PLATFORM=android-23 -DCMAKE_BUILD_TYPE=Debug .
40-
cmake --build .
39+
-DANDROID_TOOLCHAIN_NAME=arm-linux-androideabi-4.9 -DANDROID_PLATFORM=android-23 -DCMAKE_BUILD_TYPE=Debug -B build/
40+
cmake --build build/
41+
```
42+
43+
## Installation
44+
45+
```shell
46+
cmake --install build/
4147
```
4248

4349
## Protip
4450
Generate the buildfiles in a sub directory to not clutter the root directory with build files:
4551

4652
```shell
47-
mkdir build && cd build && cmake -G "Unix Makefiles" .. && cmake --build .
53+
mkdir build && cd build && cmake -G .. && cmake --build .
4854
```
4955

5056
Ensure that you avoid exposing godot-cpp symbols - this might lead to hard to debug errors if you ever load multiple

0 commit comments

Comments
 (0)