RobLoach / raylib-cpp

C++ Object Oriented Wrapper for raylib
https://robloach.github.io/raylib-cpp/
zlib License
630 stars 79 forks source link

CMake find_package module for users of raylib-cpp #302

Closed Roy-Fokker closed 6 months ago

Roy-Fokker commented 7 months ago

Perhaps folks would like somewhat easier mechanism to consume this library.

I've hacked together a simple CMake module file as below: FindRaylib_Cpp.cmake

# Check that project is already consuming raylib
find_package(raylib CONFIG REQUIRED)

# if not consuming raylib error out
if (NOT raylib_FOUND)
    message(FATAL_ERROR "[Error]: Could not find raylib.")
endif()

# load fetchcontent cmake
include(FetchContent)

# get appropriate version of raylib-cpp from github.
set(RAYLIB_CPP_VERSION v5.0.1)  # git commit hash: f865785fee2cb18da6ad6a9012a2993a73f2a2b1
FetchContent_Declare(
    raylib_cpp
    GIT_REPOSITORY https://github.com/RobLoach/raylib-cpp.git
    GIT_TAG ${RAYLIB_CPP_VERSION}
    FIND_PACKAGE_ARGS NAMES raylib_cpp # tell cmake to make it available via find_package
)
# make it available to project
FetchContent_MakeAvailable(raylib_cpp)

message("\n"
    "The package raylib_cpp ${RAYLIB_CPP_VERSION} provides CMake targets: \n\n"
    "   find_package(raylib_cpp)\n"
    "   target_link_libraries(main PRIVATE raylib_cpp)\n"
)

This can then be consumed as: CMakeLists.txt

find_package(raylib_cpp)
...
target_link_libraries(my_exe_name PRIVATE raylib_cpp)

If you configure CMAKE_MODULE_PATH within your project to be something like <project folder>/cmake/' and place theFindRaylib_Cpp.cmake` with in it.

CMake will find it, download the raylib-cpp src from github and make it available for use.

few caveats

Hopefully others find this useful. Sorry if this is not appropriate location for such.

RobLoach commented 6 months ago

Ooo. This is handy

RobLoach commented 6 months ago

Can we add this as the project CMake maybe?

Roy-Fokker commented 6 months ago

Yes, you can do that. However, I don't think you need FIND_PACKAGE_ARGS NAME ... on line 26 with method described in #308 as that will add no value. That line is what enables user to call find_package(raylib_cpp). If you call fetchcontent inside CMakeLists.txt, raylib_cpp library is automagically made available.

The way I typically tend to manage 3rd party dependency is to look at VCPKG first, if it has it then download from there, if not then to look for *.cmake file within my project directory. This is mostly because I don't like dealing with git-sub-modules or having 3rd-party/external directories.

as example of using *.cmake & find_package combo:-

Now, please keep in mind, I am not a CMake expert, I just pretend to be one on the Internets :P But above described process works well for me as strictly consumer of a library.