maxmind / libmaxminddb

C library for the MaxMind DB file format
https://maxmind.github.io/libmaxminddb/
Apache License 2.0
901 stars 235 forks source link

Right way to use FetchContent to download and build for a custom project #250

Closed ncoder-1 closed 3 years ago

ncoder-1 commented 3 years ago

Hi,

I'm trying to build a sample project using CMake's FetchContent. I'm using the following block to pull and build the library so I can use it with my sample project:

include(FetchContent)
FetchContent_Declare(
        maxminddb
        GIT_REPOSITORY https://github.com/maxmind/libmaxminddb.git
        GIT_TAG 1.5.0
)
FetchContent_MakeAvailable(maxminddb)

Normally, a similar block works for other projects out there, but I'm getting the following CMake errors:

CMake Error in build/_deps/maxminddb-src/CMakeLists.txt:
  Target "maxminddb" INTERFACE_INCLUDE_DIRECTORIES property contains path:

    "/tmp/test/test-lookup/build/_deps/maxminddb-src/"

  which is prefixed in the build directory.

CMake Error in build/_deps/maxminddb-src/CMakeLists.txt:
  Target "maxminddb" INTERFACE_INCLUDE_DIRECTORIES property contains path:

    "/tmp/test/test-lookup/build/_deps/maxminddb-src/"

  which is prefixed in the build directory.Target "maxminddb"
  INTERFACE_INCLUDE_DIRECTORIES property contains path:

    "/tmp/test/test-lookup/build/_deps/maxminddb-src/"

  which is prefixed in the source directory.

What is the right way to use FetchContent within my own CMakeLists.txt to fetch, build and make libmaxminddb available for my own project?

oschwald commented 3 years ago

I only have limited CMake experience, but perhaps @bsergean would be able to provide some directions. In #243, he significantly reworked the CMake build and I think better FetchContent support was a motivation.

bsergean commented 3 years ago

Well maybe those changes were not sufficient ... I actually haven't tested it yet, I hope I can find time soon to do so but that's unclear ...

@ncoder-1 maybe you can try with ExternalProject_Add too.

bsergean commented 3 years ago

https://cmake.org/cmake/help/latest/module/ExternalProject.html

ncoder-1 commented 3 years ago

I've tried with ExternalProjects, I got it working but the issue is that ExternalProject doesn't build until I build my own project (explained here).

This causes issues with the IDE not being able to index properly and basically turns the syntax autocorrection into a nightmare.

On the other hand, nlohmann json works properly with:

FetchContent_Declare(
        json
        GIT_REPOSITORY https://github.com/ArthurSonzogni/nlohmann_json_cmake_fetchcontent
        GIT_TAG v3.9.1
)
FetchContent_MakeAvailable(json)
if (NOT json_POPULATED)
    FetchContent_Populate(json)
    add_subdirectory(${json_SOURCE_DIR} ${json_BINARY_DIR} EXCLUDE_FROM_ALL)
endif ()

as well as lifthttp:

FetchContent_Declare(
        lifthttp
        GIT_REPOSITORY https://github.com/jbaldwin/liblifthttp.git
        GIT_TAG v2020.5
)
FetchContent_MakeAvailable(lifthttp)
oschwald commented 3 years ago

@ncoder-1, from what I can tell, 1.6.0 fixes this. Please let me know if this is not the case.