Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export cmake targets #1041

Closed
wants to merge 1 commit into from
Closed

Conversation

zhehangd
Copy link
Contributor

I use cmake to build gdextension in linux. I used to make godot-cpp a submodule in my game project with add_subdirectory to create godot-cpp targets. It works fine but is a bit cumbersome.
An alternative approach would be maintaining and building godot-cpp alone then telling my game project where the godot-cpp library is.

This PR makes cmake generate necessary files so that any other cmake-based projects can import godot-cpp like regular libraries. This makes it possible to share the compiled godot-cpp library among different projects.

Example:

# Build godot-cpp somewhere
mkdir build_godot_cpp
cmake -B build_godot_cpp -S godot-cpp -DCMAKE_BUILD_TYPE=Release
cmake --build build_godot_cpp -j12

# Build my gdextension with path to the library
mkdir build_myextension
cmake -B build_myextension -S myextension_source -DCMAKE_BUILD_TYPE=Release -DCMAKE_PREFIX_PATH=$(realpath build_godot_cpp)
cmake --build build_myextension -j12

and in my CMakeLists.txt I write:

find_package(godot-cpp CONFIG REQUIRED)

# ...

target_link_libraries(libmyextension PUBLIC godot::godot-cpp)

@jacogasp
Copy link

This would be extremely useful in a CI/CD context, where it would be nice to avoid rebuilding godot-cpp each time.

@erodozer
Copy link

As someone trying to get godot-cpp working this way through vcpkg and would love to also see a portfile.cmake for godot-cpp in the official registry, I'm highly interested in seeing this merged.

@dsnopek dsnopek added enhancement This is an enhancement on the current functionality topic:buildsystem Related to the buildsystem or CI setup labels Oct 18, 2023
@dsnopek dsnopek added the cmake label Jan 3, 2024
@zhehangd
Copy link
Contributor Author

I made some changes so we can now install the library to any directory (previously it worked directly on the build directory).

cmake -S . -B build
cmake --build build -j12
cmake --install build --prefix $(realpath install)

PS: after updating my branch I found this PR #1309. The two PRs are basically the same and the major difference is just naming.

Usage of my PR

find_package(godot-cpp CONFIG REQUIRED)
target_link_libraries(mylib PRIVATE godot-cpp::godot-cpp)

Usage of #1309

find_package(GodotCpp CONFIG REQUIRED)
target_link_libraries(MyExtension PRIVATE godot::cpp)

@zhehangd zhehangd changed the title Export cmake target config to build directory Export cmake targets Sep 26, 2024
@zhehangd zhehangd changed the base branch from master to 4.3 September 26, 2024 15:07
@zhehangd zhehangd closed this Oct 7, 2024
@zhehangd zhehangd deleted the cmake_export branch October 7, 2024 13:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cmake enhancement This is an enhancement on the current functionality topic:buildsystem Related to the buildsystem or CI setup
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants