Skip to content

Commit

Permalink
Make godot-cpp installable with cmake config
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ytnuf committed Oct 13, 2024
1 parent 6facde3 commit 76b3663
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 12 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ include( ${PROJECT_SOURCE_DIR}/cmake/godotcpp.cmake )
godotcpp_options()

godotcpp_generate()

godotcpp_install()
68 changes: 63 additions & 5 deletions cmake/godotcpp.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ function( godotcpp_options )
#TODO target

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

target_include_directories(${PROJECT_NAME} ${GODOT_SYSTEM_HEADERS_ATTRIBUTE} PUBLIC
include
${CMAKE_CURRENT_BINARY_DIR}/gen/include
${GODOT_GDEXTENSION_DIR}
target_include_directories(${PROJECT_NAME} ${GODOT_CPP_SYSTEM_HEADERS_ATTRIBUTE} PUBLIC
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/gen/include>
$<BUILD_INTERFACE:${GODOT_GDEXTENSION_DIR}>
$<INSTALL_INTERFACE:include>
)

# Add the compile flags
Expand Down Expand Up @@ -235,6 +236,63 @@ function( godotcpp_generate )
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/bin"
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/bin"
OUTPUT_NAME "${OUTPUT_NAME}"
EXPORT_NAME "cpp" # This ensures that the exported target is godot::cpp
)

endfunction()

function( godotcpp_install )
include("CMakePackageConfigHelpers")
include("GNUInstallDirs")

# Install the library and headers to their respective install location
# CMAKE_INSTALL_ are used to allow the package manager to chose the install location
install(TARGETS "godot-cpp"
EXPORT "godot-cpp-config"
ARCHIVE
DESTINATION "${CMAKE_INSTALL_LIBDIR}"
)
install(
DIRECTORY
"${PROJECT_SOURCE_DIR}/include/"
"${PROJECT_BINARY_DIR}/gen/include/"
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
)

# Install the gdextension files
# The gdextension header is assumed to be the root include directory
# As the JSON file is neither a header nor lib file it goes to the datadir
install(FILES "${GODOT_GDEXTENSION_DIR}/gdextension_interface.h"
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
)
install(FILES "${GODOT_GDEXTENSION_DIR}/extension_api.json"
DESTINATION "${CMAKE_INSTALL_DATADIR}/godot-cpp"
)

# Install the export config file
# This allows this library to be easily consumed by cmake projects:
# find_package("godot-cpp" CONFIG REQUIRED)
# target_link_libaries("my-project" PRIVATE "godot::cpp")
install(EXPORT "godot-cpp-config"
NAMESPACE "godot::"
DESTINATION "${CMAKE_INSTALL_DATADIR}/godot-cpp"
)

if(CMAKE_VERSION VERSION_GREATER_EQUAL "3.19") # string(JSON...) only available in cmake v3.19+
# Use the JSON api file to get the version
file(READ "${GODOT_GDEXTENSION_DIR}/extension_api.json" GODOT_GDEXTENSION_API_JSON)
# GODOT_API_VERSION_MAJOR = GODOT_GDEXTENSION_API_JSON["header"]["version_major"]
string(JSON GODOT_API_VERSION_MAJOR GET "${GODOT_GDEXTENSION_API_JSON}" "header" "version_major")
string(JSON GODOT_API_VERSION_MINOR GET "${GODOT_GDEXTENSION_API_JSON}" "header" "version_minor")
string(JSON GODOT_API_VERSION_PATCH GET "${GODOT_GDEXTENSION_API_JSON}" "header" "version_patch")
set(GODOT_API_VERSION "${GODOT_API_VERSION_MAJOR}.${GODOT_API_VERSION_MINOR}.${GODOT_API_VERSION_PATCH}")
# Install the config version file so that the gdextension version can be specified in find_package
write_basic_package_version_file("${PROJECT_BINARY_DIR}/godot-cpp-config-version.cmake"
VERSION "${GODOT_API_VERSION}"
COMPATIBILITY SameMinorVersion
)
install(FILES "${PROJECT_BINARY_DIR}/godot-cpp-config-version.cmake"
DESTINATION "${CMAKE_INSTALL_DATADIR}/godot-cpp"
)
endif()
endfunction()
20 changes: 13 additions & 7 deletions doc/cmake.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,34 @@
## Examples
```shell
Builds a debug version:
cmake .
cmake --build .
cmake -B build/
cmake --build build/
```
Builds a release version with clang

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

``` shell
cmake -DCMAKE_TOOLCHAIN_FILE=$ANDROID_NDK/build/cmake/android.toolchain.cmake -DANDROID_NDK=$ANDROID_NDK \
-DANDROID_TOOLCHAIN_NAME=arm-linux-androideabi-4.9 -DANDROID_PLATFORM=android-23 -DCMAKE_BUILD_TYPE=Debug .
cmake --build .
-DANDROID_TOOLCHAIN_NAME=arm-linux-androideabi-4.9 -DANDROID_PLATFORM=android-23 -DCMAKE_BUILD_TYPE=Debug -B build/
cmake --build build/
```

## Installation

```shell
cmake --install build/
```

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

```shell
mkdir build && cd build && cmake -G "Unix Makefiles" .. && cmake --build .
mkdir build && cd build && cmake -G .. && cmake --build .
```

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

0 comments on commit 76b3663

Please sign in to comment.