silverqx / TinyORM

Modern C++ ORM library
https://www.tinyorm.org
MIT License
210 stars 22 forks source link

[cmake] Can't build using FetchContent #6

Closed Lucaslah closed 1 year ago

Lucaslah commented 1 year ago

I cannot get this to work with cmake, I get the following error:

[cmake] CMake Error at build/_deps/tinyorm-build/TinyOrmConfig.cmake:61 (include):
[cmake]   include could not find requested file:
[cmake] 
[cmake]     /workspaces/cssudii/build/_deps/tinyorm-build/TinyOrmTargets.cmake
[cmake] Call Stack (most recent call first):
[cmake]   CMakeLists.txt:43 (find_package)
[cmake] 
[cmake] 
[cmake] CMake Error at build/_deps/tinyorm-build/TinyOrmConfig.cmake:75 (set_target_properties):
[cmake]   set_target_properties can not be used on an ALIAS target.
[cmake] Call Stack (most recent call first):
[cmake]   CMakeLists.txt:43 (find_package)

CMake Version: 3.22 In my CMakeLists.txt

FetchContent_Declare(TinyORM GIT_REPOSITORY https://github.com/silverqx/TinyORM.git GIT_TAG origin/main)
FetchContent_MakeAvailable(TinyORM)

...

find_package(TinyOrm 0.15.1.0 CONFIG REQUIRED)

target_link_libraries(${PROJECT_NAME}
    PRIVATE
        Qt${QT_VERSION_MAJOR}::Core
        TinyOrm::TinyOrm
)

I'm not sure if I'm missing something.

silverqx commented 1 year ago

The problem obviously is with the FetchContent_Declare/MakeAvailable, I'm setting properties on the alias TinyOrm::TinyOrm inside the TinyOrmConfig.cmake:75 and it normally works when you try to build TinyOrm without these FetchContent_ CMake functions.

I have never used/tried these FetchContent_ functions. The CMake build will have to be fixed to make this work.

So I have played a little with it and the problem is that the TinyOrmTargets.cmake file is not exported and I don't know why.

Anyway, you can't build with these FetchContent_ functions, it simply doesn't work and have to be fixed.

silverqx commented 1 year ago

I think this can be related link or a very similar problem, or this. I don't understand how FetchContent works so I can't tell. It looks like you have to install the target that was FetchContent_MakeAvailable(TinyORM).

It looks like this doesn't work out of the box (maybe it works only for the header-only libraries and not for libraries that need to be compiled?):

FetchContent_Declare(TinyOrm
    GIT_REPOSITORY https://github.com/silverqx/TinyORM.git
    GIT_TAG origin/main
)
FetchContent_MakeAvailable(TinyOrm)
silverqx commented 1 year ago

I was successful to build using FetchContent, but I have discovered another bug.

For now, remove find_package and disable TOM.

Something like this:

FetchContent_Declare(TinyOrm 
    GIT_REPOSITORY https://github.com/silverqx/TinyORM.git
    GIT_TAG origin/main
)
set(TOM OFF)
FetchContent_MakeAvailable(TinyOrm)

...

target_link_libraries(${PROJECT_NAME}
    PRIVATE
        Qt${QT_VERSION_MAJOR}::Core
        TinyOrm::TinyOrm
)

That are good news though 😁, that bug looks simply fixable, I look at it later.

Lucaslah commented 1 year ago

I was successful to build using FetchContent, but I have discovered another bug.

For now, remove find_package and disable TOM.

Something like this:

FetchContent_Declare(TinyOrm 
    GIT_REPOSITORY https://github.com/silverqx/TinyORM.git
    GIT_TAG origin/main
)
set(TOM OFF)
FetchContent_MakeAvailable(TinyOrm)

...

target_link_libraries(${PROJECT_NAME}
    PRIVATE
        Qt${QT_VERSION_MAJOR}::Core
        TinyOrm::TinyOrm
)

That are good news though grin, that bug looks simply fixable, I look at it later.

This worked for me, thanks very much.

silverqx commented 1 year ago

I have already pushed fixes, currently are running CI pipelines, I'm going to make a release, I post an update here afterward.

Lucaslah commented 1 year ago

Alright sounds good.

silverqx commented 1 year ago

I have pushed a new release that contains a fix, you can look here what was the fix about.

silverqx commented 1 year ago

A few words about the FetchContent, the FetchContent_Declare() it's like git clone inside the build folder and then add the cloned folder in a similar way like add_subdirectory(<cloned_folder>) does and the FetchContent_MakeAvailable(<package>) internally calls the find_package(<package>) or if you define OVERRIDE_FIND_PACKAGE then you don't have to call the the FetchContent_MakeAvailable() and you must call the find_package(<package> x.y.z CONFIG REQUIRED) manually. An advantage of the OVERRIDE_FIND_PACKAGE is that you can call it much later and you can insert configurations between.

So you can include the TinyORM like this:

set(FETCHCONTENT_QUIET OFF)
include(FetchContent)
FetchContent_Declare(TinyOrm
    GIT_REPOSITORY https://github.com/silverqx/TinyORM.git
    GIT_TAG        origin/main
)
# Here you can configure TinyORM CMake options
set(MYSQL_PING ON)
set(TOM_EXAMPLE ON)
FetchContent_MakeAvailable(TinyOrm)

...

target_link_libraries(${PROJECT_NAME}
    PRIVATE
        Qt${QT_VERSION_MAJOR}::Core
        TinyOrm::TinyOrm
)

or like this:

set(FETCHCONTENT_QUIET OFF)
include(FetchContent)
FetchContent_Declare(TinyOrm
    GIT_REPOSITORY https://github.com/silverqx/TinyORM.git
    GIT_TAG        origin/main
    OVERRIDE_FIND_PACKAGE
)
# Here you can configure TinyORM CMake options
set(MYSQL_PING ON)
set(TOM_EXAMPLE ON)

...

find_package(TinyOrm 0.16.0 CONFIG REQUIRED)
target_link_libraries(${PROJECT_NAME}
    PRIVATE
        Qt${QT_VERSION_MAJOR}::Core
        TinyOrm::TinyOrm
)

Maybe it helps someone in the future 😎

silverqx commented 1 year ago

I have also added docs for the FetchContent method in HelloWorld docs example here.

I think it should be all 🕺