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

Use CMake FetchContent instead of vcpkg #141

Open
Journeyman1337 opened this issue Dec 14, 2022 · 8 comments
Open

Use CMake FetchContent instead of vcpkg #141

Journeyman1337 opened this issue Dec 14, 2022 · 8 comments
Assignees

Comments

@Journeyman1337
Copy link

FetchContent is a CMake feature that allows you to fetch dependencies directly from online repositories. I have begun integrating it in all of my projects.

For example:
https://github.com/Journeyman-dev/png_wrapper.h

This would make libtcod a lot easier for people to get up and running. Also, this could simplify how you manage your GitHub actions workflows.

@HexDecimal
Copy link
Collaborator

Libtcod should already support being fetched. The CMake scripts themselves are agnostic, with libtcod searching for packages with find_package and will pick packages gotten with FetchContent, as far as I know.

I don't have enough experience with FetchContent to make an example or template.

I'd be interested in libtcod using FetchContent for its own dependencies when it itself is downloaded with FetchContent, but I'm not confident that those dependencies fully support CMake.

@Journeyman1337
Copy link
Author

As of CMake version 24, you can add FIND_PACKAGE_ARGS as an argument to FetchContent_Declare and it will first attempt to use find_package. In this case, it only fetches the content if no package was found.

@HexDecimal
Copy link
Collaborator

HexDecimal commented Dec 15, 2022

I found OVERRIDE_FIND_PACKAGE which seems to make them work seamlessly with find_package.

Trying to figure out examples for libtcod's dependencies:

FetchContent_Declare(
    SDL2
    GIT_REPOSITORY https://github.com/libsdl-org/SDL
    GIT_TAG release-2.26.1
    OVERRIDE_FIND_PACKAGE
)
FetchContent_Declare(
    UTF8PROC
    GIT_REPOSITORY https://github.com/JuliaStrings/utf8proc
    GIT_TAG v2.8.0
    OVERRIDE_FIND_PACKAGE
)
FetchContent_Declare(
    ZLIB
    GIT_REPOSITORY https://github.com/madler/zlib
    GIT_TAG v1.2.13
    OVERRIDE_FIND_PACKAGE
)

LodePNG is missing a CMakeLists. STB is also not a CMake project.

@Journeyman1337
Copy link
Author

Journeyman1337 commented Dec 15, 2022

@HexDecimal
Copy link
Collaborator

Using stb_SOURCE_DIR makes sense in hindsight. With this it should be possible to build the library for LodePNG too, but I want these to export an alias matching the ones provided by Vcpkg and make them into a portable script which can be copied across projects with similar requirements.

So for a simple example. Could something like this could be made into FindStb.cmake:

FetchContent_Declare(
        stb
        GIT_REPOSITORY https://github.com/nothings/stb
)
FetchContent_MakeAvailable(stb)
set(Stb_INCLUDE_DIR "${stb_SOURCE_DIR}" CACHE PATH "Stb header directory.")

Which would seamlessly integrate with libtcod's current use of the same library from Vcpkg:

find_package(Stb REQUIRED)
target_include_directories(${PROJECT_NAME} PRIVATE ${Stb_INCLUDE_DIR})

Then something like this for FindLodePNG-c.cmake:

FetchContent_Declare(
        LodePNG
        ...
)
FetchContent_MakeAvailable(LodePNG)
... # copy lodepng.cpp, lodepng_util.cpp to *.c
add_library(lodepng-c lodepng.c lodepng_util.c)
target_include_directories(lodepng-c LodePNG_SOURCE_DIR)

I barely have any idea what I'm talking about. I'll have to re-read the CMake docs on these topics.

@Journeyman1337
Copy link
Author

Journeyman1337 commented Dec 16, 2022

Maybe you could use check_variable_exists to see if Stb_INCLUDE_DIR was defined when you called FetchContent_Declare. If not, set the variable manually.

@HexDecimal HexDecimal self-assigned this Dec 25, 2022
@HexDecimal
Copy link
Collaborator

Current attempts are generating errors such as the following:

-- Configuring done
CMake Error: install(EXPORT "libtcodTargets" ...) includes target "libtcod" which requires target "SDL2-static" that is not in any export set.
CMake Error: install(EXPORT "libtcodTargets" ...) includes target "libtcod" which requires target "utf8proc" that is not in any export set.
-- Generating done
CMake Generate step failed.  Build files cannot be regenerated correctly.

See https://github.com/libtcod/libtcod/actions/runs/3780000589/jobs/6425717835

@HexDecimal
Copy link
Collaborator

All of my previous examples can be ignored. harryisgamer pointed out how to solve the install issue and then posted multiple working examples of setting up a libtcod project using FetchContent in issue #153:

FetchContent libtcod, simple/minimal version

FetchContent libtcod, with un-vendored dependencies

What's next would be to write official instructions on how to setup a project using these.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants